通过 EnumWindows 获取进程 ID...有人可以解释一下为什么这段代码有效吗?
Getting process id via EnumWindows... Can someone please explain me why this code is working?
我正在尝试获取 windows 的句柄。然后我试图为每个句柄获取关联的进程 ID。但不知何故,我不知从哪里神奇地得到了 process_id。请在这里指导我。
如果我尝试使用 GetWindowThreadProcessId() 函数初始化 process_id,我会遇到运行时错误。
但是,如果我注释掉那部分代码并让 process_id 在两个 printf() 函数中打印,程序就会成功运行,显示数据并干净地退出。这里应该是垃圾值。
#include <stdio.h>
#include <windows.h>
WNDENUMPROC DisplayData(HWND str, LPARAM p) {
LPDWORD process_id;
DWORD P_ID;
printf("PID :: %x\n", process_id);
//this is where error occurs
//P_ID = GetWindowThreadProcessId(str, process_id);
printf("Found: %x, P_ID: %x\n", str, process_id);
return TRUE;
}
int main() {
EnumWindows( (WNDENUMPROC) DisplayData, 1);
return 0;
}
#include <stdio.h>
#include <windows.h>
LPDWORD target = (LPDWORD) 0; // Replace 0 with PID of the task from taskmgr.
HWND target_handle = NULL; // stores the handle of the target process
WNDENUMPROC DisplayData(HWND str, LPARAM p) {
LPDWORD thread_id;
DWORD process_id;
char title[30];
GetWindowTextA(str, (LPSTR) &title, 29);
process_id = GetWindowThreadProcessId(str, (PDWORD) &thread_id);
if( thread_id == target & IsWindowVisible(str) ) {
// Target thread with associated handle
// printf("Handle Addr: %lu, Thread ID: %lu, Process ID: %lu, Title: %s\n", str, thread_id, process_id, title );
target_handle = str;
}
return TRUE;
}
int main() {
EnumWindows( (WNDENUMPROC) DisplayData, 1);
ShowWindow(target_handle, SW_HIDE);
Sleep(1000);
ShowWindow(target_handle, SW_SHOW);
return 0;
}
此代码显示的线程 ID 是在任务管理器中显示为 PID 的线程 ID。 target_handle 变量包含执行 EnumWindows() 函数后的句柄地址,正如所需要的。
我正在尝试获取 windows 的句柄。然后我试图为每个句柄获取关联的进程 ID。但不知何故,我不知从哪里神奇地得到了 process_id。请在这里指导我。
如果我尝试使用 GetWindowThreadProcessId() 函数初始化 process_id,我会遇到运行时错误。 但是,如果我注释掉那部分代码并让 process_id 在两个 printf() 函数中打印,程序就会成功运行,显示数据并干净地退出。这里应该是垃圾值。
#include <stdio.h>
#include <windows.h>
WNDENUMPROC DisplayData(HWND str, LPARAM p) {
LPDWORD process_id;
DWORD P_ID;
printf("PID :: %x\n", process_id);
//this is where error occurs
//P_ID = GetWindowThreadProcessId(str, process_id);
printf("Found: %x, P_ID: %x\n", str, process_id);
return TRUE;
}
int main() {
EnumWindows( (WNDENUMPROC) DisplayData, 1);
return 0;
}
#include <stdio.h>
#include <windows.h>
LPDWORD target = (LPDWORD) 0; // Replace 0 with PID of the task from taskmgr.
HWND target_handle = NULL; // stores the handle of the target process
WNDENUMPROC DisplayData(HWND str, LPARAM p) {
LPDWORD thread_id;
DWORD process_id;
char title[30];
GetWindowTextA(str, (LPSTR) &title, 29);
process_id = GetWindowThreadProcessId(str, (PDWORD) &thread_id);
if( thread_id == target & IsWindowVisible(str) ) {
// Target thread with associated handle
// printf("Handle Addr: %lu, Thread ID: %lu, Process ID: %lu, Title: %s\n", str, thread_id, process_id, title );
target_handle = str;
}
return TRUE;
}
int main() {
EnumWindows( (WNDENUMPROC) DisplayData, 1);
ShowWindow(target_handle, SW_HIDE);
Sleep(1000);
ShowWindow(target_handle, SW_SHOW);
return 0;
}
此代码显示的线程 ID 是在任务管理器中显示为 PID 的线程 ID。 target_handle 变量包含执行 EnumWindows() 函数后的句柄地址,正如所需要的。