c语言获取子进程id,c/c++开发分享从进程名称获取进程ID

由于可能有多个进程名称实例正在运行,因此进程的映像名称与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; }

有一个示例如何使用CreateToolhelp32Snapshot , Process32First , Process32Next (您必须添加错误句柄等,并在您的代码中包含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; }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值