c语言枚举当前所有进程,c语言之进程枚举代码

#include

#include

void usage( int );

void PrintModules( DWORD );

//Define Command parameters

bool a=FALSE;

bool m=FALSE;

bool s=FALSE;

bool f=FALSE;

char * FindString;

void main(int argc, char **argv)

{

DWORD aProcesses[1024], cbNeeded, cProcesses;

unsigned int i;

unsigned int PID;

//Get command parameters

if(argc==1)

{

usage(1);

exit(0);

}

for(int j=1;j

{

if ((argv[j][0]=='/') || (argv[j][0]=='-'))

switch(argv[j][1])

{

case 'a':

case 'A': a=true; break;

case 'm':

case 'M': m=true; break;

case 's':

case 'S':

{

s=true;

if(argv[j][3])

PID=atoi(argv[j]+3);

break;

}

case 'f':

case 'F':

{

f=true;

FindString=argv[j]+3;

break;

}

case '?':

case 'h':

case 'H': usage(1);break;

default: usage(0);break;

}

}

if(a)

{

// Get the list of process identifiers.

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return;

// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name of the modules for each process.

for ( i = 0; i < cProcesses; i++ ) PrintModules( aProcesses[i] );

}

if(s)

PrintModules(PID);

}

void PrintModules( DWORD processID )

{

HMODULE hMods[1024];

HANDLE hProcess;

DWORD cbNeeded;

char szProcessName[MAX_PATH];

unsigned int i;

unsigned int ModuleNum;

// Get a list of all the modules in this process.

hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );

if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))

{

if(!f) printf( "Process ID: %u ", processID );

if(m) ModuleNum=cbNeeded / sizeof(HMODULE);

else ModuleNum=1;

for ( i = 0; i < ModuleNum; i++ )

{

char szModName[MAX_PATH];

// Get the full path to the module's file.

if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,sizeof(szModName)))

{

// Print the module name and handle value.

if(!i) strcpy(szProcessName,szModName);

if(!f) printf("\t%s (0x%08X)\n", szModName, hMods[i] );

else

{

if(strstr(szModName,FindString))

{

printf( "Process ID: %u \n", processID );

printf("%s (0x%08X)\n", szProcessName, hMods[0] );

printf("\t%s (0x%08X)\n", szModName, hMods[i] );

}

}

}

}

}

CloseHandle( hProcess );

}

void usage(int ErrorCode)

{

if(!ErrorCode) printf("Parameters Error\n");

printf("\n");

printf("\t\t\tProcess Status Tools v0.1\n");

printf("\t\t\t\tBy lcx4\n");

printf("\twww.lcx4.com\n");

printf("\n");

printf("USAGE:\n");

printf("\t/a Display All Processes\n");

printf("\t/m Include the Modules(DLLs,etc)\n");

printf("\t/s[ :n ] Display the Process PID=n\n");

printf("\t/f:string Find the Process/Modules Contain the string\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值