Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
740 views
in Technique[技术] by (71.8m points)

embed DLL in MFC C++ EXE?

Is it possible to embed an external CLI/C++ DLL into a MFC EXE as a embedded resource or something like that? My application currently connects to DLL sitting right beside it that has some basic functions like connect to database, pull information from DB, etc..

I use LoadLibrary to use the DLL functions. Then I secure my EXE with themida and pack the EXE and DLL together. The problem is though to pack the DLL and EXE I have to disable file patching in themida which is a very strong feature. I have to disable it because when I pack my EXE it needs to modify the file a bit, and then themida thinks it has been cracked or something and does not allow the application to work.

So is there a way to embed this DLL into my EXE? The DLL is not compatible with themida sadly which is why it is a separate file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1) Add a Resource Script file in the executable project.

IDR_DLL_BIN        BINARY  MOVEABLE PURE   "..\debug\myextern.dll"

2) Compile RC file to RES file using the Resource Compiler:

rc.exe /fo "Release/mydll.res" ".mydll.rc"

If you are using Visual Studio, it will build the RES file and will also bind it with executable.

3) Find and load the resource from the executable:

bool ExtractResource(const HINSTANCE hInstance, WORD resourceID, LPCTSTR szFilename)
{
  bool bSuccess = false; 
  try
  {
      // Find and load the resource
      HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(resourceID), _T(“BINARY”));
      HGLOBAL hFileResource = LoadResource(hInstance, hResource);

      // Open and map this to a disk file
      LPVOID lpFile = LockResource(hFileResource);
      DWORD dwSize = SizeofResource(hInstance, hResource);            

      // Open the file and filemap
      HANDLE hFile = CreateFile(szFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
      HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL);            
      LPVOID lpAddress = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);            

      // Write the file
      CopyMemory(lpAddress, lpFile, dwSize);            

      // Un-map the file and close the handles
      UnmapViewOfFile(lpAddress);
      CloseHandle(hFileMap);
      CloseHandle(hFile);
   }
   catch(…)
   {
        // Whatever
   } 
   return bSuccess;

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...