#include <Windows.h> #include <psapi.h> int main() { DWORD process_ID = 0; WCHAR process_name[MAX_PATH] = {}; HWND notepad = FindWindow(NULL, L"print.txt - Notepad"); BOOL ERR = GetWindowThreadProcessId(notepad, &process_ID); HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_ID); GetModuleFileNameExW(hProcess, 0, process_name, MAX_PATH); return 0; }
有了HANDLE
,您可以使用GetModuleFileNameEx()
,GetProcessImageFileName()
(XP +)或QueryFullProcessImageName()
(Vista +)来获取该进程的EXE文件的完整路径和文件名:
WCHAR process_name[MAX_PATH] = {}; if (GetProcessImageFileNameW(hProcess, process_name, MAX_PATH)) { // use process_name as needed... } else { // error handling ... }
WCHAR process_name[MAX_PATH] = {}; DWORD size = MAX_PATH; if (QueryFullProcessImageNameW(hProcess, 0, process_name, &size)) { // use process_name as needed... } else { // error handling ... }