CopyFile needs to retain timestamp.
[wine] / dlls / kernel / toolhelp.c
1 /*
2  * Misc Toolhelp functions
3  *
4  * Copyright 1996 Marcus Meissner
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <ctype.h>
30 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/winbase16.h"
34 #include "winerror.h"
35 #include "local.h"
36 #include "tlhelp32.h"
37 #include "toolhelp.h"
38 #include "wine/server.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(toolhelp);
42
43
44 /* FIXME: to make this work, we have to call back all these registered
45  * functions from all over the WINE code. Someone with more knowledge than
46  * me please do that. -Marcus
47  */
48 static struct notify
49 {
50     HTASK16   htask;
51     FARPROC16 lpfnCallback;
52     WORD     wFlags;
53 } *notifys = NULL;
54
55 static int nrofnotifys = 0;
56
57 static FARPROC16 HookNotify = NULL;
58
59 /***********************************************************************
60  *              NotifyRegister (TOOLHELP.73)
61  */
62 BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
63                               WORD wFlags )
64 {
65     int i;
66
67     FIXME("(%x,%lx,%x), semi-stub.\n",
68                       htask, (DWORD)lpfnCallback, wFlags );
69     if (!htask) htask = GetCurrentTask();
70     for (i=0;i<nrofnotifys;i++)
71         if (notifys[i].htask==htask)
72             break;
73     if (i==nrofnotifys) {
74         if (notifys==NULL)
75             notifys=(struct notify*)HeapAlloc( GetProcessHeap(), 0,
76                                                sizeof(struct notify) );
77         else
78             notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
79                                         sizeof(struct notify)*(nrofnotifys+1));
80         if (!notifys) return FALSE;
81         nrofnotifys++;
82     }
83     notifys[i].htask=htask;
84     notifys[i].lpfnCallback=lpfnCallback;
85     notifys[i].wFlags=wFlags;
86     return TRUE;
87 }
88
89 /***********************************************************************
90  *              NotifyUnregister (TOOLHELP.74)
91  */
92 BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
93 {
94     int i;
95
96     FIXME("(%x), semi-stub.\n", htask );
97     if (!htask) htask = GetCurrentTask();
98     for (i=nrofnotifys;i--;)
99         if (notifys[i].htask==htask)
100             break;
101     if (i==-1)
102         return FALSE;
103     memcpy(notifys+i,notifys+(i+1),sizeof(struct notify)*(nrofnotifys-i-1));
104     notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
105                                         (nrofnotifys-1)*sizeof(struct notify));
106     nrofnotifys--;
107     return TRUE;
108 }
109
110 /***********************************************************************
111  *              StackTraceCSIPFirst (TOOLHELP.67)
112  */
113 BOOL16 WINAPI StackTraceCSIPFirst16(STACKTRACEENTRY *ste, WORD wSS, WORD wCS, WORD wIP, WORD wBP)
114 {
115     FIXME("(%p, ss %04x, cs %04x, ip %04x, bp %04x): stub.\n", ste, wSS, wCS, wIP, wBP);
116     return TRUE;
117 }
118
119 /***********************************************************************
120  *              StackTraceFirst (TOOLHELP.66)
121  */
122 BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
123 {
124     FIXME("(%p, %04x), stub.\n", ste, Task);
125     return TRUE;
126 }
127
128 /***********************************************************************
129  *              StackTraceNext (TOOLHELP.68)
130  */
131 BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
132 {
133     FIXME("(%p), stub.\n", ste);
134     return TRUE;
135 }
136
137 /***********************************************************************
138  *              InterruptRegister (TOOLHELP.75)
139  */
140 BOOL16 WINAPI InterruptRegister16( HTASK16 task, FARPROC callback )
141 {
142     FIXME("(%04x, %p), stub.\n", task, callback);
143     return TRUE;
144 }
145
146 /***********************************************************************
147  *              InterruptUnRegister (TOOLHELP.76)
148  */
149 BOOL16 WINAPI InterruptUnRegister16( HTASK16 task )
150 {
151     FIXME("(%04x), stub.\n", task);
152     return TRUE;
153 }
154
155 /***********************************************************************
156  *           TimerCount   (TOOLHELP.80)
157  */
158 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
159 {
160     /* FIXME
161      * In standard mode, dwmsSinceStart = dwmsThisVM
162      *
163      * I tested this, under Windows in enhanced mode, and
164      * if you never switch VM (ie start/stop DOS) these
165      * values should be the same as well.
166      *
167      * Also, Wine should adjust for the hardware timer
168      * to reduce the amount of error to ~1ms.
169      * I can't be bothered, can you?
170      */
171     pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
172     return TRUE;
173 }
174
175 /***********************************************************************
176  *           SystemHeapInfo   (TOOLHELP.71)
177  */
178 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
179 {
180     WORD user = LoadLibrary16( "USER.EXE" );
181     WORD gdi = LoadLibrary16( "GDI.EXE" );
182     pHeapInfo->wUserFreePercent = (int)LOCAL_CountFree(user) * 100 / LOCAL_HeapSize(user);
183     pHeapInfo->wGDIFreePercent  = (int)LOCAL_CountFree(gdi) * 100 / LOCAL_HeapSize(gdi);
184     pHeapInfo->hUserSegment = user;
185     pHeapInfo->hGDISegment  = gdi;
186     FreeLibrary16( user );
187     FreeLibrary16( gdi );
188     return TRUE;
189 }
190
191
192 /***********************************************************************
193  *           ToolHelpHook                             (KERNEL.341)
194  *      see "Undocumented Windows"
195  */
196 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
197 {
198         FARPROC16 tmp;
199
200         FIXME("(%p), stub.\n", lpfnNotifyHandler);
201         tmp = HookNotify;
202         HookNotify = lpfnNotifyHandler;
203         /* just return previously installed notification function */
204         return tmp;
205 }
206
207
208 /***********************************************************************
209  *           CreateToolhelp32Snapshot                   (KERNEL32.@)
210  */
211 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
212 {
213     HANDLE ret;
214
215     TRACE("%lx,%lx\n", flags, process );
216     if (!(flags & (TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD|TH32CS_SNAPMODULE)))
217     {
218         FIXME("flags %lx not implemented\n", flags );
219         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
220         return INVALID_HANDLE_VALUE;
221     }
222
223     /* Now do the snapshot */
224     SERVER_START_REQ( create_snapshot )
225     {
226         req->flags = 0;
227         if (flags & TH32CS_SNAPMODULE)   req->flags |= SNAP_MODULE;
228         if (flags & TH32CS_SNAPPROCESS)  req->flags |= SNAP_PROCESS;
229         if (flags & TH32CS_SNAPTHREAD)   req->flags |= SNAP_THREAD;
230     
231         req->inherit = (flags & TH32CS_INHERIT) != 0;
232         req->pid     = process;
233         wine_server_call_err( req );
234         ret = reply->handle;
235     }
236     SERVER_END_REQ;
237     if (!ret) ret = INVALID_HANDLE_VALUE;
238     return ret;
239 }
240
241
242 /***********************************************************************
243  *              TOOLHELP_Thread32Next
244  *
245  * Implementation of Thread32First/Next
246  */
247 static BOOL TOOLHELP_Thread32Next( HANDLE handle, LPTHREADENTRY32 lpte, BOOL first )
248 {
249     BOOL ret;
250
251     if (lpte->dwSize < sizeof(THREADENTRY32))
252     {
253         SetLastError( ERROR_INSUFFICIENT_BUFFER );
254         ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(THREADENTRY32), lpte->dwSize);
255         return FALSE;
256     }
257     SERVER_START_REQ( next_thread )
258     {
259         req->handle = handle;
260         req->reset = first;
261         if ((ret = !wine_server_call_err( req )))
262         {
263             lpte->cntUsage           = reply->count;
264             lpte->th32ThreadID       = (DWORD)reply->tid;
265             lpte->th32OwnerProcessID = (DWORD)reply->pid;
266             lpte->tpBasePri          = reply->base_pri;
267             lpte->tpDeltaPri         = reply->delta_pri;
268             lpte->dwFlags            = 0;  /* SDK: "reserved; do not use" */
269         }
270     }
271     SERVER_END_REQ;
272     return ret;
273 }
274
275 /***********************************************************************
276  *              Thread32First    (KERNEL32.@)
277  *
278  * Return info about the first thread in a toolhelp32 snapshot
279  */
280 BOOL WINAPI Thread32First(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
281 {
282     return TOOLHELP_Thread32Next(hSnapshot, lpte, TRUE);
283 }
284
285 /***********************************************************************
286  *              Thread32Next   (KERNEL32.@)
287  *
288  * Return info about the "next" thread in a toolhelp32 snapshot
289  */
290 BOOL WINAPI Thread32Next(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
291 {
292     return TOOLHELP_Thread32Next(hSnapshot, lpte, FALSE);
293 }
294
295 /***********************************************************************
296  *              TOOLHELP_Process32Next
297  *
298  * Implementation of Process32First/Next. Note that the ANSI / Unicode
299  * version check is a bit of a hack as it relies on the fact that only
300  * the last field is actually different.
301  */
302 static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY32W lppe, BOOL first, BOOL unicode )
303 {
304     BOOL ret;
305     WCHAR exe[MAX_PATH-1];
306     DWORD len;
307
308     if (unicode)
309     {
310         if (lppe->dwSize < sizeof(PROCESSENTRY32W))
311         {
312             SetLastError( ERROR_INSUFFICIENT_BUFFER );
313             ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(PROCESSENTRY32W), lppe->dwSize);
314             return FALSE;
315         }
316     }
317     else
318     {
319         if (lppe->dwSize < sizeof(PROCESSENTRY32))
320         {
321             SetLastError( ERROR_INSUFFICIENT_BUFFER );
322             ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(PROCESSENTRY32), lppe->dwSize);
323             return FALSE;
324         }
325     }
326     SERVER_START_REQ( next_process )
327     {
328         req->handle = handle;
329         req->reset = first;
330         wine_server_set_reply( req, exe, sizeof(exe) );
331         if ((ret = !wine_server_call_err( req )))
332         {
333             lppe->cntUsage            = reply->count;
334             lppe->th32ProcessID       = reply->pid;
335             lppe->th32DefaultHeapID   = (DWORD)reply->heap;
336             lppe->th32ModuleID        = (DWORD)reply->module;
337             lppe->cntThreads          = reply->threads;
338             lppe->th32ParentProcessID = reply->ppid;
339             lppe->pcPriClassBase      = reply->priority;
340             lppe->dwFlags             = -1; /* FIXME */
341             if (unicode)
342             {
343                 len = wine_server_reply_size(reply) / sizeof(WCHAR);
344                 memcpy(lppe->szExeFile, reply, wine_server_reply_size(reply));
345                 lppe->szExeFile[len] = 0;
346             }
347             else
348             {
349                 LPPROCESSENTRY32 lppe_a = (LPPROCESSENTRY32) lppe;
350
351                 len = WideCharToMultiByte( CP_ACP, 0, exe, wine_server_reply_size(reply) / sizeof(WCHAR),
352                                            lppe_a->szExeFile, sizeof(lppe_a->szExeFile)-1, NULL, NULL );
353                 lppe_a->szExeFile[len] = 0;
354             }
355         }
356     }
357     SERVER_END_REQ;
358     return ret;
359 }
360
361
362 /***********************************************************************
363  *              Process32First    (KERNEL32.@)
364  *
365  * Return info about the first process in a toolhelp32 snapshot
366  */
367 BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
368 {
369     return TOOLHELP_Process32Next( hSnapshot, (LPPROCESSENTRY32W) lppe, TRUE, FALSE /* ANSI */ );
370 }
371
372 /***********************************************************************
373  *              Process32Next   (KERNEL32.@)
374  *
375  * Return info about the "next" process in a toolhelp32 snapshot
376  */
377 BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
378 {
379     return TOOLHELP_Process32Next( hSnapshot, (LPPROCESSENTRY32W) lppe, FALSE, FALSE /* ANSI */ );
380 }
381
382 /***********************************************************************
383  *              Process32FirstW    (KERNEL32.@)
384  *
385  * Return info about the first process in a toolhelp32 snapshot
386  */
387 BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
388 {
389     return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE, TRUE /* Unicode */ );
390 }
391
392 /***********************************************************************
393  *              Process32NextW   (KERNEL32.@)
394  *
395  * Return info about the "next" process in a toolhelp32 snapshot
396  */
397 BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
398 {
399     return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE, TRUE /* Unicode */ );
400 }
401
402
403 /***********************************************************************
404  *              TOOLHELP_Module32Next
405  *
406  * Implementation of Module32First/Next
407  */
408 static BOOL TOOLHELP_Module32Next( HANDLE handle, LPMODULEENTRY32 lpme, BOOL first )
409 {
410     BOOL ret;
411     WCHAR exe[MAX_PATH];
412     DWORD len;
413
414     if (lpme->dwSize < sizeof (MODULEENTRY32))
415     {
416         SetLastError( ERROR_INSUFFICIENT_BUFFER );
417         ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(MODULEENTRY32), lpme->dwSize);
418         return FALSE;
419     }
420     SERVER_START_REQ( next_module )
421     {
422         req->handle = handle;
423         req->reset = first;
424         wine_server_set_reply( req, exe, sizeof(exe) );
425         if ((ret = !wine_server_call_err( req )))
426         {
427             const char* ptr;
428             lpme->th32ModuleID   = 1; /* toolhelp internal id, never used */
429             lpme->th32ProcessID  = reply->pid;
430             lpme->GlblcntUsage   = 0xFFFF; /* FIXME */
431             lpme->ProccntUsage   = 0xFFFF; /* FIXME */
432             lpme->modBaseAddr    = reply->base;
433             lpme->modBaseSize    = reply->size;
434             lpme->hModule        = reply->base;
435             len = WideCharToMultiByte( CP_ACP, 0, exe, wine_server_reply_size(reply) / sizeof(WCHAR),
436                                        lpme->szExePath, sizeof(lpme->szExePath) - 1, NULL, NULL );
437             lpme->szExePath[len] = 0;
438             if ((ptr = strrchr(lpme->szExePath, '\\'))) ptr++;
439             else ptr = lpme->szExePath;
440             lstrcpynA( lpme->szModule, ptr, sizeof(lpme->szModule) );
441         }
442     }
443     SERVER_END_REQ;
444     return ret;
445 }
446
447 /***********************************************************************
448  *              Module32First   (KERNEL32.@)
449  *
450  * Return info about the "first" module in a toolhelp32 snapshot
451  */
452 BOOL WINAPI Module32First(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
453 {
454     return TOOLHELP_Module32Next( hSnapshot, lpme, TRUE );
455 }
456
457 /***********************************************************************
458  *              Module32Next   (KERNEL32.@)
459  *
460  * Return info about the "next" module in a toolhelp32 snapshot
461  */
462 BOOL WINAPI Module32Next(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
463 {
464     return TOOLHELP_Module32Next( hSnapshot, lpme, FALSE );
465 }
466
467 /************************************************************************
468  *              GlobalMasterHandle (KERNEL.28)
469  *
470  *
471  * Should return selector and handle of the information structure for
472  * the global heap. selector and handle are stored in the THHOOK as
473  * pGlobalHeap and hGlobalHeap.
474  * As Wine doesn't have this structure, we return both values as zero
475  * Applications should interpret this as "No Global Heap"
476  */
477 DWORD WINAPI GlobalMasterHandle16(void)
478 {
479     FIXME(": stub\n");
480     return 0;
481 }
482
483 /************************************************************************
484  *              Heap32ListFirst (KERNEL.@)
485  *
486  */
487 BOOL WINAPI Heap32ListFirst(HANDLE hSnapshot, LPHEAPLIST32 lphl)
488 {
489     FIXME(": stub\n");
490     return FALSE;
491 }
492
493 /******************************************************************
494  *              Toolhelp32ReadProcessMemory (KERNEL.@)
495  *
496  *
497  */
498 BOOL WINAPI Toolhelp32ReadProcessMemory(DWORD pid, const void* base,
499                                         void* buf, SIZE_T len, SIZE_T* r)
500 {
501     HANDLE h;
502     BOOL   ret = FALSE;
503
504     h = (pid) ? OpenProcess(PROCESS_VM_READ, FALSE, pid) : GetCurrentProcess();
505     if (h != NULL)
506     {
507         ret = ReadProcessMemory(h, base, buf, len, r);
508         if (pid) CloseHandle(h);
509     }
510     return ret;
511 }