Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/server.h"
28 #include "wine/debug.h"
29 #include "winnls.h"
30 #include "psapi.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(psapi);
33
34 /***********************************************************************
35  *           EmptyWorkingSet (PSAPI.@)
36  */
37 BOOL WINAPI EmptyWorkingSet(HANDLE hProcess)
38 {
39     return SetProcessWorkingSetSize(hProcess, 0xFFFFFFFF, 0xFFFFFFFF);
40 }
41
42 /***********************************************************************
43  *           EnumDeviceDrivers (PSAPI.@)
44  */
45 BOOL WINAPI EnumDeviceDrivers(LPVOID *lpImageBase, DWORD cb, LPDWORD lpcbNeeded)
46 {
47     FIXME("(%p, %ld, %p): stub\n", lpImageBase, cb, lpcbNeeded);
48
49     if (lpcbNeeded)
50         *lpcbNeeded = 0;
51
52     return TRUE;
53 }
54
55
56 /***********************************************************************
57  *           EnumProcesses (PSAPI.@)
58  */
59 BOOL WINAPI EnumProcesses(DWORD *lpidProcess, DWORD cb, DWORD *lpcbNeeded)
60 {
61     HANDLE      hSnapshot;
62     DWORD       count;
63     DWORD       countMax;
64     DWORD       pid;
65     int         ret;
66
67     TRACE("(%p, %ld, %p)\n", lpidProcess,cb, lpcbNeeded);
68
69     if ( lpidProcess == NULL )
70         cb = 0;
71     if ( lpcbNeeded != NULL )
72         *lpcbNeeded = 0;
73
74     SERVER_START_REQ( create_snapshot )
75     {
76         req->flags   = SNAP_PROCESS;
77         req->inherit = FALSE;
78         req->pid     = 0;
79         wine_server_call_err( req );
80         hSnapshot = reply->handle;
81     }
82     SERVER_END_REQ;
83
84     if ( hSnapshot == 0 )
85     {
86         FIXME("cannot create snapshot\n");
87         return FALSE;
88     }
89     count = 0;
90     countMax = cb / sizeof(DWORD);
91     for (;;)
92     {
93         SERVER_START_REQ( next_process )
94         {
95             req->handle = hSnapshot;
96             req->reset = (count == 0);
97             wine_server_set_reply( req, NULL, 0);
98             if ((ret = !wine_server_call_err( req )))
99                 pid = reply->pid;
100         }
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++;
107     }
108     CloseHandle( hSnapshot );
109
110     if ( lpcbNeeded != NULL )
111         *lpcbNeeded = sizeof(DWORD) * count;
112
113     TRACE("return %lu processes\n", count);
114
115     return TRUE;
116 }
117
118 /***********************************************************************
119  *           EnumProcessModules (PSAPI.@)
120  */
121 BOOL WINAPI EnumProcessModules(HANDLE hProcess, HMODULE *lphModule, 
122                                DWORD cb, LPDWORD lpcbNeeded)
123 {
124     HANDLE      hSnapshot;
125     DWORD       pid;
126     DWORD       count;
127     DWORD       countMax;
128     int         ret;
129     HMODULE     hModule;
130
131     TRACE("(hProcess=%p, %p, %ld, %p)\n",
132           hProcess, lphModule, cb, lpcbNeeded );
133
134     if ( lphModule == NULL )
135         cb = 0;
136     if ( lpcbNeeded != NULL )
137         *lpcbNeeded = 0;
138
139     SERVER_START_REQ( get_process_info )
140     {
141         req->handle = hProcess;
142         if ( !wine_server_call_err( req ) )
143             pid = (DWORD)reply->pid;
144         else
145             pid = 0;
146     }
147     SERVER_END_REQ;
148
149     if ( pid == 0 )
150     {
151         FIXME("no pid for hProcess %p\n" ,hProcess);
152         return FALSE;
153     }
154
155     SERVER_START_REQ( create_snapshot )
156     {
157         req->flags   = SNAP_MODULE;
158         req->inherit = FALSE;
159         req->pid     = pid;
160         wine_server_call_err( req );
161         hSnapshot = reply->handle;
162     }
163     SERVER_END_REQ;
164     if ( hSnapshot == 0 )
165     {
166         FIXME("cannot create snapshot\n");
167         return FALSE;
168     }
169     count = 0;
170     countMax = cb / sizeof(HMODULE);
171     for (;;)
172     {
173         SERVER_START_REQ( next_module )
174         {
175             req->handle = hSnapshot;
176             req->reset = (count == 0);
177             wine_server_set_reply( req, NULL, 0 );
178             if ((ret = !wine_server_call_err( req )))
179             {
180                 hModule = (HMODULE)reply->base;
181             }
182         }
183         SERVER_END_REQ;
184         if ( !ret ) break;
185         TRACE("module 0x%p\n", hModule);
186         if ( count < countMax )
187             lphModule[count] = hModule;
188         count++;
189     }
190     CloseHandle( hSnapshot );
191
192     if ( lpcbNeeded != NULL )
193         *lpcbNeeded = sizeof(HMODULE) * count;
194
195     TRACE("return %lu modules\n", count);
196
197     return TRUE;
198 }
199
200 /***********************************************************************
201  *          GetDeviceDriverBaseNameA (PSAPI.@)
202  */
203 DWORD WINAPI GetDeviceDriverBaseNameA(LPVOID ImageBase, LPSTR lpBaseName, 
204                                       DWORD nSize)
205 {
206     FIXME("(%p, %s, %ld): stub\n",
207           ImageBase, debugstr_a(lpBaseName), nSize);
208
209     if (lpBaseName && nSize)
210         lpBaseName[0] = '\0';
211
212     return 0;
213 }
214
215 /***********************************************************************
216  *           GetDeviceDriverBaseNameW (PSAPI.@)
217  */
218 DWORD WINAPI GetDeviceDriverBaseNameW(LPVOID ImageBase, LPWSTR lpBaseName, 
219                                       DWORD nSize)
220 {
221     FIXME("(%p, %s, %ld): stub\n",
222           ImageBase, debugstr_w(lpBaseName), nSize);
223
224     if (lpBaseName && nSize)
225         lpBaseName[0] = '\0';
226
227     return 0;
228 }
229
230 /***********************************************************************
231  *           GetDeviceDriverFileNameA (PSAPI.@)
232  */
233 DWORD WINAPI GetDeviceDriverFileNameA(LPVOID ImageBase, LPSTR lpFilename, 
234                                       DWORD nSize)
235 {
236     FIXME("(%p, %s, %ld): stub\n",
237           ImageBase, debugstr_a(lpFilename), nSize);
238
239     if (lpFilename && nSize)
240         lpFilename[0] = '\0';
241
242     return 0;
243 }
244
245 /***********************************************************************
246  *           GetDeviceDriverFileNameW (PSAPI.@)
247  */
248 DWORD WINAPI GetDeviceDriverFileNameW(LPVOID ImageBase, LPWSTR lpFilename, 
249                                       DWORD nSize)
250 {
251     FIXME("(%p, %s, %ld): stub\n",
252           ImageBase, debugstr_w(lpFilename), nSize);
253
254     if (lpFilename && nSize)
255         lpFilename[0] = '\0';
256
257     return 0;
258 }
259
260 /***********************************************************************
261  *           GetMappedFileNameA (PSAPI.@)
262  */
263 DWORD WINAPI GetMappedFileNameA(HANDLE hProcess, LPVOID lpv, LPSTR lpFilename, 
264                                 DWORD nSize)
265 {
266     FIXME("(hProcess=%p, %p, %s, %ld): stub\n",
267           hProcess, lpv, debugstr_a(lpFilename), nSize);
268
269     if (lpFilename && nSize)
270         lpFilename[0] = '\0';
271
272     return 0;
273 }
274
275 /***********************************************************************
276  *           GetMappedFileNameW (PSAPI.@)
277  */
278 DWORD WINAPI GetMappedFileNameW(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, 
279                                 DWORD nSize)
280 {
281     FIXME("(hProcess=%p, %p, %s, %ld): stub\n",
282           hProcess, lpv, debugstr_w(lpFilename), nSize);
283
284     if (lpFilename && nSize)
285         lpFilename[0] = '\0';
286
287     return 0;
288 }
289
290 /***********************************************************************
291  *           GetModuleBaseNameA (PSAPI.@)
292  */
293 DWORD WINAPI GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule, 
294                                 LPSTR lpBaseName, DWORD nSize)
295 {
296     char        tmp[MAX_PATH];
297     char*       ptr;
298
299     if (!GetModuleFileNameExA(hProcess, hModule, tmp, sizeof(tmp)))
300         return 0;
301     if ((ptr = strrchr(tmp, '\\')) != NULL) ptr++; else ptr = tmp;
302     strncpy(lpBaseName, ptr, nSize);
303     lpBaseName[nSize - 1] = '\0';
304     return strlen(lpBaseName);
305 }
306
307 /***********************************************************************
308  *           GetModuleBaseNameW (PSAPI.@)
309  */
310 DWORD WINAPI GetModuleBaseNameW(HANDLE hProcess, HMODULE hModule, 
311                                 LPWSTR lpBaseName, DWORD nSize)
312 {
313     char*       ptr;
314     DWORD       len;
315
316     TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
317           hProcess, hModule, lpBaseName, nSize);
318    
319     if (!lpBaseName || !nSize) return 0;
320
321     ptr = HeapAlloc(GetProcessHeap(), 0, nSize / 2);
322     if (!ptr) return 0;
323
324     len = GetModuleBaseNameA(hProcess, hModule, ptr, nSize / 2);
325     if (len == 0)
326     {
327         lpBaseName[0] = '\0';
328     }
329     else
330     {
331         if (!MultiByteToWideChar( CP_ACP, 0, ptr, -1, lpBaseName, nSize / 2 ))
332             lpBaseName[nSize / 2 - 1] = 0;
333     }
334
335     HeapFree(GetProcessHeap(), 0, ptr);
336     return len;
337 }
338
339 /***********************************************************************
340  *           GetModuleFileNameExA (PSAPI.@)
341  */
342 DWORD WINAPI GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule, 
343                                   LPSTR lpFileName, DWORD nSize)
344 {
345     DWORD       len = 0;
346
347     TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
348           hProcess, hModule, lpFileName, nSize);
349
350     if (!lpFileName || !nSize) return 0;
351
352     if ( hProcess == GetCurrentProcess() )
353         return GetModuleFileNameA( hModule, lpFileName, nSize );
354
355     lpFileName[0] = 0;
356
357     SERVER_START_REQ( get_dll_info )
358     {
359         req->handle       = hProcess;
360         req->base_address = (void*)hModule;
361         wine_server_set_reply( req, lpFileName, nSize - 1);
362         if (!wine_server_call_err( req ))
363         {
364             len = wine_server_reply_size(reply);
365             lpFileName[len] = 0;
366         }
367     }
368     SERVER_END_REQ;
369
370     TRACE("return %s (%lu)\n", lpFileName, len);
371
372     return len;
373 }
374
375 /***********************************************************************
376  *           GetModuleFileNameExW (PSAPI.@)
377  */
378 DWORD WINAPI GetModuleFileNameExW(HANDLE hProcess, HMODULE hModule, 
379                                   LPWSTR lpFileName, DWORD nSize)
380 {
381     char*       ptr;
382     DWORD       len;
383
384     TRACE("(hProcess=%p,hModule=%p, %p, %ld)\n",
385           hProcess, hModule, lpFileName, nSize);
386
387     if (!lpFileName || !nSize) return 0;
388
389     if ( hProcess == GetCurrentProcess() )
390         return GetModuleFileNameW( hModule, lpFileName, nSize );
391
392     ptr = HeapAlloc(GetProcessHeap(), 0, nSize / 2);
393     if (!ptr) return 0;
394
395     len = GetModuleFileNameExA(hProcess, hModule, ptr, nSize / 2);
396     if (len == 0)
397     {
398         lpFileName[0] = '\0';
399     }
400     else
401     {
402         if (!MultiByteToWideChar( CP_ACP, 0, ptr, -1, lpFileName, nSize / 2 ))
403             lpFileName[nSize / 2 - 1] = 0;
404     }
405
406     HeapFree(GetProcessHeap(), 0, ptr);
407     return len;
408 }
409
410 /***********************************************************************
411  *           GetModuleInformation (PSAPI.@)
412  */
413 BOOL WINAPI GetModuleInformation(HANDLE hProcess, HMODULE hModule, 
414                                  LPMODULEINFO lpmodinfo, DWORD cb)
415 {
416     BOOL ret = FALSE;
417
418     TRACE("(hProcess=%p, hModule=%p, %p, %ld)\n",
419           hProcess, hModule, lpmodinfo, cb);
420
421     if (cb < sizeof(MODULEINFO)) return FALSE;
422
423     SERVER_START_REQ( get_dll_info )
424     {
425         req->handle       = hProcess;
426         req->base_address = (void*)hModule;
427         if (!wine_server_call_err( req ))
428         {
429             ret = TRUE;
430             lpmodinfo->lpBaseOfDll = (void*)hModule;
431             lpmodinfo->SizeOfImage = reply->size;
432             lpmodinfo->EntryPoint  = reply->entry_point;
433         }
434     }
435     SERVER_END_REQ;
436
437     return TRUE;
438 }
439
440 /***********************************************************************
441  *           GetProcessMemoryInfo (PSAPI.@)
442  */
443 BOOL WINAPI GetProcessMemoryInfo(HANDLE Process, 
444                                  PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb)
445 {
446     FIXME("(hProcess=%p, %p, %ld): stub\n",
447           Process, ppsmemCounters, cb);
448     
449     memset(ppsmemCounters, 0, cb);
450
451     return TRUE;
452 }
453
454 /***********************************************************************
455  *           GetWsChanges (PSAPI.@)
456  */
457 BOOL WINAPI GetWsChanges(HANDLE hProcess, 
458                          PPSAPI_WS_WATCH_INFORMATION lpWatchInfo, DWORD cb)
459 {
460     FIXME("(hProcess=%p, %p, %ld): stub\n",
461           hProcess, lpWatchInfo, cb);
462
463     memset(lpWatchInfo, 0, cb);
464
465     return TRUE;
466 }
467
468 /***********************************************************************
469  *           InitializeProcessForWsWatch (PSAPI.@)
470  */
471 BOOL WINAPI InitializeProcessForWsWatch(HANDLE hProcess)
472 {
473     FIXME("(hProcess=%p): stub\n", hProcess);
474
475     return TRUE;
476 }
477
478 /***********************************************************************
479  *           QueryWorkingSet (PSAPI.?)
480  * FIXME
481  *     I haven't been able to find the ordinal for this function,
482  *     This means it can't be called from outside the DLL.
483  */
484 BOOL WINAPI QueryWorkingSet(HANDLE hProcess, LPVOID pv, DWORD cb)
485 {
486     FIXME("(hProcess=%p, %p, %ld)\n", hProcess, pv, cb);
487
488     if (pv && cb)
489         ((DWORD *) pv)[0] = 0; /* Empty WorkingSet */
490
491     return TRUE;
492 }