2 * Misc Toolhelp functions
4 * Copyright 1996 Marcus Meissner
13 #include "wine/winbase16.h"
18 #include "wine/server.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(toolhelp);
24 /* FIXME: to make this work, we have to call back all these registered
25 * functions from all over the WINE code. Someone with more knowledge than
26 * me please do that. -Marcus
31 FARPROC16 lpfnCallback;
35 static int nrofnotifys = 0;
37 static FARPROC16 HookNotify = NULL;
39 /***********************************************************************
40 * NotifyRegister (TOOLHELP.73)
42 BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
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)
55 notifys=(struct notify*)HeapAlloc( GetProcessHeap(), 0,
56 sizeof(struct notify) );
58 notifys=(struct notify*)HeapReAlloc( GetProcessHeap(), 0, notifys,
59 sizeof(struct notify)*(nrofnotifys+1));
60 if (!notifys) return FALSE;
63 notifys[i].htask=htask;
64 notifys[i].lpfnCallback=lpfnCallback;
65 notifys[i].wFlags=wFlags;
69 /***********************************************************************
70 * NotifyUnregister (TOOLHELP.74)
72 BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
76 FIXME("(%x), semi-stub.\n", htask );
77 if (!htask) htask = GetCurrentTask();
78 for (i=nrofnotifys;i--;)
79 if (notifys[i].htask==htask)
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));
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);
99 /***********************************************************************
100 * StackTraceFirst (TOOLHELP.66)
102 BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
104 FIXME("(%p, %04x), stub.\n", ste, Task);
108 /***********************************************************************
109 * StackTraceNext (TOOLHELP.68)
111 BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
113 FIXME("(%p), stub.\n", ste);
117 /***********************************************************************
118 * InterruptRegister (TOOLHELP.75)
120 BOOL16 WINAPI InterruptRegister16( HTASK16 task, FARPROC callback )
122 FIXME("(%04x, %p), stub.\n", task, callback);
126 /***********************************************************************
127 * InterruptUnRegister (TOOLHELP.76)
129 BOOL16 WINAPI InterruptUnRegister16( HTASK16 task )
131 FIXME("(%04x), stub.\n", task);
135 /***********************************************************************
136 * TimerCount (TOOLHELP.80)
138 BOOL16 WINAPI TimerCount16( TIMERINFO *pTimerInfo )
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();
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 );
172 /***********************************************************************
173 * ToolHelpHook (KERNEL.341)
174 * see "Undocumented Windows"
176 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
180 FIXME("(%p), stub.\n", lpfnNotifyHandler);
182 HookNotify = lpfnNotifyHandler;
183 /* just return previously installed notification function */
188 /***********************************************************************
189 * CreateToolhelp32Snapshot (KERNEL32.@)
191 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
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 wine_server_call_err( req );
213 if (!ret) ret = INVALID_HANDLE_VALUE;
218 /***********************************************************************
219 * TOOLHELP_Thread32Next
221 * Implementation of Thread32First/Next
223 static BOOL TOOLHELP_Thread32Next( HANDLE handle, LPTHREADENTRY32 lpte, BOOL first )
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);
233 SERVER_START_REQ( next_thread )
235 req->handle = handle;
237 if ((ret = !wine_server_call_err( req )))
239 lpte->cntUsage = reply->count;
240 lpte->th32ThreadID = (DWORD)reply->tid;
241 lpte->th32OwnerProcessID = (DWORD)reply->pid;
242 lpte->tpBasePri = reply->base_pri;
243 lpte->tpDeltaPri = reply->delta_pri;
244 lpte->dwFlags = 0; /* SDK: "reserved; do not use" */
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 )
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);
286 SERVER_START_REQ( next_process )
288 req->handle = handle;
290 if ((ret = !wine_server_call_err( req )))
292 lppe->cntUsage = reply->count;
293 lppe->th32ProcessID = (DWORD)reply->pid;
294 lppe->th32DefaultHeapID = 0; /* FIXME */
295 lppe->th32ModuleID = 0; /* FIXME */
296 lppe->cntThreads = reply->threads;
297 lppe->th32ParentProcessID = 0; /* FIXME */
298 lppe->pcPriClassBase = reply->priority;
299 lppe->dwFlags = -1; /* FIXME */
300 lppe->szExeFile[0] = 0; /* FIXME */
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 )
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);
344 SERVER_START_REQ( next_module )
346 req->handle = handle;
348 if ((ret = !wine_server_call_err( req )))
350 lpme->th32ModuleID = 0; /* toolhelp internal id, never used */
351 lpme->th32ProcessID = (DWORD)reply->pid;
352 lpme->GlblcntUsage = 0; /* FIXME */
353 lpme->ProccntUsage = 0; /* FIXME */
354 lpme->modBaseAddr = reply->base;
355 lpme->modBaseSize = 0; /* FIXME */
356 lpme->hModule = (DWORD)reply->base;
357 lpme->szModule[0] = 0; /* FIXME */
358 lpme->szExePath[0] = 0; /* FIXME */
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)