谁知道VC 如何根据进程名称找程序的目录
1个回答
展开全部
你首先需要为VC下载一个MicrosoftPlatformSDKforWindowsXPSP2,有了这个包以后,将psapi.h和psapi.lib拷贝到VC的include和lib目录,采用PSAPI编程:1、用EnumProcesses函数列出当前所有进程2、用OpenProcess打开进程3、用GetProcessImageFileName函数就可以获取程序的完整目录上面三个函数的资料在互联网上很完整,你可以用函数名作为关键字搜索一下就知道了。下面举个例子:#include<windows.h#include<stdio.h#include<tchar.h#include<psapi.h#pragmacomment(lib,psapi.lib)voidPrintProcessNameAndID(DWORDprocessID){TCHARszProcessName[MAX_PATH]=TEXT(<unknown);TCHARszProcessPath[MAX_PATH]=TEXT(<unknown);//获取进程句柄HANDLEhProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,processID);//获取进程名称和路径if(NULL!=hProcess){HMODULEhMod;DWORDcbNeeded;if(EnumProcessModules(hProcess,&hMod,sizeof(hMod),&cbNeeded)){GetModuleBaseName(hProcess,hMod,szProcessName,sizeof(szProcessName)/sizeof(TCHAR));}GetProcessImageFileName(hProcess,szProcessPath,MAX_PATH);}//打印进程名、进程号和路径地址._tprintf(TEXT(%s(PID:%u)<%s\n),szProcessName,processID,szProcessPath);CloseHandle(hProcess);}voidmain(){//获取进程列表.DWORDaProcesses[1024],cbNeeded,cProcesses;unsignedinti;if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))return;//计算当前一共多少个活动进程cProcesses=cbNeeded/sizeof(DWORD);//打印进程信息for(i=0;i<cProcesses;i++)if(aProcesses[i]!=0)PrintProcessNameAndID(aProcesses[i]);}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询