#include <stdio.h> #include <Windows.h> #include <tlhelp32.h> //进程id取进程名需要次头文件 DWORD 窗口句柄_取进程id(HWND hwnd){ DWORD dword = 0;; GetWindowThreadProcessId(hwnd, &dword); return dword; } void 进程_id取进程名(DWORD 进程id,char* nameBuf){ HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 pe32; nameBuf[0] = 0; if (hProcessSnap != INVALID_HANDLE_VALUE) { pe32.dwSize = 296; BOOL boo = Process32Next(hProcessSnap, &pe32); while (boo){ if (pe32.th32ProcessID == 进程id){ memcpy(nameBuf, pe32.szExeFile, strlen(pe32.szExeFile)+1); break; } boo = Process32Next(hProcessSnap, &pe32); } CloseHandle(hProcessSnap); } } int main(){ DWORD 进程id = 窗口句柄_取进程id((HWND)3215688); char name[250]; 进程_id取进程名(进程id, name); printf(name); printf("\r\n%d", (int)进程id); getchar(); return 0; }