本文共 1512 字,大约阅读时间需要 5 分钟。
2014年3月21日11:32:21
1、使用VS2008创建一个预编译头的控制台项目。
2、源文件内容:
#include "stdafx.h"#include#include #include using namespace std;#pragma once#pragma message("Psapi.h --> Linking with Psapi.lib")#pragma comment(lib,"Psapi.lib")// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS// and compile with -DPSAPI_VERSION=1void PrintProcessNameAndID( DWORD processID ){ 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) ); } } // Print the process name and identifier. _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID ); // Release the handle to the process. CloseHandle( hProcess );}int main( void ){ // 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 ) { PrintProcessNameAndID( aProcesses[i] ); } } char ch; cin>>ch; return 0;}
转载地址:http://tcwci.baihongyu.com/