C ++ connect to another application, how to find thread id from process id?

I would like to add an application to it. I am using SetWindowsHookEx and I can create a system hook, but I want to create a hook for a specific application. I need to have the thread id of the target application to connect it to. I know the name of the window, I know the name of exe, and from them I can get the window handle and process ID, but how to get the thread ID? I saw a message on how to do this in C # , but I don't 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); 

Thank you, Alexander.

+6
source share
2 answers

GetWindowThreadProcessId() returns the identifier of the thread. You mistakenly assign a thread identifier to a variable as a process ID variable. Instead, write:

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

Answer GetWindowThreadProcessId . It takes a window handle and returns the identifier of the thread that created the window, and optionally, the process identifier.

+2
source

Source: https://habr.com/ru/post/887518/


All Articles