开发者

Compile an exe file inside c++ [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, 开发者_运维百科visit the help center. Closed 11 years ago.

I want to create a c++ program in which

  1. 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

  2. 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

  3. 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜