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

查看:224
本文介绍了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天全站免登陆