//枚举
DWORD* pbuffer;
DWORD cbBuffer;
DWORD cbNeed; HANDLE m_handle = ::GetProcessHeap(); ::EnumProcesses(NULL,0,&cbNeed);
int err = ::GetLastError(); //重新分配缓冲大小
pbuffer = (DWORD*)::HeapAlloc(m_handle,HEAP_ZERO_MEMORY,cbNeed); if ( ::EnumProcesses(pbuffer,sizeof(pbuffer),&cbNeed) == 0 ) 
{
err = ::GetLastError();
return;
}

解决方案 »

  1.   

    为什么 我第一交使用 EnumProcesses 这个函数获得的cbNeed 是0 呢 第二次调用的时间候则为4 呢  最关键是第一次调用时为什么是0   这让我百思不解  
     
      

  2.   

    给你一段代码:
    BOOL CEmuteFileDlg::GetProcessList()
    {
        HANDLE         hProcessSnap = NULL; 
        BOOL           bRet      = FALSE; 
        PROCESSENTRY32 pe32      = {0}; 
    int iCount  = 0;
        //  Take a snapshot of all processes in the system. 
    pfGetProcessMemoryInfo=(PGetProcessMemoryInfo)GetProcAddress(LoadLibrary("psapi"),"GetProcessMemoryInfo");
        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); 
    PPROCESS_MEMORY_COUNTERS pmc;
    ZeroMemory(&pmc,sizeof(pmc));
    //  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+1, &me32, sizeof(MODULEENTRY32));   sprintf(szTemp,"Module%d",count++);
                if (bGotModule) 
                { 
                    HANDLE hProcess; 
     
                    // Get the actual priority class. 
                    hProcess = OpenProcess (PROCESS_ALL_ACCESS, 
                        FALSE, pe32.th32ProcessID);  pfGetProcessMemoryInfo(hProcess,pmc,sizeof(pmc));//获取内存信息                dwPriorityClass = GetPriorityClass (hProcess); 
                    CloseHandle (hProcess);  CString str,strList; str.Format("%d",pe32.pcPriClassBase);
    strList.Format("(%02d)Priority Class Base = %d",iCount,pe32.pcPriClassBase );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Priority Class Base",str,strDir); str.Format("%d",pe32.th32ProcessID);
    strList.Format("(%02d)PID = %d ",iCount,pe32.th32ProcessID );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"PID",str,strDir);
    str.Format("%d",pe32.cntThreads);
    strList.Format("(%02d)Thread Count = %d",iCount,pe32.cntThreads );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Thread Count",str,strDir);
    str.Format("%s",me32.szModule);
    strList.Format("(%02d)Module Name = %s ",iCount,me32.szModule );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Module Name",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);/*  str.Format("%d",pmc->PageFaultCount);
    strList.Format("(%02d)page faults = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"page faults",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);
    str.Format("%s",me32.szExePath);
    strList.Format("(%02d)Full Path = %s",iCount,me32.szExePath );
    m_strList.AddString(strList);
    WritePrivateProfileString(szTemp,"Full Path",str,strDir);*/
    RefreshThreadList(pe32.th32ProcessID,iCount);
    strList.Format("(%02d) *****************************************",iCount);
    m_strList.AddString(strList);
    iCount ++;
             } 
            } 
            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); 
    }