所以我需要获取在计算机上运行的进程列表,然后获取每个进程的完整路径。(我正在寻找每个进程运行的文件夹)。我正在使用 QueryFullProcessImageName - http://msdn.microsoft.com/en-us/library/windows/desktop/ms684919(v=vs.85).aspx但是我刚刚发现这段代码需要在 Windows XP 上运行并且功能最低的客户端是Vista。
那么有人知道这样做的另一种方法吗?
我试图使用 GetModuleFileNameEx 但它不起作用。我会传入进程句柄,然后我会在输出中返回“íííííííííííííííííííííííííííííííííííííííí”。
编辑:这是我尝试使用 GetModuleFileNameEx:
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
string name = procName;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
REPORT_ERROR("CreateToolhelp32Snapshot (of processes): err=%d\n", ::GetLastError());
return false;
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(pe32);
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First(hProcessSnap, &pe32))
{
REPORT_ERROR("Process32First: err=%d\n", ::GetLastError()); // Show cause of failure
CloseHandle(hProcessSnap); // Must clean up the snapshot object!
return false;
}
char pName[MAX_PATH];
//iterate through all processes
do {
//Find start of files name
char *szProcessFilename = strchr(pe32.szExeFile, '\\');
if (szProcessFilename == NULL)
szProcessFilename = pe32.szExeFile;
else
szProcessFilename++; //Skip over last slash
if (_stricmp(procName, szProcessFilename) == 0)
{
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, false, pe32.th32ProcessID );
char procLocationChar[MAX_PATH];
DWORD exe_path_size = MAX_PATH;
DWORD s = GetModuleFileNameEx(hProcess, NULL, pName, exe_path_size);
在此之后我做了更多的事情来获取路径,但是 pName 的值只是“ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”