如何获取DLL加载过程句柄 [英] How to get a DLL loading process handle

查看:99
本文介绍了如何获取DLL加载过程句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取从dll加载dll的过程的句柄。

I'm trying to get the handle to the process which loaded a dll from the dll.

我的方法是:DLL_PROCESS_ATTACH中的
我称为EnumWindows (EnumWindowsProc,NULL);

My approach is: in DLL_PROCESS_ATTACH I call EnumWindows(EnumWindowsProc,NULL);

我的EnumWindowsProc实现如下:

my EnumWindowsProc implementation is the following:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    if(GetCurrentProcessId() == GetWindowThreadProcessId(hWnd,NULL)){
        MessageBox(hWnd,L"I loaded your dll!",L"it's me",MB_OK);
        return TRUE;
}
    return FALSE;
}

问题是GetCurrentProcessId()== GetWindowThreadProcessId(hWnd,NULL)是永远不会正确(如果我将消息框调用放置在if块之外,则一切正常,但是每个列出的窗口都会被调用一次。)

the problem is that GetCurrentProcessId() == GetWindowThreadProcessId(hWnd,NULL) is never true (if i place the messagebox call outside the if block everything works but it gets called once for every listed window).

还有其他方法可以到达点?

Is there any other way to get to the point? Is this approach totally wrong or am I just missing something?

预先感谢

推荐答案

使用 GetCurrentProcess ,返回当前进程的伪句柄。如果需要真正的句柄,请将伪句柄传递给 DuplicateHandle

Use GetCurrentProcess, which returns a pseudo-handle to the current process. If you need a real handle, pass in the pseudo-handle to DuplicateHandle.

请注意,在 DllMain 中执行太多操作非常危险。调用除 KERNEL32 函数以外的任何东西都是非常危险的,即使那样,还有一些您不应该使用的 KERNEL32 函数。打电话。请参见 DllMain 文档,此文档和< a href = http://blogs.msdn.com/b/larryosterman/archive/2004/04/23/118979.aspx rel = nofollow noreferrer>几个 博客 帖子 来自 Microsoft开发人员建议不要在 DllMain 中做太多事情。

Note that it is very dangerous to do too much in DllMain. Calling anything other than KERNEL32 functions is quite dangerous, and even then there are some KERNEL32 functions that you shouldn't be calling. See the DllMain documentation, this document, and several blog posts from Microsoft developers recommending against doing too much in DllMain.

这篇关于如何获取DLL加载过程句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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