C++ 如何在C++中获取进程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4570174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:46:54  来源:igfitidea点击:

How to get the process name in C++

c++windowswinapi

提问by Muhammad

How do I get the process name from a PID using C++ in Windows?

如何在 Windows 中使用 C++ 从 PID 获取进程名称?

回答by Salman A

I guess the OpenProcessfunction should help, given that yourprocess possesses the necessary rights. Once you obtain a handle to the process, you can use the GetModuleFileNameExfunction to obtain full path (path to the .exe file) of the process.

我想OpenProcess函数应该会有所帮助,因为您的进程拥有必要的权限。一旦获得进程的句柄,就可以使用GetModuleFileNameEx函数获取进程的完整路径(.exe 文件的路径)。

#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "stdio.h"
#include "psapi.h"
// Important: Must include psapi.lib in additional dependencies section
// In VS2005... Project > Project Properties > Configuration Properties > Linker > Input > Additional Dependencies

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE Handle = OpenProcess(
        PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
        FALSE,
        8036 /* This is the PID, you can find one from windows task manager */
    );
    if (Handle) 
    {
        TCHAR Buffer[MAX_PATH];
        if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
        {
            // At this point, buffer contains the full path to the executable
        }
        else
        {
            // You better call GetLastError() here
        }
        CloseHandle(Handle);
    }
    return 0;
}

回答by Brian R. Bondy

You can obtain the process name by using the WIN32 API GetModuleBaseNameafter having the process handle. You can get the process handle by using OpenProcess.

您可以在获得进程句柄后使用 WIN32 API GetModuleBaseName获取进程名称。您可以使用OpenProcess获取进程句柄。

To get the executable name you can also use GetProcessImageFileName.

要获取可执行文件名称,您还可以使用GetProcessImageFileName

回答by T.s. Arun

All the above methods require psapi.dll to be loaded (Read the remarks section) and iterating through process snapshot is an option one should not even consider for getting a name of the executable file from an efficiency standpoint.

以上所有方法都需要加载 psapi.dll(阅读备注部分),并且从效率的角度来看,迭代进程快照是一种甚至不应该考虑获取可执行文件名称的选项。

The best approach, even according to MSDN recommendation, is to use QueryFullProcessImageName.

即使根据 MSDN 的建议,最好的方法是使用QueryFullProcessImageName

std::string ProcessIdToName(DWORD processId)
{
    std::string ret;
    HANDLE handle = OpenProcess(
        PROCESS_QUERY_LIMITED_INFORMATION,
        FALSE,
        processId /* This is the PID, you can find one from windows task manager */
    );
    if (handle)
    {
        DWORD buffSize = 1024;
        CHAR buffer[1024];
        if (QueryFullProcessImageNameA(handle, 0, buffer, &buffSize))
        {
            ret = buffer;
        }
        else
        {
            printf("Error GetModuleBaseNameA : %lu", GetLastError());
        }
        CloseHandle(handle);
    }
    else
    {
        printf("Error OpenProcess : %lu", GetLastError());
    }
    return ret;
}

回答by Nicolas Repiquet

If you are trying to get the executable image name of a given process, take a look at GetModuleFileName.

如果您正在尝试获取给定进程的可执行映像名称,请查看GetModuleFileName

回答by Iulian Rotaru

Try this function :

试试这个功能:

std::wstring GetProcName(DWORD aPid)
{ 
 PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);
    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (processesSnapshot == INVALID_HANDLE_VALUE)
    {
      std::wcout  << "can't get a process snapshot ";
      return 0;
    }

    for(BOOL bok =Process32First(processesSnapshot, &processInfo);bok;  bok = Process32Next(processesSnapshot, &processInfo))
    {
        if( aPid == processInfo.th32ProcessID)
        {
            std::wcout << "found running process: " << processInfo.szExeFile;
            CloseHandle(processesSnapshot);
            return processInfo.szExeFile;
        }

    }
    std::wcout << "no process with given pid" << aPid;
    CloseHandle(processesSnapshot);
    return std::wstring();
}