开发者

c++ hooking to a different application, how to find thread id from process id?

I would like to add a hook to an application. I am using SetWindowsHookEx and I can create a system wide hook, but I want to create a hook for a particular application. I need to have thread id of the target application to hook it. I know title of the window, I know exe name and from these I can get window handle and process id, but how do I get the thread id? I saw a post about how to do it in c#, but I do not see how to get a list of threads in c++.

HWND windowHandle = FindWindow(NULL, _T("SomeOtherApp"));
DWORD processId = GetWindowThreadProcessId(windowHandle, NULL);
DWORD threadId = ??? // How do I get thread id here?
HHOOK hook = ::SetWindowsHookEx( WH_C开发者_开发技巧BT, HookCBTProc, hInst, threadId);

Thanks, Alexander.


GetWindowThreadProcessId() returns the thread ID. You are erroneously assigning the thread ID to the process ID variable. Instead write:

HWND windowHandle = FindWindow(NULL, _T("SomeOtherApp"));
DWORD threadId = GetWindowThreadProcessId(windowHandle, NULL);
HHOOK hook = ::SetWindowsHookEx(WH_CBT, HookCBTProc, hInst, threadId);


The answer is GetWindowThreadProcessId. It takes the window handle and returns the ID of the thread that created the window and optionally the process ID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜