- Implement:
[wine.git] / dlls / kernel / toolhelp.c
blobb9a31fa2388581b24d0d0d0225e48e78c72dc25a
1 /*
2 * Misc Toolhelp functions
4 * Copyright 1996 Marcus Meissner
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <assert.h>
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "winerror.h"
15 #include "local.h"
16 #include "tlhelp32.h"
17 #include "toolhelp.h"
18 #include "wine/server.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(toolhelp);
24 /* FIXME: to make this working, we have to callback all these registered
25 * functions from all over the WINE code. Someone with more knowledge than
26 * me please do that. -Marcus
28 static struct notify
30 HTASK16 htask;
31 FARPROC16 lpfnCallback;
32 WORD wFlags;
33 } *notifys = NULL;
35 static int nrofnotifys = 0;
37 static FARPROC16 HookNotify = NULL;
39 /***********************************************************************
40 * NotifyRegister (TOOLHELP.73)
42 BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
43 WORD wFlags )
45 int i;
47 FIXME("(%x,%lx,%x), semi-stub.\n",
48 htask, (DWORD)lpfnCallback, wFlags );
49 if (!htask) htask = GetCurrentTask();
50 for (i=0;i<nrofnotifys;i++)
51 if (notifys[i].htask==htask)
52 break;
53 if (i==nrofnotifys) {
54 if (notifys==NULL)
55 notifys=(struct notify*)HeapAlloc( GetProcessHeap(), 0,
56 sizeof(struct notify) );
57 else
58 notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
59 sizeof(struct notify)*(nrofnotifys+1));
60 if (!notifys) return FALSE;
61 nrofnotifys++;
63 notifys[i].htask=htask;
64 notifys[i].lpfnCallback=lpfnCallback;
65 notifys[i].wFlags=wFlags;
66 return TRUE;
69 /***********************************************************************
70 * NotifyUnregister (TOOLHELP.74)
72 BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
74 int i;
76 FIXME("(%x), semi-stub.\n", htask );
77 if (!htask) htask = GetCurrentTask();
78 for (i=nrofnotifys;i--;)
79 if (notifys[i].htask==htask)
80 break;
81 if (i==-1)
82 return FALSE;
83 memcpy(notifys+i,notifys+(i+1),sizeof(struct notify)*(nrofnotifys-i-1));
84 notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
85 (nrofnotifys-1)*sizeof(struct notify));
86 nrofnotifys--;
87 return TRUE;
90 /***********************************************************************
91 * StackTraceCSIPFirst (TOOLHELP.67)
93 BOOL16 WINAPI StackTraceCSIPFirst16(STACKTRACEENTRY *ste, WORD wSS, WORD wCS, WORD wIP, WORD wBP)
95 FIXME("(%p, ss %04x, cs %04x, ip %04x, bp %04x): stub.\n", ste, wSS, wCS, wIP, wBP);
96 return TRUE;
99 /***********************************************************************
100 * StackTraceFirst (TOOLHELP.66)
102 BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
104 FIXME("(%p, %04x), stub.\n", ste, Task);
105 return TRUE;
108 /***********************************************************************
109 * StackTraceNext (TOOLHELP.68)
111 BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
113 FIXME("(%p), stub.\n", ste);
114 return TRUE;
117 /***********************************************************************
118 * InterruptRegister (TOOLHELP.75)
120 BOOL16 WINAPI InterruptRegister16( HTASK16 task, FARPROC callback )
122 FIXME("(%04x, %p), stub.\n", task, callback);
123 return TRUE;
126 /***********************************************************************
127 * InterruptUnRegister (TOOLHELP.76)
129 BOOL16 WINAPI InterruptUnRegister16( HTASK16 task )
131 FIXME("(%04x), stub.\n", task);
132 return TRUE;
135 /***********************************************************************
136 * TimerCount (TOOLHELP.80)
138 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
140 /* FIXME
141 * In standard mode, dwmsSinceStart = dwmsThisVM
143 * I tested this, under Windows in enhanced mode, and
144 * if you never switch VM (ie start/stop DOS) these
145 * values should be the same as well.
147 * Also, Wine should adjust for the hardware timer
148 * to reduce the amount of error to ~1ms.
149 * I can't be bothered, can you?
151 pTimerInfo->dwmsSinceStart = pTimerInfo->dwmsThisVM = GetTickCount();
152 return TRUE;
155 /***********************************************************************
156 * SystemHeapInfo (TOOLHELP.71)
158 BOOL16 WINAPI SystemHeapInfo16( SYSHEAPINFO *pHeapInfo )
160 WORD user = LoadLibrary16( "USER.EXE" );
161 WORD gdi = LoadLibrary16( "GDI.EXE" );
162 pHeapInfo->wUserFreePercent = (int)LOCAL_CountFree(user) * 100 / LOCAL_HeapSize(user);
163 pHeapInfo->wGDIFreePercent = (int)LOCAL_CountFree(gdi) * 100 / LOCAL_HeapSize(gdi);
164 pHeapInfo->hUserSegment = user;
165 pHeapInfo->hGDISegment = gdi;
166 FreeLibrary16( user );
167 FreeLibrary16( gdi );
168 return TRUE;
172 /***********************************************************************
173 * ToolHelpHook (KERNEL.341)
174 * see "Undocumented Windows"
176 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
178 FARPROC16 tmp;
180 FIXME("(%p), stub.\n", lpfnNotifyHandler);
181 tmp = HookNotify;
182 HookNotify = lpfnNotifyHandler;
183 /* just return previously installed notification function */
184 return tmp;
188 /***********************************************************************
189 * CreateToolhelp32Snapshot (KERNEL32.@)
191 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
193 HANDLE ret;
195 TRACE("%lx,%lx\n", flags, process );
196 if (!(flags & (TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD|TH32CS_SNAPMODULE)))
198 FIXME("flags %lx not implemented\n", flags );
199 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
200 return INVALID_HANDLE_VALUE;
203 /* Now do the snapshot */
204 SERVER_START_REQ( create_snapshot )
206 req->flags = flags & ~TH32CS_INHERIT;
207 req->inherit = (flags & TH32CS_INHERIT) != 0;
208 req->pid = (void *)process;
209 SERVER_CALL_ERR();
210 ret = req->handle;
212 SERVER_END_REQ;
213 if (!ret) ret = INVALID_HANDLE_VALUE;
214 return ret;
218 /***********************************************************************
219 * TOOLHELP_Thread32Next
221 * Implementation of Thread32First/Next
223 static BOOL TOOLHELP_Thread32Next( HANDLE handle, LPTHREADENTRY32 lpte, BOOL first )
225 BOOL ret;
227 if (lpte->dwSize < sizeof(THREADENTRY32))
229 SetLastError( ERROR_INSUFFICIENT_BUFFER );
230 ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(THREADENTRY32), lpte->dwSize);
231 return FALSE;
233 SERVER_START_REQ( next_thread )
235 req->handle = handle;
236 req->reset = first;
237 if ((ret = !SERVER_CALL_ERR()))
239 lpte->cntUsage = req->count;
240 lpte->th32ThreadID = (DWORD)req->tid;
241 lpte->th32OwnerProcessID = (DWORD)req->pid;
242 lpte->tpBasePri = req->base_pri;
243 lpte->tpDeltaPri = req->delta_pri;
244 lpte->dwFlags = 0; /* SDK: "reserved; do not use" */
247 SERVER_END_REQ;
248 return ret;
251 /***********************************************************************
252 * Thread32First (KERNEL32.@)
254 * Return info about the first thread in a toolhelp32 snapshot
256 BOOL WINAPI Thread32First(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
258 return TOOLHELP_Thread32Next(hSnapshot, lpte, TRUE);
261 /***********************************************************************
262 * Thread32Next (KERNEL32.@)
264 * Return info about the "next" thread in a toolhelp32 snapshot
266 BOOL WINAPI Thread32Next(HANDLE hSnapshot, LPTHREADENTRY32 lpte)
268 return TOOLHELP_Thread32Next(hSnapshot, lpte, FALSE);
271 /***********************************************************************
272 * TOOLHELP_Process32Next
274 * Implementation of Process32First/Next
276 static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY32 lppe, BOOL first )
278 BOOL ret;
280 if (lppe->dwSize < sizeof(PROCESSENTRY32))
282 SetLastError( ERROR_INSUFFICIENT_BUFFER );
283 ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(PROCESSENTRY32), lppe->dwSize);
284 return FALSE;
286 SERVER_START_REQ( next_process )
288 req->handle = handle;
289 req->reset = first;
290 if ((ret = !SERVER_CALL_ERR()))
292 lppe->cntUsage = req->count;
293 lppe->th32ProcessID = (DWORD)req->pid;
294 lppe->th32DefaultHeapID = 0; /* FIXME */
295 lppe->th32ModuleID = 0; /* FIXME */
296 lppe->cntThreads = req->threads;
297 lppe->th32ParentProcessID = 0; /* FIXME */
298 lppe->pcPriClassBase = req->priority;
299 lppe->dwFlags = -1; /* FIXME */
300 lppe->szExeFile[0] = 0; /* FIXME */
303 SERVER_END_REQ;
304 return ret;
308 /***********************************************************************
309 * Process32First (KERNEL32.@)
311 * Return info about the first process in a toolhelp32 snapshot
313 BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
315 return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE );
318 /***********************************************************************
319 * Process32Next (KERNEL32.@)
321 * Return info about the "next" process in a toolhelp32 snapshot
323 BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY32 lppe)
325 return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE );
329 /***********************************************************************
330 * TOOLHELP_Module32Next
332 * Implementation of Module32First/Next
334 static BOOL TOOLHELP_Module32Next( HANDLE handle, LPMODULEENTRY32 lpme, BOOL first )
336 BOOL ret;
338 if (lpme->dwSize < sizeof (MODULEENTRY32))
340 SetLastError( ERROR_INSUFFICIENT_BUFFER );
341 ERR("Result buffer too small (req: %d, was: %ld)\n", sizeof(MODULEENTRY32), lpme->dwSize);
342 return FALSE;
344 SERVER_START_REQ( next_module )
346 req->handle = handle;
347 req->reset = first;
348 if ((ret = !SERVER_CALL_ERR()))
350 lpme->th32ModuleID = 0; /* toolhelp internal id, never used */
351 lpme->th32ProcessID = (DWORD)req->pid;
352 lpme->GlblcntUsage = 0; /* FIXME */
353 lpme->ProccntUsage = 0; /* FIXME */
354 lpme->modBaseAddr = req->base;
355 lpme->modBaseSize = 0; /* FIXME */
356 lpme->hModule = (DWORD)req->base;
357 lpme->szModule[0] = 0; /* FIXME */
358 lpme->szExePath[0] = 0; /* FIXME */
361 SERVER_END_REQ;
362 return ret;
365 /***********************************************************************
366 * Module32First (KERNEL32.@)
368 * Return info about the "first" module in a toolhelp32 snapshot
370 BOOL WINAPI Module32First(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
372 return TOOLHELP_Module32Next( hSnapshot, lpme, TRUE );
375 /***********************************************************************
376 * Module32Next (KERNEL32.@)
378 * Return info about the "next" module in a toolhelp32 snapshot
380 BOOL WINAPI Module32Next(HANDLE hSnapshot, LPMODULEENTRY32 lpme)
382 return TOOLHELP_Module32Next( hSnapshot, lpme, FALSE );
385 /************************************************************************
386 * GlobalMasterHandle (KERNEL.28)
389 * Should return selector and handle of the information structure for
390 * the global heap. selector and handle are stored in the THHOOK as
391 * pGlobalHeap and hGlobalHeap.
392 * As Wine doesn't have this structure, we return both values as zero
393 * Applications should interpret this as "No Global Heap"
395 DWORD WINAPI GlobalMasterHandle16(void)
397 FIXME(": stub\n");
398 return 0;