什么函数可以获取指定进程打开的句柄数目
解决方案 »
- 64位下键盘钩子有重大发现
- 如何仿做一个com组件
- vc++ ODBC sql server 2000 image 数据插入问题 请高手指教,谢谢!
- hashtable 的线程安全?
- 请问,ATL做的Outlook插件工程该怎么安装部署?
- Default Button问题
- 请问如何改变组合框Combo Box的下拉高度??
- 软件发布 YaCompile 批量自动编译VC工程
- 200分谁知道,CRecordSet怎么样管理两个表
- 程序分析:地形
- 哪个系统API可以获取指定进程使用的虚拟内存数, 以及内存增量(散分求助!!)
- 想写一款,专门支持IE的自动添表的共享小软件。 发现不知从何下手。 要写这个需要看那些方法的知识:)
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;// Calculate how many process identifiers were returned.cProcesses = cbNeeded / sizeof(DWORD);
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;
}
{
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;
}