在console(命令行)的方式下,用c的实现:
头文件psapi.h,库psapi.lib
#pragma comment(lib,"psapi.lib") processID用EnumProcesses()得到;
  得到process的名字,见以下函数(思路是用pid找到句柄)
void #pragma comment(lib,"psapi.lib") void PrintProcessNameAndID( DWORD processID )
{
    char szProcessName[MAX_PATH] = "unknown";    // Get a handle to the process.    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );    // Get the process name.    if ( hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;        if (EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               sizeof(szProcessName) );
        }
    }    // Print the process name and identifier.    printf( "%s (Process ID: %u)\n", szProcessName, processID );    CloseHandle( hProcess );
--------------------------------------------------------------------
--------------------------------------------------------------------
但是在mfc中,我把函数PrintProcessNameAndID定义在一个class中,
也连入了psapi.h,psapi.lib,
但是在编译时出错:
error C2065: 'EnumProcessModules' : undeclared identifier
error C2065: 'GetModuleBaseName' : undeclared identifier
想了半天不得其解,希望大家指教,在mfc中如何实现枚举系统进程

解决方案 »

  1.   

    我是这样得到进程名的
    HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32* information = malloc(sizeof(PROCESSENTRY32));
    information->th32ProcessID(进程id号)
    information->szExeFile(进程映像名)
    通过下面函数得到路径
    if( EnumProcessModules( hProcess, &hModule, sizeof(hModule), &dwSize ) )
        GetModuleFileNameExA(hProcess,hModule, szFilePatch, MAX_PATH );
      

  2.   

    应该不会有问题,你把psapi.h放到stdafx.h里去试试。
      

  3.   

    EnumProcessModules只支持WIN2000以上 #include "psapi.h"   
    WIN98只能使用Process32First Process32Next. #include <tlhelp32.h>