vc怎样通过进程名获取进程的工作目录
展开全部
先用OpenProcess()函数将进程打开后,利用EnumProcessModules()函数枚举该进程的模块,利用GetModuleFileNameEx()函数就能取得该进程的路径
#include <stdio.h>
#include <windows.h>
#include "PSAPI.H"
#pragma comment( lib, "PSAPI.LIB" )
int main(void)
{
DWORD processid[1024],needed,processcount,i;
HANDLE hProcess;
HMODULE hModule;
char path[MAX_PATH] = "";
char temp[256];
printf("ShowProcessPath with [Process Status API]/n");
EnumProcesses(processid, sizeof(processid), &needed);
processcount=needed/sizeof(DWORD);
for (i=0;i<processcount;i++)
{
hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,processid[i]);
if (hProcess)
{
EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
GetShortPathName(path,path,256);
itoa(processid[i],temp,10);
printf("%s --- %s/n",path,temp);
}
else
{
printf("Failed!!!/n");
}
}
CloseHandle(hProcess);
CloseHandle(hModule);
itoa(processcount,temp,10);
printf("/nProcess Count:%s/n/n",temp);
return 0;
}
#include <stdio.h>
#include <windows.h>
#include "PSAPI.H"
#pragma comment( lib, "PSAPI.LIB" )
int main(void)
{
DWORD processid[1024],needed,processcount,i;
HANDLE hProcess;
HMODULE hModule;
char path[MAX_PATH] = "";
char temp[256];
printf("ShowProcessPath with [Process Status API]/n");
EnumProcesses(processid, sizeof(processid), &needed);
processcount=needed/sizeof(DWORD);
for (i=0;i<processcount;i++)
{
hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false,processid[i]);
if (hProcess)
{
EnumProcessModules(hProcess, &hModule, sizeof(hModule), &needed);
GetModuleFileNameEx(hProcess, hModule, path, sizeof(path));
GetShortPathName(path,path,256);
itoa(processid[i],temp,10);
printf("%s --- %s/n",path,temp);
}
else
{
printf("Failed!!!/n");
}
}
CloseHandle(hProcess);
CloseHandle(hModule);
itoa(processcount,temp,10);
printf("/nProcess Count:%s/n/n",temp);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询