ReactOS  0.4.14-dev-342-gdc047f9
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 229 of file psapi.c.

232 {
233  BOOL Ret;
234  DWORD Len;
235  LPSTR AnsiFileName;
237 
238  Len = wcslen(lpFilename);
239 
240  /* Alloc space for the ANSI string */
241  AnsiFileName = LocalAlloc(LMEM_FIXED, (Len * sizeof(CHAR)) + sizeof(ANSI_NULL));
242  if (AnsiFileName == NULL)
243  {
245  return FALSE;
246  }
247 
248  /* Convert string to ANSI */
249  if (WideCharToMultiByte(CP_ACP, 0, lpFilename, -1, AnsiFileName, (Len * sizeof(CHAR)) + sizeof(ANSI_NULL), NULL, NULL) == 0)
250  {
251  Context->dwErrCode = GetLastError();
252  LocalFree(AnsiFileName);
253  return FALSE;
254  }
255 
256  /* And finally call "real" callback */
257  Ret = Context->pCallbackRoutine(Context->lpContext, pPageFileInfo, AnsiFileName);
258  LocalFree(AnsiFileName);
259 
260  return Ret;
261 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:99
char CHAR
Definition: xmlstorage.h:175
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
char * LPSTR
Definition: xmlstorage.h:182
#define ANSI_NULL
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
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 297 of file psapi.c.

300 {
301  switch(nReason)
302  {
303  case DLL_PROCESS_ATTACH:
304  DisableThreadLibraryCalls(hDllHandle);
305  if (NtCurrentPeb()->ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_PROFILE_USER)
306  {
309  }
310  break;
311 
312  case DLL_PROCESS_DETACH:
313  if (NtCurrentPeb()->ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_PROFILE_USER)
314  {
316  }
317  break;
318  }
319 
320  return TRUE;
321 }
#define TRUE
Definition: types.h:120
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
#define RTL_USER_PROCESS_PARAMETERS_PROFILE_USER
Definition: rtltypes.h:42
#define DLL_PROCESS_DETACH
Definition: compat.h:119
static VOID NTAPI PsInitializeAndStartProfile(VOID)
Definition: psapi.c:276
static VOID NTAPI PsStopAndAnalyzeProfile(VOID)
Definition: psapi.c:285
static VOID NTAPI PsParseCommandLine(VOID)
Definition: psapi.c:267
#define NtCurrentPeb()
Definition: FLS.c:20

◆ EmptyWorkingSet()

BOOL WINAPI EmptyWorkingSet ( HANDLE  hProcess)

Definition at line 329 of file psapi.c.

330 {
331  SYSTEM_INFO SystemInfo;
332  QUOTA_LIMITS QuotaLimits;
334 
335  GetSystemInfo(&SystemInfo);
336 
337  /* Query the working set */
340  &QuotaLimits,
341  sizeof(QuotaLimits),
342  NULL);
343 
344  if (!NT_SUCCESS(Status))
345  {
347  return FALSE;
348  }
349 
350  /* Empty the working set */
351  QuotaLimits.MinimumWorkingSetSize = -1;
352  QuotaLimits.MaximumWorkingSetSize = -1;
353 
354  /* Set the working set */
357  &QuotaLimits,
358  sizeof(QuotaLimits));
360  {
362  return FALSE;
363  }
364 
365  return TRUE;
366 }
#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
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:417
INT64 MinimumWorkingSetSize
Definition: lsa.idl:289
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
NTSTATUS NTAPI NtSetInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, IN PVOID ProcessInformation, IN ULONG ProcessInformationLength)
Definition: query.c:1112
VOID WINAPI GetSystemInfo(IN LPSYSTEM_INFO lpSystemInfo)
Definition: sysinfo.c:142
INT64 MaximumWorkingSetSize
Definition: lsa.idl:290

Referenced by test_ws_functions().

◆ EnumDeviceDrivers()

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

Definition at line 374 of file psapi.c.

377 {
381  /* By default, to prevent too many reallocations, we already make room for 4 modules */
383 
384  do
385  {
386  /* Allocate a buffer to hold modules information */
388  if (!Information)
389  {
391  return FALSE;
392  }
393 
394  /* Query information */
396  /* In case of an error */
397  if (!NT_SUCCESS(Status))
398  {
399  /* Save the amount of output modules */
400  NewSize = Information->NumberOfModules;
401  /* And free buffer */
403 
404  /* If it was not a length mismatch (ie, buffer too small), just leave */
406  {
408  return FALSE;
409  }
410 
411  /* Compute new size length */
412  ASSERT(Size >= sizeof(RTL_PROCESS_MODULES));
414  NewSize += sizeof(ULONG);
415  ASSERT(NewSize >= sizeof(RTL_PROCESS_MODULES));
416  /* Check whether it is really bigger - otherwise, leave */
417  if (NewSize < Size)
418  {
419  ASSERT(NewSize > Size);
421  return FALSE;
422  }
423 
424  /* Loop again with that new buffer */
425  Size = NewSize;
426  continue;
427  }
428 
429  /* End of allocation loop */
430  break;
431  } while (TRUE);
432 
433  _SEH2_TRY
434  {
435  for (Count = 0; Count < Information->NumberOfModules && Count < cb / sizeof(LPVOID); ++Count)
436  {
437  lpImageBase[Count] = Information->Modules[Count].ImageBase;
438  }
439 
440  *lpcbNeeded = Information->NumberOfModules * sizeof(LPVOID);
441  }
443  {
445  _SEH2_YIELD(return FALSE);
446  }
447  _SEH2_END;
448 
449  return TRUE;
450 }
#define TRUE
Definition: types.h:120
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
LONG NTSTATUS
Definition: precomp.h:26
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define ERROR_NO_SYSTEM_RESOURCES
Definition: winerror.h:931
struct _RTL_PROCESS_MODULES RTL_PROCESS_MODULES
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
#define LMEM_FIXED
Definition: winbase.h:349
#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:417
#define LPVOID
Definition: nt_native.h:45
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
struct _RTL_PROCESS_MODULE_INFORMATION RTL_PROCESS_MODULE_INFORMATION
_Must_inspect_result_ _In_ USHORT NewSize
Definition: fltkernel.h:975
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_SEH2_END
Definition: create.c:4424
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
Iosb Information
Definition: create.c:4377

◆ EnumPageFilesA()

BOOL WINAPI EnumPageFilesA ( PENUM_PAGE_FILE_CALLBACKA  pCallbackRoutine,
LPVOID  lpContext 
)

Definition at line 1286 of file psapi.c.

1288 {
1289  BOOL Ret;
1291 
1292  Context.dwErrCode = ERROR_SUCCESS;
1293  Context.lpContext = lpContext;
1294  Context.pCallbackRoutine = pCallbackRoutine;
1295 
1296  /* Call W with our own callback for W -> A conversions */
1298  /* If we succeed but we have error code, fail and set error */
1299  if (Ret && Context.dwErrCode != ERROR_SUCCESS)
1300  {
1301  Ret = FALSE;
1302  SetLastError(Context.dwErrCode);
1303  }
1304 
1305  return Ret;
1306 }
#define ERROR_SUCCESS
Definition: deptool.c:10
BOOL WINAPI EnumPageFilesW(PENUM_PAGE_FILE_CALLBACKW pCallbackRoutine, LPVOID lpContext)
Definition: psapi.c:1314
static BOOL CALLBACK CallBackConvertToAscii(LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCWSTR lpFilename)
Definition: psapi.c:229
unsigned int BOOL
Definition: ntddk_ex.h:94
#define SetLastError(x)
Definition: compat.h:417
struct tagContext Context
Definition: acpixf.h:1030

◆ EnumPageFilesW()

BOOL WINAPI EnumPageFilesW ( PENUM_PAGE_FILE_CALLBACKW  pCallbackRoutine,
LPVOID  lpContext 
)

Definition at line 1314 of file psapi.c.

1316 {
1317  PWSTR Colon;
1318  NTSTATUS Status;
1319  DWORD Size = INIT_MEMORY_SIZE, Needed;
1321  PSYSTEM_PAGEFILE_INFORMATION PageFileInfoArray, PageFileInfo;
1322 
1323  /* First loop till we have all the information about page files */
1324  do
1325  {
1326  PageFileInfoArray = LocalAlloc(LMEM_FIXED, Size);
1327  if (PageFileInfoArray == NULL)
1328  {
1330  return FALSE;
1331  }
1332 
1333  Status = NtQuerySystemInformation(SystemPageFileInformation, PageFileInfoArray, Size, &Needed);
1334  if (NT_SUCCESS(Status))
1335  {
1336  break;
1337  }
1338 
1339  LocalFree(PageFileInfoArray);
1340 
1341  /* In case we have unexpected status, quit */
1343  {
1345  return FALSE;
1346  }
1347 
1348  /* If needed size is smaller than actual size, guess it's something to add to our current size */
1349  if (Needed <= Size)
1350  {
1351  Size += Needed;
1352  }
1353  /* Otherwise, take it as size to allocate */
1354  else
1355  {
1356  Size = Needed;
1357  }
1358  }
1359  while (TRUE);
1360 
1361  /* Start browsing all our entries */
1362  PageFileInfo = PageFileInfoArray;
1363  do
1364  {
1365  /* Ensure we really have an entry */
1366  if (Needed < sizeof(SYSTEM_PAGEFILE_INFORMATION))
1367  {
1368  break;
1369  }
1370 
1371  /* Prepare structure to hand to the user */
1372  Information.Reserved = 0;
1373  Information.cb = sizeof(Information);
1374  Information.TotalSize = PageFileInfo->TotalSize;
1375  Information.TotalInUse = PageFileInfo->TotalInUse;
1376  Information.PeakUsage = PageFileInfo->PeakUsage;
1377 
1378  /* Search for colon */
1379  Colon = wcschr(PageFileInfo->PageFileName.Buffer, L':');
1380  /* If it's found and not at the begin of the string */
1381  if (Colon != 0 && Colon != PageFileInfo->PageFileName.Buffer)
1382  {
1383  /* We can call the user callback routine with the colon */
1384  --Colon;
1385  pCallbackRoutine(lpContext, &Information, Colon);
1386  }
1387 
1388  /* If no next entry, then, it's over */
1389  if (PageFileInfo->NextEntryOffset == 0 || PageFileInfo->NextEntryOffset > Needed)
1390  {
1391  break;
1392  }
1393 
1394  /* Jump to next entry while keeping accurate bytes left count */
1395  Needed -= PageFileInfo->NextEntryOffset;
1396  PageFileInfo = (PSYSTEM_PAGEFILE_INFORMATION)((ULONG_PTR)PageFileInfo + PageFileInfo->NextEntryOffset);
1397  }
1398  while (TRUE);
1399 
1400  LocalFree(PageFileInfoArray);
1401  return TRUE;
1402 }
#define INIT_MEMORY_SIZE
Definition: psapi.c:34
#define TRUE
Definition: types.h:120
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
uint16_t * PWSTR
Definition: typedefs.h:54
LONG NTSTATUS
Definition: precomp.h:26
uint32_t ULONG_PTR
Definition: typedefs.h:63
struct _SYSTEM_PAGEFILE_INFORMATION * PSYSTEM_PAGEFILE_INFORMATION
UNICODE_STRING PageFileName
Definition: extypes.h:1063
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
_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:417
static const WCHAR L[]
Definition: oid.c:1250
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
Iosb Information
Definition: create.c:4377

Referenced by EnumPageFilesA().

◆ EnumProcesses()

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

Definition at line 458 of file psapi.c.

461 {
465  PSYSTEM_PROCESS_INFORMATION ProcInfoArray;
466 
467  /* First of all, query all the processes */
468  do
469  {
470  ProcInfoArray = LocalAlloc(LMEM_FIXED, Size);
471  if (ProcInfoArray == NULL)
472  {
473  return FALSE;
474  }
475 
478  {
479  LocalFree(ProcInfoArray);
480  Size += MAXSHORT;
481  continue;
482  }
483 
484  break;
485  }
486  while (TRUE);
487 
488  if (!NT_SUCCESS(Status))
489  {
490  LocalFree(ProcInfoArray);
492  return FALSE;
493  }
494 
495  /* Then, loop to output data */
496  Count = 0;
497  ProcInfo = ProcInfoArray;
498 
499  _SEH2_TRY
500  {
501  do
502  {
503  /* It may sound weird, but actually MS only updated Count on
504  * successful write. So, it cannot measure the amount of space needed!
505  * This is really tricky.
506  */
507  if (Count < cb / sizeof(DWORD))
508  {
509  lpidProcess[Count] = HandleToUlong(ProcInfo->UniqueProcessId);
510  Count++;
511  }
512 
513  if (ProcInfo->NextEntryOffset == 0)
514  {
515  break;
516  }
517 
518  ProcInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)ProcInfo + ProcInfo->NextEntryOffset);
519  }
520  while (TRUE);
521 
522  *lpcbNeeded = Count * sizeof(DWORD);
523  }
525  {
527  LocalFree(ProcInfoArray);
528  _SEH2_YIELD(return FALSE);
529  }
530  _SEH2_END;
531 
532  LocalFree(ProcInfoArray);
533  return TRUE;
534 }
#define TRUE
Definition: types.h:120
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
LONG NTSTATUS
Definition: precomp.h:26
#define HandleToUlong(h)
Definition: basetsd.h:79
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define DWORD
Definition: nt_native.h:44
_SEH2_TRY
Definition: create.c:4250
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
#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:417
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
struct _SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION
#define MAXSHORT
Definition: umtypes.h:114
_SEH2_END
Definition: create.c:4424
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
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 542 of file psapi.c.

546 {
548  DWORD NbOfModules, Count;
549  PPEB_LDR_DATA LoaderData;
550  PLIST_ENTRY ListHead, ListEntry;
551  PROCESS_BASIC_INFORMATION ProcInfo;
553 
554  /* Query the process information to get its PEB address */
555  Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcInfo, sizeof(ProcInfo), NULL);
556  if (!NT_SUCCESS(Status))
557  {
559  return FALSE;
560  }
561 
562  if (ProcInfo.PebBaseAddress == NULL)
563  {
565  return FALSE;
566  }
567 
568  /* Read loader data address from PEB */
569  if (!ReadProcessMemory(hProcess, &ProcInfo.PebBaseAddress->Ldr, &LoaderData, sizeof(LoaderData), NULL))
570  {
571  return FALSE;
572  }
573 
574  /* Store list head address */
575  ListHead = &LoaderData->InLoadOrderModuleList;
576 
577  /* Read first element in the modules list */
578  if (!ReadProcessMemory(hProcess, &LoaderData->InLoadOrderModuleList.Flink, &ListEntry, sizeof(ListEntry), NULL))
579  {
580  return FALSE;
581  }
582 
583  NbOfModules = cb / sizeof(HMODULE);
584  Count = 0;
585 
586  /* Loop on the modules */
587  while (ListEntry != ListHead)
588  {
589  /* Load module data */
591  CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
592  &CurrentModule,
593  sizeof(CurrentModule),
594  NULL))
595  {
596  return FALSE;
597  }
598 
599  /* Check if we can output module, do it if so */
600  if (Count < NbOfModules)
601  {
602  _SEH2_TRY
603  {
604  lphModule[Count] = CurrentModule.DllBase;
605  }
607  {
609  _SEH2_YIELD(return FALSE);
610  }
611  _SEH2_END;
612  }
613 
614  ++Count;
615  if (Count > MAX_MODULES)
616  {
618  return FALSE;
619  }
620 
621  /* Get to next listed module */
622  ListEntry = CurrentModule.InLoadOrderLinks.Flink;
623  }
624 
625  _SEH2_TRY
626  {
627  *lpcbNeeded = Count * sizeof(HMODULE);
628  }
630  {
632  _SEH2_YIELD(return FALSE);
633  }
634  _SEH2_END;
635 
636  return TRUE;
637 }
#define TRUE
Definition: types.h:120
LIST_ENTRY InLoadOrderModuleList
Definition: ldrtypes.h:120
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
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
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:423
_SEH2_TRY
Definition: create.c:4250
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
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
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
#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:417
Definition: btrfs_drv.h:1853
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
HANDLE HMODULE
Definition: typedefs.h:75
_SEH2_END
Definition: create.c:4424
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1889
#define STATUS_PARTIAL_COPY
Definition: ntstatus.h:181
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 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  do
53  {
54  /* Allocate a buffer to hold modules information */
56  if (!Information)
57  {
59  return FALSE;
60  }
61 
62  /* Query information */
64  /* In case of an error */
65  if (!NT_SUCCESS(Status))
66  {
67  /* Save the amount of output modules */
68  NewSize = Information->NumberOfModules;
69  /* And free buffer */
71 
72  /* If it was not a length mismatch (ie, buffer too small), just leave */
74  {
76  return FALSE;
77  }
78 
79  /* Compute new size length */
80  ASSERT(Size >= sizeof(RTL_PROCESS_MODULES));
82  NewSize += sizeof(ULONG);
84  /* Check whether it is really bigger - otherwise, leave */
85  if (NewSize < Size)
86  {
87  ASSERT(NewSize > Size);
89  return FALSE;
90  }
91 
92  /* Loop again with that new buffer */
93  Size = NewSize;
94  continue;
95  }
96 
97  /* No modules returned? Leave */
98  if (Information->NumberOfModules == 0)
99  {
100  break;
101  }
102 
103  /* Try to find which module matches the base address given */
104  for (Count = 0; Count < Information->NumberOfModules; ++Count)
105  {
106  Module = Information->Modules[Count];
107  if (Module.ImageBase == ImageBase)
108  {
109  /* Copy the matching module and leave */
110  memcpy(MatchingModule, &Module, sizeof(Module));
112  return TRUE;
113  }
114  }
115 
116  /* If we arrive here, it means we were not able to find matching base address */
117  break;
118  } while (TRUE);
119 
120  /* Release and leave */
123 
124  return FALSE;
125 }
#define TRUE
Definition: types.h:120
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 ERROR_INVALID_HANDLE
Definition: compat.h:88
LONG NTSTATUS
Definition: precomp.h:26
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define ERROR_NO_SYSTEM_RESOURCES
Definition: winerror.h:931
struct _RTL_PROCESS_MODULES RTL_PROCESS_MODULES
#define LMEM_FIXED
Definition: winbase.h:349
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
struct _RTL_PROCESS_MODULE_INFORMATION RTL_PROCESS_MODULE_INFORMATION
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
_Must_inspect_result_ _In_ USHORT NewSize
Definition: fltkernel.h:975
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
unsigned int ULONG
Definition: retypes.h:1
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
Iosb Information
Definition: create.c:4377

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 131 of file psapi.c.

134 {
135  DWORD Count;
137  PPEB_LDR_DATA LoaderData;
138  PLIST_ENTRY ListHead, ListEntry;
139  PROCESS_BASIC_INFORMATION ProcInfo;
140 
141  /* Query the process information to get its PEB address */
142  Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcInfo, sizeof(ProcInfo), NULL);
143  if (!NT_SUCCESS(Status))
144  {
146  return FALSE;
147  }
148 
149  /* If no module was provided, get base as module */
150  if (hModule == NULL)
151  {
153  {
154  return FALSE;
155  }
156  }
157 
158  /* Read loader data address from PEB */
159  if (!ReadProcessMemory(hProcess, &ProcInfo.PebBaseAddress->Ldr, &LoaderData, sizeof(LoaderData), NULL))
160  {
161  return FALSE;
162  }
163 
164  if (LoaderData == NULL)
165  {
167  return FALSE;
168  }
169 
170  /* Store list head address */
171  ListHead = &(LoaderData->InMemoryOrderModuleList);
172 
173  /* Read first element in the modules list */
175  &(LoaderData->InMemoryOrderModuleList.Flink),
176  &ListEntry,
177  sizeof(ListEntry),
178  NULL))
179  {
180  return FALSE;
181  }
182 
183  Count = 0;
184 
185  /* Loop on the modules */
186  while (ListEntry != ListHead)
187  {
188  /* Load module data */
190  CONTAINING_RECORD(ListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks),
191  Module,
192  sizeof(*Module),
193  NULL))
194  {
195  return FALSE;
196  }
197 
198  /* Does that match the module we're looking for? */
199  if (Module->DllBase == hModule)
200  {
201  return TRUE;
202  }
203 
204  ++Count;
205  if (Count > MAX_MODULES)
206  {
207  break;
208  }
209 
210  /* Get to next listed module */
211  ListEntry = Module->InMemoryOrderLinks.Flink;
212  }
213 
215  return FALSE;
216 }
#define TRUE
Definition: types.h:120
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
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:1872
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:423
PVOID ImageBaseAddress
Definition: ntddk_ex.h:245
smooth NULL
Definition: ftsmooth.c:416
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
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
Definition: btrfs_drv.h:1853
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
PPEB_LDR_DATA Ldr
Definition: btrfs_drv.h:1889
#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 645 of file psapi.c.

648 {
649  DWORD Len, LenWithNull;
651 
652  /* Get the associated device driver to the base address */
653  if (!FindDeviceDriver(ImageBase, &Module))
654  {
655  return 0;
656  }
657 
658  /* And copy as much as possible to output buffer.
659  * Try to add 1 to the len, to copy the null char as well.
660  */
661  Len =
662  LenWithNull = strlen(&Module.FullPathName[Module.OffsetToFileName]) + 1;
663  if (Len > nSize)
664  {
665  Len = nSize;
666  }
667 
668  memcpy(lpBaseName, &Module.FullPathName[Module.OffsetToFileName], Len);
669  /* In case we copied null char, remove it from final len */
670  if (Len == LenWithNull)
671  {
672  --Len;
673  }
674 
675  return Len;
676 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
static BOOL NTAPI FindDeviceDriver(IN PVOID ImageBase, OUT PRTL_PROCESS_MODULE_INFORMATION MatchingModule)
Definition: psapi.c:42
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878

Referenced by GetDeviceDriverBaseNameW(), and START_TEST().

◆ GetDeviceDriverBaseNameW()

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

Definition at line 723 of file psapi.c.

726 {
727  DWORD Len;
728  LPSTR BaseName;
729 
730  /* Allocate internal buffer for conversion */
731  BaseName = LocalAlloc(LMEM_FIXED, nSize);
732  if (BaseName == 0)
733  {
734  return 0;
735  }
736 
737  /* Call A API */
738  Len = GetDeviceDriverBaseNameA(ImageBase, BaseName, nSize);
739  if (Len == 0)
740  {
741  LocalFree(BaseName);
742  return 0;
743  }
744 
745  /* And convert output */
746  if (MultiByteToWideChar(CP_ACP, 0, BaseName, (Len < nSize) ? Len + 1 : Len, lpBaseName, nSize) == 0)
747  {
748  LocalFree(BaseName);
749  return 0;
750  }
751 
752  LocalFree(BaseName);
753  return Len;
754 }
#define CP_ACP
Definition: compat.h:99
DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName, DWORD nSize)
Definition: psapi.c:645
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
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:100
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 684 of file psapi.c.

687 {
688  DWORD Len, LenWithNull;
690 
691  /* Get the associated device driver to the base address */
692  if (!FindDeviceDriver(ImageBase, &Module))
693  {
694  return 0;
695  }
696 
697  /* And copy as much as possible to output buffer.
698  * Try to add 1 to the len, to copy the null char as well.
699  */
700  Len =
701  LenWithNull = strlen(Module.FullPathName) + 1;
702  if (Len > nSize)
703  {
704  Len = nSize;
705  }
706 
707  memcpy(lpFilename, Module.FullPathName, Len);
708  /* In case we copied null char, remove it from final len */
709  if (Len == LenWithNull)
710  {
711  --Len;
712  }
713 
714  return Len;
715 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
static BOOL NTAPI FindDeviceDriver(IN PVOID ImageBase, OUT PRTL_PROCESS_MODULE_INFORMATION MatchingModule)
Definition: psapi.c:42
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878

Referenced by GetDeviceDriverFileNameW(), and START_TEST().

◆ GetDeviceDriverFileNameW()

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

Definition at line 762 of file psapi.c.

765 {
766  DWORD Len;
767  LPSTR FileName;
768 
769  /* Allocate internal buffer for conversion */
771  if (FileName == 0)
772  {
773  return 0;
774  }
775 
776  /* Call A API */
778  if (Len == 0)
779  {
781  return 0;
782  }
783 
784  /* And convert output */
785  if (MultiByteToWideChar(CP_ACP, 0, FileName, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize) == 0)
786  {
788  return 0;
789  }
790 
792  return Len;
793 }
#define CP_ACP
Definition: compat.h:99
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename, DWORD nSize)
Definition: psapi.c:684
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:884
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MultiByteToWideChar
Definition: compat.h:100
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 801 of file psapi.c.

805 {
806  DWORD Len;
808 
809  DPRINT("GetMappedFileNameA(%p, %p, %p, %lu)\n", hProcess, lpv, lpFilename, nSize);
810 
811  /* Allocate internal buffer for conversion */
812  FileName = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
813  if (FileName == NULL)
814  {
815  return 0;
816  }
817 
818  /* Call W API */
820 
821  /* And convert output */
822  if (WideCharToMultiByte(CP_ACP, 0, FileName, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize, NULL, NULL) == 0)
823  {
824  Len = 0;
825  }
826 
828  return Len;
829 }
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:99
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
smooth NULL
Definition: ftsmooth.c:416
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:837
__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:884
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
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 837 of file psapi.c.

841 {
842  DWORD Len;
843  SIZE_T OutSize;
845  struct
846  {
848  WCHAR CharBuffer[MAX_PATH];
849  } SectionName;
850 
851  DPRINT("GetMappedFileNameW(%p, %p, %p, %lu)\n", hProcess, lpv, lpFilename, nSize);
852 
853  /* If no buffer, no need to keep going on */
854  if (nSize == 0)
855  {
857  return 0;
858  }
859 
860  /* Query section name */
862  &SectionName, sizeof(SectionName), &OutSize);
863  if (!NT_SUCCESS(Status))
864  {
866  return 0;
867  }
868 
869  /* Prepare to copy file name */
870  Len =
871  OutSize = SectionName.SectionFileName.Length / sizeof(WCHAR);
872  if (OutSize + 1 > nSize)
873  {
874  Len = nSize - 1;
875  OutSize = nSize;
877  }
878  else
879  {
881  }
882 
883  /* Copy, zero and return */
884  memcpy(lpFilename, SectionName.SectionFileName.Buffer, Len * sizeof(WCHAR));
885  lpFilename[Len] = 0;
886 
887  return OutSize;
888 }
#define ERROR_SUCCESS
Definition: deptool.c:10
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
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:4284
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
void DPRINT(...)
Definition: polytest.cpp:61
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:417
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
ULONG_PTR SIZE_T
Definition: typedefs.h:78
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 896 of file psapi.c.

900 {
901  DWORD Len;
902  PWSTR BaseName;
903 
904  /* Allocate internal buffer for conversion */
905  BaseName = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
906  if (BaseName == NULL)
907  {
908  return 0;
909  }
910 
911  /* Call W API */
913  /* And convert output */
914  if (WideCharToMultiByte(CP_ACP, 0, BaseName, (Len < nSize) ? Len + 1 : Len, lpBaseName, nSize, NULL, NULL) == 0)
915  {
916  Len = 0;
917  }
918 
919  LocalFree(BaseName);
920 
921  return Len;
922 }
#define WideCharToMultiByte
Definition: compat.h:101
uint16_t * PWSTR
Definition: typedefs.h:54
#define CP_ACP
Definition: compat.h:99
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
smooth NULL
Definition: ftsmooth.c:416
DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule, LPWSTR lpBaseName, DWORD nSize)
Definition: psapi.c:930
#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
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 930 of file psapi.c.

934 {
935  DWORD Len;
936  LDR_DATA_TABLE_ENTRY Module;
937 
938  /* Get the matching module */
939  if (!FindModule(hProcess, hModule, &Module))
940  {
941  return 0;
942  }
943 
944  /* Get the maximum len we have/can write in given size */
945  Len = Module.BaseDllName.Length + sizeof(UNICODE_NULL);
946  if (nSize * sizeof(WCHAR) < Len)
947  {
948  Len = nSize * sizeof(WCHAR);
949  }
950 
951  /* Read string */
952  if (!ReadProcessMemory(hProcess, (&Module.BaseDllName)->Buffer, lpBaseName, Len, NULL))
953  {
954  return 0;
955  }
956 
957  /* If we are at the end of the string, prepare to override to nullify string */
958  if (Len == Module.BaseDllName.Length + sizeof(UNICODE_NULL))
959  {
960  Len -= sizeof(UNICODE_NULL);
961  }
962 
963  /* Nullify at the end if needed */
964  if (Len >= nSize * sizeof(WCHAR))
965  {
966  if (nSize)
967  {
968  ASSERT(nSize >= sizeof(UNICODE_NULL));
969  lpBaseName[nSize - 1] = UNICODE_NULL;
970  }
971  }
972  /* Otherwise, nullify at last written char */
973  else
974  {
975  ASSERT(Len + sizeof(UNICODE_NULL) <= nSize * sizeof(WCHAR));
976  lpBaseName[Len / sizeof(WCHAR)] = UNICODE_NULL;
977  }
978 
979  return Len / sizeof(WCHAR);
980 }
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:131
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:423
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Definition: btrfs_drv.h:1853
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
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 988 of file psapi.c.

992 {
993  DWORD Len;
994  PWSTR Filename;
995 
996  /* Allocate internal buffer for conversion */
997  Filename = LocalAlloc(LMEM_FIXED, nSize * sizeof(WCHAR));
998  if (Filename == NULL)
999  {
1000  return 0;
1001  }
1002 
1003  /* Call W API */
1005  /* And convert output */
1006  if (WideCharToMultiByte(CP_ACP, 0, Filename, (Len < nSize) ? Len + 1 : Len, lpFilename, nSize, NULL, NULL) == 0)
1007  {
1008  Len = 0;
1009  }
1010 
1012 
1013  return Len;
1014 }
#define WideCharToMultiByte
Definition: compat.h:101
uint16_t * PWSTR
Definition: typedefs.h:54
#define CP_ACP
Definition: compat.h:99
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:925
smooth NULL
Definition: ftsmooth.c:416
#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:1022
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
HMODULE hModule
Definition: animate.c:44

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

◆ GetModuleFileNameExW()

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

Definition at line 1022 of file psapi.c.

1026 {
1027  DWORD Len;
1028  LDR_DATA_TABLE_ENTRY Module;
1029 
1030  /* Get the matching module */
1031  if (!FindModule(hProcess, hModule, &Module))
1032  {
1033  return 0;
1034  }
1035 
1036  /* Get the maximum len we have/can write in given size */
1037  Len = Module.FullDllName.Length + sizeof(UNICODE_NULL);
1038  if (nSize * sizeof(WCHAR) < Len)
1039  {
1040  Len = nSize * sizeof(WCHAR);
1041  }
1042 
1043  /* Read string */
1044  if (!ReadProcessMemory(hProcess, (&Module.FullDllName)->Buffer, lpFilename, Len, NULL))
1045  {
1046  return 0;
1047  }
1048 
1049  /* If we are at the end of the string, prepare to override to nullify string */
1050  if (Len == Module.FullDllName.Length + sizeof(UNICODE_NULL))
1051  {
1052  Len -= sizeof(UNICODE_NULL);
1053  }
1054 
1055  /* Nullify at the end if needed */
1056  if (Len >= nSize * sizeof(WCHAR))
1057  {
1058  if (nSize)
1059  {
1060  ASSERT(nSize >= sizeof(UNICODE_NULL));
1061  lpFilename[nSize - 1] = UNICODE_NULL;
1062  }
1063  }
1064  /* Otherwise, nullify at last written char */
1065  else
1066  {
1067  ASSERT(Len + sizeof(UNICODE_NULL) <= nSize * sizeof(WCHAR));
1068  lpFilename[Len / sizeof(WCHAR)] = UNICODE_NULL;
1069  }
1070 
1071  return Len / sizeof(WCHAR);
1072 }
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:131
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:423
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Definition: btrfs_drv.h:1853
UNICODE_STRING FullDllName
Definition: btrfs_drv.h:1859
HMODULE hModule
Definition: animate.c:44

Referenced by GetModuleFileNameExA().

◆ GetModuleInformation()

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

Definition at line 1080 of file psapi.c.

1084 {
1085  MODULEINFO LocalInfo;
1086  LDR_DATA_TABLE_ENTRY Module;
1087 
1088  /* Check output size */
1089  if (cb < sizeof(MODULEINFO))
1090  {
1092  return FALSE;
1093  }
1094 
1095  /* Get the matching module */
1096  if (!FindModule(hProcess, hModule, &Module))
1097  {
1098  return FALSE;
1099  }
1100 
1101  /* Get a local copy first, to check for valid pointer once */
1102  LocalInfo.lpBaseOfDll = hModule;
1103  LocalInfo.SizeOfImage = Module.SizeOfImage;
1104  LocalInfo.EntryPoint = Module.EntryPoint;
1105 
1106  /* Attempt to copy to output */
1107  _SEH2_TRY
1108  {
1109  memcpy(lpmodinfo, &LocalInfo, sizeof(LocalInfo));
1110  }
1112  {
1114  _SEH2_YIELD(return FALSE);
1115  }
1116  _SEH2_END;
1117 
1118  return TRUE;
1119 }
DWORD SizeOfImage
Definition: compat.h:576
#define TRUE
Definition: types.h:120
static BOOL NTAPI FindModule(IN HANDLE hProcess, IN HMODULE hModule OPTIONAL, OUT PLDR_DATA_TABLE_ENTRY Module)
Definition: psapi.c:131
ULONG SizeOfImage
Definition: ldrtypes.h:143
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
_SEH2_TRY
Definition: create.c:4250
LPVOID EntryPoint
Definition: compat.h:577
PVOID EntryPoint
Definition: ntddk_ex.h:203
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
LPVOID lpBaseOfDll
Definition: compat.h:575
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
#define SetLastError(x)
Definition: compat.h:417
Definition: btrfs_drv.h:1853
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_SEH2_END
Definition: create.c:4424
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 EnumerateLoadedModulesW64(), test_EnumProcessModules(), test_GetModuleInformation(), and ModuleData::Update().

◆ GetPerformanceInfo()

BOOL WINAPI GetPerformanceInfo ( PPERFORMANCE_INFORMATION  pPerformanceInformation,
DWORD  cb 
)

Definition at line 1410 of file psapi.c.

1412 {
1413  NTSTATUS Status;
1416  SYSTEM_FILECACHE_INFORMATION SystemFileCacheInfo;
1417  PSYSTEM_PROCESS_INFORMATION ProcInfoArray, SystemProcInfo;
1418  DWORD Size = INIT_MEMORY_SIZE, Needed, ProcCount, ThreadsCount, HandleCount;
1419 
1420  /* Validate output buffer */
1421  if (cb < sizeof(PERFORMANCE_INFORMATION))
1422  {
1424  return FALSE;
1425  }
1426 
1427  /* First, gather as many information about the system as possible */
1429  &SystemBasicInfo,
1430  sizeof(SystemBasicInfo),
1431  NULL);
1432  if (!NT_SUCCESS(Status))
1433  {
1435  return FALSE;
1436  }
1437 
1439  &SystemPerfInfo,
1440  sizeof(SystemPerfInfo),
1441  NULL);
1442  if (!NT_SUCCESS(Status))
1443  {
1445  return FALSE;
1446  }
1447 
1449  &SystemFileCacheInfo,
1450  sizeof(SystemFileCacheInfo),
1451  NULL);
1452  if (!NT_SUCCESS(Status))
1453  {
1455  return FALSE;
1456  }
1457 
1458  /* Then loop till we have all the information about processes */
1459  do
1460  {
1461  ProcInfoArray = LocalAlloc(LMEM_FIXED, Size);
1462  if (ProcInfoArray == NULL)
1463  {
1465  return FALSE;
1466  }
1467 
1469  ProcInfoArray,
1470  Size,
1471  &Needed);
1472  if (NT_SUCCESS(Status))
1473  {
1474  break;
1475  }
1476 
1477  LocalFree(ProcInfoArray);
1478 
1479  /* In case we have unexpected status, quit */
1481  {
1483  return FALSE;
1484  }
1485 
1486  /* If needed size is smaller than actual size, guess it's something to add to our current size */
1487  if (Needed <= Size)
1488  {
1489  Size += Needed;
1490  }
1491  /* Otherwise, take it as size to allocate */
1492  else
1493  {
1494  Size = Needed;
1495  }
1496  } while (TRUE);
1497 
1498  /* Start browsing all our entries */
1499  ProcCount = 0;
1500  HandleCount = 0;
1501  ThreadsCount = 0;
1502  SystemProcInfo = ProcInfoArray;
1503  do
1504  {
1505  /* Ensure we really have an entry */
1506  if (Needed < sizeof(SYSTEM_PROCESS_INFORMATION))
1507  {
1508  break;
1509  }
1510 
1511  /* Sum procs, threads and handles */
1512  ++ProcCount;
1513  ThreadsCount += SystemProcInfo->NumberOfThreads;
1514  HandleCount += SystemProcInfo->HandleCount;
1515 
1516  /* If no next entry, then, it's over */
1517  if (SystemProcInfo->NextEntryOffset == 0 || SystemProcInfo->NextEntryOffset > Needed)
1518  {
1519  break;
1520  }
1521 
1522  /* Jump to next entry while keeping accurate bytes left count */
1523  Needed -= SystemProcInfo->NextEntryOffset;
1524  SystemProcInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)SystemProcInfo + SystemProcInfo->NextEntryOffset);
1525  }
1526  while (TRUE);
1527 
1528  LocalFree(ProcInfoArray);
1529 
1530  /* Output data */
1531  pPerformanceInformation->CommitTotal = SystemPerfInfo.CommittedPages;
1532  pPerformanceInformation->CommitLimit = SystemPerfInfo.CommitLimit;
1533  pPerformanceInformation->CommitPeak = SystemPerfInfo.PeakCommitment;
1534  pPerformanceInformation->PhysicalTotal = SystemBasicInfo.NumberOfPhysicalPages;
1535  pPerformanceInformation->PhysicalAvailable = SystemPerfInfo.AvailablePages;
1536  pPerformanceInformation->SystemCache = SystemFileCacheInfo.CurrentSizeIncludingTransitionInPages;
1537  pPerformanceInformation->KernelNonpaged = SystemPerfInfo.NonPagedPoolPages;
1538  pPerformanceInformation->PageSize = SystemBasicInfo.PageSize;
1539  pPerformanceInformation->cb = sizeof(PERFORMANCE_INFORMATION);
1541  pPerformanceInformation->KernelPaged = SystemPerfInfo.PagedPoolPages;
1542  pPerformanceInformation->HandleCount = HandleCount;
1543  pPerformanceInformation->ProcessCount = ProcCount;
1544  pPerformanceInformation->ThreadCount = ThreadsCount;
1545 
1546  return TRUE;
1547 }
#define INIT_MEMORY_SIZE
Definition: psapi.c:34
#define TRUE
Definition: types.h:120
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
LONG NTSTATUS
Definition: precomp.h:26
uint32_t ULONG_PTR
Definition: typedefs.h:63
SIZE_T CurrentSizeIncludingTransitionInPages
Definition: extypes.h:1115
smooth NULL
Definition: ftsmooth.c:416
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
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:417
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
struct _SYSTEM_PROCESS_INFORMATION * PSYSTEM_PROCESS_INFORMATION
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
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 1232 of file psapi.c.

1235 {
1236  PUNICODE_STRING ImageFileName;
1238  NTSTATUS Status;
1239  DWORD Len;
1240 
1241  /* Allocate string big enough to hold name */
1242  BufferSize = sizeof(UNICODE_STRING) + (nSize * sizeof(WCHAR));
1243  ImageFileName = LocalAlloc(LMEM_FIXED, BufferSize);
1244  if (ImageFileName == NULL)
1245  {
1246  return 0;
1247  }
1248 
1249  /* Query name */
1252  ImageFileName,
1253  BufferSize,
1254  NULL);
1255  /* Len mismatch => buffer too small */
1257  {
1259  }
1260  if (!NT_SUCCESS(Status))
1261  {
1263  LocalFree(ImageFileName);
1264  return 0;
1265  }
1266 
1267  /* Copy name */
1268  Len = WideCharToMultiByte(CP_ACP, 0, ImageFileName->Buffer,
1269  ImageFileName->Length, lpImageFileName, nSize, NULL, NULL);
1270  /* If conversion was successful, don't return len with added \0 */
1271  if (Len != 0)
1272  {
1273  Len -= sizeof(ANSI_NULL);
1274  }
1275 
1276  LocalFree(ImageFileName);
1277  return Len;
1278 }
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:99
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
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
#define ANSI_NULL
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define BufferSize
Definition: classpnp.h:419
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:417
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
ULONG_PTR SIZE_T
Definition: typedefs.h:78
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
struct _UNICODE_STRING UNICODE_STRING
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by test_GetProcessImageFileName().

◆ GetProcessImageFileNameW()

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

Definition at line 1179 of file psapi.c.

1182 {
1183  PUNICODE_STRING ImageFileName;
1185  NTSTATUS Status;
1186  DWORD Len;
1187 
1188  /* Allocate string big enough to hold name */
1189  BufferSize = sizeof(UNICODE_STRING) + (nSize * sizeof(WCHAR));
1190  ImageFileName = LocalAlloc(LMEM_FIXED, BufferSize);
1191  if (ImageFileName == NULL)
1192  {
1193  return 0;
1194  }
1195 
1196  /* Query name */
1199  ImageFileName,
1200  BufferSize,
1201  NULL);
1202  /* Len mismatch => buffer too small */
1204  {
1206  }
1207  if (!NT_SUCCESS(Status))
1208  {
1210  LocalFree(ImageFileName);
1211  return 0;
1212  }
1213 
1214  /* Copy name and null-terminate if possible */
1215  memcpy(lpImageFileName, ImageFileName->Buffer, ImageFileName->Length);
1216  Len = ImageFileName->Length / sizeof(WCHAR);
1217  if (Len < nSize)
1218  {
1219  lpImageFileName[Len] = UNICODE_NULL;
1220  }
1221 
1222  LocalFree(ImageFileName);
1223  return Len;
1224 }
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
*nSize LPSTR _Inout_ LPDWORD nSize
Definition: winbase.h:2031
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
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
#define LMEM_FIXED
Definition: winbase.h:349
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define BufferSize
Definition: classpnp.h:419
unsigned long DWORD
Definition: ntddk_ex.h:95
#define Len
Definition: deflate.h:82
#define SetLastError(x)
Definition: compat.h:417
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
ULONG_PTR SIZE_T
Definition: typedefs.h:78
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
struct _UNICODE_STRING UNICODE_STRING
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

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

◆ GetProcessMemoryInfo()

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

Definition at line 1555 of file psapi.c.

1558 {
1559  NTSTATUS Status;
1560  VM_COUNTERS_EX Counters;
1561 
1562  /* Validate output size
1563  * It can be either PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX
1564  */
1565  if (cb < sizeof(PROCESS_MEMORY_COUNTERS))
1566  {
1568  return FALSE;
1569  }
1570 
1571  _SEH2_TRY
1572  {
1573  ppsmemCounters->PeakPagefileUsage = 0;
1574 
1575  /* Query counters */
1578  &Counters,
1579  sizeof(Counters),
1580  NULL);
1581  if (!NT_SUCCESS(Status))
1582  {
1584  _SEH2_YIELD(return FALSE);
1585  }
1586 
1587  /* Properly set cb, according to what we received */
1588  if (cb >= sizeof(PROCESS_MEMORY_COUNTERS_EX))
1589  {
1590  ppsmemCounters->cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
1591  }
1592  else
1593  {
1594  ppsmemCounters->cb = sizeof(PROCESS_MEMORY_COUNTERS);
1595  }
1596 
1597  /* Output data */
1598  ppsmemCounters->PageFaultCount = Counters.PageFaultCount;
1599  ppsmemCounters->PeakWorkingSetSize = Counters.PeakWorkingSetSize;
1600  ppsmemCounters->WorkingSetSize = Counters.WorkingSetSize;
1601  ppsmemCounters->QuotaPeakPagedPoolUsage = Counters.QuotaPeakPagedPoolUsage;
1602  ppsmemCounters->QuotaPagedPoolUsage = Counters.QuotaPagedPoolUsage;
1603  ppsmemCounters->QuotaPeakNonPagedPoolUsage = Counters.QuotaPeakNonPagedPoolUsage;
1604  ppsmemCounters->QuotaNonPagedPoolUsage = Counters.QuotaNonPagedPoolUsage;
1605  ppsmemCounters->PagefileUsage = Counters.PagefileUsage;
1606  ppsmemCounters->PeakPagefileUsage = Counters.PeakPagefileUsage;
1607  /* And if needed, additional field for _EX version */
1608  if (cb >= sizeof(PROCESS_MEMORY_COUNTERS_EX))
1609  {
1610  ((PPROCESS_MEMORY_COUNTERS_EX)ppsmemCounters)->PrivateUsage = Counters.PrivateUsage;
1611  }
1612  }
1614  {
1616  _SEH2_YIELD(return FALSE);
1617  }
1618  _SEH2_END;
1619 
1620  return TRUE;
1621 }
ULONG PageFaultCount
Definition: pstypes.h:108
#define TRUE
Definition: types.h:120
SIZE_T PeakWorkingSetSize
Definition: psapi.h:40
SIZE_T PeakPagefileUsage
Definition: psapi.h:47
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:4250
struct _PROCESS_MEMORY_COUNTERS_EX PROCESS_MEMORY_COUNTERS_EX
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: pstypes.h:113
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
SIZE_T QuotaNonPagedPoolUsage
Definition: psapi.h:45
SIZE_T PrivateUsage
Definition: pstypes.h:117
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:417
SIZE_T QuotaPeakNonPagedPoolUsage
Definition: psapi.h:44
struct _PROCESS_MEMORY_COUNTERS_EX * PPROCESS_MEMORY_COUNTERS_EX
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_SEH2_END
Definition: create.c:4424
SIZE_T QuotaPeakPagedPoolUsage
Definition: psapi.h:42
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 1152 of file psapi.c.

1155 {
1156  NTSTATUS Status;
1157 
1158  /* Simply forward the call */
1161  lpWatchInfo,
1162  cb,
1163  NULL);
1164  if(!NT_SUCCESS(Status))
1165  {
1167  return FALSE;
1168  }
1169 
1170  return TRUE;
1171 }
#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
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:417
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

Referenced by test_ws_functions().

◆ InitializeProcessForWsWatch()

BOOL WINAPI InitializeProcessForWsWatch ( HANDLE  hProcess)

Definition at line 1127 of file psapi.c.

1128 {
1129  NTSTATUS Status;
1130 
1131  /* Simply forward the call */
1134  NULL,
1135  0);
1136  /* In case the function returns this, MS considers the call as a success */
1138  {
1139  return TRUE;
1140  }
1141 
1143  return FALSE;
1144 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
#define SetLastError(x)
Definition: compat.h:417
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
NTSTATUS NTAPI NtSetInformationProcess(IN HANDLE ProcessHandle, IN PROCESSINFOCLASS ProcessInformationClass, IN PVOID ProcessInformation, IN ULONG ProcessInformationLength)
Definition: query.c:1112
#define STATUS_PORT_ALREADY_SET
Definition: ntstatus.h:294

Referenced by test_ws_functions().

◆ PsInitializeAndStartProfile()

static VOID NTAPI PsInitializeAndStartProfile ( VOID  )
static

Definition at line 276 of file psapi.c.

277 {
279 }
#define UNIMPLEMENTED
Definition: debug.h:114

Referenced by DllMain().

◆ PsParseCommandLine()

static VOID NTAPI PsParseCommandLine ( VOID  )
static

Definition at line 267 of file psapi.c.

268 {
270 }
#define UNIMPLEMENTED
Definition: debug.h:114

Referenced by DllMain().

◆ PsStopAndAnalyzeProfile()

static VOID NTAPI PsStopAndAnalyzeProfile ( VOID  )
static

Definition at line 285 of file psapi.c.

286 {
288 }
#define UNIMPLEMENTED
Definition: debug.h:114

Referenced by DllMain().

◆ QueryWorkingSet()

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

Definition at line 1629 of file psapi.c.

1632 {
1633  NTSTATUS Status;
1634 
1635  /* Simply forward the call */
1637  NULL,
1639  pv,
1640  cb,
1641  NULL);
1642  if (!NT_SUCCESS(Status))
1643  {
1645  return FALSE;
1646  }
1647 
1648  return TRUE;
1649 }
#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:4284
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:417
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
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 1656 of file psapi.c.

1659 {
1660  NTSTATUS Status;
1661 
1662  /* Simply forward the call */
1664  NULL,
1666  pv,
1667  cb,
1668  NULL);
1669  if (!NT_SUCCESS(Status))
1670  {
1672  return FALSE;
1673  }
1674 
1675  return TRUE;
1676 }
#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:4284
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define SetLastError(x)
Definition: compat.h:417
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32