用EnumProcesses()枚举进程
生活随笔
收集整理的這篇文章主要介紹了
用EnumProcesses()枚举进程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參照msdn的例子,用EnumProcesses()枚舉進程并輸入進程名和句柄。以下代碼在vs2008中測試通過:
#include "stdafx.h" #include <windows.h> #include "psapi.h" #pragma comment (lib, "psapi.lib ")void MyEnumProcess() {// Get the list of process identifiers.DWORD aProcesses[1024], cbNeeded, cProcesses;unsigned int i;if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) //枚舉進程return;cProcesses = cbNeeded / sizeof(DWORD); //計算進程個數for ( i = 0; i < cProcesses; i++ )if( aProcesses[i] != 0 ){TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");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) ); //取得主模塊全名,每個進程第一模塊則為進程主模塊}}_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, aProcesses[i] ); //輸出進程名及PIDCloseHandle( hProcess );} }void main( ) {MyEnumProcess();system("pause"); }代碼在我電腦上的運行結果如下圖:
總結
以上是生活随笔為你收集整理的用EnumProcesses()枚举进程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kylin启动异常:java.lang.
- 下一篇: 我的ELK搭建笔记(阿里云上部署)