Large-scale renaming of all Win32 functions and types to use the
[wine/hacks.git] / misc / toolhelp.c
bloba04764c0e0e86b4e8ab82a781548dd082e19112e
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 "process.h"
16 #include "tlhelp32.h"
17 #include "toolhelp.h"
18 #include "heap.h"
19 #include "k32obj.h"
20 #include "server.h"
21 #include "debug.h"
24 /* The K32 snapshot object object */
25 typedef struct
27 K32OBJ header;
28 } SNAPSHOT_OBJECT;
30 /* FIXME: to make this working, we have to callback all these registered
31 * functions from all over the WINE code. Someone with more knowledge than
32 * me please do that. -Marcus
34 static struct notify
36 HTASK16 htask;
37 FARPROC16 lpfnCallback;
38 WORD wFlags;
39 } *notifys = NULL;
41 static int nrofnotifys = 0;
43 static FARPROC16 HookNotify = NULL;
45 BOOL16 WINAPI NotifyRegister16( HTASK16 htask, FARPROC16 lpfnCallback,
46 WORD wFlags )
48 int i;
50 TRACE(toolhelp, "(%x,%lx,%x) called.\n",
51 htask, (DWORD)lpfnCallback, wFlags );
52 if (!htask) htask = GetCurrentTask();
53 for (i=0;i<nrofnotifys;i++)
54 if (notifys[i].htask==htask)
55 break;
56 if (i==nrofnotifys) {
57 if (notifys==NULL)
58 notifys=(struct notify*)HeapAlloc( SystemHeap, 0,
59 sizeof(struct notify) );
60 else
61 notifys=(struct notify*)HeapReAlloc( SystemHeap, 0, notifys,
62 sizeof(struct notify)*(nrofnotifys+1));
63 if (!notifys) return FALSE;
64 nrofnotifys++;
66 notifys[i].htask=htask;
67 notifys[i].lpfnCallback=lpfnCallback;
68 notifys[i].wFlags=wFlags;
69 return TRUE;
72 BOOL16 WINAPI NotifyUnregister16( HTASK16 htask )
74 int i;
76 TRACE(toolhelp, "(%x) called.\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( SystemHeap, 0, notifys,
85 (nrofnotifys-1)*sizeof(struct notify));
86 nrofnotifys--;
87 return TRUE;
90 BOOL16 WINAPI StackTraceCSIPFirst16(STACKTRACEENTRY *ste, WORD wSS, WORD wCS, WORD wIP, WORD wBP)
92 return TRUE;
95 BOOL16 WINAPI StackTraceFirst16(STACKTRACEENTRY *ste, HTASK16 Task)
97 return TRUE;
100 BOOL16 WINAPI StackTraceNext16(STACKTRACEENTRY *ste)
102 return TRUE;
105 /***********************************************************************
106 * ToolHelpHook (KERNEL.341)
107 * see "Undocumented Windows"
109 FARPROC16 WINAPI ToolHelpHook16(FARPROC16 lpfnNotifyHandler)
111 FARPROC16 tmp;
112 tmp = HookNotify;
113 HookNotify = lpfnNotifyHandler;
114 /* just return previously installed notification function */
115 return tmp;
119 /***********************************************************************
120 * CreateToolHelp32Snapshot (KERNEL32.179)
122 HANDLE WINAPI CreateToolhelp32Snapshot( DWORD flags, DWORD process )
124 SNAPSHOT_OBJECT *snapshot;
125 struct create_snapshot_request req;
126 struct create_snapshot_reply reply;
128 TRACE( toolhelp, "%lx,%lx\n", flags, process );
129 if (flags & (TH32CS_SNAPHEAPLIST|TH32CS_SNAPMODULE|TH32CS_SNAPTHREAD))
130 FIXME( toolhelp, "flags %lx not implemented\n", flags );
131 if (!(flags & TH32CS_SNAPPROCESS))
133 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
134 return INVALID_HANDLE_VALUE;
136 /* Now do the snapshot */
137 if (!(snapshot = HeapAlloc( SystemHeap, 0, sizeof(*snapshot) )))
138 return INVALID_HANDLE_VALUE;
139 snapshot->header.type = K32OBJ_TOOLHELP_SNAPSHOT;
140 snapshot->header.refcount = 1;
142 req.flags = flags & ~TH32CS_INHERIT;
143 req.inherit = (flags & TH32CS_INHERIT) != 0;
144 CLIENT_SendRequest( REQ_CREATE_SNAPSHOT, -1, 1, &req, sizeof(req) );
145 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
147 HeapFree( SystemHeap, 0, snapshot );
148 return INVALID_HANDLE_VALUE;
150 return HANDLE_Alloc( PROCESS_Current(), &snapshot->header, 0, req.inherit, reply.handle );
154 /***********************************************************************
155 * TOOLHELP_Process32Next
157 * Implementation of Process32First/Next
159 static BOOL TOOLHELP_Process32Next( HANDLE handle, LPPROCESSENTRY lppe, BOOL first )
161 struct next_process_request req;
162 struct next_process_reply reply;
164 if (lppe->dwSize < sizeof (PROCESSENTRY))
166 SetLastError( ERROR_INSUFFICIENT_BUFFER );
167 ERR (toolhelp, "Result buffer too small\n");
168 return FALSE;
170 if ((req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
171 K32OBJ_TOOLHELP_SNAPSHOT, 0 )) == -1)
172 return FALSE;
173 req.reset = first;
174 CLIENT_SendRequest( REQ_NEXT_PROCESS, -1, 1, &req, sizeof(req) );
175 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
176 lppe->cntUsage = 1;
177 lppe->th32ProcessID = (DWORD)reply.pid;
178 lppe->th32DefaultHeapID = 0; /* FIXME */
179 lppe->th32ModuleID = 0; /* FIXME */
180 lppe->cntThreads = reply.threads;
181 lppe->th32ParentProcessID = 0; /* FIXME */
182 lppe->pcPriClassBase = reply.priority;
183 lppe->dwFlags = -1; /* FIXME */
184 lppe->szExeFile[0] = 0; /* FIXME */
185 return TRUE;
189 /***********************************************************************
190 * Process32First (KERNEL32.555)
192 * Return info about the first process in a toolhelp32 snapshot
194 BOOL WINAPI Process32First(HANDLE hSnapshot, LPPROCESSENTRY lppe)
196 return TOOLHELP_Process32Next( hSnapshot, lppe, TRUE );
199 /***********************************************************************
200 * Process32Next (KERNEL32.556)
202 * Return info about the "next" process in a toolhelp32 snapshot
204 BOOL WINAPI Process32Next(HANDLE hSnapshot, LPPROCESSENTRY lppe)
206 return TOOLHELP_Process32Next( hSnapshot, lppe, FALSE );