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

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

我猜OpenProcess函数应该有所帮助,因为你的进程拥有必要的权限。 一旦获得进程的句柄,就可以使用GetmoduleeFileNameEx函数来获取进程的完整路径(.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 (GetmoduleeFileNameEx(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; } 

在进行处理之后,您可以通过使用WIN32 API GetmoduleeBaseName来获取进程名称。 你可以通过使用OpenProcess获取进程句柄。

要获得可执行文件的名字,你也可以使用GetProcessImageFileName 。

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

查看工具帮助库中的enumerocess功能:

http://msdn.microsoft.com/en-us/library/ms682629(v=vs.85).aspx

很好的例子@ http://msdn.microsoft.com/en-us/library/ms682623(v=vs.85).aspx

试试这个功能:

 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(); }