'How to embed an exe file into another exe file as a resource in C++?
I am trying to use a pre-build .exe file as a resource in my C++ project, after searching I done the following steps:
Step1 Create a new C++ project and place the following code in the Source.cpp file
#include<iostream>
#include<Windows.h>
#include<fstream>
#define IDB_EMBEDEXE 52
using namespace std;
int main() {
int count = 2;
HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(IDB_EMBEDEXE), __TEXT("BINARY"));
HGLOBAL hGlobal = LoadResource(NULL, hResource);
size_t exeSiz = SizeofResource(NULL, hResource);
void* exeBuf = LockResource(hGlobal);
ofstream outfile((char*)exeBuf, ios::out | ios::binary);
if (!outfile)
exit(9);
outfile.write((char*)exeBuf, exeSiz);
system((char*)exeBuf);
return 0;
}
Step2 Create a new resource file (.rc)
1- Project>Add new item
2- From Installed>Visual C++>Resource choose Resource File (.rc) let us name it Resource.rc
Step3 Edit the header file resource.h that has just created from Step2, add the following line to the header file:#define IDB_EMBEDEXE 52
Step4 Edit the Resource.rc file by right click on it>Open with>Source Code (Text) Editor, add the following code just after the first #include statment (i.e. after #include "resource.h"
#define IDB_EMBEDEXE 52
IDB_EMBEDEXE BINARY "C:\\Users\\Almohandis\\source\\repos\\Project7\\sum2num.exe"
After doing exactly these steps, I still get the following error in the cmd (not in the Visual Studio):
'MZ' is not recognized as an internal or external command, operable program or batch file.
And the program ends with code 0
I think calling system() what causes the problem, especially the argument I passed.
Note that the binary representation of the sum2num.exe file starts with MZ....
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
