Avoid code duplication by moving handle lookup into get_algid_info.
[wine/wine-kai.git] / dlls / psapi / psapi_main.c
blobc61770ee52a8d3d8ae2a9b5b418e41ebeca5430a
1 /*
2 * PSAPI library
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/server.h"
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
30 #include "winnls.h"
31 #include "psapi.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(psapi);
35 /***********************************************************************
36 * EmptyWorkingSet (PSAPI.@)
38 BOOL WINAPI EmptyWorkingSet(HANDLE hProcess)
40 return SetProcessWorkingSetSize(hProcess, 0xFFFFFFFF, 0xFFFFFFFF);
43 /***********************************************************************
44 * EnumDeviceDrivers (PSAPI.@)
46 BOOL WINAPI EnumDeviceDrivers(LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded)
48 FIXME("(%p, %ld, %p): stub\n", lpImageBase, cb, lpcbNeeded);
50 if (lpcbNeeded)
51 *lpcbNeeded = 0;
53 return TRUE;
57 /***********************************************************************
58 * EnumProcesses (PSAPI.@)
60 BOOL WINAPI EnumProcesses(DWORD *lpidProcess, DWORD cb, DWORD *lpcbNeeded)
62 HANDLE hSnapshot;
63 DWORD count;
64 DWORD countMax;
65 DWORD pid;
66 int ret;
68 TRACE("(%p, %ld, %p)\n", lpidProcess,cb, lpcbNeeded);
70 if ( lpidProcess == NULL )
71 cb = 0;
72 if ( lpcbNeeded != NULL )
73 *lpcbNeeded = 0;
75 SERVER_START_REQ( create_snapshot )
77 req->flags = SNAP_PROCESS;
78 req->inherit = FALSE;
79 req->pid = 0;
80 wine_server_call_err( req );
81 hSnapshot = reply->handle;
83 SERVER_END_REQ;
85 if ( hSnapshot == 0 )
87 FIXME("cannot create snapshot\n");
88 return FALSE;
90 count = 0;
91 countMax = cb / sizeof(DWORD);
92 for (;;)
94 SERVER_START_REQ( next_process )
96 req->handle = hSnapshot;
97 req->reset = (count == 0);
98 if ((ret = !wine_server_call_err( req )))
99 pid = reply->pid;
101 SERVER_END_REQ;
102 if (!ret) break;
103 TRACE("process 0x%08lx\n", pid);
104 if ( count < countMax )
105 lpidProcess[count] = pid;
106 count++;
108 CloseHandle( hSnapshot );
110 if ( lpcbNeeded != NULL )
111 *lpcbNeeded = sizeof(DWORD) * count;
113 TRACE("return %lu processes\n", count);
115 return TRUE;
118 /***********************************************************************
119 * EnumProcessModules (PSAPI.@)
121 BOOL WINAPI EnumProcessModules(HANDLE hProcess, HMODULE *lphModule,
122 DWORD cb, LPDWORD lpcbNeeded)
124 HANDLE hSnapshot;
125 DWORD pid;
126 DWORD count;
127 DWORD countMax;
128 int ret;
129 HMODULE hModule;
131 TRACE("(hProcess=%p, %p, %ld, %p)\n",
132 hProcess, lphModule, cb, lpcbNeeded );
134 if ( lphModule == NULL )
135 cb = 0;
136 if ( lpcbNeeded != NULL )
137 *lpcbNeeded = 0;
139 SERVER_START_REQ( get_process_info )
141 req->handle = hProcess;
142 if ( !wine_server_call_err( req ) )
143 pid = (DWORD)reply->pid;
144 else
145 pid = 0;
147 SERVER_END_REQ;
149 if ( pid == 0 )
151 FIXME("no pid for hProcess %p\n" ,hProcess);
152 return FALSE;
155 SERVER_START_REQ( create_snapshot )
157 req->flags = SNAP_MODULE;
158 req->inherit = FALSE;
159 req->pid = pid;
160 wine_server_call_err( req );
161 hSnapshot = reply->handle;
163 SERVER_END_REQ;
164 if ( hSnapshot == 0 )
166 FIXME("cannot create snapshot\n");
167 return FALSE;
169 count = 0;
170 countMax = cb / sizeof(HMODULE);
171 for (;;)
173 SERVER_START_REQ( next_module )
175 req->handle = hSnapshot;
176 req->reset = (count == 0);
177 if ((ret = !wine_server_call_err( req )))
179 hModule = (HMODULE)reply->base;
182 SERVER_END_REQ;
183 if ( !ret ) break;
184 TRACE("module 0x%p\n", hModule);
185 if ( count < countMax )
186 lphModule[count] = hModule;
187 count++;
189 CloseHandle( hSnapshot );
191 if ( lpcbNeeded != NULL )
192 *lpcbNeeded = sizeof(HMODULE) * count;
194 TRACE("return %lu modules\n", count);
196 return TRUE;
199 /***********************************************************************
200 * GetDeviceDriverBaseNameA (PSAPI.@)
202 DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName,
203 DWORD nSize)
205 FIXME("(%p, %s, %ld): stub\n",
206 ImageBase, debugstr_a(lpBaseName), nSize);
208 if (lpBaseName && nSize)
209 lpBaseName[0] = '\0';
211 return 0;
214 /***********************************************************************
215 * GetDeviceDriverBaseNameW (PSAPI.@)
217 DWORD WINAPI GetDeviceDriverBaseNameW(LPVOID ImageBase, LPWSTR lpBaseName,
218 DWORD nSize)
220 FIXME("(%p, %s, %ld): stub\n",
221 ImageBase, debugstr_w(lpBaseName), nSize);
223 if (lpBaseName && nSize)
224 lpBaseName[0] = '\0';
226 return 0;
229 /***********************************************************************
230 * GetDeviceDriverFileNameA (PSAPI.@)
232 DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename,
233 DWORD nSize)
235 FIXME("(%p, %s, %ld): stub\n",
236 ImageBase, debugstr_a(lpFilename), nSize);
238 if (lpFilename && nSize)
239 lpFilename[0] = '\0';
241 return 0;
244 /***********************************************************************
245 * GetDeviceDriverFileNameW (PSAPI.@)
247 DWORD WINAPI GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename,
248 DWORD nSize)
250 FIXME("(%p, %s, %ld): stub\n",
251 ImageBase, debugstr_w(lpFilename), nSize);
253 if (lpFilename && nSize)
254 lpFilename[0] = '\0';
256 return 0;
259 /***********************************************************************
260 * GetMappedFileNameA (PSAPI.@)
262 DWORD WINAPI GetMappedFileNameA(HANDLE hProcess, LPVOID lpv, LPSTR lpFilename,
263 DWORD nSize)
265 FIXME("(hProcess=%p, %p, %s, %ld): stub\n",
266 hProcess, lpv, debugstr_a(lpFilename), nSize);
268 if (lpFilename && nSize)
269 lpFilename[0] = '\0';
271 return 0;
274 /***********************************************************************
275 * GetMappedFileNameW (PSAPI.@)
277 DWORD WINAPI GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename,
278 DWORD nSize)
280 FIXME("(hProcess=%p, %p, %s, %ld): stub\n",
281 hProcess, lpv, debugstr_w(lpFilename), nSize);
283 if (lpFilename && nSize)
284 lpFilename[0] = '\0';
286 return 0;
289 /***********************************************************************
290 * GetModuleBaseNameA (PSAPI.@)
292 DWORD WINAPI GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule,
293 LPSTR lpBaseName, DWORD nSize)
295 WCHAR *lpBaseNameW;
296 DWORD buflenW, ret = 0;
298 if(!lpBaseName || !nSize) {
299 SetLastError(ERROR_INVALID_PARAMETER);
300 return 0;
302 lpBaseNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * nSize);
303 buflenW = GetModuleBaseNameW(hProcess, hModule, lpBaseNameW, nSize);
304 TRACE("%ld, %s\n", buflenW, debugstr_w(lpBaseNameW));
305 if (buflenW)
307 ret = WideCharToMultiByte(CP_ACP, 0, lpBaseNameW, buflenW,
308 lpBaseName, nSize, NULL, NULL);
309 if (ret < nSize) lpBaseName[ret] = 0;
311 HeapFree(GetProcessHeap(), 0, lpBaseNameW);
312 return ret;
315 /***********************************************************************
316 * GetModuleBaseNameW (PSAPI.@)
318 DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule,
319 LPWSTR lpBaseName, DWORD nSize)
321 WCHAR tmp[MAX_PATH];
322 WCHAR* ptr;
324 if(!lpBaseName || !nSize) {
325 SetLastError(ERROR_INVALID_PARAMETER);
326 return 0;
329 if (!GetModuleFileNameExW(hProcess, hModule, tmp,
330 sizeof(tmp)/sizeof(WCHAR)))
331 return 0;
332 TRACE("%s\n", debugstr_w(tmp));
333 if ((ptr = strrchrW(tmp, '\\')) != NULL) ptr++; else ptr = tmp;
334 strncpyW(lpBaseName, ptr, nSize);
335 return min(strlenW(ptr), nSize);
338 /***********************************************************************
339 * GetModuleFileNameExA (PSAPI.@)
341 DWORD WINAPI GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule,
342 LPSTR lpFileName, DWORD nSize)
344 WCHAR *ptr;
346 TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
347 hProcess, hModule, lpFileName, nSize);
349 if (!lpFileName || !nSize) return 0;
351 if ( hProcess == GetCurrentProcess() )
353 DWORD len = GetModuleFileNameA( hModule, lpFileName, nSize );
354 if (nSize) lpFileName[nSize - 1] = '\0';
355 return len;
358 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, nSize * sizeof(WCHAR)))) return 0;
360 if (!GetModuleFileNameExW(hProcess, hModule, ptr, nSize))
362 lpFileName[0] = '\0';
364 else
366 if (!WideCharToMultiByte( CP_ACP, 0, ptr, -1, lpFileName, nSize, NULL, NULL ))
367 lpFileName[nSize - 1] = 0;
370 HeapFree(GetProcessHeap(), 0, ptr);
371 return strlen(lpFileName);
374 /***********************************************************************
375 * GetModuleFileNameExW (PSAPI.@)
377 DWORD WINAPI GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule,
378 LPWSTR lpFileName, DWORD nSize)
380 DWORD len = 0;
382 TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
383 hProcess, hModule, lpFileName, nSize);
385 if (!lpFileName || !nSize) return 0;
387 if ( hProcess == GetCurrentProcess() )
389 DWORD len = GetModuleFileNameW( hModule, lpFileName, nSize );
390 if (nSize) lpFileName[nSize - 1] = '\0';
391 TRACE("return (cur) %s (%lu)\n", debugstr_w(lpFileName), len);
392 return len;
395 lpFileName[0] = 0;
397 SERVER_START_REQ( get_dll_info )
399 req->handle = hProcess;
400 req->base_address = hModule;
401 wine_server_set_reply( req, lpFileName, (nSize - 1) * sizeof(WCHAR) );
402 if (!wine_server_call_err( req ))
404 len = wine_server_reply_size(reply) / sizeof(WCHAR);
405 lpFileName[len] = 0;
408 SERVER_END_REQ;
410 TRACE("return %s (%lu)\n", debugstr_w(lpFileName), len);
412 return len;
415 /***********************************************************************
416 * GetModuleInformation (PSAPI.@)
418 BOOL WINAPI GetModuleInformation(HANDLE hProcess, HMODULE hModule,
419 LPMODULEINFO lpmodinfo, DWORD cb)
421 BOOL ret = FALSE;
423 TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
424 hProcess, hModule, lpmodinfo, cb);
426 if (cb < sizeof(MODULEINFO)) return FALSE;
428 SERVER_START_REQ( get_dll_info )
430 req->handle = hProcess;
431 req->base_address = (void*)hModule;
432 if (!wine_server_call_err( req ))
434 ret = TRUE;
435 lpmodinfo->lpBaseOfDll = (void*)hModule;
436 lpmodinfo->SizeOfImage = reply->size;
437 lpmodinfo->EntryPoint = reply->entry_point;
440 SERVER_END_REQ;
442 return TRUE;
445 /***********************************************************************
446 * GetProcessMemoryInfo (PSAPI.@)
448 BOOL WINAPI GetProcessMemoryInfo(HANDLE Process,
449 PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
451 FIXME("(hProcess=%p, %p, %ld): stub\n",
452 Process, ppsmemCounters, cb);
454 memset(ppsmemCounters, 0, cb);
456 return TRUE;
459 /***********************************************************************
460 * GetWsChanges (PSAPI.@)
462 BOOL WINAPI GetWsChanges(HANDLE hProcess,
463 PPSAPI_WS_WATCH_INFORMATION lpWatchInfo, DWORD cb)
465 FIXME("(hProcess=%p, %p, %ld): stub\n",
466 hProcess, lpWatchInfo, cb);
468 memset(lpWatchInfo, 0, cb);
470 return TRUE;
473 /***********************************************************************
474 * InitializeProcessForWsWatch (PSAPI.@)
476 BOOL WINAPI InitializeProcessForWsWatch(HANDLE hProcess)
478 FIXME("(hProcess=%p): stub\n", hProcess);
480 return TRUE;
483 /***********************************************************************
484 * QueryWorkingSet (PSAPI.@)
486 BOOL WINAPI QueryWorkingSet(HANDLE hProcess, LPVOID pv, DWORD cb)
488 FIXME("(hProcess=%p, %p, %ld)\n", hProcess, pv, cb);
490 if (pv && cb)
491 ((DWORD *) pv)[0] = 0; /* Empty WorkingSet */
493 return TRUE;