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