我正在尝试加载一个 .dll
文件并让它在加载时显示一个消息框。根据我的理解,一旦加载了 .dll
,它就会调用 dllmain()
并切换到 DLL_PROCESS_ATTACH
选项。我已经为 .dll
和加载它的 .exe
编写了代码。 .exe
可以正确加载它并打印出加载dll 的地址,但我没有看到显示的消息框。我在 Microsoft.com 的某个地方读到,dll 在加载时进入“锁定”状态,以防止出于安全目的执行某些功能或代码。此功能是否阻止显示消息框?是否有解决方法,例如提升权限、系统等...?我也不确定 DEP 是否有任何影响,我将其设置为仅保护关键的 Windows 进程。
调用过程:
#include <iostream>
#include <windows.h>
int main()
{
HMODULE hDll = LoadLibraryA("dll.dll");
if (hDll == NULL)
std::cerr << "Unable to load dll";
else
std::cout << "Dll loaded @ " << hDll;
FreeLibrary(hDll);
}
dll文件:
#include <windows.h>
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL, "Dll has been loaded.", "Loaded", MB_OK);
break;
}
return TRUE;
}
如果我有办法通过调试器运行 .dll
并查看 MessageBox()
返回的内容,我认为这可能对我有帮助,但我不确定如何要做到这一点。谢谢!
最佳答案
Raymond Chen 在他的博客文章中对此有话要说 Some reasons not to do anything scary in your DllMain :
And absolutely under no circumstances should you be doing anything as crazy as creating a window inside your DLL_PROCESS_ATTACH. In addition to the thread affinity issues, there's the problem of global hooks. Hooks running inside the loader lock are a recipe for disaster. Don't be surprised if your machine deadlocks.
关于c++ - DLL_PROCESS_ATTACH 无法在 Windows 7 C++ 上执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556782/