#include <windows.h>
#include <stdio.h>
#include "psapi.h"void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown"; // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION and
PROCESS_VM_READ,
FALSE, processID ); // Get the process name. if ( hProcess )
{
HMODULE hMod;
DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleFileNameEx(hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
} // Print the process name and identifier. printf( "%s (Process ID: %u)\n", szProcessName, processID ); CloseHandle( hProcess );
}void main( )
{
// Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return; // 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++ )
PrintProcessNameAndID( aProcesses[i] );
}
编译以上代码,就可以实现功能,注意PSAPI.H和PSAPI.LIB你可以在VISUAL C++的
SAMPLES中找到,将这两个文件分别加入到C++的INCLUDE目录和LIB目录中,另外在工程编译时,在PROJECT的SETTINGS的LINK中加入PSAPI.LIB的应用,GOOD LUCK!
#include <stdio.h>
#include "psapi.h"void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown"; // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION and
PROCESS_VM_READ,
FALSE, processID ); // Get the process name. if ( hProcess )
{
HMODULE hMod;
DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleFileNameEx(hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
} // Print the process name and identifier. printf( "%s (Process ID: %u)\n", szProcessName, processID ); CloseHandle( hProcess );
}void main( )
{
// Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return; // 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++ )
PrintProcessNameAndID( aProcesses[i] );
}
编译以上代码,就可以实现功能,注意PSAPI.H和PSAPI.LIB你可以在VISUAL C++的
SAMPLES中找到,将这两个文件分别加入到C++的INCLUDE目录和LIB目录中,另外在工程编译时,在PROJECT的SETTINGS的LINK中加入PSAPI.LIB的应用,GOOD LUCK!
能确认吗?
加分了。