list and kill process

类别:编程语言 点击:0 评论:0 推荐:
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
#pragma comment(lib,"psapi.lib")

void usage( int );


//Define Command parameters
bool l=FALSE;
bool a=FALSE;
bool k=FALSE;



//kill process by id
void processkill (unsigned int PID)
{
    DWORD ObjectRetn;
    HANDLE Killprcshd;
    UINT fuExitcode;
    if((Killprcshd = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID))==NULL)
        {
            printf("\nOpen Process %d failed:%d \n",PID,GetLastError());
            exit;
        
        }


    printf("The Process id is : %d\n",PID);
    //PostMessage(Killprcshd, WM_CLOSE, 0, 0);
    //终止对应的进程
    TerminateProcess(Killprcshd, fuExitcode);
    //判断是否正常终止该进程}
    ObjectRetn =WaitForSingleObject(Killprcshd, INFINITE);
    if(ObjectRetn!=WAIT_FAILED)
    printf("Kill Process succeeds!\n");
    else
    {
        printf("Access is denied!\n");
    printf("Kill Process Fail:Error %d",GetLastError());
    return;
    }
    CloseHandle(Killprcshd);

}

void PrintProcessNameAndId(DWORD processID,bool a)
{
    char szProcessName[MAX_PATH]="unknown";
    char szProcessName1[MAX_PATH]="unknown";
    int i,ModuleNum;

    
    //打开进程
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|
        PROCESS_VM_READ,
        FALSE,processID);
    
    printf( "\n %u ", processID );
    
    if(hProcess)
    {
        HMODULE hMod[1024];
        DWORD cbNeeded;
        //调用EnumProcessModules枚举该进程调用的所有模块
        if( EnumProcessModules(hProcess,hMod,sizeof(hMod),&cbNeeded))
    
        {
            ModuleNum=cbNeeded / sizeof(HMODULE);
            if(!a) ModuleNum=1;
            
            //调用循环以取得所有模块
            for(i=0;i<ModuleNum;i++)
            {
            
            //GetModuleFileNameEx获得各个模块文件的全路径
            GetModuleFileNameEx(hProcess, hMod, szProcessName1, sizeof(szProcessName1));
            printf("\t%-20s \n", szProcessName1);

            }

        }

    CloseHandle(hProcess);
    }

}

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<argc;j++)
    {
        if ((argv[j][0]=='/') || (argv[j][0]=='-'))
            switch(argv[j][1])
            {
                case 'a':
                case 'A': a=true; break;
                case 'l':
                case 'L': l=true; break;
                case 'k':
                case 'K':
                    {
                        k=true;
                        if(argv[j][3])
                        PID=atoi(argv[j]+3);
                        break;
                    }
                case '?':
                case 'h':
                case 'H': usage(1);break;
                default: usage(0);break;
            }
    }
    if(l)
    {
        printf("\n\nID             Process\n");

        // 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++ ) PrintProcessNameAndId( aProcesses,a );
    
    }
    if(k)
        processkill(PID);
}




void usage(int ErrorCode)
{
    if(!ErrorCode) printf("Parameters Error\n");
    printf("\n");
    printf("\t\t\tlist and kill tools \n");
    printf("\tCode by alpha @www.cnwill.com /www.securityfaq.org\n");
    printf("\n");
    printf("USAGE:\n");
    printf("\t/l                 List All Processes\n");
    printf("\t/l /a              List All Processes And Moudles\n");
    printf("\t/k processid       Kill the process by processid\n");
    
}

本文地址:http://com.8s8s.com/it/it22635.htm