Get the number of handles of the specified process

zhaozj2021-02-16  92

The environment is VC6.0 Win2000 cannot use the getProcessHandlecount function.

/ ************************************************** ********************************** /

DentiStryDoctor provides the following scenario:

GetProcessHandleCount not not be used in VC6.0, but can only be used under XP and 2003. Option One #ifndef WINVER # define WINVER 0X501 # endif # ifndef _WIN32_WINNT # define _WIN32_WINNT 0x501 # endif # ifndef _WIN32_WINDOWS // allows the use of Windows XP Or higher versions of specific features. #define _win32_windows 0x0501 // Changes to Windows XP and updated versions to the appropriate value. #ENDIF

Solution 2: Using PSAPI (can be run correctly above 200) #include #include #pragma Comment (lib, "pdh.lib") # include # pragma comment (lib, "psapi.lib") // returns the specified process handles BOOL EnableDebugPrivilege (BOOL fEnable) {BOOL fOK = FALSE; hANDLE hToken = NULL; if (OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, & hToken)) { TOKEN_PRIVILEGES tp; tp.PrivilegeCount = 1; LookupPrivilegeValue (NULL, SE_DEBUG_NAME, & tp.Privileges [0] .Luid); tp.Privileges [0] .Attributes = fEnable SE_PRIVILEGE_ENABLED: 0; AdjustTokenPrivileges (hToken, FALSE, & tp, sizeof (? tp), NULL, NULL); fOK = (GetLastError () == ERROR_SUCCESS); CloseHandle (hToken);} return fOK;!} DWORD GetProcHandleCount (HANDLE hProcess) {if (EnableDebugPrivilege (TRUE)) {return 0;} HANDLE hQuery = NULL; PdhOpenQuery (NULL, 0, & hQuery); if (NULL == hQuery) {return 0;} HANDLE hCounter = NULL; char szProcessName [MAX_PATH] = "unknown"; HMODULE hMod = NULL; GetModuleBaseName (hProcess, hMod SzprocessName, SizeOf (SzprocessName); char szname [max_path] = ""; _TSPLITPATH ​​(S zProcessName, NULL, NULL, szName, NULL); char szCounterPath [MAX_PATH] = ""; sprintf (szCounterPath, "// Process (% s) // Handle Count", szName); PdhAddCounter (hQuery, szCounterPath, 0, & hCounter ); if (NULL == hCounter || INVALID_HANDLE_VALUE == hCounter) {PdhCloseQuery (hQuery); return 0;} DWORD 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);}} (null! = hcounter &&

! INVALID_HANDLE_VALUE = hCounter) PdhRemoveCounter (hCounter);!! If (NULL = hQuery && INVALID_HANDLE_VALUE = hQuery) PdhCloseQuery (hQuery); EnableDebugPrivilege (FALSE); return nHandles;} More to contact me (readytogo@163.com, DentiSTRYDOctor@msn.com) / ************************************************************ ****************************** /

The problem has been solved ^ _ ^ method used by DentistryDoctor (dentist): BOOL EnabledeBugPrivilege (Bool Fenable) and DWORD GETPROCHLECOUNT (Handle HProcess)

At Win2000, VC6.0, I use #include and #pragma comment (lib, "psapi.lib") will be wrong, the system can't find PSAPI.H and PSAPI.LIB, so only to go Local search found these two files, put it under the directory, it seems to be the case later, because I am going to provide a DLL, this DLL is used to query all aspects of the specified process.

转载请注明原文地址:https://www.9cbs.com/read-12144.html

New Post(0)