Compile an exe file inside c++ [closed]
I want to create a c++ program in which
I can read an external file (that can be exe,dll,apk...etc...etc). That is read the file convert them into bytes and store them in an array
Next,I want to compile the bytes inside the array Now this is the tricky part i want to compile the bytes into an array just to check that if the bytes are working well
- You may say i am converting a file into bytes and then converting those bytes back to the same file....(Yes indeed i am doing so)
Is this possible?
To test whether an executable can be loaded (not quite the same as execution):
- it will succeed unless
- there is a lack of permissions
- the file is not accessable
- one of the dependencies are not accessable (dependency libraries, e.g.)
Note that on UNIX, the same can be achieved using dlopen
.
// A simple program that uses LoadLibrary
#include <windows.h>
#include <stdio.h>
int main( void )
{
HINSTANCE hinstLib;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("input.exe")); // or dll
fFreeResult = FreeLibrary(hinstLib);
if (hinstLib != NULL)
printf("Message printed from executable\n");
return 0;
}
See also
- LoadLibrary Function
- LoadLibraryEx Function
Use stream copy
#include <sstream>
#include <fstream>
int main()
{
std::stringstream in_memory(
std::ios_base::out |
std::ios_base::in |
std::ios::binary);
{
// reading whole file into memory
std::ifstream ifs("input.exe", std::ios::in | std::ios::binary);
in_memory << ifs.rdbuf();
}
{
// optionally write it out
std::ofstream ofs("input.exe.copy", std::ios::out | std::ios::binary);
ofs << in_memory.rdbuf();
}
return 0;
}
If you don't use an in-memory stage, it'll be far more efficient (and very large files will not cause problems):
#include <sstream>
#include <fstream>
int main()
{
std::ofstream("input.exe.copy2", std::ios::out | std::ios::binary)
<< std::ifstream("input.exe", std::ios::in | std::ios::binary).rdbuf();
return 0;
}
精彩评论