DLL_PROCESS_ATTACH无法在Windows 7 C ++上执行 [英] DLL_PROCESS_ATTACH failing to execute on Windows 7 C++

查看:622
本文介绍了DLL_PROCESS_ATTACH无法在Windows 7 C ++上执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载 .dll 文件,并在加载时显示一个消息框。从我的理解,一旦 .dll 被加载,它调用 dllmain()并切换到 DLL_PROCESS_ATTACH 选项。我已经为 .dll 和加载它的 .exe 编写了代码。 .exe 可以正确加载它,并打印出加载dll的地址,但我看不到显示的消息框。我在Microsoft.com上的某个地方看到,dll在加载时输入锁定,以防止某些功能或代码为了安全起见而被执行。此功能是否阻止显示消息框?有什么工作,如提升的特权,系统等?我不知道DEP是否有任何影响,我只将其设置为仅保护关键的Windows进程。

I am trying to load a .dll file and have it display a message box when loaded. From my understanding, once a .dll is loaded, it makes a call to dllmain() and switches to the DLL_PROCESS_ATTACH option. I have written the code for both the .dll and the .exe which loads it. The .exe can load it correctly and print out the address in which the dll has been loaded, but I do not see a message box being displayed. I read somewhere on Microsoft.com that the dll enters a "lock" when loaded as to prevent certain functions or code from being executed for security purposes. Is this feature blocking a message box from being displayed? Is there a work around such as elevated privileges, system, etc...? I am not sure if DEP has any effect either, I have it set to only protect critical Windows processes.

调用过程:

#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()返回,但我不知道该怎么做。谢谢!

I think it might help me if I had a way to run the .dll though a debugger and see what MessageBox() returned, but I am not sure how to do that. Thanks!

推荐答案

Raymond Chen在他的博客文章某些原因在DllMain中不要做任何事情

Raymond Chen has something to say about this in his blog entry titled Some reasons not to do anything scary in your DllMain:


绝对不要在任何情况下做任何事情,像在DLL_PROCESS_ATTACH中创建一个窗口一样疯狂。除了线程亲和性问题,还有全局钩子的问题。在装载机锁中运行的钩子是灾难的秘诀。不要惊讶,如果您的机器死锁。

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.

这篇关于DLL_PROCESS_ATTACH无法在Windows 7 C ++上执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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