#pragma comment (lib,"Psapi.lib")
//得到所有进程全路径
BOOL GetProcessName(DWORD dwPID, PTSTR szFullPath, DWORD cchSize)
{
BOOL bReturn = FALSE;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwPID);
if (hProcess == NULL)
{
_tcscpy_s(szFullPath, cchSize, TEXT("???"));
return bReturn;
}
//*******************************************************************/
//For the best results use the following table to convert paths.
//Windows 2000 = GetModuleFileNameEx()
//Windows XP x32 = GetProcessImageFileName()
//Windows XP x64 = GetProcessImageFileName()
//Windows Vista = QueryFullProcessImageName()
//Windows 7 = QueryFullProcessImageName()
//********************************************************************/
DWORD dwRet = 0;
dwRet = GetModuleFileNameEx(hProcess, (HMODULE)0, szFullPath, cchSize);
if (0 == dwRet || strlen(szFullPath) <= _MAX_DRIVE)
{
//[注意]GetProcessImageFileName()返回的系统进程不是以系统盘符开头,而是\Device\HarddiskVolume1
//dwRet = GetProcessImageFileName(hProcess, szFullPath, cchSize);
//if (0 == dwRet || strlen(szFullPath) <= _MAX_DRIVE)
{
//QueryFullProcessImageName()针对6.0版本及以上(6.0 Vista; 6.1 win7; 6.2 win8)
dwRet = QueryFullProcessImageName(hProcess, 0, szFullPath, &cchSize);
if (0 == dwRet || strlen(szFullPath) <= _MAX_DRIVE)
{
_tcscpy_s(szFullPath, cchSize, TEXT("???"));
}
}
}
CloseHandle(hProcess);
return bReturn;
}
void main()
{
//[备注]QueryDosDevice()将系统盘符"C:"转为Targt"\Device\HarddiskVolume1",这里仅为保留参考
//char szTargt[MAX_PATH] = { 0 };
//QueryDosDevice("C:", szTargt, MAX_PATH);
DWORD szProcess[1024] = { 0 };
DWORD dwProcess = 0;
DWORD dwNeeded = 0;
//枚举进程ID列表
if (!EnumProcesses(szProcess, sizeof(szProcess), &dwNeeded))
{
return;
}
//计算进程数量
dwProcess = dwNeeded / sizeof(DWORD);
//输出每个进程的名称和ID
for (int i = 0; i < dwProcess; i++)
{
char szFullPath[MAX_PATH] = { 0 };
GetProcessName(szProcess[i], szFullPath, MAX_PATH);
printf("%6d %s\n", szProcess[i], szFullPath);
}
system("pause");
return;
}