' ########################################################################################
' Microsoft Windows
' File: Psapi.inc
' Contents: Include file for APIs provided by PSAPI.DLL
' Copyright (c) 2011 Jos? Roca
' Portions Copyright (c) Microsoft Corporation.
' All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
#IF NOT %DEF(PBX_Psapi_PBI)
#DEFINE PBX_Psapi_PBI
%EQUATE `PSAPI_INC = 1
#INCLUDE ONCE "Core/Windows.inc"
%EQUATE `LIST_MODULES_DEFAULT = &H0 ' // This is the default one app would get without any flag.
%EQUATE `LIST_MODULES_32BIT = &H01 ' // list 32bit modules in the target process.
%EQUATE `LIST_MODULES_64BIT = &H02 ' // list all 64bit modules. 32bit exe will be stripped off.
'// list all the modules
%EQUATE `LIST_MODULES_ALL = %LIST_MODULES_32BIT OR %LIST_MODULES_64BIT
DECLARE FUNCTION EnumProcesses LIB "psapi.dll" ALIAS "EnumProcesses" ( BYREF lpidProcess AS QUAD , BYVAL cb AS QUAD , BYREF lpcbNeeded AS QUAD ) AS LONG
DECLARE FUNCTION EnumProcessModules LIB "psapi.dll" ALIAS "EnumProcessModules" ( BYVAL hProcess AS QUAD
 , BYREF lphModule AS DWORD
 , BYVAL cb AS DWORD
 , BYREF lpcbNeeded AS DWORD
 ) AS LONG
DECLARE FUNCTION EnumProcessModulesEx LIB "psapi.dll" ALIAS "EnumProcessModulesEx" ( BYVAL hProcess AS DWORD
 , BYVAL lphModule AS DWORD
 , BYVAL cb AS DWORD
 , BYREF lpcbNeeded AS DWORD
 , BYVAL dwFilterFlag AS DWORD
 ) AS LONG
DECLARE FUNCTION GetModuleBaseNameA LIB "psapi.dll" ALIAS "GetModuleBaseNameA" ( BYVAL hProcess AS DWORD
 , BYVAL hModule AS DWORD
 , BYREF lpBaseName AS ASCIIZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetModuleBaseNameW LIB "psapi.dll" ALIAS "GetModuleBaseNameW" ( BYVAL hProcess AS DWORD
 , BYVAL hModule AS DWORD
 , BYREF lpBaseName AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetModuleBaseName = GetModuleBaseNameW
#ELSE
 MACRO GetModuleBaseName = GetModuleBaseNameA
#ENDIF
DECLARE FUNCTION GetModuleFileNameExA LIB "psapi.dll" ALIAS "GetModuleFileNameExA" ( BYVAL hProcess AS QUAD , BYVAL hModule AS QUAD , BYREF lpFilename AS ASCIIZ , BYVAL nSize AS QUAD ) AS QUAD
DECLARE FUNCTION GetModuleFileNameExW LIB "psapi.dll" ALIAS "GetModuleFileNameExW" ( BYVAL hProcess AS QUAD
 , BYVAL hModule AS DWORD
 , BYREF lpFilename AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetModuleFileNameEx = GetModuleFileNameExW
#ELSE
 MACRO GetModuleFileNameEx = GetModuleFileNameExA
#ENDIF
' // Size = 12 bytes
TYPE MODULEINFO DWORD
 lpBaseOfDll AS DWORD
 SizeOfImage AS DWORD
 EntryPoint AS DWORD
END TYPE
DECLARE FUNCTION GetModuleInformation LIB "psapi.dll" ALIAS "GetModuleInformation" ( BYVAL hProcess AS QUAD , BYVAL hModule AS QUAD , BYREF lpmodinfo AS MODULEINFO , BYVAL cb AS QUAD ) AS LONG
DECLARE FUNCTION EmptyWorkingSet LIB "psapi.dll" ALIAS "EmptyWorkingSet" ( BYVAL hProcess AS QUAD
 ) AS LONG
'//
'// Working set information structures. All non-specified bits are reserved.
'//
'typedef union _PSAPI_WORKING_SET_BLOCK {
' ULONG_PTR Flags;
' struct {
' ULONG_PTR Protection : 5;
' ULONG_PTR ShareCount : 3;
' ULONG_PTR Shared : 1;
' ULONG_PTR Reserved : 3;
'#if defined(_WIN64)
' ULONG_PTR VirtualPage : 52;
'#else
' ULONG_PTR VirtualPage : 20;
'#endif
' };
'} PSAPI_WORKING_SET_BLOCK, *PPSAPI_WORKING_SET_BLOCK;
'#IF %DEF(%WIN64) THEN
'' // Size = 8 bytes
'TYPE PSAPI_WORKING_SET_BLOCK_STRUCT QWORD
' Protection AS BIT * 5 IN QUAD ' ULONG_PTR
' ShareCount AS BIT * 3
' Shared AS BIT * 1
' Reserved AS BIT * 3
' VirtualPage AS BIT * 52
'END TYPE
'#ELSE
' // Size = 4 bytes
TYPE PSAPI_WORKING_SET_BLOCK_STRUCT DWORD
 Protection AS BIT * 5 IN DWORD ' ULONG_PTR
 ShareCount AS BIT * 3
 Shared AS BIT * 1
 Reserved AS BIT * 3
 VirtualPage AS BIT * 20
END TYPE
'#ENDIF
'#IF %DEF(%WIN64) THEN
' // Size = 8 bytes
'UNION PSAPI_WORKING_SET_BLOCK QWORD
' Flags AS QUAD ' ULONG_PTR
' PSAPI_WORKING_SET_BLOCK_STRUCT
'END UNION
'#ELSE
' // Size = 4 bytes
UNION PSAPI_WORKING_SET_BLOCK DWORD
 Flags AS DWORD ' ULONG_PTR
 PSAPI_WORKING_SET_BLOCK_STRUCT
END UNION
'#ENDIF
' // Size = 8 bytes
TYPE PSAPI_WORKING_SET_INFORMATION DWORD
 NumberOfEntries AS DWORD ' ULONG_PTR
 WorkingSetInfo(0) AS PSAPI_WORKING_SET_BLOCK
END TYPE
'typedef union _PSAPI_WORKING_SET_EX_BLOCK {
' ULONG_PTR Flags;
' struct {
' ULONG_PTR Valid : 1; // The following fields are valid only if this bit is set
' ULONG_PTR ShareCount : 3;
' ULONG_PTR Win32Protection : 11;
' ULONG_PTR Shared : 1;
' ULONG_PTR Node : 6;
' ULONG_PTR Locked : 1;
' ULONG_PTR LargePage : 1;
' };
'} PSAPI_WORKING_SET_EX_BLOCK, *PPSAPI_WORKING_SET_EX_BLOCK;
'#IF %DEF(%WIN64) THEN
' // Size = 8 bytes
'TYPE PSAPI_WORKING_SET_EX_BLOCK_STRUCT QDWORD
' Valid AS BIT * 1 IN QUAD ' ULONG_PTR // The following fields are valid only if this bit is set
' ShareCount AS BIT * 3
' Win32Protection AS BIT * 11
' Shared AS BIT * 1
' Node AS BIT * 6
' Locked AS BIT * 1
' LargePage AS BIT * 1
'END TYPE
'#ELSE
' // Size = 4 bytes
TYPE PSAPI_WORKING_SET_EX_BLOCK_STRUCT DWORD
 Valid AS BIT * 1 IN DWORD ' ULONG_PTR // The following fields are valid only if this bit is set
 ShareCount AS BIT * 3
 Win32Protection AS BIT * 11
 Shared AS BIT * 1
 Node AS BIT * 6
 Locked AS BIT * 1
 LargePage AS BIT * 1
END TYPE
'#ENDIF
' // Size = 4 bytes
UNION PSAPI_WORKING_SET_EX_BLOCK DWORD
 Flags AS DWORD ' ULONG_PTR
 PSAPI_WORKING_SET_EX_BLOCK_STRUCT
END UNION
' // Size = 8 bytes
TYPE PSAPI_WORKING_SET_EX_INFORMATION DWORD
 VirtualAddress AS DWORD
 VirtualAttributes AS PSAPI_WORKING_SET_EX_BLOCK
END TYPE
DECLARE FUNCTION QueryWorkingSet LIB "psapi.dll" ALIAS "QueryWorkingSet" ( BYVAL hProcess AS QUAD , BYVAL pv AS QUAD , BYVAL cb AS QUAD ) AS LONG
DECLARE FUNCTION QueryWorkingSetEx LIB "psapi.dll" ALIAS "QueryWorkingSetEx" ( BYVAL hProcess AS QUAD
 , BYVAL pv AS DWORD
 , BYVAL cb AS DWORD
 ) AS LONG
DECLARE FUNCTION InitializeProcessForWsWatch LIB "psapi.dll" ALIAS "InitializeProcessForWsWatch" ( BYVAL hProcess AS DWORD
 ) AS LONG
' // Size = 8 bytes
TYPE PSAPI_WS_WATCH_INFORMATION DWORD
 FaultingPc AS DWORD
 FaultingVa AS DWORD
END TYPE
' // Size = 16 bytes
TYPE PSAPI_WS_WATCH_INFORMATION_EX DWORD
 BasicInfo AS PSAPI_WS_WATCH_INFORMATION
 FaultingThreadId AS DWORD ' ULONG_PTR
 Flags AS DWORD ' ULONG_PTR // Reserved
END TYPE
DECLARE FUNCTION GetWsChanges LIB "psapi.dll" ALIAS "GetWsChanges" ( BYVAL hProcess AS QUAD , BYREF lpWatchInfo AS PSAPI_WS_WATCH_INFORMATION , BYVAL cb AS QUAD ) AS LONG
DECLARE FUNCTION GetWsChangesEx LIB "psapi.dll" ALIAS "GetWsChangesEx" ( BYVAL hProcess AS QUAD
 , BYREF lpWatchInfoEx AS PSAPI_WS_WATCH_INFORMATION_EX
 , BYVAL cb AS DWORD
 ) AS LONG
DECLARE FUNCTION GetMappedFileNameW LIB "psapi.dll" ALIAS "GetMappedFileNameW" ( BYVAL hProcess AS DWORD
 , BYVAL lpv AS DWORD
 , BYREF lpFilename AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetMappedFileNameA LIB "psapi.dll" ALIAS "GetMappedFileNameA" ( BYVAL hProcess AS DWORD
 , BYVAL lpv AS DWORD
 , BYREF lpFilename AS ASCIIZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetMappedFileName = GetMappedFileNameW
#ELSE
 MACRO GetMappedFileName = GetMappedFileNameA
#ENDIF
DECLARE FUNCTION EnumDeviceDrivers LIB "psapi.dll" ALIAS "EnumDeviceDrivers" ( BYREF lpImageBase AS ANY , BYVAL cb AS QUAD , BYREF lpcbNeeded AS QUAD ) AS LONG
DECLARE FUNCTION GetDeviceDriverBaseNameA LIB "psapi.dll" ALIAS "GetDeviceDriverBaseNameA" ( BYVAL ImageBase AS QUAD
 , BYREF lpFilename AS ASCIIZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
DECLARE FUNCTION GetDeviceDriverBaseNameW LIB "psapi.dll" ALIAS "GetDeviceDriverBaseNameW" ( BYVAL ImageBase AS DWORD
 , BYREF lpBaseName AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetDeviceDriverBaseName = GetDeviceDriverBaseNameW
#ELSE
 MACRO GetDeviceDriverBaseName = GetDeviceDriverBaseNameA
#ENDIF
DECLARE FUNCTION GetDeviceDriverFileNameA LIB "psapi.dll" ALIAS "GetDeviceDriverFileNameA" ( BYVAL ImageBase AS QUAD , BYREF lpFilename AS ASCIIZ , BYVAL nSize AS QUAD ) AS QUAD
DECLARE FUNCTION GetDeviceDriverFileNameW LIB "psapi.dll" ALIAS "GetDeviceDriverFileNameW" ( BYVAL ImageBase AS QUAD
 , BYREF lpFilename AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetDeviceDriverFileName = GetDeviceDriverFileNameW
#ELSE
 MACRO GetDeviceDriverFileName = GetDeviceDriverFileNameA
#ENDIF
'// Structure for GetProcessMemoryInfo()
' // Size = 40 bytes
TYPE PROCESS_MEMORY_COUNTERS DWORD
 cb AS DWORD
 PageFaultCount AS DWORD
 PeakWorkingSetSize AS DWORD ' SIZE_T
 WorkingSetSize AS DWORD ' SIZE_T
 QuotaPeakPagedPoolUsage AS DWORD ' SIZE_T
 QuotaPagedPoolUsage AS DWORD ' SIZE_T
 QuotaPeakNonPagedPoolUsage AS DWORD ' SIZE_T
 QuotaNonPagedPoolUsage AS DWORD ' SIZE_T
 PagefileUsage AS DWORD ' SIZE_T
 PeakPagefileUsage AS DWORD ' SIZE_T
END TYPE
'#if (_WIN32_WINNT >= 0x0501)
' Requires Windows Vista or Windows XP SP2.
' // Size = 44 bytes
TYPE PROCESS_MEMORY_COUNTERS_EX DWORD
 cb AS DWORD
 PageFaultCount AS DWORD
 PeakWorkingSetSize AS DWORD ' SIZE_T
 WorkingSetSize AS DWORD ' SIZE_T
 QuotaPeakPagedPoolUsage AS DWORD ' SIZE_T
 QuotaPagedPoolUsage AS DWORD ' SIZE_T
 QuotaPeakNonPagedPoolUsage AS DWORD ' SIZE_T
 QuotaNonPagedPoolUsage AS DWORD ' SIZE_T
 PagefileUsage AS DWORD ' SIZE_T
 PeakPagefileUsage AS DWORD ' SIZE_T
 PrivateUsage AS DWORD ' SIZE_T
END TYPE
'#endif
DECLARE FUNCTION GetProcessMemoryInfo LIB "psapi.dll" ALIAS "GetProcessMemoryInfo" ( BYVAL hProcess AS QUAD , BYREF ppsmemCounters AS PROCESS_MEMORY_COUNTERS , BYVAL cb AS QUAD ) AS LONG
' // Size = 56 bytes
TYPE PERFORMANCE_INFORMATION DWORD
 cb AS DWORD
 CommitTotal AS DWORD ' SIZE_T
 CommitLimit AS DWORD ' SIZE_T
 CommitPeak AS DWORD ' SIZE_T
 PhysicalTotal AS DWORD ' SIZE_T
 PhysicalAvailable AS DWORD ' SIZE_T
 SystemCache AS DWORD ' SIZE_T
 KernelTotal AS DWORD ' SIZE_T
 KernelPaged AS DWORD ' SIZE_T
 KernelNonpaged AS DWORD ' SIZE_T
 PageSize AS DWORD ' SIZE_T
 HandleCount AS DWORD
 ProcessCount AS DWORD
 ThreadCount AS DWORD
END TYPE
DECLARE FUNCTION GetPerformanceInfo LIB "psapi.dll" ALIAS "GetPerformanceInfo" ( BYREF pPerformanceInformation AS PERFORMANCE_INFORMATION , BYREF cb AS QUAD ) AS LONG
' // Size = 20 bytes
TYPE ENUM_PAGE_FILE_INFORMATION DWORD
 cb AS DWORD
 Reserved AS DWORD
 TotalSize AS DWORD ' SIZE_T
 TotalInUse AS DWORD ' SIZE_T
 PeakUsage AS DWORD ' SIZE_T
END TYPE
'typedef BOOL (*PENUM_PAGE_FILE_CALLBACKW) (LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCWSTR lpFilename);
'FUNCTION ENUM_PAGE_FILE_CALLBACKW ( ' BYVAL pContext AS DWORD
' , BYREF pPageFileInfo AS ENUM_PAGE_FILE_INFORMATION
' , BYREF lpFileName AS WSTRINGZ
' ) AS LONG
'END FUNCTION
'typedef BOOL (*PENUM_PAGE_FILE_CALLBACKA) (LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCSTR lpFilename);
'FUNCTION ENUM_PAGE_FILE_CALLBACKA ( ' BYVAL pContext AS DWORD
' , BYREF pPageFileInfo AS ENUM_PAGE_FILE_INFORMATION
' , BYREF lpFileName AS ASCIIZ
' ) AS LONG
'END FUNCTION
DECLARE FUNCTION EnumPageFilesW LIB "psapi.dll" ALIAS "EnumPageFilesW" ( BYREF pCallbackRoutine AS QUAD , BYVAL lpContext AS QUAD ) AS LONG
DECLARE FUNCTION EnumPageFilesA LIB "psapi.dll" ALIAS "EnumPageFilesA" ( BYREF pCallbackRoutine AS QUAD
 , BYVAL lpContext AS DWORD
 ) AS LONG
#IF %DEF(%UNICODE)
 MACRO EnumPageFiles = EnumPageFilesW
#ELSE
 MACRO EnumPageFiles = EnumPageFilesA
#ENDIF
DECLARE FUNCTION GetProcessImageFileNameA LIB "psapi.dll" ALIAS "GetProcessImageFileNameA" ( BYVAL hProcess AS QUAD , BYREF lpImageFileName AS ASCIIZ , BYVAL nSize AS QUAD ) AS QUAD
DECLARE FUNCTION GetProcessImageFileNameW LIB "psapi.dll" ALIAS "GetProcessImageFileNameA" ( BYVAL hProcess AS QUAD
 , BYREF lpImageFileName AS WSTRINGZ
 , BYVAL nSize AS DWORD
 ) AS DWORD
#IF %DEF(%UNICODE)
 MACRO GetProcessImageFileName = GetProcessImageFileNameW
#ELSE
 MACRO GetProcessImageFileName = GetProcessImageFileNameA
#ENDIF
#ENDIF
