嵌入DLL在MFC C ++ EXE? [英] embed DLL in MFC C++ EXE?

查看:505
本文介绍了嵌入DLL在MFC C ++ EXE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将外部CLI / C ++ DLL嵌入到MFC EXE中作为嵌入式资源或类似内容?我的应用程序当前连接到DLL坐在它旁边,它有一些基本的功能,如连接数据库,从DB等信息。

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功能。然后我安全我的EXE与themida和打包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)使用Resource Compiler将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;

 }

这篇关于嵌入DLL在MFC C ++ EXE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆