ReactOS  0.4.15-dev-1374-g8d3e80e
psapi.c File Reference
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winnls.h>
#include <ndk/exfuncs.h>
#include <ndk/mmfuncs.h>
#include <ndk/psfuncs.h>
#include <ndk/rtlfuncs.h>
#include <psapi.h>
#include <pseh/pseh2.h>
#include <debug.h>
Include dependency graph for psapi.c:

Go to the source code of this file.

Classes

struct  _INTERNAL_ENUM_PAGE_FILES_CONTEXT
 

Macros

#define WIN32_NO_STATUS
 
#define NTOS_MODE_USER
 
#define NDEBUG
 
#define MAX_MODULES   0x2710
 
#define INIT_MEMORY_SIZE   0x1000
 

Typedefs

typedef struct _INTERNAL_ENUM_PAGE_FILES_CONTEXT INTERNAL_ENUM_PAGE_FILES_CONTEXT
 
typedef struct _INTERNAL_ENUM_PAGE_FILES_CONTEXTPINTERNAL_ENUM_PAGE_FILES_CONTEXT
 

Functions

static BOOL NTAPI FindDeviceDriver (IN PVOID ImageBase, OUT PRTL_PROCESS_MODULE_INFORMATION MatchingModule)
 
static BOOL NTAPI FindModule (IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
 
static BOOL CALLBACK CallBackConvertToAscii (LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCWSTR lpFilename)
 
static VOID NTAPI PsParseCommandLine (VOID)
 
static VOID NTAPI PsInitializeAndStartProfile (VOID)
 
static VOID NTAPI PsStopAndAnalyzeProfile (VOID)
 
BOOLEAN WINAPI DllMain (HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved)
 
BOOL WINAPI EmptyWorkingSet (HANDLE hProcess)
 
BOOL WINAPI EnumDeviceDrivers (LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded)
 
BOOL WINAPI EnumProcesses (DWORD *lpidProcess, DWORD cb, LPDWORD lpcbNeeded)
 
BOOL WINAPI EnumProcessModules (HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded)
 
DWORD WINAPI GetDeviceDriverBaseNameA (LPVOID ImageBase, LPSTR lpBaseName, DWORD nSize)
 
DWORD WINAPI GetDeviceDriverFileNameA (LPVOID ImageBase, LPSTR lpFilename, DWORD nSize)
 
DWORD WINAPI GetDeviceDriverBaseNameW (LPVOID ImageBase, LPWSTR lpBaseName, DWORD nSize)
 
DWORD WINAPI GetDeviceDriverFileNameW (LPVOID ImageBase, LPWSTR lpFilename, DWORD nSize)
 
DWORD WINAPI GetMappedFileNameA (HANDLE hProcess, LPVOID lpv, LPSTR lpFilename, DWORD nSize)
 
DWORD WINAPI GetMappedFileNameW (HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, DWORD nSize)
 
DWORD WINAPI GetModuleBaseNameA (HANDLE hProcess, HMODULE hModule, LPSTR lpBaseName, DWORD nSize)
 
DWORD WINAPI GetModuleBaseNameW (HANDLE hProcess, HMODULE hModule, LPWSTR lpBaseName, DWORD nSize)
 
DWORD WINAPI GetModuleFileNameExA (HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize)
 
DWORD WINAPI GetModuleFileNameExW (HANDLE hProcess, HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
 
BOOL WINAPI GetModuleInformation (HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb)
 
BOOL WINAPI InitializeProcessForWsWatch (HANDLE hProcess)
 
BOOL WINAPI GetWsChanges (HANDLE hProcess, PPSAPI_WS_WATCH_INFORMATION lpWatchInfo, DWORD cb)
 
DWORD WINAPI GetProcessImageFileNameW (HANDLE hProcess, LPWSTR lpImageFileName, DWORD nSize)
 
DWORD WINAPI GetProcessImageFileNameA (HANDLE hProcess, LPSTR lpImageFileName, DWORD nSize)
 
BOOL WINAPI EnumPageFilesA (PENUM_PAGE_FILE_CALLBACKA pCallbackRoutine, LPVOID lpContext)
 
BOOL WINAPI EnumPageFilesW (PENUM_PAGE_FILE_CALLBACKW pCallbackRoutine, LPVOID lpContext)
 
BOOL WINAPI GetPerformanceInfo (PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb)
 
BOOL WINAPI GetProcessMemoryInfo (HANDLE Process, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
 
BOOL WINAPI QueryWorkingSet (HANDLE hProcess, PVOID pv, DWORD cb)
 
BOOL WINAPI QueryWorkingSetEx (IN HANDLE hProcess, IN OUT PVOID pv, IN DWORD cb)
 

Macro Definition Documentation

◆ INIT_MEMORY_SIZE

#define INIT_MEMORY_SIZE   0x1000

Definition at line 34 of file psapi.c.

◆ MAX_MODULES

#define MAX_MODULES   0x2710

Definition at line 33 of file psapi.c.

◆ NDEBUG

#define NDEBUG

Definition at line 30 of file psapi.c.

◆ NTOS_MODE_USER

#define NTOS_MODE_USER

Definition at line 20 of file psapi.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 16 of file psapi.c.

Typedef Documentation

◆ INTERNAL_ENUM_PAGE_FILES_CONTEXT

◆ PINTERNAL_ENUM_PAGE_FILES_CONTEXT

Function Documentation

◆ CallBackConvertToAscii()

static BOOL CALLBACK CallBackConvertToAscii ( LPVOID  pContext,
PENUM_PAGE_FILE_INFORMATION  pPageFileInfo,
LPCWSTR  lpFilename 
)
static

Definition at line 213 of file psapi.c.

216 {
217  BOOL Ret;
218  SIZE_T Len;
219  LPSTR AnsiFileName;
221 
222  Len = wcslen(lpFilename);
223 
224  /* Alloc space for the ANSI string */
225  AnsiFileName = LocalAlloc(LMEM_FIXED, (Len * sizeof(CHAR)) + sizeof(ANSI_NULL));
226  if (AnsiFileName == NULL)
227  {
229  return FALSE;
230  }
231 
232  /* Convert string to ANSI */
233  if (WideCharToMultiByte(CP_ACP, 0, lpFilename, -1, AnsiFileName, (Len * sizeof(CHAR)) + sizeof(ANSI_NULL), NULL, NULL) == 0)
234  {
235  Context->dwErrCode = GetLastError();
236  LocalFree(AnsiFileName);
237  return FALSE;
238  }
239 
240  /* And finally call "real" callback */
241  Ret = Context->pCallbackRoutine(Context->lpContext, pPageFileInfo, AnsiFileName);
242  LocalFree(AnsiFileName);
243 
244  return Ret;
245 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define WideCharToMultiByte
Definition: compat.h:111
#define CP_ACP
Definition: compat.h:109
char CHAR
Definition: xmlstorage.h:175
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
char * LPSTR
Definition: xmlstorage.h:182
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define ANSI_NULL
unsigned int BOOL
Definition: ntddk_ex.h:94
#define LMEM_FIXED
Definition: winbase.h:349
#define Len
Definition: deflate.h:82
ULONG_PTR SIZE_T
Definition: typedefs.h:80
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
struct _INTERNAL_ENUM_PAGE_FILES_CONTEXT * PINTERNAL_ENUM_PAGE_FILES_CONTEXT
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by EnumPageFilesA().

◆ DllMain()

BOOLEAN WINAPI DllMain ( HINSTANCE  hDllHandle,
DWORD  nReason,
LPVOID  Reserved 
)

Definition at line 281 of file psapi.c.

284 {
285  switch(nReason)
286  {
287  case DLL_PROCESS_ATTACH:
288  DisableThreadLibraryCalls(hDllHandle);
289  if (NtCurrentPeb()->ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_PROFILE_USER)
290  {
293  }
294  break;
295 
296  case DLL_PROCESS_DETACH:
297  if (NtCurrentPeb()->ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_PROFILE_USER)
298  {
300  }
301  break;
302  }
303 
304  return TRUE;
305 }
#define TRUE
Definition: types.h:120
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define RTL_USER_PROCESS_PARAMETERS_PROFILE_USER
Definition: rtltypes.h:42
#define DLL_PROCESS_DETACH
Definition: compat.h:130
static VOID NTAPI PsInitializeAndStartProfile(VOID)
Definition: psapi.c:260
static VOID NTAPI PsStopAndAnalyzeProfile(VOID)
Definition: psapi.c:269
static VOID NTAPI PsParseCommandLine(VOID)
Definition: psapi.c:251
#define NtCurrentPeb()
Definition: FLS.c:20

◆ EmptyWorkingSet()

BOOL WINAPI EmptyWorkingSet ( HANDLE  hProcess)

Definition at line 313 of file psapi.c.

314 {
315  SYSTEM_INFO SystemInfo;
316  QUOTA_LIMITS QuotaLimits;
318 
319  GetSystemInfo(&SystemInfo);
320 
321  /* Query the working set */
324  &QuotaLimits,
325  sizeof(QuotaLimits),
326  NULL);
327 
328  if (!NT_SUCCESS(Status))
329  {
331  return FALSE;
332  }
333 
334  /* Empty the working set */
335  QuotaLimits.MinimumWorkingSetSize = -1;
336  QuotaLimits.MaximumWorkingSetSize = -1;
337 
338  /* Set the working set */
341  &QuotaLimits,
342  sizeof(QuotaLimits));
344  {
346  return FALSE;
347  }
348 
349  return TRUE;
350 }
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:611
INT64 MinimumWorkingSetSize
Definition: lsa.idl:289
NTSTATUS NTAPI NtSetInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, IN PVOID ProcessInformation, IN ULONG ProcessInformationLength)
Definition: query.c:1107
VOID WINAPI GetSystemInfo(IN LPSYSTEM_INFO lpSystemInfo)
Definition: sysinfo.c:142
#define NULL
Definition: types.h:112
INT64 MaximumWorkingSetSize
Definition: lsa.idl:290

Referenced by test_ws_functions().

◆ EnumDeviceDrivers()

BOOL WINAPI EnumDeviceDrivers ( LPVOID lpImageBase,
DWORD  cb,
LPDWORD  lpcbNeeded 
)

Definition at line 358 of file psapi.c.

361 {
365  /* By default, to prevent too many reallocations, we already make room for 4 modules */
367 
368  do
369  {
370  /* Allocate a buffer to hold modules information */
372  if (!Information)
373  {
375  return FALSE;
376  }
377 
378  /* Query information */
380  /* In case of an error */
381  if (!NT_SUCCESS(Status))
382  {
383  /* Save the amount of output modules */
384  NewSize = Information->NumberOfModules;
385  /* And free buffer */
387 
388  /* If it was not a length mismatch (ie, buffer too small), just leave */
390  {
392  return FALSE;
393  }
394 
395  /* Compute new size length */
396  ASSERT(Size >= sizeof(RTL_PROCESS_MODULES));
398  NewSize += sizeof(ULONG);
399  ASSERT(NewSize >= sizeof(RTL_PROCESS_MODULES));
400  /* Check whether it is really bigger - otherwise, leave */
401  if (NewSize < Size)
402  {
403  ASSERT(NewSize > Size);
405  return FALSE;
406  }
407 
408  /* Loop again with that new buffer */
409  Size = NewSize;
410  continue;
411  }
412 
413  /* End of allocation loop */
414  break;
415  } while (TRUE);
416 
417  _SEH2_TRY
418  {
419  for (Count = 0; Count < Information->NumberOfModules && Count < cb / sizeof(LPVOID); ++Count)
420  {
421  lpImageBase[Count] = Information->Modules[Count].ImageBase;
422  }
423 
424  *lpcbNeeded = Information->NumberOfModules * sizeof(LPVOID);
425  }
427  {
429  _SEH2_YIELD(return FALSE);
430  }
431  _SEH2_END;
432 
433  return TRUE;
434 }
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
_In_ WDFREQUEST _In_ NTSTATUS _In_ ULONG_PTR Information
Definition: wdfrequest.h:1044
#define ERROR_NO_SYSTEM_RESOURCES
Definition: winerror.h:931
struct _RTL_PROCESS_MODULES RTL_PROCESS_MODULES
_SEH2_TRY
Definition: create.c:4226
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
int Count
Definition: noreturn.cpp:7
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
#define LPVOID
Definition: nt_native.h:45
struct _RTL_PROCESS_MODULE_INFORMATION RTL_PROCESS_MODULE_INFORMATION
_Must_inspect_result_ _In_ USHORT NewSize
Definition: fltkernel.h:975
_SEH2_END
Definition: create.c:4400
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
unsigned int ULONG
Definition: retypes.h:1
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12

◆ EnumPageFilesA()

BOOL WINAPI EnumPageFilesA ( PENUM_PAGE_FILE_CALLBACKA  pCallbackRoutine,
LPVOID  lpContext 
)

Definition at line 1270 of file psapi.c.

1272 {
1273  BOOL Ret;
1275 
1276  Context.dwErrCode = ERROR_SUCCESS;
1277  Context.lpContext = lpContext;
1278  Context.pCallbackRoutine = pCallbackRoutine;
1279 
1280  /* Call W with our own callback for W -> A conversions */
1282  /* If we succeed but we have error code, fail and set error */
1283  if (Ret && Context.dwErrCode != ERROR_SUCCESS)
1284  {
1285  Ret = FALSE;
1286  SetLastError(Context.dwErrCode);
1287  }
1288 
1289  return Ret;
1290 }
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL WINAPI EnumPageFilesW(PENUM_PAGE_FILE_CALLBACKW pCallbackRoutine, LPVOID lpContext)
Definition: psapi.c:1298
static BOOL CALLBACK CallBackConvertToAscii(LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCWSTR lpFilename)
Definition: psapi.c:213
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
#define SetLastError(x)
Definition: compat.h:611
struct tagContext Context
Definition: acpixf.h:1034

◆ EnumPageFilesW()

BOOL WINAPI EnumPageFilesW ( PENUM_PAGE_FILE_CALLBACKW  pCallbackRoutine,
LPVOID  lpContext 
)

Definition at line 1298 of file psapi.c.

1300 {
1301  PWSTR Colon;
1302  NTSTATUS Status;
1303  DWORD Size = INIT_MEMORY_SIZE, Needed;
1305  PSYSTEM_PAGEFILE_INFORMATION PageFileInfoArray, PageFileInfo;
1306 
1307  /* First loop till we have all the information about page files */
1308  do
1309  {
1310  PageFileInfoArray = LocalAlloc(LMEM_FIXED, Size);
1311  if (PageFileInfoArray == NULL)
1312  {
1314  return FALSE;
1315  }
1316 
1317  Status = NtQuerySystemInformation(SystemPageFileInformation, PageFileInfoArray, Size, &Needed);
1318  if (NT_SUCCESS(Status))
1319  {
1320  break;
1321  }
1322 
1323  LocalFree(PageFileInfoArray);
1324 
1325  /* In case we have unexpected status, quit */
1327  {
1329  return FALSE;
1330  }
1331 
1332  /* If needed size is smaller than actual size, guess it's something to add to our current size */
1333  if (Needed <= Size)
1334  {
1335  Size += Needed;
1336  }
1337  /* Otherwise, take it as size to allocate */
1338  else
1339  {
1340  Size = Needed;
1341  }
1342  }
1343  while (TRUE);
1344 
1345  /* Start browsing all our entries */
1346  PageFileInfo = PageFileInfoArray;
1347  do
1348  {
1349  /* Ensure we really have an entry */
1350  if (Needed < sizeof(SYSTEM_PAGEFILE_INFORMATION))
1351  {
1352  break;
1353  }
1354 
1355  /* Prepare structure to hand to the user */
1356  Information.Reserved = 0;
1357  Information.cb = sizeof(Information);
1358  Information.TotalSize = PageFileInfo->TotalSize;
1359  Information.TotalInUse = PageFileInfo->TotalInUse;
1360  Information.PeakUsage = PageFileInfo->PeakUsage;
1361 
1362  /* Search for colon */
1363  Colon = wcschr(PageFileInfo->PageFileName.Buffer, L':');
1364  /* If it's found and not at the begin of the string */
1365  if (Colon != 0 && Colon != PageFileInfo->PageFileName.Buffer)
1366  {
1367  /* We can call the user callback routine with the colon */
1368  --Colon;
1369  pCallbackRoutine(lpContext, &Information, Colon);
1370  }
1371 
1372  /* If no next entry, then, it's over */
1373  if (PageFileInfo->NextEntryOffset == 0 || PageFileInfo->NextEntryOffset > Needed)
1374  {
1375  break;
1376  }
1377 
1378  /* Jump to next entry while keeping accurate bytes left count */
1379  Needed -= PageFileInfo->NextEntryOffset;
1380  PageFileInfo = (PSYSTEM_PAGEFILE_INFORMATION)((ULONG_PTR)PageFileInfo + PageFileInfo->NextEntryOffset);
1381  }
1382  while (TRUE);
1383 
1384  LocalFree(PageFileInfoArray);
1385  return TRUE;
1386 }
#define INIT_MEMORY_SIZE
Definition: psapi.c:34
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define TRUE
Definition: types.h:120
uint16_t * PWSTR
Definition: typedefs.h:56
LONG NTSTATUS
Definition: precomp.h:26
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
_In_ WDFREQUEST _In_ NTSTATUS _In_ ULONG_PTR Information
Definition: wdfrequest.h:1044
uint32_t ULONG_PTR
Definition: typedefs.h:65
struct _SYSTEM_PAGEFILE_INFORMATION * PSYSTEM_PAGEFILE_INFORMATION
UNICODE_STRING PageFileName
Definition: extypes.h:1063
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
_CONST_RETURN wchar_t *__cdecl wcschr(_In_z_ const wchar_t *_Str, wchar_t _Ch)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
static const WCHAR L[]
Definition: oid.c:1250
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by EnumPageFilesA().

◆ EnumProcesses()

BOOL WINAPI EnumProcesses ( DWORD lpidProcess,
DWORD  cb,
LPDWORD  lpcbNeeded 
)

Definition at line 442 of file psapi.c.

445 {
449  PSYSTEM_PROCESS_INFORMATION ProcInfoArray;
450 
451  /* First of all, query all the processes */
452  do
453  {
454  ProcInfoArray = LocalAlloc(LMEM_FIXED, Size);
455  if (ProcInfoArray == NULL)
456  {
457  return FALSE;
458  }
459 
462  {
463  LocalFree(ProcInfoArray);
464  Size += MAXSHORT;
465  continue;
466  }
467 
468  break;
469  }
470  while (TRUE);
471 
472  if (!NT_SUCCESS(Status))
473  {
474  LocalFree(ProcInfoArray);
476  return FALSE;
477  }
478 
479  /* Then, loop to output data */
480  Count = 0;
481  ProcInfo = ProcInfoArray;
482 
483  _SEH2_TRY
484  {
485  do
486  {
487  /* It may sound weird, but actually MS only updated Count on
488  * successful write. So, it cannot measure the amount of space needed!
489  * This is really tricky.
490  */
491  if (Count < cb / sizeof(DWORD))
492  {
493  lpidProcess[Count] = HandleToUlong(ProcInfo->UniqueProcessId);
494  Count++;
495  }
496 
497  if (ProcInfo->NextEntryOffset == 0)
498  {
499  break;
500  }
501 
502  ProcInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)ProcInfo + ProcInfo->NextEntryOffset);
503  }
504  while (TRUE);
505 
506  *lpcbNeeded = Count * sizeof(DWORD);
507  }
509  {
511  LocalFree(ProcInfoArray);
512  _SEH2_YIELD(return FALSE);
513  }
514  _SEH2_END;
515 
516  LocalFree(ProcInfoArray);
517  return TRUE;
518 }
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define HandleToUlong(h)
Definition: basetsd.h:79
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
#define DWORD
Definition: nt_native.h:44
_SEH2_TRY
Definition: create.c:4226
uint32_t ULONG_PTR
Definition: typedefs.h:65
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
int Count
Definition: noreturn.cpp:7
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
struct _SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION
#define MAXSHORT
Definition: umtypes.h:114
_SEH2_END
Definition: create.c:4400
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12

Referenced by enumerate_processes(), ProcessList_Update(), and test_EnumProcesses().

◆ EnumProcessModules()

BOOL WINAPI EnumProcessModules ( HANDLE  hProcess,
HMODULE lphModule,
DWORD  cb,
LPDWORD  lpcbNeeded 
)

Definition at line 526 of file psapi.c.

530 {
532  DWORD NbOfModules, Count;
533  PPEB_LDR_DATA LoaderData;
534  PLIST_ENTRY ListHead, ListEntry;
535  PROCESS_BASIC_INFORMATION ProcInfo;
537 
538  /* Query the process information to get its PEB address */
539  Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcInfo, sizeof(ProcInfo), NULL);
540  if (!NT_SUCCESS(Status))
541  {
543  return FALSE;
544  }
545 
546  if (ProcInfo.PebBaseAddress == NULL)
547  {
549  return FALSE;
550  }
551 
552  /* Read loader data address from PEB */
553  if (!ReadProcessMemory(hProcess, &ProcInfo.PebBaseAddress->Ldr, &LoaderData, sizeof(LoaderData), NULL))
554  {
555  return FALSE;
556  }
557 
558  /* Store list head address */
559  ListHead = &LoaderData->InLoadOrderModuleList;
560 
561  /* Read first element in the modules list */
562  if (!ReadProcessMemory(hProcess, &LoaderData->InLoadOrderModuleList.Flink, &ListEntry, sizeof(ListEntry), NULL))
563  {
564  return FALSE;
565  }
566 
567  NbOfModules = cb / sizeof(HMODULE);
568  Count = 0;
569 
570  /* Loop on the modules */
571  while (ListEntry != ListHead)
572  {
573  /* Load module data */
575  CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
576  &CurrentModule,
577  sizeof(CurrentModule),
578  NULL))
579  {
580  return FALSE;
581  }
582 
583  /* Check if we can output module, do it if so */
584  if (Count < NbOfModules)
585  {
586  _SEH2_TRY
587  {
588  lphModule[Count] = CurrentModule.DllBase;
589  }
591  {
593  _SEH2_YIELD(return FALSE);
594  }
595  _SEH2_END;
596  }
597 
598  ++Count;
599  if (Count > MAX_MODULES)
600  {
602  return FALSE;
603  }
604 
605  /* Get to next listed module */
606  ListEntry = CurrentModule.InLoadOrderLinks.Flink;
607  }
608 
609  _SEH2_TRY
610  {
611  *lpcbNeeded = Count * sizeof(HMODULE);
612  }
614  {
616  _SEH2_YIELD(return FALSE);
617  }
618  _SEH2_END;
619 
620  return TRUE;
621 }
LIST_ENTRY InLoadOrderModuleList
Definition: ldrtypes.h:120
#define TRUE
Definition: types.h:120
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:617
_SEH2_TRY
Definition: create.c:4226
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
PLOADER_MODULE CurrentModule
Definition: mboot.c:66
Status
Definition: gdiplustypes.h:24
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
int Count
Definition: noreturn.cpp:7
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
Definition: btrfs_drv.h:1922
Definition: typedefs.h:119
HANDLE HMODULE
Definition: typedefs.h:77
_SEH2_END
Definition: create.c:4400
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1958
#define NULL
Definition: types.h:112
#define STATUS_PARTIAL_COPY
Definition: ntstatus.h:193
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
#define MAX_MODULES
Definition: psapi.c:33

Referenced by debug_target_init_modules_info(), DllMain(), EnumerateLoadedModulesW64(), get_process_name_from_pid(), and test_EnumProcessModules().

◆ FindDeviceDriver()

static BOOL NTAPI FindDeviceDriver ( IN PVOID  ImageBase,
OUT PRTL_PROCESS_MODULE_INFORMATION  MatchingModule 
)
static

Definition at line 42 of file psapi.c.

44 {
49  /* By default, to prevent too many reallocations, we already make room for 4 modules */
51 
52  while (TRUE)
53  {
54  /* Allocate a buffer to hold modules information */
56  if (!Information)
57  {
59  return FALSE;
60  }
61 
62  /* Query information */
64  if (!NT_SUCCESS(Status))
65  {
66  /* Free the current buffer */
68 
69  /* If it was not a length mismatch (ie, buffer too small), just leave */
71  {
73  return FALSE;
74  }
75 
76  /* Try again with the required size */
78  continue;
79  }
80 
81  /* No modules returned? Leave */
82  if (Information->NumberOfModules == 0)
83  {
84  break;
85  }
86 
87  /* Try to find which module matches the base address given */
88  for (i = 0; i < Information->NumberOfModules; ++i)
89  {
90  Module = Information->Modules[i];
91  if (Module.ImageBase == ImageBase)
92  {
93  /* Copy the matching module and leave */
94  memcpy(MatchingModule, &Module, sizeof(Module));
96  return TRUE;
97  }
98  }
99 
100  /* If we arrive here, it means we were not able to find matching base address */
101  break;
102  }
103 
104  /* Release and leave */
107 
108  return FALSE;
109 }
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define TRUE
Definition: types.h:120
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
LONG NTSTATUS
Definition: precomp.h:26
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
_In_ WDFREQUEST _In_ NTSTATUS _In_ ULONG_PTR Information
Definition: wdfrequest.h:1044
#define ERROR_NO_SYSTEM_RESOURCES
Definition: winerror.h:931
struct _RTL_PROCESS_MODULES RTL_PROCESS_MODULES
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ ULONG _Out_ PVOID _Out_ PULONG RequiredSize
Definition: wdfdevice.h:4431

Referenced by GetDeviceDriverBaseNameA(), and GetDeviceDriverFileNameA().

◆ FindModule()

static BOOL NTAPI FindModule ( IN HANDLE  hProcess,
IN HMODULE hModule  OPTIONAL,
OUT PLDR_DATA_TABLE_ENTRY  Module 
)
static

Definition at line 115 of file psapi.c.

118 {
119  DWORD Count;
121  PPEB_LDR_DATA LoaderData;
122  PLIST_ENTRY ListHead, ListEntry;
123  PROCESS_BASIC_INFORMATION ProcInfo;
124 
125  /* Query the process information to get its PEB address */
126  Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcInfo, sizeof(ProcInfo), NULL);
127  if (!NT_SUCCESS(Status))
128  {
130  return FALSE;
131  }
132 
133  /* If no module was provided, get base as module */
134  if (hModule == NULL)
135  {
137  {
138  return FALSE;
139  }
140  }
141 
142  /* Read loader data address from PEB */
143  if (!ReadProcessMemory(hProcess, &ProcInfo.PebBaseAddress->Ldr, &LoaderData, sizeof(LoaderData), NULL))
144  {
145  return FALSE;
146  }
147 
148  if (LoaderData == NULL)
149  {
151  return FALSE;
152  }
153 
154  /* Store list head address */
155  ListHead = &(LoaderData->InMemoryOrderModuleList);
156 
157  /* Read first element in the modules list */
159  &(LoaderData->InMemoryOrderModuleList.Flink),
160  &ListEntry,
161  sizeof(ListEntry),
162  NULL))
163  {
164  return FALSE;
165  }
166 
167  Count = 0;
168 
169  /* Loop on the modules */
170  while (ListEntry != ListHead)
171  {
172  /* Load module data */
174  CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks),
175  Module,
176  sizeof(*Module),
177  NULL))
178  {
179  return FALSE;
180  }
181 
182  /* Does that match the module we're looking for? */
183  if (Module->DllBase == hModule)
184  {
185  return TRUE;
186  }
187 
188  ++Count;
189  if (Count > MAX_MODULES)
190  {
191  break;
192  }
193 
194  /* Get to next listed module */
195  ListEntry = Module->InMemoryOrderLinks.Flink;
196  }
197 
199  return FALSE;
200 }
#define TRUE
Definition: types.h:120
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
LIST_ENTRY InMemoryOrderModuleList
Definition: btrfs_drv.h:1941
PVOID ImageBaseAddress
Definition: ntddk_ex.h:245
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:617
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
Status
Definition: gdiplustypes.h:24
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
int Count
Definition: noreturn.cpp:7
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
Definition: btrfs_drv.h:1922
Definition: typedefs.h:119
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1958
#define NULL
Definition: types.h:112
#define MAX_MODULES
Definition: psapi.c:33
HMODULE hModule
Definition: animate.c:44

Referenced by GetModuleBaseNameW(), GetModuleFileNameExW(), and GetModuleInformation().

◆ GetDeviceDriverBaseNameA()

DWORD WINAPI GetDeviceDriverBaseNameA ( LPVOID  ImageBase,
LPSTR  lpBaseName,
DWORD  nSize 
)

Definition at line 629 of file psapi.c.

632 {
633  SIZE_T Len, LenWithNull;
635 
636  /* Get the associated device driver to the base address */
637  if (!FindDeviceDriver(ImageBase, &Module))
638  {
639  return 0;
640  }
641 
642  /* And copy as much as possible to output buffer.
643  * Try to add 1 to the len, to copy the null char as well.
644  */
645  Len =
646  LenWithNull = strlen(&Module.FullPathName[Module.OffsetToFileName]) + 1;
647  if (Len > nSize)
648  {
649  Len = nSize;
650  }
651 
652  memcpy(lpBaseName, &Module.FullPathName[Module.OffsetToFileName], Len);
653  /* In case we copied null char, remove it from final len */
654  if (Len == LenWithNull)
655  {
656  --Len;
657  }
658 
659  return Len;
660 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
static BOOL NTAPI FindDeviceDriver(IN PVOID ImageBase, OUT PRTL_PROCESS_MODULE_INFORMATION MatchingModule)
Definition: psapi.c:42
#define Len
Definition: deflate.h:82
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
ULONG_PTR SIZE_T
Definition: typedefs.h:80

Referenced by GetDeviceDriverBaseNameW(), and START_TEST().

◆ GetDeviceDriverBaseNameW()

DWORD WINAPI GetDeviceDriverBaseNameW ( LPVOID  ImageBase,
LPWSTR  lpBaseName,
DWORD  nSize 
)

Definition at line 707 of file psapi.c.

710 {
711  DWORD Len;
712  LPSTR BaseName;
713 
714  /* Allocate internal buffer for conversion */
715  BaseName = LocalAlloc(LMEM_FIXED, nSize);
716  if (BaseName == 0)
717  {
718  return 0;
719  }
720 
721  /* Call A API */
722  Len = GetDeviceDriverBaseNameA(ImageBase, BaseName, nSize);
723  if (Len == 0)
724  {
725  LocalFree(BaseName);
726  return 0;
727  }
728 
729  /* And convert output */
730  if (MultiByteToWideChar(CP_ACP, 0, BaseName, (Len < nSize) ? Len + 1 : Len, lpBaseName, nSize) == 0)
731  {
732  LocalFree(BaseName);
733  return 0;
734  }
735 
736  LocalFree(BaseName);
737  return Len;
738 }
#define CP_ACP
Definition: compat.h:109
DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName, DWORD nSize)
Definition: psapi.c:629
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
char * LPSTR
Definition: xmlstorage.h:182
#define LMEM_FIXED
Definition: winbase.h:349
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MultiByteToWideChar
Definition: compat.h:110
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

◆ GetDeviceDriverFileNameA()

DWORD WINAPI GetDeviceDriverFileNameA ( LPVOID  ImageBase,
LPSTR  lpFilename,
DWORD  nSize 
)

Definition at line 668 of file psapi.c.

671 {
672  SIZE_T Len, LenWithNull;
674 
675  /* Get the associated device driver to the base address */
676  if (!FindDeviceDriver(ImageBase, &Module))
677  {
678  return 0;
679  }
680 
681  /* And copy as much as possible to output buffer.
682  * Try to add 1 to the len, to copy the null char as well.
683  */
684  Len =
685  LenWithNull = strlen(Module.FullPathName) + 1;
686  if (Len > nSize)
687  {
688  Len = nSize;
689  }
690 
691  memcpy(lpFilename, Module.FullPathName, Len);
692  /* In case we copied null char, remove it from final len */
693  if (Len == LenWithNull)
694  {
695  --Len;
696  }
697 
698  return Len;
699 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
static BOOL NTAPI FindDeviceDriver(IN PVOID ImageBase, OUT PRTL_PROCESS_MODULE_INFORMATION MatchingModule)
Definition: psapi.c:42
#define Len
Definition: deflate.h:82
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
ULONG_PTR SIZE_T
Definition: typedefs.h:80

Referenced by GetDeviceDriverFileNameW(), and START_TEST().

◆ GetDeviceDriverFileNameW()

DWORD WINAPI GetDeviceDriverFileNameW ( LPVOID  ImageBase,
LPWSTR  lpFilename,
DWORD  nSize 
)

Definition at line 746 of file psapi.c.

749 {
750  DWORD Len;
751  LPSTR FileName;
752 
753  /* Allocate internal buffer for conversion */
755  if (FileName == 0)
756  {
757  return 0;
758  }
759 
760  /* Call A API */
762  if (Len == 0)
763  {
765  return 0;
766  }
767 
768  /* And convert output */
769  if (MultiByteToWideChar(CP_ACP, 0, FileName, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize) == 0)
770  {
772  return 0;
773  }
774 
776  return Len;
777 }
#define CP_ACP
Definition: compat.h:109
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename, DWORD nSize)
Definition: psapi.c:668
char * LPSTR
Definition: xmlstorage.h:182
#define LMEM_FIXED
Definition: winbase.h:349
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
struct _FileName FileName
Definition: fatprocs.h:893
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MultiByteToWideChar
Definition: compat.h:110
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

◆ GetMappedFileNameA()

DWORD WINAPI GetMappedFileNameA ( HANDLE  hProcess,
LPVOID  lpv,
LPSTR  lpFilename,
DWORD  nSize 
)

Definition at line 785 of file psapi.c.

789 {
790  DWORD Len;
792 
793  DPRINT("GetMappedFileNameA(%p, %p, %p, %lu)\n", hProcess, lpv, lpFilename, nSize);
794 
795  /* Allocate internal buffer for conversion */
796  FileName = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
797  if (FileName == NULL)
798  {
799  return 0;
800  }
801 
802  /* Call W API */
804 
805  /* And convert output */
806  if (WideCharToMultiByte(CP_ACP, 0, FileName, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize, NULL, NULL) == 0)
807  {
808  Len = 0;
809  }
810 
812  return Len;
813 }
#define WideCharToMultiByte
Definition: compat.h:111
#define CP_ACP
Definition: compat.h:109
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
void DPRINT(...)
Definition: polytest.cpp:61
#define LMEM_FIXED
Definition: winbase.h:349
DWORD WINAPI GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, DWORD nSize)
Definition: psapi.c:821
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
struct _FileName FileName
Definition: fatprocs.h:893
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by test_GetMappedFileName(), and test_GetProcessImageFileName().

◆ GetMappedFileNameW()

DWORD WINAPI GetMappedFileNameW ( HANDLE  hProcess,
LPVOID  lpv,
LPWSTR  lpFilename,
DWORD  nSize 
)

Definition at line 821 of file psapi.c.

825 {
826  DWORD Len;
827  SIZE_T OutSize;
829  struct
830  {
832  WCHAR CharBuffer[MAX_PATH];
833  } SectionName;
834 
835  DPRINT("GetMappedFileNameW(%p, %p, %p, %lu)\n", hProcess, lpv, lpFilename, nSize);
836 
837  /* If no buffer, no need to keep going on */
838  if (nSize == 0)
839  {
841  return 0;
842  }
843 
844  /* Query section name */
846  &SectionName, sizeof(SectionName), &OutSize);
847  if (!NT_SUCCESS(Status))
848  {
850  return 0;
851  }
852 
853  /* Prepare to copy file name */
854  Len =
855  OutSize = SectionName.SectionFileName.Length / sizeof(WCHAR);
856  if (OutSize + 1 > nSize)
857  {
858  Len = nSize - 1;
859  OutSize = nSize;
861  }
862  else
863  {
865  }
866 
867  /* Copy, zero and return */
868  memcpy(lpFilename, SectionName.SectionFileName.Buffer, Len * sizeof(WCHAR));
869  lpFilename[Len] = 0;
870 
871  return OutSize;
872 }
#define ERROR_SUCCESS
Definition: deptool.c:10
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN MEMORY_INFORMATION_CLASS MemoryInformationClass, OUT PVOID MemoryInformation, IN SIZE_T MemoryInformationLength, OUT PSIZE_T ReturnLength)
Definition: virtual.c:4324
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
void DPRINT(...)
Definition: polytest.cpp:61
Status
Definition: gdiplustypes.h:24
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:611
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
ULONG_PTR SIZE_T
Definition: typedefs.h:80
struct _MEMORY_SECTION_NAME MEMORY_SECTION_NAME
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by GetMappedFileNameA(), and test_GetMappedFileName().

◆ GetModuleBaseNameA()

DWORD WINAPI GetModuleBaseNameA ( HANDLE  hProcess,
HMODULE  hModule,
LPSTR  lpBaseName,
DWORD  nSize 
)

Definition at line 880 of file psapi.c.

884 {
885  DWORD Len;
886  PWSTR BaseName;
887 
888  /* Allocate internal buffer for conversion */
889  BaseName = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
890  if (BaseName == NULL)
891  {
892  return 0;
893  }
894 
895  /* Call W API */
897  /* And convert output */
898  if (WideCharToMultiByte(CP_ACP, 0, BaseName, (Len < nSize) ? Len + 1 : Len, lpBaseName, nSize, NULL, NULL) == 0)
899  {
900  Len = 0;
901  }
902 
903  LocalFree(BaseName);
904 
905  return Len;
906 }
#define WideCharToMultiByte
Definition: compat.h:111
uint16_t * PWSTR
Definition: typedefs.h:56
#define CP_ACP
Definition: compat.h:109
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule, LPWSTR lpBaseName, DWORD nSize)
Definition: psapi.c:914
#define LMEM_FIXED
Definition: winbase.h:349
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
HMODULE hModule
Definition: animate.c:44

Referenced by test_EnumProcessModules(), and test_GetModuleBaseName().

◆ GetModuleBaseNameW()

DWORD WINAPI GetModuleBaseNameW ( HANDLE  hProcess,
HMODULE  hModule,
LPWSTR  lpBaseName,
DWORD  nSize 
)

Definition at line 914 of file psapi.c.

918 {
919  DWORD Len;
920  LDR_DATA_TABLE_ENTRY Module;
921 
922  /* Get the matching module */
923  if (!FindModule(hProcess, hModule, &Module))
924  {
925  return 0;
926  }
927 
928  /* Get the maximum len we have/can write in given size */
929  Len = Module.BaseDllName.Length + sizeof(UNICODE_NULL);
930  if (nSize * sizeof(WCHAR) < Len)
931  {
932  Len = nSize * sizeof(WCHAR);
933  }
934 
935  /* Read string */
936  if (!ReadProcessMemory(hProcess, (&Module.BaseDllName)->Buffer, lpBaseName, Len, NULL))
937  {
938  return 0;
939  }
940 
941  /* If we are at the end of the string, prepare to override to nullify string */
942  if (Len == Module.BaseDllName.Length + sizeof(UNICODE_NULL))
943  {
944  Len -= sizeof(UNICODE_NULL);
945  }
946 
947  /* Nullify at the end if needed */
948  if (Len >= nSize * sizeof(WCHAR))
949  {
950  if (nSize)
951  {
952  ASSERT(nSize >= sizeof(UNICODE_NULL));
953  lpBaseName[nSize - 1] = UNICODE_NULL;
954  }
955  }
956  /* Otherwise, nullify at last written char */
957  else
958  {
959  ASSERT(Len + sizeof(UNICODE_NULL) <= nSize * sizeof(WCHAR));
960  lpBaseName[Len / sizeof(WCHAR)] = UNICODE_NULL;
961  }
962 
963  return Len / sizeof(WCHAR);
964 }
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:115
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:617
#define UNICODE_NULL
#define ASSERT(a)
Definition: mode.c:45
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
Definition: btrfs_drv.h:1922
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
#define NULL
Definition: types.h:112
HMODULE hModule
Definition: animate.c:44

Referenced by EnumerateLoadedModulesW64(), get_process_name_from_pid(), GetModuleBaseNameA(), and GetOwnerModuleFromPidEntry().

◆ GetModuleFileNameExA()

DWORD WINAPI GetModuleFileNameExA ( HANDLE  hProcess,
HMODULE  hModule,
LPSTR  lpFilename,
DWORD  nSize 
)

Definition at line 972 of file psapi.c.

976 {
977  DWORD Len;
978  PWSTR Filename;
979 
980  /* Allocate internal buffer for conversion */
981  Filename = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
982  if (Filename == NULL)
983  {
984  return 0;
985  }
986 
987  /* Call W API */
989  /* And convert output */
990  if (WideCharToMultiByte(CP_ACP, 0, Filename, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize, NULL, NULL) == 0)
991  {
992  Len = 0;
993  }
994 
996 
997  return Len;
998 }
#define WideCharToMultiByte
Definition: compat.h:111
uint16_t * PWSTR
Definition: typedefs.h:56
#define CP_ACP
Definition: compat.h:109
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:934
#define LMEM_FIXED
Definition: winbase.h:349
__wchar_t WCHAR
Definition: xmlstorage.h:180
DWORD WINAPI GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: psapi.c:1006
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
HMODULE hModule
Definition: animate.c:44

Referenced by debug_target_init_modules_info(), test_EnumProcessModules(), test_GetModuleFileNameEx(), and ModuleData::Update().

◆ GetModuleFileNameExW()

DWORD WINAPI GetModuleFileNameExW ( HANDLE  hProcess,
HMODULE  hModule,
LPWSTR  lpFilename,
DWORD  nSize 
)

Definition at line 1006 of file psapi.c.

1010 {
1011  DWORD Len;
1012  LDR_DATA_TABLE_ENTRY Module;
1013 
1014  /* Get the matching module */
1015  if (!FindModule(hProcess, hModule, &Module))
1016  {
1017  return 0;
1018  }
1019 
1020  /* Get the maximum len we have/can write in given size */
1021  Len = Module.FullDllName.Length + sizeof(UNICODE_NULL);
1022  if (nSize * sizeof(WCHAR) < Len)
1023  {
1024  Len = nSize * sizeof(WCHAR);
1025  }
1026 
1027  /* Read string */
1028  if (!ReadProcessMemory(hProcess, (&Module.FullDllName)->Buffer, lpFilename, Len, NULL))
1029  {
1030  return 0;
1031  }
1032 
1033  /* If we are at the end of the string, prepare to override to nullify string */
1034  if (Len == Module.FullDllName.Length + sizeof(UNICODE_NULL))
1035  {
1036  Len -= sizeof(UNICODE_NULL);
1037  }
1038 
1039  /* Nullify at the end if needed */
1040  if (Len >= nSize * sizeof(WCHAR))
1041  {
1042  if (nSize)
1043  {
1044  ASSERT(nSize >= sizeof(UNICODE_NULL));
1045  lpFilename[nSize - 1] = UNICODE_NULL;
1046  }
1047  }
1048  /* Otherwise, nullify at last written char */
1049  else
1050  {
1051  ASSERT(Len + sizeof(UNICODE_NULL) <= nSize * sizeof(WCHAR));
1052  lpFilename[Len / sizeof(WCHAR)] = UNICODE_NULL;
1053  }
1054 
1055  return Len / sizeof(WCHAR);
1056 }
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:115
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:617
#define UNICODE_NULL
#define ASSERT(a)
Definition: mode.c:45
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
Definition: btrfs_drv.h:1922
UNICODE_STRING FullDllName
Definition: btrfs_drv.h:1928
#define NULL
Definition: types.h:112
HMODULE hModule
Definition: animate.c:44

Referenced by GetModuleFileNameExA().

◆ GetModuleInformation()

BOOL WINAPI GetModuleInformation ( HANDLE  hProcess,
HMODULE  hModule,
LPMODULEINFO  lpmodinfo,
DWORD  cb 
)

Definition at line 1064 of file psapi.c.

1068 {
1069  MODULEINFO LocalInfo;
1070  LDR_DATA_TABLE_ENTRY Module;
1071 
1072  /* Check output size */
1073  if (cb < sizeof(MODULEINFO))
1074  {
1076  return FALSE;
1077  }
1078 
1079  /* Get the matching module */
1080  if (!FindModule(hProcess, hModule, &Module))
1081  {
1082  return FALSE;
1083  }
1084 
1085  /* Get a local copy first, to check for valid pointer once */
1086  LocalInfo.lpBaseOfDll = hModule;
1087  LocalInfo.SizeOfImage = Module.SizeOfImage;
1088  LocalInfo.EntryPoint = Module.EntryPoint;
1089 
1090  /* Attempt to copy to output */
1091  _SEH2_TRY
1092  {
1093  memcpy(lpmodinfo, &LocalInfo, sizeof(LocalInfo));
1094  }
1096  {
1098  _SEH2_YIELD(return FALSE);
1099  }
1100  _SEH2_END;
1101 
1102  return TRUE;
1103 }
DWORD SizeOfImage
Definition: compat.h:778
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:115
#define TRUE
Definition: types.h:120
ULONG SizeOfImage
Definition: ldrtypes.h:143
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
_SEH2_TRY
Definition: create.c:4226
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
LPVOID EntryPoint
Definition: compat.h:779
PVOID EntryPoint
Definition: ntddk_ex.h:203
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
LPVOID lpBaseOfDll
Definition: compat.h:777
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define SetLastError(x)
Definition: compat.h:611
Definition: btrfs_drv.h:1922
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
_SEH2_END
Definition: create.c:4400
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
HMODULE hModule
Definition: animate.c:44
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by debug_target_init_modules_info(), EnumerateLoadedModulesW64(), test_EnumProcessModules(), test_GetModuleInformation(), and ModuleData::Update().

◆ GetPerformanceInfo()

BOOL WINAPI GetPerformanceInfo ( PPERFORMANCE_INFORMATION  pPerformanceInformation,
DWORD  cb 
)

Definition at line 1394 of file psapi.c.

1396 {
1397  NTSTATUS Status;
1400  SYSTEM_FILECACHE_INFORMATION SystemFileCacheInfo;
1401  PSYSTEM_PROCESS_INFORMATION ProcInfoArray, SystemProcInfo;
1402  DWORD Size = INIT_MEMORY_SIZE, Needed, ProcCount, ThreadsCount, HandleCount;
1403 
1404  /* Validate output buffer */
1405  if (cb < sizeof(PERFORMANCE_INFORMATION))
1406  {
1408  return FALSE;
1409  }
1410 
1411  /* First, gather as many information about the system as possible */
1413  &SystemBasicInfo,
1414  sizeof(SystemBasicInfo),
1415  NULL);
1416  if (!NT_SUCCESS(Status))
1417  {
1419  return FALSE;
1420  }
1421 
1423  &SystemPerfInfo,
1424  sizeof(SystemPerfInfo),
1425  NULL);
1426  if (!NT_SUCCESS(Status))
1427  {
1429  return FALSE;
1430  }
1431 
1433  &SystemFileCacheInfo,
1434  sizeof(SystemFileCacheInfo),
1435  NULL);
1436  if (!NT_SUCCESS(Status))
1437  {
1439  return FALSE;
1440  }
1441 
1442  /* Then loop till we have all the information about processes */
1443  do
1444  {
1445  ProcInfoArray = LocalAlloc(LMEM_FIXED, Size);
1446  if (ProcInfoArray == NULL)
1447  {
1449  return FALSE;
1450  }
1451 
1453  ProcInfoArray,
1454  Size,
1455  &Needed);
1456  if (NT_SUCCESS(Status))
1457  {
1458  break;
1459  }
1460 
1461  LocalFree(ProcInfoArray);
1462 
1463  /* In case we have unexpected status, quit */
1465  {
1467  return FALSE;
1468  }
1469 
1470  /* If needed size is smaller than actual size, guess it's something to add to our current size */
1471  if (Needed <= Size)
1472  {
1473  Size += Needed;
1474  }
1475  /* Otherwise, take it as size to allocate */
1476  else
1477  {
1478  Size = Needed;
1479  }
1480  } while (TRUE);
1481 
1482  /* Start browsing all our entries */
1483  ProcCount = 0;
1484  HandleCount = 0;
1485  ThreadsCount = 0;
1486  SystemProcInfo = ProcInfoArray;
1487  do
1488  {
1489  /* Ensure we really have an entry */
1490  if (Needed < sizeof(SYSTEM_PROCESS_INFORMATION))
1491  {
1492  break;
1493  }
1494 
1495  /* Sum procs, threads and handles */
1496  ++ProcCount;
1497  ThreadsCount += SystemProcInfo->NumberOfThreads;
1498  HandleCount += SystemProcInfo->HandleCount;
1499 
1500  /* If no next entry, then, it's over */
1501  if (SystemProcInfo->NextEntryOffset == 0 || SystemProcInfo->NextEntryOffset > Needed)
1502  {
1503  break;
1504  }
1505 
1506  /* Jump to next entry while keeping accurate bytes left count */
1507  Needed -= SystemProcInfo->NextEntryOffset;
1508  SystemProcInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)SystemProcInfo + SystemProcInfo->NextEntryOffset);
1509  }
1510  while (TRUE);
1511 
1512  LocalFree(ProcInfoArray);
1513 
1514  /* Output data */
1515  pPerformanceInformation->CommitTotal = SystemPerfInfo.CommittedPages;
1516  pPerformanceInformation->CommitLimit = SystemPerfInfo.CommitLimit;
1517  pPerformanceInformation->CommitPeak = SystemPerfInfo.PeakCommitment;
1518  pPerformanceInformation->PhysicalTotal = SystemBasicInfo.NumberOfPhysicalPages;
1519  pPerformanceInformation->PhysicalAvailable = SystemPerfInfo.AvailablePages;
1520  pPerformanceInformation->SystemCache = SystemFileCacheInfo.CurrentSizeIncludingTransitionInPages;
1521  pPerformanceInformation->KernelNonpaged = SystemPerfInfo.NonPagedPoolPages;
1522  pPerformanceInformation->PageSize = SystemBasicInfo.PageSize;
1523  pPerformanceInformation->cb = sizeof(PERFORMANCE_INFORMATION);
1525  pPerformanceInformation->KernelPaged = SystemPerfInfo.PagedPoolPages;
1526  pPerformanceInformation->HandleCount = HandleCount;
1527  pPerformanceInformation->ProcessCount = ProcCount;
1528  pPerformanceInformation->ThreadCount = ThreadsCount;
1529 
1530  return TRUE;
1531 }
#define INIT_MEMORY_SIZE
Definition: psapi.c:34
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
uint32_t ULONG_PTR
Definition: typedefs.h:65
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
SIZE_T CurrentSizeIncludingTransitionInPages
Definition: extypes.h:1115
SIZE_T PhysicalAvailable
Definition: psapi.h:70
#define SystemPerformanceInformation
Definition: memtest.h:87
SYSTEM_PERFORMANCE_INFORMATION SystemPerfInfo
Definition: perfdata.c:44
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
struct _PERFORMANCE_INFORMATION PERFORMANCE_INFORMATION
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
SYSTEM_BASIC_INFORMATION SystemBasicInfo
Definition: perfdata.c:45
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:611
struct _SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by test_GetPerformanceInfo().

◆ GetProcessImageFileNameA()

DWORD WINAPI GetProcessImageFileNameA ( HANDLE  hProcess,
LPSTR  lpImageFileName,
DWORD  nSize 
)

Definition at line 1216 of file psapi.c.

1219 {
1220  PUNICODE_STRING ImageFileName;
1222  NTSTATUS Status;
1223  DWORD Len;
1224 
1225  /* Allocate string big enough to hold name */
1226  BufferSize = sizeof(UNICODE_STRING) + (nSize * sizeof(WCHAR));
1227  ImageFileName = LocalAlloc(LMEM_FIXED, BufferSize);
1228  if (ImageFileName == NULL)
1229  {
1230  return 0;
1231  }
1232 
1233  /* Query name */
1236  ImageFileName,
1237  BufferSize,
1238  NULL);
1239  /* Len mismatch => buffer too small */
1241  {
1243  }
1244  if (!NT_SUCCESS(Status))
1245  {
1247  LocalFree(ImageFileName);
1248  return 0;
1249  }
1250 
1251  /* Copy name */
1252  Len = WideCharToMultiByte(CP_ACP, 0, ImageFileName->Buffer,
1253  ImageFileName->Length, lpImageFileName, nSize, NULL, NULL);
1254  /* If conversion was successful, don't return len with added \0 */
1255  if (Len != 0)
1256  {
1257  Len -= sizeof(ANSI_NULL);
1258  }
1259 
1260  LocalFree(ImageFileName);
1261  return Len;
1262 }
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define WideCharToMultiByte
Definition: compat.h:111
#define CP_ACP
Definition: compat.h:109
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define ANSI_NULL
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:611
ULONG_PTR SIZE_T
Definition: typedefs.h:80
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
struct _UNICODE_STRING UNICODE_STRING
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
#define BufferSize
Definition: mmc.h:75
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:251

Referenced by test_GetProcessImageFileName().

◆ GetProcessImageFileNameW()

DWORD WINAPI GetProcessImageFileNameW ( HANDLE  hProcess,
LPWSTR  lpImageFileName,
DWORD  nSize 
)

Definition at line 1163 of file psapi.c.

1166 {
1167  PUNICODE_STRING ImageFileName;
1169  NTSTATUS Status;
1170  DWORD Len;
1171 
1172  /* Allocate string big enough to hold name */
1173  BufferSize = sizeof(UNICODE_STRING) + (nSize * sizeof(WCHAR));
1174  ImageFileName = LocalAlloc(LMEM_FIXED, BufferSize);
1175  if (ImageFileName == NULL)
1176  {
1177  return 0;
1178  }
1179 
1180  /* Query name */
1183  ImageFileName,
1184  BufferSize,
1185  NULL);
1186  /* Len mismatch => buffer too small */
1188  {
1190  }
1191  if (!NT_SUCCESS(Status))
1192  {
1194  LocalFree(ImageFileName);
1195  return 0;
1196  }
1197 
1198  /* Copy name and null-terminate if possible */
1199  memcpy(lpImageFileName, ImageFileName->Buffer, ImageFileName->Length);
1200  Len = ImageFileName->Length / sizeof(WCHAR);
1201  if (Len < nSize)
1202  {
1203  lpImageFileName[Len] = UNICODE_NULL;
1204  }
1205 
1206  LocalFree(ImageFileName);
1207  return Len;
1208 }
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2037
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define UNICODE_NULL
#define LMEM_FIXED
Definition: winbase.h:349
Status
Definition: gdiplustypes.h:24
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:611
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
ULONG_PTR SIZE_T
Definition: typedefs.h:80
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define NULL
Definition: types.h:112
struct _UNICODE_STRING UNICODE_STRING
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
#define BufferSize
Definition: mmc.h:75
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:251

Referenced by NonConsoleProcessShutdown(), test_GetProcessImageFileName(), and UserClientShutdown().

◆ GetProcessMemoryInfo()

BOOL WINAPI GetProcessMemoryInfo ( HANDLE  Process,
PPROCESS_MEMORY_COUNTERS  ppsmemCounters,
DWORD  cb 
)

Definition at line 1539 of file psapi.c.

1542 {
1543  NTSTATUS Status;
1544  VM_COUNTERS_EX Counters;
1545 
1546  /* Validate output size
1547  * It can be either PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX
1548  */
1549  if (cb < sizeof(PROCESS_MEMORY_COUNTERS))
1550  {
1552  return FALSE;
1553  }
1554 
1555  _SEH2_TRY
1556  {
1557  ppsmemCounters->PeakPagefileUsage = 0;
1558 
1559  /* Query counters */
1562  &Counters,
1563  sizeof(Counters),
1564  NULL);
1565  if (!NT_SUCCESS(Status))
1566  {
1568  _SEH2_YIELD(return FALSE);
1569  }
1570 
1571  /* Properly set cb, according to what we received */
1572  if (cb >= sizeof(PROCESS_MEMORY_COUNTERS_EX))
1573  {
1574  ppsmemCounters->cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
1575  }
1576  else
1577  {
1578  ppsmemCounters->cb = sizeof(PROCESS_MEMORY_COUNTERS);
1579  }
1580 
1581  /* Output data */
1582  ppsmemCounters->PageFaultCount = Counters.PageFaultCount;
1583  ppsmemCounters->PeakWorkingSetSize = Counters.PeakWorkingSetSize;
1584  ppsmemCounters->WorkingSetSize = Counters.WorkingSetSize;
1585  ppsmemCounters->QuotaPeakPagedPoolUsage = Counters.QuotaPeakPagedPoolUsage;
1586  ppsmemCounters->QuotaPagedPoolUsage = Counters.QuotaPagedPoolUsage;
1587  ppsmemCounters->QuotaPeakNonPagedPoolUsage = Counters.QuotaPeakNonPagedPoolUsage;
1588  ppsmemCounters->QuotaNonPagedPoolUsage = Counters.QuotaNonPagedPoolUsage;
1589  ppsmemCounters->PagefileUsage = Counters.PagefileUsage;
1590  ppsmemCounters->PeakPagefileUsage = Counters.PeakPagefileUsage;
1591  /* And if needed, additional field for _EX version */
1592  if (cb >= sizeof(PROCESS_MEMORY_COUNTERS_EX))
1593  {
1594  ((PPROCESS_MEMORY_COUNTERS_EX)ppsmemCounters)->PrivateUsage = Counters.PrivateUsage;
1595  }
1596  }
1598  {
1600  _SEH2_YIELD(return FALSE);
1601  }
1602  _SEH2_END;
1603 
1604  return TRUE;
1605 }
ULONG PageFaultCount
Definition: pstypes.h:108
SIZE_T PeakWorkingSetSize
Definition: psapi.h:40
SIZE_T PeakPagefileUsage
Definition: psapi.h:47
#define TRUE
Definition: types.h:120
SIZE_T QuotaPagedPoolUsage
Definition: psapi.h:43
SIZE_T PeakPagefileUsage
Definition: pstypes.h:116
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
SIZE_T WorkingSetSize
Definition: pstypes.h:110
SIZE_T QuotaNonPagedPoolUsage
Definition: pstypes.h:114
_SEH2_TRY
Definition: create.c:4226
struct _PROCESS_MEMORY_COUNTERS_EX PROCESS_MEMORY_COUNTERS_EX
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: pstypes.h:113
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
SIZE_T QuotaNonPagedPoolUsage
Definition: psapi.h:45
SIZE_T PrivateUsage
Definition: pstypes.h:117
Status
Definition: gdiplustypes.h:24
SIZE_T QuotaPagedPoolUsage
Definition: pstypes.h:112
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:611
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: psapi.h:44
struct _PROCESS_MEMORY_COUNTERS_EX * PPROCESS_MEMORY_COUNTERS_EX
_SEH2_END
Definition: create.c:4400
SIZE_T QuotaPeakPagedPoolUsage
Definition: psapi.h:42
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
struct _PROCESS_MEMORY_COUNTERS PROCESS_MEMORY_COUNTERS
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
SIZE_T PeakWorkingSetSize
Definition: pstypes.h:109
SIZE_T PagefileUsage
Definition: pstypes.h:115
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
SIZE_T QuotaPeakPagedPoolUsage
Definition: pstypes.h:111
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by test_GetProcessMemoryInfo().

◆ GetWsChanges()

BOOL WINAPI GetWsChanges ( HANDLE  hProcess,
PPSAPI_WS_WATCH_INFORMATION  lpWatchInfo,
DWORD  cb 
)

Definition at line 1136 of file psapi.c.

1139 {
1140  NTSTATUS Status;
1141 
1142  /* Simply forward the call */
1145  lpWatchInfo,
1146  cb,
1147  NULL);
1148  if(!NT_SUCCESS(Status))
1149  {
1151  return FALSE;
1152  }
1153 
1154  return TRUE;
1155 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, OUT PVOID ProcessInformation, IN ULONG ProcessInformationLength, OUT PULONG ReturnLength OPTIONAL)
Definition: query.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:611
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

Referenced by test_ws_functions().

◆ InitializeProcessForWsWatch()

BOOL WINAPI InitializeProcessForWsWatch ( HANDLE  hProcess)

Definition at line 1111 of file psapi.c.

1112 {
1113  NTSTATUS Status;
1114 
1115  /* Simply forward the call */
1118  NULL,
1119  0);
1120  /* In case the function returns this, MS considers the call as a success */
1122  {
1123  return TRUE;
1124  }
1125 
1127  return FALSE;
1128 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
#define SetLastError(x)
Definition: compat.h:611
NTSTATUS NTAPI NtSetInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, IN PVOID ProcessInformation, IN ULONG ProcessInformationLength)
Definition: query.c:1107
#define NULL
Definition: types.h:112
#define STATUS_PORT_ALREADY_SET
Definition: ntstatus.h:308

Referenced by test_ws_functions().

◆ PsInitializeAndStartProfile()

static VOID NTAPI PsInitializeAndStartProfile ( VOID  )
static

Definition at line 260 of file psapi.c.

261 {
263 }
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by DllMain().

◆ PsParseCommandLine()

static VOID NTAPI PsParseCommandLine ( VOID  )
static

Definition at line 251 of file psapi.c.

252 {
254 }
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by DllMain().

◆ PsStopAndAnalyzeProfile()

static VOID NTAPI PsStopAndAnalyzeProfile ( VOID  )
static

Definition at line 269 of file psapi.c.

270 {
272 }
#define UNIMPLEMENTED
Definition: debug.h:115

Referenced by DllMain().

◆ QueryWorkingSet()

BOOL WINAPI QueryWorkingSet ( HANDLE  hProcess,
PVOID  pv,
DWORD  cb 
)

Definition at line 1613 of file psapi.c.

1616 {
1617  NTSTATUS Status;
1618 
1619  /* Simply forward the call */
1621  NULL,
1623  pv,
1624  cb,
1625  NULL);
1626  if (!NT_SUCCESS(Status))
1627  {
1629  return FALSE;
1630  }
1631 
1632  return TRUE;
1633 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN MEMORY_INFORMATION_CLASS MemoryInformationClass, OUT PVOID MemoryInformation, IN SIZE_T MemoryInformationLength, OUT PSIZE_T ReturnLength)
Definition: virtual.c:4324
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:611
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

Referenced by test_ws_functions().

◆ QueryWorkingSetEx()

BOOL WINAPI QueryWorkingSetEx ( IN HANDLE  hProcess,
IN OUT PVOID  pv,
IN DWORD  cb 
)

Definition at line 1640 of file psapi.c.

1643 {
1644  NTSTATUS Status;
1645 
1646  /* Simply forward the call */
1648  NULL,
1650  pv,
1651  cb,
1652  NULL);
1653  if (!NT_SUCCESS(Status))
1654  {
1656  return FALSE;
1657  }
1658 
1659  return TRUE;
1660 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN MEMORY_INFORMATION_CLASS MemoryInformationClass, OUT PVOID MemoryInformation, IN SIZE_T MemoryInformationLength, OUT PSIZE_T ReturnLength)
Definition: virtual.c:4324
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
#define FALSE
Definition: types.h:117
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:611
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32