#include <windows.h>
#include <psapi.h> // 需要包含Psapi库头文件
#include <tchar.h>
#include <vector>
#include <string>
std::wstring GetProcessPathByName(const std::wstring& processName)
{
std::wstring processPath;
DWORD processIds[1024], bytesReturned;
if (!EnumProcesses(processIds, sizeof(processIds), &bytesReturned))
return processPath;
DWORD processCount = bytesReturned / sizeof(DWORD);
for (DWORD i = 0; i < processCount; ++i)
{
if (processIds[i] != 0)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processIds[i]);
if (hProcess != NULL)
{
TCHAR processNameBuffer[MAX_PATH];
DWORD nameSize = MAX_PATH;
if (QueryFullProcessImageName(hProcess, 0, processNameBuffer, &nameSize))
{
std::wstring currentProcessName(processNameBuffer);
if (currentProcessName.find(processName) != std::wstring::npos)
{
processPath = currentProcessName;
break;
}
}
CloseHandle(hProcess);
}
}
}
return processPath;
}
int main()
{
std::wstring targetProcessName = L"notepad.exe"; // 假设我们要查找记事本程序
std::wstring processPath = GetProcessPathByName(targetProcessName);
if (!processPath.empty())
{
std::wcout << L"The path of process " << targetProcessName << L" is: " << processPath << std::endl;
}
else
{
std::wcout << L"Process " << targetProcessName << L" not found." << std::endl;
}
return 0;
}
VC++根据进程名查找程序位置
最新推荐文章于 2024-08-03 18:36:56 发布