winealsa.drv: Implement volume control interfaces.
[wine] / dlls / psapi / psapi_main.c
1 /*
2  *      PSAPI library
3  *
4  * Copyright 1998 Patrik Stridvall
5  * Copyright 2003 Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31 #include "winnls.h"
32 #include "winternl.h"
33 #include "psapi.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psapi);
36
37 typedef struct
38 {
39     HANDLE hProcess;             
40     PLIST_ENTRY pHead, pCurrent;
41     LDR_MODULE LdrModule;
42 } MODULE_ITERATOR;
43
44 /***********************************************************************
45  *           PSAPI_ModuleIteratorInit [internal]
46  *
47  * Prepares to iterate through the loaded modules of the given process.
48  *
49  * RETURNS
50  *  Success: TRUE
51  *  Failure: FALSE
52  */
53 static BOOL PSAPI_ModuleIteratorInit(MODULE_ITERATOR *iter, HANDLE hProcess)
54 {
55     PROCESS_BASIC_INFORMATION pbi;
56     PPEB_LDR_DATA pLdrData;
57     NTSTATUS status;
58
59     /* Get address of PEB */
60     status = NtQueryInformationProcess(hProcess, ProcessBasicInformation,
61                                        &pbi, sizeof(pbi), NULL);
62     if (status != STATUS_SUCCESS)
63     {
64         SetLastError(RtlNtStatusToDosError(status));
65         return FALSE;
66     }
67
68     /* Read address of LdrData from PEB */
69     if (!ReadProcessMemory(hProcess, &pbi.PebBaseAddress->LdrData,
70                            &pLdrData, sizeof(pLdrData), NULL))
71         return FALSE;
72
73     /* Read address of first module from LdrData */
74     if (!ReadProcessMemory(hProcess,
75                            &pLdrData->InLoadOrderModuleList.Flink,
76                            &iter->pCurrent, sizeof(iter->pCurrent), NULL))
77         return FALSE;
78
79     iter->pHead = &pLdrData->InLoadOrderModuleList;
80     iter->hProcess = hProcess;
81
82     return TRUE;
83 }
84
85 /***********************************************************************
86  *           PSAPI_ModuleIteratorNext [internal]
87  *
88  * Iterates to the next module.
89  *
90  * RETURNS
91  *   1 : Success
92  *   0 : No more modules
93  *  -1 : Failure
94  *
95  * NOTES
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.
100  */
101 static INT PSAPI_ModuleIteratorNext(MODULE_ITERATOR *iter)
102 {
103     if (iter->pCurrent == iter->pHead)
104         return 0;
105
106     if (!ReadProcessMemory(iter->hProcess, CONTAINING_RECORD(iter->pCurrent,
107                            LDR_MODULE, InLoadOrderModuleList),
108                            &iter->LdrModule, sizeof(iter->LdrModule), NULL))
109          return -1;
110     else
111          iter->pCurrent = iter->LdrModule.InLoadOrderModuleList.Flink;
112
113     return 1;
114 }
115
116 /***********************************************************************
117  *           PSAPI_GetLdrModule [internal]
118  *
119  * Reads the LDR_MODULE structure of the given module.
120  *
121  * RETURNS
122  *  Success: TRUE
123  *  Failure: FALSE
124  */
125
126 static BOOL PSAPI_GetLdrModule(HANDLE hProcess, HMODULE hModule, 
127                                LDR_MODULE *pLdrModule)
128 {
129     MODULE_ITERATOR iter;
130     INT ret;
131
132     if (!PSAPI_ModuleIteratorInit(&iter, hProcess))
133         return FALSE;
134
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)
139         {
140             *pLdrModule = iter.LdrModule;
141             return TRUE;
142         }
143
144     if (ret == 0)
145         SetLastError(ERROR_INVALID_HANDLE);
146
147     return FALSE;
148 }
149
150 /***********************************************************************
151  *           EnumDeviceDrivers (PSAPI.@)
152  */
153 BOOL WINAPI EnumDeviceDrivers(LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded)
154 {
155     FIXME("(%p, %d, %p): stub\n", lpImageBase, cb, lpcbNeeded);
156
157     if (lpcbNeeded)
158         *lpcbNeeded = 0;
159
160     return TRUE;
161 }
162
163 /***********************************************************************
164  *           EnumPageFilesA (PSAPI.@)
165  */
166 BOOL WINAPI EnumPageFilesA( PENUM_PAGE_FILE_CALLBACKA callback, LPVOID context )
167 {
168     FIXME("(%p, %p) stub\n", callback, context );
169     return FALSE;
170 }
171
172 /***********************************************************************
173  *           EnumPageFilesW (PSAPI.@)
174  */
175 BOOL WINAPI EnumPageFilesW( PENUM_PAGE_FILE_CALLBACKW callback, LPVOID context )
176 {
177     FIXME("(%p, %p) stub\n", callback, context );
178     return FALSE;
179 }
180
181 /***********************************************************************
182  *           EnumProcessModules (PSAPI.@)
183  *
184  * NOTES
185  *  Returned list is in load order.
186  */
187 BOOL WINAPI EnumProcessModules(HANDLE hProcess, HMODULE *lphModule,
188                                DWORD cb, LPDWORD lpcbNeeded)
189 {
190     MODULE_ITERATOR iter;
191     INT ret;
192
193     if (!PSAPI_ModuleIteratorInit(&iter, hProcess))
194         return FALSE;
195
196     *lpcbNeeded = 0;
197         
198     while ((ret = PSAPI_ModuleIteratorNext(&iter)) > 0)
199     {
200         if (cb >= sizeof(HMODULE))
201         {
202             *lphModule++ = iter.LdrModule.BaseAddress;
203             cb -= sizeof(HMODULE);
204         }
205         *lpcbNeeded += sizeof(HMODULE);
206     }
207
208     return (ret == 0);
209 }
210
211 /***********************************************************************
212  *          GetDeviceDriverBaseNameA (PSAPI.@)
213  */
214 DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName, 
215                                       DWORD nSize)
216 {
217     FIXME("(%p, %p, %d): stub\n", ImageBase, lpBaseName, nSize);
218
219     if (lpBaseName && nSize)
220         lpBaseName[0] = '\0';
221
222     return 0;
223 }
224
225 /***********************************************************************
226  *           GetDeviceDriverBaseNameW (PSAPI.@)
227  */
228 DWORD WINAPI GetDeviceDriverBaseNameW(LPVOID ImageBase, LPWSTR lpBaseName, 
229                                       DWORD nSize)
230 {
231     FIXME("(%p, %p, %d): stub\n", ImageBase, lpBaseName, nSize);
232
233     if (lpBaseName && nSize)
234         lpBaseName[0] = '\0';
235
236     return 0;
237 }
238
239 /***********************************************************************
240  *           GetDeviceDriverFileNameA (PSAPI.@)
241  */
242 DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename, 
243                                       DWORD nSize)
244 {
245     FIXME("(%p, %p, %d): stub\n", ImageBase, lpFilename, nSize);
246
247     if (lpFilename && nSize)
248         lpFilename[0] = '\0';
249
250     return 0;
251 }
252
253 /***********************************************************************
254  *           GetDeviceDriverFileNameW (PSAPI.@)
255  */
256 DWORD WINAPI GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename, 
257                                       DWORD nSize)
258 {
259     FIXME("(%p, %p, %d): stub\n", ImageBase, lpFilename, nSize);
260
261     if (lpFilename && nSize)
262         lpFilename[0] = '\0';
263
264     return 0;
265 }
266
267 /***********************************************************************
268  *           GetMappedFileNameA (PSAPI.@)
269  */
270 DWORD WINAPI GetMappedFileNameA(HANDLE hProcess, LPVOID lpv, LPSTR lpFilename, 
271                                 DWORD nSize)
272 {
273     FIXME("(%p, %p, %p, %d): stub\n", hProcess, lpv, lpFilename, nSize);
274
275     if (lpFilename && nSize)
276         lpFilename[0] = '\0';
277
278     return 0;
279 }
280
281 /***********************************************************************
282  *           GetMappedFileNameW (PSAPI.@)
283  */
284 DWORD WINAPI GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, 
285                                 DWORD nSize)
286 {
287     FIXME("(%p, %p, %p, %d): stub\n", hProcess, lpv, lpFilename, nSize);
288
289     if (lpFilename && nSize)
290         lpFilename[0] = '\0';
291
292     return 0;
293 }
294
295 /***********************************************************************
296  *           GetModuleBaseNameA (PSAPI.@)
297  */
298 DWORD WINAPI GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule, 
299                                 LPSTR lpBaseName, DWORD nSize)
300 {
301     WCHAR *lpBaseNameW;
302     DWORD buflenW, ret = 0;
303
304     if(!lpBaseName || !nSize) {
305         SetLastError(ERROR_INVALID_PARAMETER);
306         return 0;
307     }
308     lpBaseNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * nSize);
309     buflenW = GetModuleBaseNameW(hProcess, hModule, lpBaseNameW, nSize);
310     TRACE("%d, %s\n", buflenW, debugstr_w(lpBaseNameW));
311     if (buflenW)
312     {
313         ret = WideCharToMultiByte(CP_ACP, 0, lpBaseNameW, buflenW,
314                                   lpBaseName, nSize, NULL, NULL);
315         if (ret < nSize) lpBaseName[ret] = 0;
316     }
317     HeapFree(GetProcessHeap(), 0, lpBaseNameW);
318     return ret;
319 }
320
321 /***********************************************************************
322  *           GetModuleBaseNameW (PSAPI.@)
323  */
324 DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule, 
325                                 LPWSTR lpBaseName, DWORD nSize)
326 {
327     LDR_MODULE LdrModule;
328
329     if (!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
330         return 0;
331
332     nSize = min(LdrModule.BaseDllName.Length / sizeof(WCHAR), nSize);
333     if (!ReadProcessMemory(hProcess, LdrModule.BaseDllName.Buffer,
334                            lpBaseName, nSize * sizeof(WCHAR), NULL))
335         return 0;
336
337     lpBaseName[nSize] = 0;
338     return nSize;
339 }
340
341 /***********************************************************************
342  *           GetModuleFileNameExA (PSAPI.@)
343  */
344 DWORD WINAPI GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule, 
345                                   LPSTR lpFileName, DWORD nSize)
346 {
347     WCHAR *ptr;
348
349     TRACE("(hProcess=%p, hModule=%p, %p, %d)\n",
350           hProcess, hModule, lpFileName, nSize);
351
352     if (!lpFileName || !nSize) return 0;
353
354     if ( hProcess == GetCurrentProcess() )
355     {
356         DWORD len = GetModuleFileNameA( hModule, lpFileName, nSize );
357         if (nSize) lpFileName[nSize - 1] = '\0';
358         return len;
359     }
360
361     if (!(ptr = HeapAlloc(GetProcessHeap(), 0, nSize * sizeof(WCHAR)))) return 0;
362
363     if (!GetModuleFileNameExW(hProcess, hModule, ptr, nSize))
364     {
365         lpFileName[0] = '\0';
366     }
367     else
368     {
369         if (!WideCharToMultiByte( CP_ACP, 0, ptr, -1, lpFileName, nSize, NULL, NULL ))
370             lpFileName[nSize - 1] = 0;
371     }
372
373     HeapFree(GetProcessHeap(), 0, ptr);
374     return strlen(lpFileName);
375 }
376
377 /***********************************************************************
378  *           GetModuleFileNameExW (PSAPI.@)
379  */
380 DWORD WINAPI GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule, 
381                                   LPWSTR lpFileName, DWORD nSize)
382 {
383     LDR_MODULE LdrModule;
384     
385     if(!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
386         return 0;
387         
388     nSize = min(LdrModule.FullDllName.Length / sizeof(WCHAR), nSize);
389     if (!ReadProcessMemory(hProcess, LdrModule.FullDllName.Buffer,
390                            lpFileName, nSize * sizeof(WCHAR), NULL))
391         return 0;
392
393     lpFileName[nSize] = 0;
394     return nSize;
395 }
396
397 /***********************************************************************
398  *           GetModuleInformation (PSAPI.@)
399  */
400 BOOL WINAPI GetModuleInformation(HANDLE hProcess, HMODULE hModule, 
401                                  LPMODULEINFO lpmodinfo, DWORD cb)
402 {
403     LDR_MODULE LdrModule;
404
405     if (cb < sizeof(MODULEINFO)) 
406     {
407         SetLastError(ERROR_INSUFFICIENT_BUFFER);
408         return FALSE;
409     }
410
411     if (!PSAPI_GetLdrModule(hProcess, hModule, &LdrModule))
412         return FALSE;
413
414     lpmodinfo->lpBaseOfDll = LdrModule.BaseAddress;
415     lpmodinfo->SizeOfImage = LdrModule.SizeOfImage;
416     lpmodinfo->EntryPoint  = LdrModule.EntryPoint;
417     return TRUE;
418 }
419
420 /***********************************************************************
421  *           GetPerformanceInfo (PSAPI.@)
422  */
423 BOOL WINAPI GetPerformanceInfo( PPERFORMANCE_INFORMATION info, DWORD size )
424 {
425     NTSTATUS status;
426
427     TRACE( "(%p, %d)\n", info, size );
428
429     status = NtQuerySystemInformation( SystemPerformanceInformation, info, size, NULL );
430
431     if (status)
432     {
433         SetLastError( RtlNtStatusToDosError( status ) );
434         return FALSE;
435     }
436     return TRUE;
437 }
438
439 /***********************************************************************
440  *           GetProcessMemoryInfo (PSAPI.@)
441  *
442  * Retrieve memory usage information for a given process
443  *
444  */
445 BOOL WINAPI GetProcessMemoryInfo(HANDLE hProcess, 
446                                  PPROCESS_MEMORY_COUNTERS pmc, DWORD cb)
447 {
448     NTSTATUS status;
449     VM_COUNTERS vmc;
450
451     if (cb < sizeof(PROCESS_MEMORY_COUNTERS))
452     {
453         SetLastError(ERROR_INSUFFICIENT_BUFFER);
454         return FALSE;
455     }
456
457     status = NtQueryInformationProcess(hProcess, ProcessVmCounters, 
458                                        &vmc, sizeof(vmc), NULL);
459
460     if (status)
461     {
462         SetLastError(RtlNtStatusToDosError(status));
463         return FALSE;
464     }
465
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;
476
477     return TRUE;
478 }
479
480 /***********************************************************************
481  *           GetWsChanges (PSAPI.@)
482  */
483 BOOL WINAPI GetWsChanges( HANDLE process, PPSAPI_WS_WATCH_INFORMATION watchinfo, DWORD size )
484 {
485     NTSTATUS status;
486
487     TRACE( "(%p, %p, %d)\n", process, watchinfo, size );
488
489     status = NtQueryInformationProcess( process, ProcessWorkingSetWatch, watchinfo, size, NULL );
490
491     if (status)
492     {
493         SetLastError( RtlNtStatusToDosError( status ) );
494         return FALSE;
495     }
496     return TRUE;
497 }
498
499 /***********************************************************************
500  *           InitializeProcessForWsWatch (PSAPI.@)
501  */
502 BOOL WINAPI InitializeProcessForWsWatch(HANDLE hProcess)
503 {
504     FIXME("(hProcess=%p): stub\n", hProcess);
505
506     return TRUE;
507 }
508
509 /***********************************************************************
510  *           QueryWorkingSet (PSAPI.@)
511  */
512 BOOL WINAPI QueryWorkingSet( HANDLE process, LPVOID buffer, DWORD size )
513 {
514     NTSTATUS status;
515
516     TRACE( "(%p, %p, %d)\n", process, buffer, size );
517
518     status = NtQueryVirtualMemory( process, NULL, MemoryWorkingSetList, buffer, size, NULL );
519
520     if (status)
521     {
522         SetLastError( RtlNtStatusToDosError( status ) );
523         return FALSE;
524     }
525     return TRUE;
526 }
527
528 /***********************************************************************
529  *           QueryWorkingSetEx (PSAPI.@)
530  */
531 BOOL WINAPI QueryWorkingSetEx( HANDLE process, LPVOID buffer, DWORD size )
532 {
533     NTSTATUS status;
534
535     TRACE( "(%p, %p, %d)\n", process, buffer, size );
536
537     status = NtQueryVirtualMemory( process, NULL, MemoryWorkingSetList, buffer,  size, NULL );
538
539     if (status)
540     {
541         SetLastError( RtlNtStatusToDosError( status ) );
542         return FALSE;
543     }
544     return TRUE;
545 }