Added a few more Unicode digits from Unicode version 4.1.
[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 "winerror.h"
34 #include "tlhelp32.h"
35 #include "wine/server.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(toolhelp);
40
41
42 /***********************************************************************
43  *           CreateToolhelp32Snapshot                   (KERNEL32.@)
44  */
45 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
46 {
47     HANDLE ret;
48
49     TRACE("%lx,%lx\n", flags, process );
50     if (!(flags & (TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD|TH32CS_SNAPMODULE)))
51     {
52         FIXME("flags %lx not implemented\n", flags );
53         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
54         return INVALID_HANDLE_VALUE;
55     }
56
57     /* Now do the snapshot */
58     SERVER_START_REQ( create_snapshot )
59     {
60         req->flags = 0;
61         if (flags & TH32CS_SNAPMODULE)   req->flags |= SNAP_MODULE;
62         if (flags & TH32CS_SNAPPROCESS)  req->flags |= SNAP_PROCESS;
63         if (flags & TH32CS_SNAPTHREAD)   req->flags |= SNAP_THREAD;
64     
65         req->inherit = (flags & TH32CS_INHERIT) != 0;
66         req->pid     = process;
67         wine_server_call_err( req );
68         ret = reply->handle;
69     }
70     SERVER_END_REQ;
71     if (!ret) ret = INVALID_HANDLE_VALUE;
72     return ret;
73 }
74
75
76 /***********************************************************************
77  *              TOOLHELP_Thread32Next
78  *
79  * Implementation of Thread32First/Next
80  */
81 static BOOL TOOLHELP_Thread32Next( HANDLE handle, LPTHREADENTRY32 lpte, BOOL first )
82 {
83     BOOL ret;
84
85     if (lpte->dwSize < sizeof(THREADENTRY32))
86     {
87         SetLastError( ERROR_INSUFFICIENT_BUFFER );
88         ERR("Result buffer too small (%ld)\n", lpte->dwSize);
89         return FALSE;
90     }
91     SERVER_START_REQ( next_thread )
92     {
93         req->handle = handle;
94         req->reset = first;
95         if ((ret = !wine_server_call_err( req )))
96         {
97             lpte->cntUsage           = reply->count;
98             lpte->th32ThreadID       = (DWORD)reply->tid;
99             lpte->th32OwnerProcessID = (DWORD)reply->pid;
100             lpte->tpBasePri          = reply->base_pri;
101             lpte->tpDeltaPri         = reply->delta_pri;
102             lpte->dwFlags            = 0;  /* SDK: "reserved; do not use" */
103         }
104     }
105     SERVER_END_REQ;
106     return ret;
107 }
108
109 /***********************************************************************
110  *              Thread32First    (KERNEL32.@)
111  *
112  * Return info about the first thread in a toolhelp32 snapshot
113  */
114 BOOL WINAPI Thread32First(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
115 {
116     return TOOLHELP_Thread32Next(hSnapshot, lpte, TRUE);
117 }
118
119 /***********************************************************************
120  *              Thread32Next   (KERNEL32.@)
121  *
122  * Return info about the "next" thread in a toolhelp32 snapshot
123  */
124 BOOL WINAPI Thread32Next(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
125 {
126     return TOOLHELP_Thread32Next(hSnapshot, lpte, FALSE);
127 }
128
129 /***********************************************************************
130  *              TOOLHELP_Process32Next
131  *
132  * Implementation of Process32First/Next. Note that the ANSI / Unicode
133  * version check is a bit of a hack as it relies on the fact that only
134  * the last field is actually different.
135  */
136 static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY32W lppe, BOOL first, BOOL unicode )
137 {
138     BOOL ret;
139     WCHAR exe[MAX_PATH-1];
140     DWORD len, sz;
141
142     sz = unicode ? sizeof(PROCESSENTRY32W) : sizeof(PROCESSENTRY32);
143     if (lppe->dwSize < sz)
144     {
145         SetLastError( ERROR_INSUFFICIENT_BUFFER );
146         ERR("Result buffer too small (req: %ld, was: %ld)\n", sz, lppe->dwSize);
147         return FALSE;
148     }
149
150     SERVER_START_REQ( next_process )
151     {
152         req->handle = handle;
153         req->reset = first;
154         wine_server_set_reply( req, exe, sizeof(exe) );
155         if ((ret = !wine_server_call_err( req )))
156         {
157             lppe->cntUsage            = reply->count;
158             lppe->th32ProcessID       = reply->pid;
159             lppe->th32DefaultHeapID   = (DWORD)reply->heap;
160             lppe->th32ModuleID        = (DWORD)reply->module;
161             lppe->cntThreads          = reply->threads;
162             lppe->th32ParentProcessID = reply->ppid;
163             switch (reply->priority)
164             {
165             case PROCESS_PRIOCLASS_IDLE:
166                 lppe->pcPriClassBase = IDLE_PRIORITY_CLASS; break;
167             case PROCESS_PRIOCLASS_BELOW_NORMAL:
168                 lppe->pcPriClassBase = BELOW_NORMAL_PRIORITY_CLASS; break;
169             case PROCESS_PRIOCLASS_NORMAL:
170                 lppe->pcPriClassBase = NORMAL_PRIORITY_CLASS; break;
171             case PROCESS_PRIOCLASS_ABOVE_NORMAL:
172                 lppe->pcPriClassBase = ABOVE_NORMAL_PRIORITY_CLASS; break;
173             case PROCESS_PRIOCLASS_HIGH:
174                 lppe->pcPriClassBase = HIGH_PRIORITY_CLASS; break;
175             case PROCESS_PRIOCLASS_REALTIME:
176                 lppe->pcPriClassBase = REALTIME_PRIORITY_CLASS; break;
177             default:
178                 FIXME("Unknown NT priority class %d, setting to normal\n", reply->priority);
179                 lppe->pcPriClassBase = NORMAL_PRIORITY_CLASS;
180                 break;
181             }
182             lppe->dwFlags             = -1; /* FIXME */
183             if (unicode)
184             {
185                 len = wine_server_reply_size(reply) / sizeof(WCHAR);
186                 memcpy(lppe->szExeFile, reply, wine_server_reply_size(reply));
187                 lppe->szExeFile[len] = 0;
188             }
189             else
190             {
191                 LPPROCESSENTRY32 lppe_a = (LPPROCESSENTRY32) lppe;
192
193                 len = WideCharToMultiByte( CP_ACP, 0, exe, wine_server_reply_size(reply) / sizeof(WCHAR),
194                                            lppe_a->szExeFile, sizeof(lppe_a->szExeFile)-1, NULL, NULL );
195                 lppe_a->szExeFile[len] = 0;
196             }
197         }
198     }
199     SERVER_END_REQ;
200     return ret;
201 }
202
203
204 /***********************************************************************
205  *              Process32First    (KERNEL32.@)
206  *
207  * Return info about the first process in a toolhelp32 snapshot
208  */
209 BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
210 {
211     return TOOLHELP_Process32Next( hSnapshot, (LPPROCESSENTRY32W) lppe, TRUE, FALSE /* ANSI */ );
212 }
213
214 /***********************************************************************
215  *              Process32Next   (KERNEL32.@)
216  *
217  * Return info about the "next" process in a toolhelp32 snapshot
218  */
219 BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
220 {
221     return TOOLHELP_Process32Next( hSnapshot, (LPPROCESSENTRY32W) lppe, FALSE, FALSE /* ANSI */ );
222 }
223
224 /***********************************************************************
225  *              Process32FirstW    (KERNEL32.@)
226  *
227  * Return info about the first process in a toolhelp32 snapshot
228  */
229 BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
230 {
231     return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE, TRUE /* Unicode */ );
232 }
233
234 /***********************************************************************
235  *              Process32NextW   (KERNEL32.@)
236  *
237  * Return info about the "next" process in a toolhelp32 snapshot
238  */
239 BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
240 {
241     return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE, TRUE /* Unicode */ );
242 }
243
244
245 /***********************************************************************
246  *              TOOLHELP_Module32NextW
247  *
248  * Implementation of Module32First/Next
249  */
250 static BOOL TOOLHELP_Module32NextW( HANDLE handle, LPMODULEENTRY32W lpme, BOOL first )
251 {
252     BOOL ret;
253     WCHAR exe[MAX_PATH];
254     DWORD len;
255
256     if (lpme->dwSize < sizeof (MODULEENTRY32W))
257     {
258         SetLastError( ERROR_INSUFFICIENT_BUFFER );
259         ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(MODULEENTRY32W), lpme->dwSize);
260         return FALSE;
261     }
262     SERVER_START_REQ( next_module )
263     {
264         req->handle = handle;
265         req->reset = first;
266         wine_server_set_reply( req, exe, sizeof(exe) );
267         if ((ret = !wine_server_call_err( req )))
268         {
269             const WCHAR* ptr;
270             lpme->th32ModuleID   = 1; /* toolhelp internal id, never used */
271             lpme->th32ProcessID  = reply->pid;
272             lpme->GlblcntUsage   = 0xFFFF; /* FIXME */
273             lpme->ProccntUsage   = 0xFFFF; /* FIXME */
274             lpme->modBaseAddr    = reply->base;
275             lpme->modBaseSize    = reply->size;
276             lpme->hModule        = reply->base;
277             len = wine_server_reply_size(reply) / sizeof(WCHAR);
278             memcpy(lpme->szExePath, exe, wine_server_reply_size(reply));
279             lpme->szExePath[len] = 0;
280             if ((ptr = strrchrW(lpme->szExePath, '\\'))) ptr++;
281             else ptr = lpme->szExePath;
282             lstrcpynW( lpme->szModule, ptr, sizeof(lpme->szModule)/sizeof(lpme->szModule[0]));
283         }
284     }
285     SERVER_END_REQ;
286     return ret;
287 }
288
289 /***********************************************************************
290  *              TOOLHELP_Module32NextA
291  *
292  * Implementation of Module32First/Next
293  */
294 static BOOL TOOLHELP_Module32NextA( HANDLE handle, LPMODULEENTRY32 lpme, BOOL first )
295 {
296     BOOL ret;
297     MODULEENTRY32W mew;
298     
299     if (lpme->dwSize < sizeof (MODULEENTRY32))
300     {
301         SetLastError( ERROR_INSUFFICIENT_BUFFER );
302         ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(MODULEENTRY32), lpme->dwSize);
303         return FALSE;
304     }
305     
306     mew.dwSize = sizeof(mew);
307     if ((ret = TOOLHELP_Module32NextW( handle, &mew, first )))
308     {
309         lpme->th32ModuleID  = mew.th32ModuleID;
310         lpme->th32ProcessID = mew.th32ProcessID;
311         lpme->GlblcntUsage  = mew.GlblcntUsage;
312         lpme->ProccntUsage  = mew.ProccntUsage;
313         lpme->modBaseAddr   = mew.modBaseAddr;
314         lpme->hModule       = mew.hModule;
315         WideCharToMultiByte( CP_ACP, 0, mew.szModule, -1, lpme->szModule, sizeof(lpme->szModule), NULL, NULL );
316         WideCharToMultiByte( CP_ACP, 0, mew.szExePath, -1, lpme->szExePath, sizeof(lpme->szExePath), NULL, NULL );
317     }
318     return ret;
319 }
320
321 /***********************************************************************
322  *              Module32FirstW   (KERNEL32.@)
323  *
324  * Return info about the "first" module in a toolhelp32 snapshot
325  */
326 BOOL WINAPI Module32FirstW(HANDLE hSnapshot, LPMODULEENTRY32W lpme)
327 {
328     return TOOLHELP_Module32NextW( hSnapshot, lpme, TRUE );
329 }
330
331 /***********************************************************************
332  *              Module32First   (KERNEL32.@)
333  *
334  * Return info about the "first" module in a toolhelp32 snapshot
335  */
336 BOOL WINAPI Module32First(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
337 {
338     return TOOLHELP_Module32NextA( hSnapshot, lpme, TRUE );
339 }
340
341 /***********************************************************************
342  *              Module32NextW   (KERNEL32.@)
343  *
344  * Return info about the "next" module in a toolhelp32 snapshot
345  */
346 BOOL WINAPI Module32NextW(HANDLE hSnapshot, LPMODULEENTRY32W lpme)
347 {
348     return TOOLHELP_Module32NextW( hSnapshot, lpme, FALSE );
349 }
350
351 /***********************************************************************
352  *              Module32Next   (KERNEL32.@)
353  *
354  * Return info about the "next" module in a toolhelp32 snapshot
355  */
356 BOOL WINAPI Module32Next(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
357 {
358     return TOOLHELP_Module32NextA( hSnapshot, lpme, FALSE );
359 }
360
361 /************************************************************************
362  *              Heap32ListFirst (KERNEL32.@)
363  *
364  */
365 BOOL WINAPI Heap32ListFirst(HANDLE hSnapshot, LPHEAPLIST32 lphl)
366 {
367     FIXME(": stub\n");
368     return FALSE;
369 }
370
371 /******************************************************************
372  *              Toolhelp32ReadProcessMemory (KERNEL32.@)
373  *
374  *
375  */
376 BOOL WINAPI Toolhelp32ReadProcessMemory(DWORD pid, const void* base,
377                                         void* buf, SIZE_T len, SIZE_T* r)
378 {
379     HANDLE h;
380     BOOL   ret = FALSE;
381
382     h = (pid) ? OpenProcess(PROCESS_VM_READ, FALSE, pid) : GetCurrentProcess();
383     if (h != NULL)
384     {
385         ret = ReadProcessMemory(h, base, buf, len, r);
386         if (pid) CloseHandle(h);
387     }
388     return ret;
389 }