4 * Copyright 1998 Patrik Stridvall
5 * Copyright 2003 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define WIN32_NO_STATUS
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psapi);
40 PLIST_ENTRY pHead, pCurrent;
44 /***********************************************************************
45 * PSAPI_ModuleIteratorInit [internal]
47 * Prepares to iterate through the loaded modules of the given process.
53 static BOOL PSAPI_ModuleIteratorInit(MODULE_ITERATOR *iter, HANDLE hProcess)
55 PROCESS_BASIC_INFORMATION pbi;
56 PPEB_LDR_DATA pLdrData;
59 /* Get address of PEB */
60 status = NtQueryInformationProcess(hProcess, ProcessBasicInformation,
61 &pbi, sizeof(pbi), NULL);
62 if (status != STATUS_SUCCESS)
64 SetLastError(RtlNtStatusToDosError(status));
68 /* Read address of LdrData from PEB */
69 if (!ReadProcessMemory(hProcess, &pbi.PebBaseAddress->LdrData,
70 &pLdrData, sizeof(pLdrData), NULL))
73 /* Read address of first module from LdrData */
74 if (!ReadProcessMemory(hProcess,
75 &pLdrData->InLoadOrderModuleList.Flink,
76 &iter->pCurrent, sizeof(iter->pCurrent), NULL))
79 iter->pHead = &pLdrData->InLoadOrderModuleList;
80 iter->hProcess = hProcess;
85 /***********************************************************************
86 * PSAPI_ModuleIteratorNext [internal]
88 * Iterates to the next module.
96 * Every function which uses this routine suffers from a race condition
97 * when a module is unloaded during the enumeration which can cause the
98 * function to fail. As there is no way to lock the loader of another
99 * process we can't avoid that.
101 static INT PSAPI_ModuleIteratorNext(MODULE_ITERATOR *iter)
103 if (iter->pCurrent == iter->pHead)
106 if (!ReadProcessMemory(iter->hProcess, CONTAINING_RECORD(iter->pCurrent,
107 LDR_MODULE, InLoadOrderModuleList),
108 &iter->LdrModule, sizeof(iter->LdrModule), NULL))
111 iter->pCurrent = iter->LdrModule.InLoadOrderModuleList.Flink;
116 /***********************************************************************
117 * PSAPI_GetLdrModule [internal]
119 * Reads the LDR_MODULE structure of the given module.
126 static BOOL PSAPI_GetLdrModule(HANDLE hProcess, HMODULE hModule,
127 LDR_MODULE *pLdrModule)
129 MODULE_ITERATOR iter;
132 if (!PSAPI_ModuleIteratorInit(&iter, hProcess))
135 while ((ret = PSAPI_ModuleIteratorNext(&iter)) > 0)
136 /* When hModule is NULL we return the process image - which will be
137 * the first module since our iterator uses InLoadOrderModuleList */
138 if (!hModule || hModule == iter.LdrModule.BaseAddress)
140 *pLdrModule = iter.LdrModule;
145 SetLastError(ERROR_INVALID_HANDLE);
150 /***********************************************************************
151 * EnumDeviceDrivers (PSAPI.@)
153 BOOL WINAPI EnumDeviceDrivers(LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded)
155 FIXME("(%p, %d, %p): stub\n", lpImageBase, cb, lpcbNeeded);
163 /***********************************************************************
164 * EnumPageFilesA (PSAPI.@)
166 BOOL WINAPI EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback, LPVOID context )
168 FIXME("(%p, %p) stub\n", callback, context );
172 /***********************************************************************
173 * EnumPageFilesW (PSAPI.@)
175 BOOL WINAPI EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback, LPVOID context )
177 FIXME("(%p, %p) stub\n", callback, context );
181 /***********************************************************************
182 * EnumProcessModules (PSAPI.@)
185 * Returned list is in load order.
187 BOOL WINAPI EnumProcessModules(HANDLE hProcess, HMODULE *lphModule,
188 DWORD cb, LPDWORD lpcbNeeded)
190 MODULE_ITERATOR iter;
193 if (!PSAPI_ModuleIteratorInit(&iter, hProcess))
198 while ((ret = PSAPI_ModuleIteratorNext(&iter)) > 0)
200 if (cb >= sizeof(HMODULE))
202 *lphModule++ = iter.LdrModule.BaseAddress;
203 cb -= sizeof(HMODULE);
205 *lpcbNeeded += sizeof(HMODULE);
211 /***********************************************************************
212 * GetDeviceDriverBaseNameA (PSAPI.@)
214 DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName,
217 FIXME("(%p, %p, %d): stub\n", ImageBase, lpBaseName, nSize);
219 if (lpBaseName && nSize)
220 lpBaseName[0] = '\0';
225 /***********************************************************************
226 * GetDeviceDriverBaseNameW (PSAPI.@)
228 DWORD WINAPI GetDeviceDriverBaseNameW(LPVOID ImageBase, LPWSTR lpBaseName,
231 FIXME("(%p, %p, %d): stub\n", ImageBase, lpBaseName, nSize);
233 if (lpBaseName && nSize)
234 lpBaseName[0] = '\0';
239 /***********************************************************************
240 * GetDeviceDriverFileNameA (PSAPI.@)
242 DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename,
245 FIXME("(%p, %p, %d): stub\n", ImageBase, lpFilename, nSize);
247 if (lpFilename && nSize)
248 lpFilename[0] = '\0';
253 /***********************************************************************
254 * GetDeviceDriverFileNameW (PSAPI.@)
256 DWORD WINAPI GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename,
259 FIXME("(%p, %p, %d): stub\n", ImageBase, lpFilename, nSize);
261 if (lpFilename && nSize)
262 lpFilename[0] = '\0';
267 /***********************************************************************
268 * GetMappedFileNameA (PSAPI.@)
270 DWORD WINAPI GetMappedFileNameA(HANDLE hProcess, LPVOID lpv, LPSTR lpFilename,
273 FIXME("(%p, %p, %p, %d): stub\n", hProcess, lpv, lpFilename, nSize);
275 if (lpFilename && nSize)
276 lpFilename[0] = '\0';
281 /***********************************************************************
282 * GetMappedFileNameW (PSAPI.@)
284 DWORD WINAPI GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename,
287 FIXME("(%p, %p, %p, %d): stub\n", hProcess, lpv, lpFilename, nSize);
289 if (lpFilename && nSize)
290 lpFilename[0] = '\0';
295 /***********************************************************************
296 * GetModuleBaseNameA (PSAPI.@)
298 DWORD WINAPI GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule,
299 LPSTR lpBaseName, DWORD nSize)
302 DWORD buflenW, ret = 0;
304 if(!lpBaseName || !nSize) {
305 SetLastError(ERROR_INVALID_PARAMETER);
308 lpBaseNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * nSize);
309 buflenW = GetModuleBaseNameW(hProcess, hModule, lpBaseNameW, nSize);
310 TRACE("%d, %s\n", buflenW, debugstr_w(lpBaseNameW));
313 ret = WideCharToMultiByte(CP_ACP, 0, lpBaseNameW, buflenW,
314 lpBaseName, nSize, NULL, NULL);
315 if (ret < nSize) lpBaseName[ret] = 0;
317 HeapFree(GetProcessHeap(), 0, lpBaseNameW);
321 /***********************************************************************
322 * GetModuleBaseNameW (PSAPI.@)
324 DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule,
325 LPWSTR lpBaseName, DWORD nSize)
327 LDR_MODULE LdrModule;
329 if (!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
332 nSize = min(LdrModule.BaseDllName.Length / sizeof(WCHAR), nSize);
333 if (!ReadProcessMemory(hProcess, LdrModule.BaseDllName.Buffer,
334 lpBaseName, nSize * sizeof(WCHAR), NULL))
337 lpBaseName[nSize] = 0;
341 /***********************************************************************
342 * GetModuleFileNameExA (PSAPI.@)
344 DWORD WINAPI GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule,
345 LPSTR lpFileName, DWORD nSize)
349 TRACE("(hProcess=%p, hModule=%p, %p, %d)\n",
350 hProcess, hModule, lpFileName, nSize);
352 if (!lpFileName || !nSize) return 0;
354 if ( hProcess == GetCurrentProcess() )
356 DWORD len = GetModuleFileNameA( hModule, lpFileName, nSize );
357 if (nSize) lpFileName[nSize - 1] = '\0';
361 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, nSize * sizeof(WCHAR)))) return 0;
363 if (!GetModuleFileNameExW(hProcess, hModule, ptr, nSize))
365 lpFileName[0] = '\0';
369 if (!WideCharToMultiByte( CP_ACP, 0, ptr, -1, lpFileName, nSize, NULL, NULL ))
370 lpFileName[nSize - 1] = 0;
373 HeapFree(GetProcessHeap(), 0, ptr);
374 return strlen(lpFileName);
377 /***********************************************************************
378 * GetModuleFileNameExW (PSAPI.@)
380 DWORD WINAPI GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule,
381 LPWSTR lpFileName, DWORD nSize)
383 LDR_MODULE LdrModule;
385 if(!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
388 nSize = min(LdrModule.FullDllName.Length / sizeof(WCHAR), nSize);
389 if (!ReadProcessMemory(hProcess, LdrModule.FullDllName.Buffer,
390 lpFileName, nSize * sizeof(WCHAR), NULL))
393 lpFileName[nSize] = 0;
397 /***********************************************************************
398 * GetModuleInformation (PSAPI.@)
400 BOOL WINAPI GetModuleInformation(HANDLE hProcess, HMODULE hModule,
401 LPMODULEINFO lpmodinfo, DWORD cb)
403 LDR_MODULE LdrModule;
405 if (cb < sizeof(MODULEINFO))
407 SetLastError(ERROR_INSUFFICIENT_BUFFER);
411 if (!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
414 lpmodinfo->lpBaseOfDll = LdrModule.BaseAddress;
415 lpmodinfo->SizeOfImage = LdrModule.SizeOfImage;
416 lpmodinfo->EntryPoint = LdrModule.EntryPoint;
420 /***********************************************************************
421 * GetPerformanceInfo (PSAPI.@)
423 BOOL WINAPI GetPerformanceInfo( PPERFORMANCE_INFORMATION info, DWORD size )
427 TRACE( "(%p, %d)\n", info, size );
429 status = NtQuerySystemInformation( SystemPerformanceInformation, info, size, NULL );
433 SetLastError( RtlNtStatusToDosError( status ) );
439 /***********************************************************************
440 * GetProcessMemoryInfo (PSAPI.@)
442 * Retrieve memory usage information for a given process
445 BOOL WINAPI GetProcessMemoryInfo(HANDLE hProcess,
446 PPROCESS_MEMORY_COUNTERS pmc, DWORD cb)
451 if (cb < sizeof(PROCESS_MEMORY_COUNTERS))
453 SetLastError(ERROR_INSUFFICIENT_BUFFER);
457 status = NtQueryInformationProcess(hProcess, ProcessVmCounters,
458 &vmc, sizeof(vmc), NULL);
462 SetLastError(RtlNtStatusToDosError(status));
466 pmc->cb = sizeof(PROCESS_MEMORY_COUNTERS);
467 pmc->PageFaultCount = vmc.PageFaultCount;
468 pmc->PeakWorkingSetSize = vmc.PeakWorkingSetSize;
469 pmc->WorkingSetSize = vmc.WorkingSetSize;
470 pmc->QuotaPeakPagedPoolUsage = vmc.QuotaPeakPagedPoolUsage;
471 pmc->QuotaPagedPoolUsage = vmc.QuotaPagedPoolUsage;
472 pmc->QuotaPeakNonPagedPoolUsage = vmc.QuotaPeakNonPagedPoolUsage;
473 pmc->QuotaNonPagedPoolUsage = vmc.QuotaNonPagedPoolUsage;
474 pmc->PagefileUsage = vmc.PagefileUsage;
475 pmc->PeakPagefileUsage = vmc.PeakPagefileUsage;
480 /***********************************************************************
481 * GetWsChanges (PSAPI.@)
483 BOOL WINAPI GetWsChanges( HANDLE process, PPSAPI_WS_WATCH_INFORMATION watchinfo, DWORD size )
487 TRACE( "(%p, %p, %d)\n", process, watchinfo, size );
489 status = NtQueryInformationProcess( process, ProcessWorkingSetWatch, watchinfo, size, NULL );
493 SetLastError( RtlNtStatusToDosError( status ) );
499 /***********************************************************************
500 * InitializeProcessForWsWatch (PSAPI.@)
502 BOOL WINAPI InitializeProcessForWsWatch(HANDLE hProcess)
504 FIXME("(hProcess=%p): stub\n", hProcess);
509 /***********************************************************************
510 * QueryWorkingSet (PSAPI.@)
512 BOOL WINAPI QueryWorkingSet( HANDLE process, LPVOID buffer, DWORD size )
516 TRACE( "(%p, %p, %d)\n", process, buffer, size );
518 status = NtQueryVirtualMemory( process, NULL, MemoryWorkingSetList, buffer, size, NULL );
522 SetLastError( RtlNtStatusToDosError( status ) );
528 /***********************************************************************
529 * QueryWorkingSetEx (PSAPI.@)
531 BOOL WINAPI QueryWorkingSetEx( HANDLE process, LPVOID buffer, DWORD size )
535 TRACE( "(%p, %p, %d)\n", process, buffer, size );
537 status = NtQueryVirtualMemory( process, NULL, MemoryWorkingSetList, buffer, size, NULL );
541 SetLastError( RtlNtStatusToDosError( status ) );