4 * Copyright 2005 Eric Pouech
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
33 static char selfname[MAX_PATH];
35 /* Some functions are only in later versions of kernel32.dll */
36 static HANDLE (WINAPI *pCreateToolhelp32Snapshot)(DWORD, DWORD);
37 static BOOL (WINAPI *pModule32First)(HANDLE, LPMODULEENTRY32);
38 static BOOL (WINAPI *pModule32Next)(HANDLE, LPMODULEENTRY32);
39 static BOOL (WINAPI *pProcess32First)(HANDLE, LPPROCESSENTRY32);
40 static BOOL (WINAPI *pProcess32Next)(HANDLE, LPPROCESSENTRY32);
41 static BOOL (WINAPI *pThread32First)(HANDLE, LPTHREADENTRY32);
42 static BOOL (WINAPI *pThread32Next)(HANDLE, LPTHREADENTRY32);
44 /* 1 minute should be more than enough */
45 #define WAIT_TIME (60 * 1000)
47 static DWORD WINAPI sub_thread(void* pmt)
49 DWORD w = WaitForSingleObject((HANDLE)pmt, WAIT_TIME);
53 /******************************************************************
56 * generates basic information like:
57 * selfname: the way to reinvoke ourselves
61 * doesn't return if child
67 HANDLE ev1, ev2, ev3, hThread;
70 argc = winetest_get_mainargs( &argv );
71 strcpy(selfname, argv[0]);
75 case 2: /* the test program */
77 case 4: /* the sub-process */
78 ev1 = (HANDLE)atoi(argv[2]);
79 ev2 = (HANDLE)atoi(argv[3]);
80 ev3 = CreateEvent(NULL, FALSE, FALSE, NULL);
82 if (ev3 == NULL) ExitProcess(WAIT_ABANDONED);
83 hThread = CreateThread(NULL, 0, sub_thread, ev3, 0, NULL);
84 if (hThread == NULL) ExitProcess(WAIT_ABANDONED);
85 if (!LoadLibraryA("shell32.dll")) ExitProcess(WAIT_ABANDONED);
87 /* signal init of sub-process is done */
89 /* wait for parent to have done all its queries */
90 w = WaitForSingleObject(ev2, WAIT_TIME);
91 if (w != WAIT_OBJECT_0) ExitProcess(w);
92 /* signal sub-thread to terminate */
94 w = WaitForSingleObject(hThread, WAIT_TIME);
95 if (w != WAIT_OBJECT_0) ExitProcess(w);
96 GetExitCodeThread(hThread, &w);
103 static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
112 hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
113 ok(hSnapshot != NULL, "Cannot create snapshot\n");
115 /* check that this current process is enumerated */
116 pe.dwSize = sizeof(pe);
117 if (pProcess32First( hSnapshot, &pe ))
121 if (pe.th32ProcessID == curr_pid) found++;
122 if (pe.th32ProcessID == sub_pcs_pid) found++;
123 trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
125 } while (pProcess32Next( hSnapshot, &pe ));
127 ok(found == 2, "couldn't find self and/or sub-process in process list\n");
129 /* check that first really resets the enumeration */
131 if (pProcess32First( hSnapshot, &pe ))
135 if (pe.th32ProcessID == curr_pid) found++;
136 if (pe.th32ProcessID == sub_pcs_pid) found++;
137 trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
139 } while (pProcess32Next( hSnapshot, &pe ));
141 ok(found == 2, "couldn't find self and/or sub-process in process list\n");
142 ok(!num, "mismatch in counting\n");
144 te.dwSize = sizeof(te);
145 ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
147 me.dwSize = sizeof(me);
148 ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
150 CloseHandle(hSnapshot);
151 ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
154 static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
161 unsigned curr_found = 0;
162 unsigned sub_found = 0;
164 hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
165 ok(hSnapshot != NULL, "Cannot create snapshot\n");
167 /* check that this current process is enumerated */
168 te.dwSize = sizeof(te);
169 if (pThread32First( hSnapshot, &te ))
173 if (te.th32OwnerProcessID == curr_pid) curr_found++;
174 if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
175 trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
177 } while (pThread32Next( hSnapshot, &te ));
179 ok(curr_found == 1, "couldn't find self in thread list\n");
180 ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
182 /* check that first really resets enumeration */
185 if (pThread32First( hSnapshot, &te ))
189 if (te.th32OwnerProcessID == curr_pid) curr_found++;
190 if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
191 trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
193 } while (pThread32Next( hSnapshot, &te ));
195 ok(curr_found == 1, "couldn't find self in thread list\n");
196 ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
198 pe.dwSize = sizeof(pe);
199 ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
201 me.dwSize = sizeof(me);
202 ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
204 CloseHandle(hSnapshot);
205 ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
208 static const char* curr_expected_modules[] =
212 /* FIXME: could test for ntdll on NT and Wine */
214 static const char* sub_expected_modules[] =
219 /* FIXME: could test for ntdll on NT and Wine */
221 #define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
223 static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
233 ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
235 hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
236 ok(hSnapshot != NULL, "Cannot create snapshot\n");
238 for (i = 0; i < num_expected; i++) found[i] = 0;
239 me.dwSize = sizeof(me);
240 if (pModule32First( hSnapshot, &me ))
244 trace("PID=%x base=%p size=%x %s %s\n",
245 me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
246 ok(me.th32ProcessID == pid, "wrong returned process id\n");
247 for (i = 0; i < num_expected; i++)
248 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
250 } while (pModule32Next( hSnapshot, &me ));
252 for (i = 0; i < num_expected; i++)
253 ok(found[i] == 1, "Module %s is %s\n",
254 expected[i], found[i] ? "listed more than once" : "not listed");
256 /* check that first really resets the enumeration */
257 for (i = 0; i < num_expected; i++) found[i] = 0;
258 me.dwSize = sizeof(me);
259 if (pModule32First( hSnapshot, &me ))
263 trace("PID=%x base=%p size=%x %s %s\n",
264 me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
265 for (i = 0; i < num_expected; i++)
266 if (!lstrcmpi(expected[i], me.szModule)) found[i]++;
268 } while (pModule32Next( hSnapshot, &me ));
270 for (i = 0; i < num_expected; i++)
271 ok(found[i] == 1, "Module %s is %s\n",
272 expected[i], found[i] ? "listed more than once" : "not listed");
273 ok(!num, "mismatch in counting\n");
275 pe.dwSize = sizeof(pe);
276 ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
278 me.dwSize = sizeof(me);
279 ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
281 CloseHandle(hSnapshot);
282 ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
287 DWORD pid = GetCurrentProcessId();
289 char buffer[MAX_PATH];
290 SECURITY_ATTRIBUTES sa;
291 PROCESS_INFORMATION info;
292 STARTUPINFOA startup;
295 HANDLE hkernel32 = GetModuleHandleA("kernel32");
297 pCreateToolhelp32Snapshot = (VOID *) GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
298 pModule32First = (VOID *) GetProcAddress(hkernel32, "Module32First");
299 pModule32Next = (VOID *) GetProcAddress(hkernel32, "Module32Next");
300 pProcess32First = (VOID *) GetProcAddress(hkernel32, "Process32First");
301 pProcess32Next = (VOID *) GetProcAddress(hkernel32, "Process32Next");
302 pThread32First = (VOID *) GetProcAddress(hkernel32, "Thread32First");
303 pThread32Next = (VOID *) GetProcAddress(hkernel32, "Thread32Next");
305 if (!pCreateToolhelp32Snapshot ||
306 !pModule32First || !pModule32Next ||
307 !pProcess32First || !pProcess32Next ||
308 !pThread32First || !pThread32Next)
310 skip("Needed functions are not available, most likely running on Windows NT\n");
315 ok(r == 0, "Basic init of sub-process test\n");
318 sa.nLength = sizeof(sa);
319 sa.lpSecurityDescriptor = NULL;
320 sa.bInheritHandle = TRUE;
322 ev1 = CreateEvent(&sa, FALSE, FALSE, NULL);
323 ev2 = CreateEvent(&sa, FALSE, FALSE, NULL);
324 ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
325 memset(&startup, 0, sizeof(startup));
326 startup.cb = sizeof(startup);
327 startup.dwFlags = STARTF_USESHOWWINDOW;
328 startup.wShowWindow = SW_SHOWNORMAL;
330 sprintf(buffer, "%s tests/toolhelp.c %u %u", selfname, (DWORD)ev1, (DWORD)ev2);
331 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
332 /* wait for child to be initialized */
333 w = WaitForSingleObject(ev1, WAIT_TIME);
334 ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
336 test_process(pid, info.dwProcessId);
337 test_thread(pid, info.dwProcessId);
338 test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
339 test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
342 w = WaitForSingleObject(info.hProcess, WAIT_TIME);
343 ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process termination\n");
344 ok(GetExitCodeProcess(info.hProcess, &w), "couldn't get process exit code\n");
345 ok(w == WAIT_OBJECT_0, "Sub-Process failed to terminate properly\n");