win32 - 基于hwnd获取进程名字(GetModuleFileNameEx)


#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 ...
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM