在可执行文件中使用嵌入式.dll [英] Using an embedded .dll in an executable

查看:216
本文介绍了在可执行文件中使用嵌入式.dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的。所以我知道有很多关于如何在exes中嵌入dll的问题,但我的问题是相当不同。 (具体来说,我使用fmod库在我的程序中播放声音,我嵌入了fmod.dll,但这是除了这一点。)

Okay. So I know there's lots of questions about how to embed dlls inside exes, but my problem is rather different. (Specifically, I'm using the fmod library to play sounds in my program, and I'm embedding the fmod.dll, but that's beside the point.)

我使用Visual C ++ 2010终极。我已经成功地将.dll嵌入.exe。我的resources.h文件包含

I am using Visual C++ 2010 Ultimate. I have successfully embedded the .dll inside the .exe. My resources.h file contains

#define IDR_DLL1  144

和我的.rc文件包含

IDR_DLL1  DLL  MOVEABLE PURE  "data\\fmod.dll"

我在代码中有以下函数另一个stackoverflow问题):

I have the following function in my code (that I totally stole from another stackoverflow question):

bool extractResource(const HINSTANCE hInstance, WORD resourceID, LPCTSTR szFilename)
{
bool bSuccess = false; 
try
{
    // Find and load the resource
    HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(resourceID), L"DLL");
    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);
    bSuccess = true;
}
catch(...)
{
    // Whatever
} 
return bSuccess;
}

然后,我在WinMain函数中首先调用以下代码: / p>

and then, I call the following code first thing in my WinMain function:

int WINAPI WinMain(HINSTANCE h1, HINSTANCE h2, LPSTR l, int a)
{
    extractResource(h1, IDR_DLL1, L"fmod.dll");
    /* etc */
}

它成功提取嵌入的fmod.dll的内容,并将其保存为同一目录中的文件...只有... 当已经有一个fmod.dll在那里。如果fmod .dll不是已经存在,我只是得到一个弹出消息,说

It works. It successfully extracts out the contents of the embedded fmod.dll, and saves it as a file in the same directory... only... when there already was an fmod.dll there beforehand. If fmod.dll was NOT already there, I just get a popup message that says

The program can't start because fmod.dll is missing from your computer. Try reinstalling the program to fix this problem.

...换句话说,我只能覆盖已经存在的fmod.dll。例如,如果我改为我的代码为

...In other words, I can only overwrite an fmod.dll that was already there. For example, if I instead change my code to

extractResource(h1, IDR_DLL1, L"fmod2.dll");

它会写出完全相同的文件,具有完全相同的内容,名为fmod2.dll。我可以在那一刻摆脱原来的fmod.dll,并重命名新创建的fmod2.dll到fmod.dll,它会工作。

it will write out the exact same file, with the exact same contents, titled fmod2.dll. I can at that point get rid of the original fmod.dll, and rename the newly created fmod2.dll to fmod.dll, and it will work.

 
 

   

很明显,问题是它寻找fmod.dll的存在,甚至到达我的程序的入口点。我的程序甚至不能执行任何代码之前任何fmod的东西,需要实际使用。这似乎...极不公平。

So obviously, the issue is that it looks for the presence of an fmod.dll, BEFORE even hitting the entry point of my program. My program can't even execute any code before any fmod stuff is needed to actually be used. This seems... wildly unfair. What is the point of even being able to embed dlls then?

那么,我的问题是


  1. 是否可以直接从.exe文件中使用.dll文件,而无需将其解压为文件? (我的首选方法)

  1. Is it possible to use the .dll directly from inside the .exe, without unpacking it as a file? (My preferred method)

如果不可能,那么我如何至少修改我的代码,对于?

If 1.) is not possible, then how can I at least modify my code to write out the file before its presence is checked for?


推荐答案

我刚才关注延迟加载dll的 DyP的建议

Well, the solution was surprisingly easy. I just followed DyP's suggestion on delay-loading the dll.

我只是在我的项目属性中添加了一个东西,例如:

I just added one thing in my project properties, like so:

就是这样!

编程问题有很容易的解决方案。 :)

I love it when programming problems have easy solutions. :)

这篇关于在可执行文件中使用嵌入式.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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