如何枚举当前系统的所有进程(高手指点)。

解决方案 »

  1.   

    //赚点分,msdn上的例子代码,《windows核心编成》上也有例子#include <windows.h>
    #include <tlhelp32.h>
    #include <stdio.h>BOOL GetProcessList () 

        HANDLE         hProcessSnap = NULL; 
        BOOL           bRet      = FALSE; 
        PROCESSENTRY32 pe32      = {0}; 
     
        //  Take a snapshot of all processes in the system.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);     if (hProcessSnap == INVALID_HANDLE_VALUE) 
            return (FALSE); 
     
        //  Fill in the size of the structure before using it.     pe32.dwSize = sizeof(PROCESSENTRY32); 
     
        //  Walk the snapshot of the processes, and for each process, 
        //  display information.     if (Process32First(hProcessSnap, &pe32)) 
        { 
            DWORD         dwPriorityClass; 
            BOOL          bGotModule = FALSE; 
            MODULEENTRY32 me32       = {0}; 
     
            do 
            { 
                bGotModule = GetProcessModule(pe32.th32ProcessID, 
                    pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));             if (bGotModule) 
                { 
                    HANDLE hProcess; 
     
                    // Get the actual priority class. 
                    hProcess = OpenProcess (PROCESS_ALL_ACCESS, 
                        FALSE, pe32.th32ProcessID); 
                    dwPriorityClass = GetPriorityClass (hProcess); 
                    CloseHandle (hProcess);                 // Print the process's information. 
                    printf( "\nPriority Class Base\t%d\n", 
                        pe32.pcPriClassBase); 
                    printf( "PID\t\t\t%d\n", pe32.th32ProcessID);
                    printf( "Thread Count\t\t%d\n", pe32.cntThreads);
                    printf( "Module Name\t\t%s\n", me32.szModule);
                    printf( "Full Path\t\t%s\n\n", me32.szExePath);
                } 
            } 
            while (Process32Next(hProcessSnap, &pe32)); 
            bRet = TRUE; 
        } 
        else 
            bRet = FALSE;    // could not walk the list of processes 
     
        // Do not forget to clean up the snapshot object.     CloseHandle (hProcessSnap); 
        return (bRet); 

      

  2.   

    最酷的枚举方法,想隐藏都不行,不过要写驱动哦:
    NTSTATUS EnumProcess()
    {
    PLIST_ENTRY KPEBListHead, KPEBListPtr;
    //ULONG KPEBListOffset=0xa0;                      //定义链表相对KPEB的偏移值
            //ULONG ProcessNameOffset=0x1fc;                  //定义ProcessName相对KPEB的偏移值
         //ULONG ProcessContextOffset=0x18;                //定义Process Context相对KPEB的偏移值
         //ULONG PIDOffset=0x9c;                           //定义PID相对KPEB的偏移值
         void *kpeb;
            char ProcessName[16];
            ULONG ProcessContext;
            ULONG PID;
            byte ps;
         BYTE BasePriority;
        
    DbgPrint("%S:EnumProcess\n",DRIVER_DEVICE_NAME);
    DbgPrint("\n CR3\t\tKPEB Addr\tPID\t Name\t\tState(0:Ready,1:Idle)\tPriority");
    DbgPrint("\n ---\t\t-------- \t---\t ----\n");

    KPEBListHead=KPEBListPtr=(PLIST_ENTRY)(*(ULONG *)PsInitialSystemProcess+KPEBListOffset);
         while (KPEBListPtr->Flink!=KPEBListHead)
         {
            

            //取KPEB
            kpeb=(void *)(((char *)KPEBListPtr)-KPEBListOffset); 

            //取ProcessName
            memset(ProcessName, 0, sizeof(ProcessName));
            memcpy(ProcessName, ((char *)kpeb)+ProcessNameOffset, 16);

            //取Process Context
            ProcessContext=*(ULONG *)(((char *)kpeb)+ProcessContextOffset);

            //取PID
            PID=*(ULONG *)(((char *)kpeb)+PIDOffset);

    //state
    ps=*(byte *)(((char *)kpeb)+PStateOffset);
    BasePriority=*(byte *)(((char *)kpeb)+PBasePriorityOffset);
            //向Debugger输出结果
            DbgPrint(" %08X\t%08X\t%04X\t %s\t\t%d\t%d\n",ProcessContext, kpeb,PID,ProcessName,ps,BasePriority);

            //指向下一链表
            KPEBListPtr=KPEBListPtr->Flink;
         } 
     
      //show idle process
    _asm
    {
    mov eax,fs:[0x12C]
    add eax,0x44
    mov eax,[eax]
    mov kpeb,eax
    }

            memset(ProcessName, 0, sizeof(ProcessName));
            memcpy(ProcessName, ((char *)kpeb)+ProcessNameOffset, 16);        //取Process Context
            ProcessContext=*(ULONG *)(((char *)kpeb)+ProcessContextOffset);        //取PID
            PID=*(ULONG *)(((char *)kpeb)+PIDOffset);        //向Debugger输出结果
            DbgPrint(" %08X\t%08X\t%04X\t %s\n",ProcessContext, kpeb,PID,ProcessName);
            

    return STATUS_SUCCESS;
    }