*30. July 2010, 00:21:47
Welcome, Guest. Please login or register.
30. July 2010, 00:21:47

Login with username, password and session length
857 Posts in 351 Topics by 54 Members - Latest Member: Elias Montoya

Please do not more post here, but use the NEW FORUM:

IT-Consultant-Forum NEW
Search:     Advanced search
IT-Consultant-Forum (IT-Berater)
* Home Help Search Calendar Login Register
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Print
Author Topic: PSAPI - Process Status API  (Read 3313 times)
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« on: 24. September 2006, 03:22:37 »

 
PSAPI

The process status API (PSAPI) provides sets of functions for retrieving the following information:

  • Process Information
  • Module Information
  • Device Driver Information
  • Process Memory Usage Information
  • Working Set Information
  • Memory-Mapped File Information

PSAPI.INC

The attached file contains the structures and functions needed to use PSAPI with the PowerBASIC compilers.

Code:
' ########################################################################################
' PSAPI.INC - Process Status Helper
' The process status helper functions make it easier for you to obtain information about
' processes and device drivers.
' ########################################################################################

#IF NOT %DEF(%PSAPI_INC)
    %PSAPI_INC = 1

   #IF NOT %DEF(%WINAPI)
       #INCLUDE "WIN32API.INC"
   #ENDIF

' ========================================================================================
' ENUM_PAGE_FILE_INFORMATION
' The ENUM_PAGE_FILE_INFORMATION structure contains information about a pagefile.
' ========================================================================================

TYPE ENUM_PAGE_FILE_INFORMATION
   cb AS DWORD                 ' DWORD  // Size of this structure, in bytes.
   Reserved AS DWORD           ' DWORD  // This member is reserved.
   TotalSize AS DWORD          ' SIZE_T // Total size of the pagefile, in bytes.
   TotalInUse AS DWORD         ' SIZE_T // Current pagefile usage, in bytes.
   PeakUsage AS DWORD          ' SIZE_T // Peak pagefile usage, in bytes.
END TYPE

' ========================================================================================
' MODULEINFO
' The MODULEINFO structure contains the module load address, size, and entry point.
' ========================================================================================

TYPE MODULEINFO
   lpBaseOfDll AS DWORD        ' LPVOID // Load address of the module.
   SizeOfImage AS DWORD        ' DWORD  // Size of the linear space that the module occupies, in bytes.
   EntryPoint AS DWORD         ' LPVOID // Entry point of the module.
END TYPE

' ========================================================================================
' PERFORMANCE_INFORMATION
' The PERFORMANCE_INFORMATION structure contains performance information.
' ========================================================================================

TYPE PERFORMANCE_INFORMATION
   cb AS DWORD                 ' DWORD  // Size of this structure, in bytes.
   CommitTotal AS DWORD        ' SIZE_T // Total number of pages committed by the system.
   CommitLimit AS DWORD        ' SIZE_T // Current maximum number of page commits that
                               '        // can be performed by the system. This number
                               '        // can change if memory is added or deleted, or
                               '        // if pagefiles have grown, shrunk, or been added.
   CommitPeak AS DWORD         ' SIZE_T // Maximum number of page commit totals that have
                               '        // occurred since the last reboot.
   PhysicalTotal AS DWORD      ' SIZE_T // Total amount of physical memory, in pages.
   PhysicalAvailable AS DWORD  ' SIZE_T // Amount of physical memory available to user processes, in pages.
   SystemCache AS DWORD        ' SIZE_T // Total amount of system cache memory, in pages.
   KernelTotal AS DWORD        ' SIZE_T // Total amount of the sum of the paged and nonpaged kernel pools, in pages.
   KernelPaged AS DWORD        ' SIZE_T // Total amount of the paged kernel pool, in pages.
   KernelNonpaged AS DWORD     ' SIZE_T // Total amount of the nonpaged kernel pool, in pages.
   PageSize AS DWORD           ' SIZE_T // Size of a page, in bytes.
   HandleCount AS DWORD        ' DWORD  // Total number of open handles.
   ProcessCount AS DWORD       ' DWORD  // Total number of processes.
   nThreadCount AS DWORD       ' DWORD  // Total number of threads.
END TYPE

' ========================================================================================
' PROCESS_MEMORY_COUNTERS
' The PROCESS_MEMORY_COUNTERS structure contains the memory statistics for a process.
' ========================================================================================

TYPE PROCESS_MEMORY_COUNTERS
   cb AS DWORD                           ' DWORD // Size of the structure, in bytes.
   PageFaultCount AS DWORD               ' DWORD // Number of page faults.
   PeakWorkingSetSize AS DWORD           ' SIZE_T // Peak working set size.
   WorkingSetSize AS DWORD               ' SIZE_T // Current working set size.
   QuotaPeakPagedPoolUsage AS DWORD      ' SIZE_T // Peak paged pool usage.
   QuotaPagedPoolUsage AS DWORD          ' SIZE_T // Current paged pool usage.
   QuotaPeakNonPagedPoolUsage AS DWORD   ' SIZE_T // Peak nonpaged pool usage.
   QuotaNonPagedPoolUsage AS DWORD       ' SIZE_T // Current nonpaged pool usage.
   PagefileUsage AS DWORD                ' SIZE_T // Current space allocated for the pagefile.
                                         ' // Those pages may or may not be in memory.
   PeakPagefileUsage AS DWORD            ' SIZE_T // Peak space allocated for the pagefile.
END TYPE

' ========================================================================================
' PROCESS_MEMORY_COUNTERS_EX
' The PROCESS_MEMORY_COUNTERS_EX structure contains extended memory statistics for a process.
' Requires Windows XP SP2.
' ========================================================================================

TYPE PROCESS_MEMORY_COUNTERS_EX
   cb AS DWORD                           ' DWORD  // Size of the structure, in bytes.
   PageFaultCount AS DWORD               ' DWORD  // Number of page faults.
   PeakWorkingSetSize AS DWORD           ' SIZE_T // Peak working set size.
   WorkingSetSize AS DWORD               ' SIZE_T // Current working set size.
   QuotaPeakPagedPoolUsage AS DWORD      ' SIZE_T // Peak paged pool usage.
   QuotaPagedPoolUsage AS DWORD          ' SIZE_T // Current paged pool usage.
   QuotaPeakNonPagedPoolUsage AS DWORD   ' SIZE_T // Peak nonpaged pool usage.
   QuotaNonPagedPoolUsage AS DWORD       ' SIZE_T // Current nonpaged pool usage.
   PagefileUsage AS DWORD                ' SIZE_T // Current space allocated for the pagefile.
                                         '        // Those pages may or may not be in memory.
   PeakPagefileUsage AS DWORD            ' SIZE_T // Peak space allocated for the pagefile.
   PrivateUsage AS DWORD                 ' SIZE_T // Current process private usage, in pages.
END TYPE

' ========================================================================================
' PSAPI_WS_WATCH_INFORMATION
' The PSAPI_WS_WATCH_INFORMATION structure contains information about a page added to a
' process working set.
' ========================================================================================

TYPE PSAPI_WS_WATCH_INFORMATION
   FaultingPc AS DWORD             ' LPVOID // Pointer to the instruction that caused the page fault.
   FaultingVa AS DWORD             ' LPVOID // Pointer to the page that was added to the working set.
END TYPE


' ========================================================================================
' The EmptyWorkingSet function removes as many pages as possible from the working set of
' the specified process.
' ========================================================================================

DECLARE FUNCTION EmptyWorkingSet LIB "PSAPI.DLL" ALIAS "EmptyWorkingSet" ( _
   BYVAL hProcess AS DWORD _                     ' [in] HANDLE hProcess
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The EnumDeviceDrivers function retrieves the load address for each device driver in
' the system.
' ========================================================================================

DECLARE FUNCTION EnumDeviceDrivers LIB "PSAPI.DLL" ALIAS "EnumDeviceDrivers" ( _
   BYREF lpImageBase AS DWORD, _                 ' [out] LPVOID* lpImageBase
   BYVAL cb AS DWORD, _                          ' [in]  DWORD cb
   BYREF lpcbNeeded AS DWORD _                   ' [out] LPDWORD lpcbNeeded
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The EnumPageFiles function calls the callback routine for each installed pagefile in
' the system.
' ========================================================================================

DECLARE FUNCTION EnumPageFiles LIB "PSAPI.DLL" ALIAS "EnumPageFilesA" ( _
   BYREF pCallbacRoutine AS DWORD, _             ' [out] PENUM_PAGE_CALLBACK pCallbackRoutine
   BYVAL lpContext AS DWORD _                    ' [in]  LPVOID lpContext
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The EnumPageFilesProc function is an application-defined callback function used with the
' EnumPageFiles function.
' The PENUM_PAGE_FILE_CALLBACK type defines a pointer to this callback function.
' EnumPageFilesProc is a placeholder for the application-defined function name.
' ========================================================================================

DECLARE FUNCTION EnumPageFilesProc ( _
   BYVAL pContext AS DWORD, _                             ' [in] LPVOID pContext
   BYREF pPageFileInfo AS ENUM_PAGE_FILE_INFORMATION, _   ' [in] ENUM_PAGE_FILE_INFORMATION pPageFileInfo
   BYREF lpFileName AS ASCIIZ _                           ' [in] LPCSTR lpFileName
   ) AS LONG                                              ' BOOL

DECLARE FUNCTION EnumPageFilesProcW ( _
   BYVAL pContext AS DWORD, _                             ' [in] LPVOID pContext
   BYREF pPageFileInfo AS ENUM_PAGE_FILE_INFORMATION, _   ' [in] ENUM_PAGE_FILE_INFORMATION pPageFileInfo
   BYVAL lpFileName AS DWORD _                            ' [in] LPCWSTR lpFileName
   ) AS LONG                                              ' BOOL

' ========================================================================================
' The EnumProcesses function retrieves the process identifier for each process object in
' the system.
' ========================================================================================

DECLARE FUNCTION EnumProcesses LIB "PSAPI.DLL" ALIAS "EnumProcesses" ( _
   BYREF pProcessIds AS DWORD, _                 ' [out] DWORD pProcessIds
   BYVAL cb AS DWORD, _                          ' [in]  DWORD cb
   BYREF pBytesReturned AS DWORD _               ' [out] DWORD* pBytesReturned
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The EnumProcessModules function retrieves a handle for each module in the specified process.
' ========================================================================================

DECLARE FUNCTION EnumProcessModules LIB "PSAPI.DLL" ALIAS "EnumProcessModules" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYREF lphModule AS DWORD, _                   ' [out] HMODULE lphModule
   BYVAL cb AS DWORD, _                          ' [in]  DWORD cb
   BYREF lpcbNeeded AS DWORD _                   ' [out] LPDWORD lphModule
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The GetDeviceDriverBaseName function retrieves the base name of the specified device driver.
' ========================================================================================

DECLARE FUNCTION GetDeviceDriverBaseName LIB "PSAPI.DLL" ALIAS "GetDeviceDriverBaseNameA" ( _
   BYVAL ImageBase AS DWORD, _                   ' [in]  LPVOID ImageBase
   BYREF lpBaseName AS ASCIIZ, _                 ' [out] LPCSTR lpBaseName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD

DECLARE FUNCTION GetDeviceDriverBaseNameW LIB "PSAPI.DLL" ALIAS "GetDeviceDriverBaseNameW" ( _
   BYVAL ImageBase AS DWORD, _                   ' [in]  LPVOID ImageBase
   BYVAL lpBaseName AS DWORD, _                  ' [out] LPWSTR lpBaseName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD

' ========================================================================================
' The GetDeviceDriverFileName function retrieves the fully qualified path for the
' specified device driver.
' ========================================================================================

DECLARE FUNCTION GetDeviceDriverFileName LIB "PSAPI.DLL" ALIAS "GetDeviceDriverFileNameA" ( _
   BYVAL ImageBase AS DWORD, _                   ' [in]  LPVOID ImageBase
   BYREF lpFileName AS ASCIIZ, _                 ' [out] LPCSTR lpFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

DECLARE FUNCTION GetDeviceDriverFileNameW LIB "PSAPI.DLL" ALIAS "GetDeviceDriverFileNameW" ( _
   BYVAL ImageBase AS DWORD, _                   ' [in]  LPVOID ImageBase
   BYVAL lpFileName AS DWORD, _                  ' [out] LPCWSTR lpFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

' ========================================================================================
' The GetMappedFileName function checks if the specified address is within a memory-mapped
' file in the address space of the specified process. If so, the function returns the name
' of the memory-mapped file.
' ========================================================================================

DECLARE FUNCTION GetMappedFileName LIB "PSAPI.DLL" ALIAS "GetMappedFileNameA" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL lpv AS DWORD, _                         ' [in]  LPVOID lpv
   BYREF lpFileName AS ASCIIZ, _                 ' [out] LPCSTR lpFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

DECLARE FUNCTION GetMappedFileNameW LIB "PSAPI.DLL" ALIAS "GetMappedFileNameW" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL lpv AS DWORD, _                         ' [in]  LPVOID lpv
   BYVAL lpFileName AS DWORD, _                  ' [out] LPCWSTR lpFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

' ========================================================================================
' The GetModuleBaseName function retrieves the base name of the specified module.
' ========================================================================================

DECLARE FUNCTION GetModuleBaseName LIB "PSAPI.DLL" ALIAS "GetModuleBaseNameA" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL hModule AS DWORD, _                     ' [in]  HMODULE hModule
   BYREF lpBaseName AS ASCIIZ, _                 ' [out] LPCSTR lpBaseName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

DECLARE FUNCTION GetModuleBaseNameW LIB "PSAPI.DLL" ALIAS "GetModuleBaseNameW" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL hModule AS DWORD, _                     ' [in]  HMODULE hModule
   BYVAL lpBaseName AS DWORD, _                  ' [out] LPCWSTR lpBaseName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

' ========================================================================================
' The GetModuleFileNameEx function retrieves the fully-qualified path for the file
' containing the specified module.
' ========================================================================================

DECLARE FUNCTION GetModuleFileNameEx LIB "PSAPI.DLL" ALIAS "GetModuleFileNameExA" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL hModule AS DWORD, _                     ' [in]  HMODULE hModule
   BYREF lpFilename AS ASCIIZ, _                 ' [out] LPCSTR lpFilename
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

DECLARE FUNCTION GetModuleFileNameExW LIB "PSAPI.DLL" ALIAS "GetModuleFileNameExW" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL hModule AS DWORD, _                     ' [in]  HMODULE hModule
   BYVAL lpFilename AS DWORD, _                  ' [out] LPCWSTR lpFilename
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

' ========================================================================================
' The GetModuleInformation function retrieves information about the specified module in
' the MODULEINFO structure.
' ========================================================================================

DECLARE FUNCTION GetModuleInformation LIB "PSAPI.DLL" ALIAS "GetModuleInformation" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL hModule AS DWORD, _                     ' [in]  HMODULE hModule
   BYREF lpmodinfo AS MODULEINFO, _              ' [out] LPMODULEINFO lpmodinfo
   BYVAL cb AS DWORD _                           ' [in]  DWORD cb
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The GetPerformanceInfo function returns the performance values contained in the
' PERFORMANCE_INFORMATION structure.
' ========================================================================================

DECLARE FUNCTION GetPerformanceInfo LIB "PSAPI.DLL" ALIAS "GetPerformanceInfo" ( _
   BYREF pPerformanceInformation AS PERFORMANCE_INFORMATION, _   ' [out] PPERFORMANCE_INFORMATION pPerformanceInformation
   BYREF cb AS DWORD _                                           ' [in]  DWORD cb
   ) AS LONG                                                     ' BOOL

' ========================================================================================
' The GetProcessImageFileName function retrieves the name of the executable file for the
' specified process.
' ========================================================================================

DECLARE FUNCTION GetProcessImageFileName LIB "PSAPI.DLL" ALIAS "GetProcessImageFileNameA" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYREF lpImageFileName AS ASCIIZ, _            ' [out] LPCSTR lpImageFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

DECLARE FUNCTION GetProcessImageFileNameW LIB "PSAPI.DLL" ALIAS "GetProcessImageFileNameW" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL lpImageFileName AS DWORD, _             ' [out] LPCWSTR lpImageFileName
   BYVAL nSize AS DWORD _                        ' [in]  DWORD nSize
   ) AS DWORD                                    ' DWORD

' ========================================================================================
' The GetProcessMemoryInfo function retrieves information about the memory usage of the
' specified process.
' ========================================================================================

DECLARE FUNCTION GetProcessMemoryInfo LIB "PSAPI.DLL" ALIAS "GetProcessMemoryInfo" ( _
   BYVAL hProcess AS DWORD, _                           ' [in]  HANDLE hProcess
   BYREF ppsmemCounters AS PROCESS_MEMORY_COUNTERS, _   ' [out] PPROCESS_MEMORY_COUNTERS ppsmemCounters
   BYVAL cb AS DWORD _                                  ' [in]  DWORD cb
   ) AS LONG                                            ' BOOL

' ========================================================================================
' The GetWsChanges function retrieves information about the pages that have been added to
' the working set of the specified process since the InitializeProcessForWsWatch function
' initiated monitoring.
' ========================================================================================

DECLARE FUNCTION GetWsChanges LIB "PSAPI.DLL" ALIAS "GetWsChanges" ( _
   BYVAL hProcess AS DWORD, _                           ' [in]  HANDLE hProcess
   BYREF lpWatchInfo AS PSAPI_WS_WATCH_INFORMATION, _   ' [out] PPSAPI_WS_WATCH_INFORMATION lpWatchInfo
   BYVAL cb AS DWORD _                                  ' [in]  DWORD cb
   ) AS LONG                                            ' BOOL

' ========================================================================================
' The InitializeProcessForWsWatch function initiates monitoring of the working set of the
' specified process. You must call this function before calling the GetWsChanges function.
' ========================================================================================

DECLARE FUNCTION InitializeProcessForWsWatch LIB "PSAPI.DLL" ALIAS "InitializeProcessForWsWatch" ( _
   BYVAL hProcess AS DWORD _                     ' [in] HANDLE hProcess
   ) AS LONG                                     ' BOOL

' ========================================================================================
' The QueryWorkingSet function retrieves information about the pages currently added to
' the working set of the specified process.
' ========================================================================================

DECLARE FUNCTION QueryWorkingSet LIB "PSAPI.DLL" ALIAS "QueryWorkingSet" ( _
   BYVAL hProcess AS DWORD, _                    ' [in]  HANDLE hProcess
   BYVAL pv AS DWORD, _                          ' [out] PVOID pv
   BYVAL cb AS DWORD _                           ' [in]  DWORD cb
   ) AS LONG                                     ' BOOL

#ENDIF
« Last Edit: 09. November 2006, 16:53:48 by José Roca » Logged
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« Reply #1 on: 24. September 2006, 03:34:03 »

 
The following sample code uses the EnumProcesses function to enumerate the current processes in the system.

Code:
' ========================================================================================
' Enumerating all processes.
' This is a translation of an example included in the MSDN documentation for PSAPI.
' ========================================================================================

' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "PSAPI.INC"

' ========================================================================================
' Displays the processes
' ========================================================================================
SUB PrintProcessNameAndID (BYVAL processID AS DWORD)

   LOCAL szProcessName AS ASCIIZ * %MAX_PATH
   LOCAL hProcess AS DWORD
   LOCAL hMod AS DWORD
   LOCAL cbNeeded AS DWORD

   szProcessName = "unknown"
   '// Get a handle to the process.
   hProcess = OpenProcess(%PROCESS_QUERY_INFORMATION OR _
                          %PROCESS_VM_READ, %FALSE, processID)
   '// Get the process name.
   IF ISFALSE hProcess THEN EXIT SUB
   IF ISFALSE EnumProcessModules(hProcess, hMod, SIZEOF(hMod), cbNeeded) THEN EXIT SUB
   GetModuleBaseName hProcess, hMod, szProcessName, SIZEOF(szProcessName)

   '// Print the process name and identifier.
   PRINT "Process ID: ", szProcessName, processID

   CloseHandle hProcess

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   '// Get the list of process identifiers.
   DIM aProcesses(0 TO 1023) AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL cProcesses AS DWORD
   LOCAL i AS DWORD

   IF ISFALSE EnumProcesses(aProcesses(LBOUND(aProcesses)), _
      (UBOUND(aProcesses) - LBOUND(aProcesses) + 1) * 4, cbNeeded) THEN EXIT FUNCTION

   '// Calculate how many process identifiers were returned.
   cProcesses = cbNeeded \ 4

   '// Print the name and process identifier for each process.

   FOR i = 0 TO cProcesses - 1
      PrintProcessNameAndId aProcesses(i)
   NEXT

   WAITKEY$

END FUNCTION
' ========================================================================================
Logged
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« Reply #2 on: 24. September 2006, 03:35:10 »

 
To determine which processes have loaded a particular DLL, you must enumerate the modules for each process. The following sample code uses the EnumProcessModules function to enumerate the modules of current processes in the system.

Code:
' ========================================================================================
' Enumerating all modules for a process.
' This is a translation of an example included in the MSDN documentation for PSAPI.
' ========================================================================================

' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "PSAPI.INC"

' ========================================================================================
' Displays the modules
' ========================================================================================
SUB PrintModules (BYVAL processID AS DWORD)

   DIM   hMods(0 TO 1023) AS DWORD
   LOCAL hProcess AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL i AS LONG
   LOCAL szModName AS ASCIIZ * %MAX_PATH

   '// Print the process identifier.
   PRINT "Process ID: ", processID

   '// Get a list of all the modules in this process.
   hProcess = Openprocess (%PROCESS_QUERY_INFORMATION OR _
                           %PROCESS_VM_READ, %FALSE, processID)
   IF hProcess = 0 THEN EXIT SUB

   IF ISFALSE EnumProcessModules(hProcess, hMods(LBOUND(hMods)), _
      (UBOUND(hMods) - LBOUND(hMods) + 1) * 4, cbNeeded) THEN EXIT SUB

   FOR i = 0 TO cbNeeded \ 4
      '// Get full path to the module's file
      IF GetModuleFileNameEx(hProcess, hMods(i), szModName, SIZEOF(szModName)) THEN
         '// Print the module name and handle value.
         PRINT szModName, hMods(i)
      END IF
   NEXT

   CloseHandle hProcess

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   DIM aProcesses(1024) AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL cProcesses AS DWORD
   LOCAL i AS LONG

   IF ISFALSE EnumProcesses(aProcesses(LBOUND(aProcesses)), _
      (UBOUND(aProcesses) - LBOUND(aProcesses) + 1) * 4, cbNeeded) THEN EXIT FUNCTION

   '// Calculate how many process identifiers were returned.
   cProcesses = cbNeeded \ 4

   '// Print the name of the module for each process.

   FOR i = 0 TO cProcesses - 1
      PrintModules aProcesses(i)
   NEXT

   WAITKEY$

END FUNCTION
' ========================================================================================
Logged
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« Reply #3 on: 24. September 2006, 03:36:24 »

 
To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo function to obtain information about the memory usage of a process.

Code:
' ========================================================================================
' Collecting memory usage information for a process.
' This is a translation of an example included in the MSDN documentation for PSAPI.
' ========================================================================================

' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "PSAPI.INC"

' ========================================================================================
' Displays the information
' ========================================================================================
SUB PrintMemoryInfo (BYVAL processID AS DWORD)

   LOCAL hProcess AS DWORD
   LOCAL pmc AS PROCESS_MEMORY_COUNTERS

   '// Print the process identifier
   PRINT "Process ID: " processID

   '// Print information about the memory usage of the process.
   hProcess = Openprocess (%PROCESS_QUERY_INFORMATION OR _
                           %PROCESS_VM_READ, %FALSE, processID)

   IF hProcess = 0 THEN EXIT SUB

   IF (GetProcessMemoryInfo(hProcess, pmc, SIZEOF(pmc))) THEN
      PRINT "PageFaultCount: ", pmc.PageFaultCount
      PRINT "PeakWorkinSetSize: ", pmc.PeakWorkingSetSize
      PRINT "WorkingSetSize: "pmc.WorkingSetSize
      PRINT "QuotaPeakPagegPoolUsage: ", pmc.QuotaPeakPagedPoolUsage
      PRINT "QuotaPagedPoolUsage: ", pmc.QuotaPagedPoolUsage
      PRINT "QuotaPeakNonPagedPoolUsage: ", pmc.QuotaPeakNonPagedPoolUsage
      PRINT "QuotaNonPagedPoolUsage: ", pmc.QuotaNonPagedPoolUsage
      PRINT "PageFileUsage: ", pmc.PageFileUsage
      PRINT "PeakPageFileUsage: ", pmc.PeakPageFileUsage
   END IF

   CloseHandle hProcess

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   DIM   aProcesses(0 TO 1023) AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL cProcesses AS DWORD
   LOCAL i AS LONG

   '// Get the list of process identifiers
   IF ISFALSE EnumProcesses(aProcesses(LBOUND(aProcesses)), _
      (UBOUND(aProcesses) - LBOUND(aProcesses) + 1) * 4, cbNeeded) THEN EXIT FUNCTION

   '// Calculate how many process identifiers were returned.
   cProcesses = cbNeeded \ 4

   '// Print the memory usage for each process
   FOR i= 0 TO cProcesses - 1
      PrintMemoryInfo aProcesses(i)
   NEXT

   WAITKEY$

END FUNCTION
' ========================================================================================
Logged
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« Reply #4 on: 24. September 2006, 03:41:44 »

 
The following example obtains a list of load addresses by using the EnumDeviceDrivers function and displays the device driver address and base name of each one.

Code:
' ========================================================================================
' Enumerating all device drivers.
' The main function obtains a list of load addresses by using the EnumDeviceDrivers
' function. For each address, main calls the PrintDeviceDriverbaseName function passing
' to it the driver address.
' ========================================================================================

' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "PSAPI.INC"

' ========================================================================================
' Displays the device driver address and base name
' ========================================================================================
SUB PrintDeviceDriverBaseName (BYVAL dwImageBase AS DWORD)

    LOCAL szBaseName AS ASCIIZ * %MAX_PATH

    PRINT "Driver address: ", dwImageBase;
    GetDeviceDriverBaseName dwImageBase, szBaseName, SIZEOF(szBaseName)
    PRINT szBaseName

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   DIM   aDrivers(0 TO 1023) AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL cAddresses AS DWORD
   LOCAL i AS LONG

   '// Get the list of device drivers addresses
   IF ISFALSE EnumDeviceDrivers(aDrivers(LBOUND(aDrivers)), _
      (UBOUND(aDrivers) - LBOUND(adrivers) + 1) * 4, cbNeeded) THEN EXIT FUNCTION

   '// Calculate how many addresses were returned
   cAddresses = cbNeeded \ 4

   '// Print the base names of the drivers
   FOR i = 0 TO cAddresses - 1
      PrintDeviceDriverBaseName aDrivers(i)
   NEXT

   WAITKEY$

END FUNCTION
' ========================================================================================

Logged
José Roca
Administrator
Hero Member
*****

Forum Reputation: +17/-0
Offline Offline

Gender: Male
Posts: 630



WWW
« Reply #5 on: 24. September 2006, 03:43:58 »

 
The following example obtains a list of load addresses by using the EnumDeviceDrivers function and displays the device driver address and the driver file name of each one.

Code:
' ========================================================================================
' Enumerating all device drivers.
' The main function obtains a list of load addresses by using the EnumDeviceDrivers
' function. For each address, main calls the PrintDeviceDriverFileName function passing
' to it the driver address.
' ========================================================================================

' SED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "PSAPI.INC"

' ========================================================================================
' Displays the device driver file name
' ========================================================================================
SUB PrintDeviceDriverFileName (BYVAL dwImageBase AS DWORD)

    LOCAL szFileName AS ASCIIZ * %MAX_PATH

    PRINT "Driver address: ", dwImageBase;
    GetDeviceDriverFileName dwImageBase, szFileName, SIZEOF(szFileName)
    PRINT szFileName

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   DIM   aDrivers(0 TO 1023) AS DWORD
   LOCAL cbNeeded AS DWORD
   LOCAL cAddresses AS DWORD
   LOCAL i AS LONG

   '// Get the list of device drivers addresses
   IF ISFALSE EnumDeviceDrivers(aDrivers(LBOUND(aDrivers)), _
      (UBOUND(aDrivers) - LBOUND(adrivers) + 1) * 4, cbNeeded) THEN EXIT FUNCTION

   '// Calculate how many addresses were returned
   cAddresses = cbNeeded \ 4

   '// Print the base names of the drivers
   FOR i = 0 TO cAddresses - 1
      PrintDeviceDriverFileName aDrivers(i)
   NEXT

   WAITKEY$

END FUNCTION
' ========================================================================================
Logged
Pages: [1] Go Up Print
« previous next »
Jump to:  

Powered by MySQL Powered by PHP IT-Consultant-Forum (IT-Berater) | Powered by SMF 1.1 RC2.
© 2001-2005, Lewis Media. All Rights Reserved.

Themis design by Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.206 seconds with 20 queries.