Suj*_*jaM 6 c++ windows winapi qt pid
我试图在Windows中使用winapi获取当前窗口或活动窗口以及该窗口的进程名称.
所以,我能够获得活动窗口GetForegroundWindow()并且我正在使用OpenProcess()以获取进程,问题是OpenProcess需要进程ID,所以我虽然我可以使用GetProcessId()但是这个接收窗口的HANDLE类型我有HWND类型的当前窗口.
我尝试了几件事,但无法使其发挥作用.所以任何人都可以告诉我如何在HWND中获取窗口的进程ID?或者获取当前窗口的句柄?
我将代码保留在这里,以防有些人看到可能对我有帮助的解决方案.我正在使用Qt和C++
char wnd_title[256];
HWND hwnd=GetForegroundWindow(); // get handle of currently active window
GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
HANDLE Handle = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
GetProcessId(hwnd) // GetProcessId is returning 0
);
if (Handle)
{
TCHAR Buffer[MAX_PATH];
if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
printf("Paht: %s", Buffer);
// At this point, buffer contains the full path to the executable
}
CloseHandle(Handle);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以使用GetWindowThreadProcessId(),它接收HWND并输出窗口拥有过程的ID.
例如:
#include <tchar.h>
TCHAR wnd_title[256];
HWND hwnd = GetForegroundWindow(); // get handle of currently active window
GetWindowTextA(hwnd, wnd_title, 256);
DWORD dwPID;
GetWindowThreadProcessId(hwnd, &dwPID);
HANDLE Handle = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
dwPID
);
if (Handle)
{
TCHAR Buffer[MAX_PATH];
if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
_tprintf(_T("Path: %s"), Buffer);
// At this point, buffer contains the full path to the executable
}
CloseHandle(Handle);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3686 次 |
| 最近记录: |