2006-04-08 11:22:38 +02:00
|
|
|
/*
|
|
|
|
* Toolhelp
|
|
|
|
*
|
|
|
|
* Copyright 2005 Eric Pouech
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-04-08 11:22:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2007-11-19 01:42:43 +01:00
|
|
|
#include "tlhelp32.h"
|
|
|
|
#include "wine/test.h"
|
2006-04-08 11:22:38 +02:00
|
|
|
#include "winuser.h"
|
|
|
|
|
|
|
|
static char selfname[MAX_PATH];
|
|
|
|
|
2006-04-14 09:17:06 +02:00
|
|
|
/* Some functions are only in later versions of kernel32.dll */
|
|
|
|
static HANDLE (WINAPI *pCreateToolhelp32Snapshot)(DWORD, DWORD);
|
|
|
|
static BOOL (WINAPI *pModule32First)(HANDLE, LPMODULEENTRY32);
|
|
|
|
static BOOL (WINAPI *pModule32Next)(HANDLE, LPMODULEENTRY32);
|
|
|
|
static BOOL (WINAPI *pProcess32First)(HANDLE, LPPROCESSENTRY32);
|
|
|
|
static BOOL (WINAPI *pProcess32Next)(HANDLE, LPPROCESSENTRY32);
|
|
|
|
static BOOL (WINAPI *pThread32First)(HANDLE, LPTHREADENTRY32);
|
|
|
|
static BOOL (WINAPI *pThread32Next)(HANDLE, LPTHREADENTRY32);
|
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
/* 1 minute should be more than enough */
|
|
|
|
#define WAIT_TIME (60 * 1000)
|
|
|
|
|
|
|
|
static DWORD WINAPI sub_thread(void* pmt)
|
|
|
|
{
|
2009-03-11 09:39:16 +01:00
|
|
|
DWORD w = WaitForSingleObject(pmt, WAIT_TIME);
|
2006-04-08 11:22:38 +02:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* init
|
|
|
|
*
|
|
|
|
* generates basic information like:
|
|
|
|
* selfname: the way to reinvoke ourselves
|
|
|
|
* returns:
|
|
|
|
* -1 on error
|
|
|
|
* 0 if parent
|
|
|
|
* doesn't return if child
|
|
|
|
*/
|
|
|
|
static int init(void)
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char** argv;
|
|
|
|
HANDLE ev1, ev2, ev3, hThread;
|
2008-05-01 10:56:09 +02:00
|
|
|
DWORD w, tid;
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
argc = winetest_get_mainargs( &argv );
|
|
|
|
strcpy(selfname, argv[0]);
|
|
|
|
|
|
|
|
switch (argc)
|
|
|
|
{
|
|
|
|
case 2: /* the test program */
|
|
|
|
return 0;
|
|
|
|
case 4: /* the sub-process */
|
2009-01-03 20:17:31 +01:00
|
|
|
ev1 = (HANDLE)(INT_PTR)atoi(argv[2]);
|
|
|
|
ev2 = (HANDLE)(INT_PTR)atoi(argv[3]);
|
2013-10-21 10:02:53 +02:00
|
|
|
ev3 = CreateEventW(NULL, FALSE, FALSE, NULL);
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
if (ev3 == NULL) ExitProcess(WAIT_ABANDONED);
|
2008-05-01 10:56:09 +02:00
|
|
|
hThread = CreateThread(NULL, 0, sub_thread, ev3, 0, &tid);
|
2006-04-08 11:22:38 +02:00
|
|
|
if (hThread == NULL) ExitProcess(WAIT_ABANDONED);
|
|
|
|
if (!LoadLibraryA("shell32.dll")) ExitProcess(WAIT_ABANDONED);
|
2013-10-21 10:02:53 +02:00
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
/* signal init of sub-process is done */
|
|
|
|
SetEvent(ev1);
|
|
|
|
/* wait for parent to have done all its queries */
|
|
|
|
w = WaitForSingleObject(ev2, WAIT_TIME);
|
|
|
|
if (w != WAIT_OBJECT_0) ExitProcess(w);
|
|
|
|
/* signal sub-thread to terminate */
|
|
|
|
SetEvent(ev3);
|
|
|
|
w = WaitForSingleObject(hThread, WAIT_TIME);
|
|
|
|
if (w != WAIT_OBJECT_0) ExitProcess(w);
|
|
|
|
GetExitCodeThread(hThread, &w);
|
|
|
|
ExitProcess(w);
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
|
|
|
|
{
|
|
|
|
HANDLE hSnapshot;
|
|
|
|
PROCESSENTRY32 pe;
|
|
|
|
MODULEENTRY32 me;
|
|
|
|
unsigned found = 0;
|
|
|
|
int num = 0;
|
2008-01-13 21:55:29 +01:00
|
|
|
int childpos = -1;
|
2006-04-08 11:22:38 +02:00
|
|
|
|
2006-04-14 09:17:06 +02:00
|
|
|
hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
|
2006-04-08 11:22:38 +02:00
|
|
|
ok(hSnapshot != NULL, "Cannot create snapshot\n");
|
|
|
|
|
|
|
|
/* check that this current process is enumerated */
|
|
|
|
pe.dwSize = sizeof(pe);
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pProcess32First( hSnapshot, &pe ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (pe.th32ProcessID == curr_pid) found++;
|
2008-01-13 21:55:29 +01:00
|
|
|
if (pe.th32ProcessID == sub_pcs_pid) { childpos = num; found++; }
|
2006-10-10 01:06:48 +02:00
|
|
|
trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
|
2006-04-08 11:22:38 +02:00
|
|
|
num++;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pProcess32Next( hSnapshot, &pe ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
ok(found == 2, "couldn't find self and/or sub-process in process list\n");
|
|
|
|
|
|
|
|
/* check that first really resets the enumeration */
|
|
|
|
found = 0;
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pProcess32First( hSnapshot, &pe ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (pe.th32ProcessID == curr_pid) found++;
|
|
|
|
if (pe.th32ProcessID == sub_pcs_pid) found++;
|
2006-10-10 01:06:48 +02:00
|
|
|
trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
|
2006-04-08 11:22:38 +02:00
|
|
|
num--;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pProcess32Next( hSnapshot, &pe ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
ok(found == 2, "couldn't find self and/or sub-process in process list\n");
|
|
|
|
ok(!num, "mismatch in counting\n");
|
|
|
|
|
2008-01-13 21:55:29 +01:00
|
|
|
/* one broken program does Process32First() and does not expect anything
|
|
|
|
* interesting to be there, especially not the just forked off child */
|
|
|
|
ok (childpos !=0, "child is not expected to be at position 0.\n");
|
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
me.dwSize = sizeof(me);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
CloseHandle(hSnapshot);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_thread(DWORD curr_pid, DWORD sub_pcs_pid)
|
|
|
|
{
|
|
|
|
HANDLE hSnapshot;
|
|
|
|
THREADENTRY32 te;
|
|
|
|
MODULEENTRY32 me;
|
|
|
|
int num = 0;
|
|
|
|
unsigned curr_found = 0;
|
|
|
|
unsigned sub_found = 0;
|
|
|
|
|
2006-04-14 09:17:06 +02:00
|
|
|
hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
|
2006-04-08 11:22:38 +02:00
|
|
|
ok(hSnapshot != NULL, "Cannot create snapshot\n");
|
|
|
|
|
|
|
|
/* check that this current process is enumerated */
|
|
|
|
te.dwSize = sizeof(te);
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pThread32First( hSnapshot, &te ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (te.th32OwnerProcessID == curr_pid) curr_found++;
|
|
|
|
if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
|
2008-06-10 11:34:11 +02:00
|
|
|
if (winetest_debug > 1)
|
|
|
|
trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
|
2006-04-08 11:22:38 +02:00
|
|
|
num++;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pThread32Next( hSnapshot, &te ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
ok(curr_found == 1, "couldn't find self in thread list\n");
|
|
|
|
ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
|
|
|
|
|
|
|
|
/* check that first really resets enumeration */
|
|
|
|
curr_found = 0;
|
|
|
|
sub_found = 0;
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pThread32First( hSnapshot, &te ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (te.th32OwnerProcessID == curr_pid) curr_found++;
|
|
|
|
if (te.th32OwnerProcessID == sub_pcs_pid) sub_found++;
|
2008-06-10 11:34:11 +02:00
|
|
|
if (winetest_debug > 1)
|
|
|
|
trace("PID=%x TID=%x %d\n", te.th32OwnerProcessID, te.th32ThreadID, te.tpBasePri);
|
2006-04-08 11:22:38 +02:00
|
|
|
num--;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pThread32Next( hSnapshot, &te ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
ok(curr_found == 1, "couldn't find self in thread list\n");
|
|
|
|
ok(sub_found == 2, "couldn't find sub-process thread's in thread list\n");
|
|
|
|
|
|
|
|
me.dwSize = sizeof(me);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
CloseHandle(hSnapshot);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* curr_expected_modules[] =
|
|
|
|
{
|
2014-04-15 00:00:05 +02:00
|
|
|
"kernel32_test.exe",
|
2009-01-04 14:12:14 +01:00
|
|
|
"kernel32.dll",
|
2014-04-15 00:00:05 +02:00
|
|
|
"ntdll.dll"
|
2006-04-08 11:22:38 +02:00
|
|
|
};
|
2014-04-15 00:00:05 +02:00
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
static const char* sub_expected_modules[] =
|
|
|
|
{
|
|
|
|
"kernel32_test.exe",
|
2009-01-04 14:12:14 +01:00
|
|
|
"kernel32.dll",
|
2011-03-17 18:33:19 +01:00
|
|
|
"shell32.dll",
|
2014-04-15 00:00:05 +02:00
|
|
|
"ntdll.dll"
|
2006-04-08 11:22:38 +02:00
|
|
|
};
|
2014-04-15 00:00:05 +02:00
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
#define NUM_OF(x) (sizeof(x) / sizeof(x[0]))
|
|
|
|
|
|
|
|
static void test_module(DWORD pid, const char* expected[], unsigned num_expected)
|
|
|
|
{
|
|
|
|
HANDLE hSnapshot;
|
|
|
|
PROCESSENTRY32 pe;
|
|
|
|
THREADENTRY32 te;
|
|
|
|
MODULEENTRY32 me;
|
|
|
|
unsigned found[32];
|
2007-05-27 13:18:36 +02:00
|
|
|
unsigned i;
|
|
|
|
int num = 0;
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
ok(NUM_OF(found) >= num_expected, "Internal: bump found[] size\n");
|
|
|
|
|
2006-04-14 09:17:06 +02:00
|
|
|
hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPMODULE, pid );
|
2006-04-08 11:22:38 +02:00
|
|
|
ok(hSnapshot != NULL, "Cannot create snapshot\n");
|
|
|
|
|
|
|
|
for (i = 0; i < num_expected; i++) found[i] = 0;
|
|
|
|
me.dwSize = sizeof(me);
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pModule32First( hSnapshot, &me ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2006-10-10 01:06:48 +02:00
|
|
|
trace("PID=%x base=%p size=%x %s %s\n",
|
2006-04-08 11:22:38 +02:00
|
|
|
me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
|
2006-05-12 00:32:31 +02:00
|
|
|
ok(me.th32ProcessID == pid, "wrong returned process id\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
for (i = 0; i < num_expected; i++)
|
2013-10-21 10:02:53 +02:00
|
|
|
if (!lstrcmpiA(expected[i], me.szModule)) found[i]++;
|
2006-04-08 11:22:38 +02:00
|
|
|
num++;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pModule32Next( hSnapshot, &me ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < num_expected; i++)
|
|
|
|
ok(found[i] == 1, "Module %s is %s\n",
|
|
|
|
expected[i], found[i] ? "listed more than once" : "not listed");
|
|
|
|
|
|
|
|
/* check that first really resets the enumeration */
|
|
|
|
for (i = 0; i < num_expected; i++) found[i] = 0;
|
|
|
|
me.dwSize = sizeof(me);
|
2006-04-14 09:17:06 +02:00
|
|
|
if (pModule32First( hSnapshot, &me ))
|
2006-04-08 11:22:38 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2006-10-10 01:06:48 +02:00
|
|
|
trace("PID=%x base=%p size=%x %s %s\n",
|
2006-04-08 11:22:38 +02:00
|
|
|
me.th32ProcessID, me.modBaseAddr, me.modBaseSize, me.szExePath, me.szModule);
|
|
|
|
for (i = 0; i < num_expected; i++)
|
2013-10-21 10:02:53 +02:00
|
|
|
if (!lstrcmpiA(expected[i], me.szModule)) found[i]++;
|
2006-04-08 11:22:38 +02:00
|
|
|
num--;
|
2006-04-14 09:17:06 +02:00
|
|
|
} while (pModule32Next( hSnapshot, &me ));
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
for (i = 0; i < num_expected; i++)
|
|
|
|
ok(found[i] == 1, "Module %s is %s\n",
|
|
|
|
expected[i], found[i] ? "listed more than once" : "not listed");
|
|
|
|
ok(!num, "mismatch in counting\n");
|
|
|
|
|
|
|
|
pe.dwSize = sizeof(pe);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pProcess32First( hSnapshot, &pe ), "shouldn't return a process\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
|
2008-06-19 16:35:00 +02:00
|
|
|
te.dwSize = sizeof(te);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
CloseHandle(hSnapshot);
|
2006-04-14 09:17:06 +02:00
|
|
|
ok(!pModule32First( hSnapshot, &me ), "shouldn't return a module\n");
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(toolhelp)
|
|
|
|
{
|
|
|
|
DWORD pid = GetCurrentProcessId();
|
|
|
|
int r;
|
2009-01-04 14:12:14 +01:00
|
|
|
char *p, module[MAX_PATH];
|
2006-04-08 11:22:38 +02:00
|
|
|
char buffer[MAX_PATH];
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
PROCESS_INFORMATION info;
|
|
|
|
STARTUPINFOA startup;
|
|
|
|
HANDLE ev1, ev2;
|
|
|
|
DWORD w;
|
2006-04-14 09:17:06 +02:00
|
|
|
HANDLE hkernel32 = GetModuleHandleA("kernel32");
|
|
|
|
|
|
|
|
pCreateToolhelp32Snapshot = (VOID *) GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
|
|
|
|
pModule32First = (VOID *) GetProcAddress(hkernel32, "Module32First");
|
|
|
|
pModule32Next = (VOID *) GetProcAddress(hkernel32, "Module32Next");
|
|
|
|
pProcess32First = (VOID *) GetProcAddress(hkernel32, "Process32First");
|
|
|
|
pProcess32Next = (VOID *) GetProcAddress(hkernel32, "Process32Next");
|
|
|
|
pThread32First = (VOID *) GetProcAddress(hkernel32, "Thread32First");
|
|
|
|
pThread32Next = (VOID *) GetProcAddress(hkernel32, "Thread32Next");
|
|
|
|
|
|
|
|
if (!pCreateToolhelp32Snapshot ||
|
|
|
|
!pModule32First || !pModule32Next ||
|
|
|
|
!pProcess32First || !pProcess32Next ||
|
2007-07-05 14:31:50 +02:00
|
|
|
!pThread32First || !pThread32Next)
|
|
|
|
{
|
2009-02-24 00:03:07 +01:00
|
|
|
win_skip("Needed functions are not available, most likely running on Windows NT\n");
|
2007-07-05 14:31:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-04-08 11:22:38 +02:00
|
|
|
|
|
|
|
r = init();
|
|
|
|
ok(r == 0, "Basic init of sub-process test\n");
|
|
|
|
if (r != 0) return;
|
|
|
|
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
2013-10-21 10:02:53 +02:00
|
|
|
ev1 = CreateEventW(&sa, FALSE, FALSE, NULL);
|
|
|
|
ev2 = CreateEventW(&sa, FALSE, FALSE, NULL);
|
2006-04-08 11:22:38 +02:00
|
|
|
ok (ev1 != NULL && ev2 != NULL, "Couldn't create events\n");
|
|
|
|
memset(&startup, 0, sizeof(startup));
|
|
|
|
startup.cb = sizeof(startup);
|
|
|
|
startup.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
startup.wShowWindow = SW_SHOWNORMAL;
|
|
|
|
|
2009-01-03 20:17:31 +01:00
|
|
|
sprintf(buffer, "%s tests/toolhelp.c %lu %lu", selfname, (DWORD_PTR)ev1, (DWORD_PTR)ev2);
|
2006-04-08 11:22:38 +02:00
|
|
|
ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
|
|
|
|
/* wait for child to be initialized */
|
|
|
|
w = WaitForSingleObject(ev1, WAIT_TIME);
|
|
|
|
ok(w == WAIT_OBJECT_0, "Failed to wait on sub-process startup\n");
|
|
|
|
|
2009-01-04 14:12:14 +01:00
|
|
|
GetModuleFileNameA( 0, module, sizeof(module) );
|
|
|
|
if (!(p = strrchr( module, '\\' ))) p = module;
|
|
|
|
else p++;
|
|
|
|
curr_expected_modules[0] = p;
|
|
|
|
sub_expected_modules[0] = p;
|
|
|
|
|
2006-04-08 11:22:38 +02:00
|
|
|
test_process(pid, info.dwProcessId);
|
|
|
|
test_thread(pid, info.dwProcessId);
|
|
|
|
test_module(pid, curr_expected_modules, NUM_OF(curr_expected_modules));
|
|
|
|
test_module(info.dwProcessId, sub_expected_modules, NUM_OF(sub_expected_modules));
|
|
|
|
|
|
|
|
SetEvent(ev2);
|
2008-02-07 12:56:00 +01:00
|
|
|
winetest_wait_child_process( info.hProcess );
|
2006-04-08 11:22:38 +02:00
|
|
|
}
|