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