C实现枚举进程

Published on with 1,476 views

/* 
*  文件名:EnumProcess.c

*  用途:枚举进程
*  编程环境:WinXP SP2+CL 8.0
*  完成日期: 2006.2   Ver 0.01

*  作者: 88250

*  联系方式: E-mail: DL88250@gmail.com  QQ:845765

*/

#include 
<stdio.h>

#include 
<windows.h>

#include 
<psapi.h>

#pragma comment(lib, "psapi.lib")



int main(void)

{

    DWORD processid[
1024], pBytesReturned[1024], processSum, i;

    HANDLE hProcess;

    HMODULE hModule;

    
char path[MAX_PATH] = "", temp[256];



    printf(
"ShowProcessPath with [Process Status API] ");



    EnumProcesses(processid, 
sizeof(processid), pBytesReturned);

    processSum 
= *pBytesReturned / sizeof(DWORD);



    
for (i = 0; i < processSum; i++){

        hProcess 
= OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processid[i]);

        
if (hProcess){

            EnumProcessModules(hProcess, 
&hModule, sizeof(hModule), pBytesReturned);

            GetModuleFileNameEx(hProcess, hModule, path, 
sizeof(path));

            GetShortPathName(path, path, 
256);

            itoa(processid[i], temp, 
10);

            printf(
"%s --- %s ", path, temp);        

        }
else{

            printf(
"Failed!!! ");        // 权限不够,不能访问有的进程

        }

    }



    CloseHandle(hProcess);

    CloseHandle(hModule);



    itoa(processSum, temp, 
10);

    printf(
" Process Count:%s ", temp);



    
return 0;

}
 

Responses