c++ - DLL_PROCESS_ATTACH 无法在 Windows 7 C++ 上执行

标签 c++ dll windows-7 messagebox loadlibrary

我正在尝试加载一个 .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/

相关文章:

c++ - 在 Eclipse 中运行多个 Qt 测试

c++ - C++11标准中实例化单元的含义是什么?

c++ - 合并按 vector 排序的 N C++

c++ - 移动构造函数行为

c++ - 将 QPixmap 保存为 JPEG 会产生垂直线

dll - Windows Vista 中“属性”中的“版本”选项卡的等效项是什么?

.net - 在哪里可以获得 Microft SharePoint 程序集?

c++ - 为什么我的 VS2010 native C++ 应用程序不会突然启动,并给出并排错误?

windows-7 - powershell 作为 gvim(vim) :shell

windows-7 - 操作无法完成,因为文件夹或其中的文件已在另一个程序中打开