【windows-API】根据pid获取进程名

代码样例

void ShowProcName(uint32_t pid)
{
    char tempProcName[MAX_PATH] = { 0 };
    //必须具有的权限
    HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
    if (processHandle == NULL) {
        return;
    }
    auto len = GetModuleBaseNameA(processHandle, NULL, tempProcName, MAX_PATH);
    if (len == 0) {
        printf("Get base namefailed, err: %u", GetLastError());
    }
    printf("%s\n", tempProcName);

    GetModuleFileNameEx(processHandle, NULL, tempProcName, MAX_PATH);
    printf("%s\n", tempProcName);
    
    GetProcessImageFileName(processHandle, tempProcName, MAX_PATH);
    printf("%s\n", tempProcName);

   
    CloseHandle(processHandle);
}

输出:

notepad.exe 
C:\Windows\System32\notepad.exe
\Device\HarddiskVolume3\Windows\System32\notepad.exe
posted @ 2021-03-06 17:03  mooooonlight  阅读(2936)  评论(0编辑  收藏  举报