HANDLE hQuery=NULL;
PdhOpenQuery(NULL,0,&hQuery);
HANDLE hCounter=NULL;
DWORD nHandles = 0;
PdhAddCounter(hQuery,L"D:\Program Files\Tencent\QQ\QQ.exe",0,&hCounter);
if(NULL==hCounter || INVALID_HANDLE_VALUE==hCounter)
{
PdhCloseQuery(hQuery);
nHandles = 0; //句柄数
}
if(ERROR_SUCCESS==PdhCollectQueryData(hQuery))
{
PDH_FMT_COUNTERVALUE pdhfmtHandles;
if(ERROR_SUCCESS==PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG, NULL, &pdhfmtHandles))
nHandles = DWORD(pdhfmtHandles.longValue);
}
if(NULL!=hCounter && INVALID_HANDLE_VALUE!=hCounter)
PdhRemoveCounter(hCounter);
if(NULL!=hQuery && INVALID_HANDLE_VALUE!=hQuery)
PdhCloseQuery(hQuery);
// 句柄数
label17->Text = ((UInt32)nHandles).ToString();
PdhOpenQuery(NULL,0,&hQuery);
HANDLE hCounter=NULL;
DWORD nHandles = 0;
PdhAddCounter(hQuery,L"D:\Program Files\Tencent\QQ\QQ.exe",0,&hCounter);
if(NULL==hCounter || INVALID_HANDLE_VALUE==hCounter)
{
PdhCloseQuery(hQuery);
nHandles = 0; //句柄数
}
if(ERROR_SUCCESS==PdhCollectQueryData(hQuery))
{
PDH_FMT_COUNTERVALUE pdhfmtHandles;
if(ERROR_SUCCESS==PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG, NULL, &pdhfmtHandles))
nHandles = DWORD(pdhfmtHandles.longValue);
}
if(NULL!=hCounter && INVALID_HANDLE_VALUE!=hCounter)
PdhRemoveCounter(hCounter);
if(NULL!=hQuery && INVALID_HANDLE_VALUE!=hQuery)
PdhCloseQuery(hQuery);
// 句柄数
label17->Text = ((UInt32)nHandles).ToString();
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <Winbase.h>
#include <psapi.h>
#include <oledb.h>
#include <pdh.h>
#include <stdlib.h>
#include <wchar.h> #pragma comment(lib,"pdh.lib")
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "ADVAPI32.lib")这句PdhAddCounter(hQuery,L"D:\Program Files\Tencent\QQ\QQ.exe",0,&hCounter);
执行完以后 hCounter值仍然为空.
#define ProcessHandleCount 20typedef long (__stdcall *NTQUERYINFORMATIONPROCESS)(HANDLE ProcessHandle, int ProcessInformationClass, PVOID ProcessInformation, ULONG ProcessInformationLength, PULONG ReturnLength);DWORD GetProcessHandleCount(DWORD ProcessId)
{
DWORD handleCount = -1;
HMODULE lib = LoadLibrary(L"NTDLL.dll");
if (lib != NULL)
{
NTQUERYINFORMATIONPROCESS NtQueryInformationProcess = (NTQUERYINFORMATIONPROCESS)GetProcAddress(lib, "NtQueryInformationProcess");
if (NtQueryInformationProcess != NULL)
{
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ProcessId);
if (process != NULL)
{
NtQueryInformationProcess(process, 20, &handleCount, sizeof(handleCount), NULL);
::CloseHandle(process);
}
}
FreeLibrary(lib);
}
return handleCount;
}