vc中在运行其它应用程序时如何获得exe文件路径
1个回答
展开全部
#include <tlhelp32.h> // for CreateToolhelp32Snapshot
#include <psapi.h>
#pragma comment(lib, "psapi")
/****************************************************************************
TCHAR FilePath[MAX_PATH];
if ( GetPathByProcessID(pe32.th32ProcessID,FilePath) )
{
trace(FilePath);
}
****************************************************************************/
static
bool GetPathByProcessID(DWORD pid,TCHAR* FilePath)
{
HANDLE hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
if (!hProcess)
{
// tracef("无权访问该进程");
return false;
}
DWORD ret= GetModuleFileNameEx(hProcess, NULL, FilePath, MAX_PATH) ;
// tracef(_T("GetModuleFileNameEx : %s ") , ret ? _T("successfully") : _T("failed") );
CloseHandle(hProcess);
return ret!=0;
}
#include <vector>
using namespace std;
bool GetAllProcessPath(vector<string> & Paths)
{
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
trace("无法创建进程的快照");
return false;
}
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32))
{
trace("无法获得进程列表");
CloseHandle (hProcessSnap);
return false;
}
do
{
TCHAR FilePath[MAX_PATH];
if ( GetPathByProcessID(pe32.th32ProcessID,FilePath) )
{
trace(FilePath);
Paths.push_back(FilePath);
}
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle (hProcessSnap);
return true;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询