c/c++语言开发共享从进程名称获取进程ID

嗨,我正在尝试使用C语言的Windows API做一个项目。 我项目中的一小部分是获取lsass.exe的进程ID。

我试过下面的程序,但它不会工作。 我已经阅读了有关CreateToolhelp32Snapshot,Process32First,Process32Next函数的任何人可以帮助我解释如何在代码中使用它们。

所以请帮助我。 我是Windows API的初学者,所以如果有人能建议我推荐一本好的电子书,我会很感激。

    由于可能有多个进程名称实例正在运行,因此进程的映像名称与PID之间没有一对一的关联。 您必须使用EnumProcesses枚举进程并检查每个进程的基本模块名称,如Burgos所述。

    FWIW,。Net通过提供GetProcessesByName API来解决此问题,该API返回一组进程对象。 当然没什么用的:-(

    我不知道简单的方式。 这是通过查找每个正在运行的PID并将其名称与“lsass.exe”进行比较来实现的。

    // pid.cpp : Defines the entry point for the console application. #include "stdafx.h" #include  #include  int PrintProcessNameAndID( DWORD processID, const char *name ) { TCHAR szProcessName[MAX_PATH] = TEXT(""); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) ) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } if(strcmp(szProcessName, name) == 0) // right process { CloseHandle(hProcess); return 1; } // Release the handle to the process. CloseHandle( hProcess ); return 0; } int find(const char *name) { // Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) { return 1; } // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ ) { if( aProcesses[i] != 0 ) { if(PrintProcessNameAndID( aProcesses[i], name )) { //found it _tprintf("%d %sn", aProcesses[i], name); } } } } int _tmain(int argc, _TCHAR* argv[]) { find("lsass.exe"); return 0; } 

    有一个示例如何使用CreateToolhelp32SnapshotProcess32FirstProcess32Next (您必须添加错误句柄等,并在您的代码中包含tlhelp32.h )。 顺便说一下,这个函数与Windows NT不兼容:

     BOOL GetProcessList(const char *processname, DWORD **processIds, int *numprocess) { HANDLE hProcessSnap; PROCESSENTRY32 pe32; DWORD *processIdsTmp; *processIds = NULL; *numprocess = 0; // Take a snapshot of all processes in the system. hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); if( INVALID_HANDLE_VALUE == hProcessSnap ) return( FALSE ); // Retrieve information about the first process, // and exit if unsuccessful if( !Process32First( hProcessSnap, &pe32 ) ) { CloseHandle( hProcessSnap ); // clean the snapshot object return( FALSE ); } do { if (0 == strcasecmp(processname, pe32.szExeFile)) { processIdsTmp = realloc(*processIds, sizeof(DWORD) * ((*numprocess) + 1)); if (NULL == processIdsTmp) { free(*processIds); *processIds = NULL; *numprocess = 0; CloseHandle( hProcessSnap ); // clean the snapshot object return( FALSE ); } *processIds = processIdsTmp; (*processIds)[(*numprocess)++] = pe32.th32ProcessID; } } while( Process32Next( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap ); return( TRUE ); } 

    这是一个使用这个function的完整例子。

    这是Luis G. Costantini R.代码的修改。

    它使用MFC:

     #include "TlHelp32.h" BOOL GetProcessList(const TCHAR *processname, CArray &PIDs) { PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); // Take a snapshot of all processes in the system. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE == hProcessSnap) return FALSE; // Retrieve information about the first process, // and exit if unsuccessful if (!::Process32First(hProcessSnap, &pe32)) { CloseHandle(hProcessSnap); // clean the snapshot object return FALSE; } do { if (0 == _tcsicmp(processname, pe32.szExeFile)) { PIDs.Add(pe32.th32ProcessID); } } while (::Process32Next(hProcessSnap, &pe32)); ::CloseHandle(hProcessSnap); return TRUE; } 

      以上就是c/c++开发分享从进程名称获取进程ID相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/562643.html

      (0)
      上一篇 2021年2月5日
      下一篇 2021年2月5日

      精彩推荐