在 MFC C++ EXE 中嵌入 DLL?

是否可以将外部 CLI/C++ DLL 作为嵌入式资源或类似的东西嵌入到 MFC EXE 中?我的应用程序当前连接到它旁边的 DLL,该 DLL 具有一些基本功能,例如连接到数据库、从数据库中提取信息等.

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

我使用 LoadLibrary 来使用 DLL 函数.然后我用themida保护我的EXE并将EXE和DLL打包在一起.问题是虽然要打包 DLL 和 EXE,但我必须在 themida 中禁用文件修补,这是一个非常强大的功能.我必须禁用它,因为当我打包我的EXE时,它需要稍微修改文件,然后themida认为它已经被破解或其他东西并且不允许应用程序工作.

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.

那么有没有办法将此 DLL 嵌入到我的 EXE 中?遗憾的是,DLL 与 themida 不兼容,这就是为什么它是一个单独的文件.

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.

推荐答案

1) 在可执行项目中添加资源脚本文件.

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

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

2) 使用资源编译器将 RC 文件编译为 RES 文件:

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

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

如果您使用的是 Visual Studio,它将构建 RES 文件并将其与可执行文件绑定.

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

3) 从可执行文件中查找并加载资源:

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;

 }

相关文章