embed DLL in MFC C++ EXE?

前端 未结 2 1820
孤独总比滥情好
孤独总比滥情好 2020-12-06 20:23

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 i

相关标签:
2条回答
  • 2020-12-06 21:06

    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;
    
     }
    
    0 讨论(0)
  • 2020-12-06 21:26

    You could try using Enigma Virtual Box. This will pack DLLs into an EXE so that LoadLibrary "just works". Of course, if it's a C++/CLI DLL then you'll still need the .NET framework installed.

    0 讨论(0)
提交回复
热议问题