什么函数可以获取指定进程打开的句柄数目

解决方案 »

  1.   

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;// Calculate how many process identifiers were returned.cProcesses = cbNeeded / sizeof(DWORD);
      

  2.   

    《Windows图形编程中》有这个例子
      

  3.   

    使用nataveAPI
    ULONG CProcessInfoManager::GetProcessInfo()
    {
    NTSTATUS nsRet;
    PSYSTEM_PROCESSES pBuf = NULL;
    ULONG ulSize=4096, ulRetSize;
    PLIterator it;

    do
    {
    LocalFree(pBuf);
    pBuf = (PSYSTEM_PROCESSES)LocalAlloc(LPTR, ulSize);
    if(pBuf == NULL)
    {
    BbsLog.LogMessage("localalloc  failed");
    }
    nsRet = m_pfNtQuerySystemInformation(SystemProcessesAndThreadsInformation, (LPVOID)pBuf, ulSize, &ulRetSize);
    ulSize <<= 1;
    } while (nsRet == STATUS_INFO_LENGTH_MISMATCH);


    if (STATUS_SUCCESS == nsRet)
    {
    PSYSTEM_PROCESSES pInfo = pBuf, pPre = pInfo;
    do
    {
    it = FindProcess(pInfo->ProcessName.Buffer);
    if(it != m_ProcessList.end())
    {
    it->ulPid = pInfo->ProcessId;
    it->ulCpuTime = (pInfo->KernelTime.QuadPart + pInfo->UserTime.QuadPart) / 10000000;
    it->ulPagefileUsage = pInfo->VmCounters.PagefileUsage;
    it->ulMaxWorkset = pInfo->VmCounters.PeakWorkingSetSize;
    it->ulWorksetInc = pInfo->VmCounters.WorkingSetSize - it->ulWorkset;
    it->ulWorkset = pInfo->VmCounters.WorkingSetSize; // set working set
    it->ulThreadCount = pInfo->ThreadCount; // set thread count
    it->ulHandleCount = pInfo->HandleCount; // set handle count
    it->szTime = GetTime();
    }
    pPre = pInfo;
    pInfo = (PSYSTEM_PROCESSES)((char *)pInfo + pInfo->NextEntryDelta);
    } while (pInfo != pPre);
    }

    LocalFree(pBuf);
    return nsRet;
    }
      

  4.   

    BOOL CProcessInfoCollector::LoadFunction()
    {
    m_hDLL = LoadLibrary("ntdll.dll");
    if (NULL == m_hDLL)
    {
    BbsLog.LogMessage("[信息]加载ntdll.dll失败\n[错误代码]=%u", GetLastError());
    return FALSE;
    }

    m_pfNtQuerySystemInformation = (pfNtQuerySystemInformation)GetProcAddress(m_hDLL, "NtQuerySystemInformation");
    if (NULL == m_pfNtQuerySystemInformation)
    {
    BbsLog.LogMessage("[信息]载入NtQuerySystemInformation失败\n[代码]=%u", GetLastError());
    return FALSE;
    } m_pfNtQueryInformationProcess = (pfNtQueryInformationProcess)GetProcAddress(m_hDLL, "NtQueryInformationProcess");
    if (NULL == m_pfNtQueryInformationProcess)
    {
    BbsLog.LogMessage("[信息]载入NtQueryInformationProcess失败\n[代码]=%u", GetLastError());
    return FALSE;
    } return TRUE;
    }ULONG CProcessInfoCollector::GetProcessInfo()
    {
    NTSTATUS nsRet;
    PSYSTEM_PROCESSES pBuf = NULL;
    ULONG ulSize = 4096;
    ULONG ulRetSize;
    PLIterator it;

    do
    {
    LocalFree(pBuf);
    pBuf = (PSYSTEM_PROCESSES)LocalAlloc(LPTR, ulSize);
    if(pBuf == NULL)
    {
    BbsLog.LogMessage("localalloc  failed");
    }
    nsRet = m_pfNtQuerySystemInformation(SystemProcessesAndThreadsInformation, (LPVOID)pBuf, ulSize, &ulRetSize);
    ulSize <<= 1;
    } while (nsRet == STATUS_INFO_LENGTH_MISMATCH);


    if (STATUS_SUCCESS == nsRet)
    {
    PSYSTEM_PROCESSES pInfo = pBuf, pPre = pInfo;
    do
    {
    it = FindProcess(pInfo->ProcessName.Buffer);
    if(it != m_ProcessList.end())
    {
    ULONG ulLastCpuTime = it->ulCpuTime;
    ULONG ulLastSysTime = it->lTime; 
    ULONG ulLastWorkset = it->ulWorkset;
    ULONG ulTempCpuTime = 0;
    it->ulPid = pInfo->ProcessId;
    it->ulCpuTime = (pInfo->KernelTime.QuadPart + pInfo->UserTime.QuadPart) / 10000;
    it->ulPagefileUsage = pInfo->VmCounters.PagefileUsage / 1024;
    it->ulMaxWorkset = pInfo->VmCounters.PeakWorkingSetSize / 1024;
    if(ulLastWorkset == 0)
    { //第一次采样时
    it->ulWorksetInc = 0;
    }
    else
    {
    it->ulWorksetInc = pInfo->VmCounters.WorkingSetSize / 1024 - ulLastWorkset;
    }
    it->ulWorkset = pInfo->VmCounters.WorkingSetSize / 1024; // set working set
    it->ulThreadCount = pInfo->ThreadCount; // set thread count
    it->ulHandleCount = pInfo->HandleCount; // set handle count
    it->lTime = GetSystemTime();
    if(ulLastCpuTime == 0)
    {
    //一般只有第一次采样信息时,之前的cpu使用时间为零
    //此时cpu利用率默认为零
    it->ulCpu = 0;
    }
    else
    {
    //因为系统时间单位是秒,cpu时间单位是毫秒,如果两次
    //采集时间间隔太短,计算cpu利用率会产生较大误差,此处
    //当cpu利用率超过100时,默认为100
    ulTempCpuTime = (it->ulCpuTime - ulLastCpuTime) * 100 / ((it->lTime - ulLastSysTime) * 1000) ;
    if(ulTempCpuTime > 100)
    {
    ulTempCpuTime = 100;
    }
    it->ulCpu = ulTempCpuTime;
    }
    it->bGotInfo = true;
    }
    pPre = pInfo;
    pInfo = (PSYSTEM_PROCESSES)((char *)pInfo + pInfo->NextEntryDelta);
    } while (pInfo != pPre);
    }

    LocalFree(pBuf);
    return nsRet;
    }