VC ++,如何获取正在运行的进程执行路径及其位?

时间:2019-03-02 06:05:59

标签: visual-c++ process executable

在VC ++中如何获取正在运行的进程”

MyReport.exe

“可执行文件路径和有点体系结构(不重要),我在下面尝试了一些操作,但我无法继续下一步..

       int IsReporterRunning()
 {
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
    return -1;
}
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++)
{
    if (aProcesses[i] != 0)
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
        TCHAR a[260] = TEXT("MyReport.exe");
        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
        if (NULL != hProcess)
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
            {
                GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR));
                if (_tcscmp(a, szProcessName) == 0) {
                    printf("\nProcess ID: %u\n", aProcesses[i]);
                    _tprintf(TEXT("%s  (PID: %u)\n"), szProcessName, aProcesses[i]);
                    return 1;
                }
            }
        }
    }
}
return 0;
}

0 个答案:

没有答案