c ++钩到一个不同的应用程序,如何从进程id找到线程ID? [英] c++ hooking to a different application, how to find thread id from process id?

查看:217
本文介绍了c ++钩到一个不同的应用程序,如何从进程id找到线程ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向应用程序添加一个钩子。我使用 SetWindowsHookEx ,我可以创建一个系统范围的钩子,但我想为一个特定的应用程序创建一个钩子。我需要有目标应用程序的线程标识钩住它。我知道窗口的标题,我知道exe名称,从这些我可以得到窗口句柄和进程标识,但是如何获得线程标识?我看到一篇有关如何在 c#,但是我没有看到如何获得c ++中的线程列表。

  HWND windowHandle = FindWindow _T(SomeOtherApp)); 
DWORD processId = GetWindowThreadProcessId(windowHandle,NULL);
DWORD threadId = ??? //我如何在这里获得线程ID?
HHOOK hook = :: SetWindowsHookEx(WH_CBT,HookCBTProc,hInst,threadId);




h2_lin>解决方案

GetWindowThreadProcessId()返回线程ID。您错误地将线程标识分配给进程标识变量。改为写:

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


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_CBT, 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);

这篇关于c ++钩到一个不同的应用程序,如何从进程id找到线程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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