[软件]列进程和中止进程的小程序

比较糙的代码,都扔main里了,忍了哈

使用方法:
列现有进程和PID:KillProcess
杀进程 :KillProcess 进程名

例如
KillProcess explorer.exe
KillProcess iexplore.exe

进程名区分大小写

/*******************************************************/
/*Source:    KillProcess.c                             */
/*Author:    Transparentmask                           */
/*Date:      2006.8.31                                 */
/*Comment:   Just For Fun!                             */
/*******************************************************/

#include 
#include 
#include 

int main(int argc, char* argv[])
{
    BOOL    bList = FALSE;
    char    szExeFile[256] = {0};

    if (argc == 1)
    {
        // Just List Process.
        bList = TRUE;
    }
    else if (argc >= 3)
    {
        // Input Format Error.
        printf("Input Error!\n");
        printf("Input Format:\n");
        printf("List Process:%s\n", argv[0]);
        printf("Kill Process:%s ProcessName\n", argv[0]);
        return 0;
    }
    else if (strlen(argv[1]) > 0)
    {
        strcpy(szExeFile, argv[1]);
    }

    HANDLE            hProcessSnap = NULL;
    PROCESSENTRY32    pe32 = {0};

    // Get Process Snapshot.
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE)
    {
        printf("Can not take Process Snapshot!\n");
        return 0;
    }

    pe32.dwSize = sizeof(PROCESSENTRY32);

    if (bList)
    {
        // List Process Name & ID.
        if (Process32First(hProcessSnap, &pe32))
        {
            printf("   PID ProcessName\n");
            printf("====== =========================\n");
            do
            {
                printf("%6d ", pe32.th32ProcessID);
                printf("%-s\n", pe32.szExeFile);
            }
            while (Process32Next(hProcessSnap, &pe32));
        }
    }
    else
    {
        HANDLE hProcess;
        int    count = 0;
        if (Process32First(hProcessSnap, &pe32))
        {
            do
            {
                // Find ProcessName which your input.
                if (!strcmp(pe32.szExeFile, szExeFile))
                {
                    // Terminate this Process.
                    hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
                    if (hProcess)
                    {
                        if(!TerminateProcess(hProcess, 0))
                        {
                            DWORD dwErrorCode = GetLastError();
                            printf("Can not Terminate the given Process(%s).\nThe error code is %d.\n", szExeFile, dwErrorCode);
                            return 0;
                        }
                    }
                    count ++;
                }
            }
            while (Process32Next(hProcessSnap, &pe32));

            if (count == 0)
            {
                printf("Can not find Process!\n");
                return 0;
            }
        }
        else
        {
            printf("Can not find Process!\n");
            return 0;
        }
    }

    CloseHandle(hProcessSnap);

    return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注