kernelbase: Let GetModuleBaseName succeed on 64bit modules in wow64.
[wine.git] / dlls / psapi / tests / psapi_main.c
blob15b168cf83f086b956eb7b8f1ca08ec4d41856d9
1 /*
2 * Unit test suite for PSAPI
4 * Copyright (C) 2005 Felix Nawothnig
5 * Copyright (C) 2012 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnt.h"
31 #include "winternl.h"
32 #include "winnls.h"
33 #include "winuser.h"
34 #define PSAPI_VERSION 1
35 #include "psapi.h"
36 #include "wine/test.h"
38 static NTSTATUS (WINAPI *pNtQuerySystemInformation)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG);
39 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, MEMORY_INFORMATION_CLASS, PVOID, SIZE_T, SIZE_T *);
40 static BOOL (WINAPI *pIsWow64Process)(HANDLE, BOOL *);
41 static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(void **);
42 static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(void *);
43 static BOOL (WINAPI *pQueryWorkingSetEx)(HANDLE, PVOID, DWORD);
45 static BOOL wow64;
46 static char** main_argv;
48 static BOOL init_func_ptrs(void)
50 pNtQuerySystemInformation = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQuerySystemInformation");
51 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
52 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
53 pWow64DisableWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64DisableWow64FsRedirection");
54 pWow64RevertWow64FsRedirection = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "Wow64RevertWow64FsRedirection");
55 pQueryWorkingSetEx = (void *)GetProcAddress(GetModuleHandleA("psapi.dll"), "QueryWorkingSetEx");
56 return TRUE;
59 static HANDLE hpSR, hpQI, hpVR, hpQV;
60 static const HANDLE hBad = (HANDLE)0xdeadbeef;
62 static void test_EnumProcesses(void)
64 DWORD pid, ret, cbUsed = 0xdeadbeef;
66 SetLastError(0xdeadbeef);
67 ret = EnumProcesses(NULL, 0, &cbUsed);
68 ok(ret == 1, "failed with %ld\n", GetLastError());
69 ok(cbUsed == 0, "cbUsed=%ld\n", cbUsed);
71 SetLastError(0xdeadbeef);
72 ret = EnumProcesses(&pid, 4, &cbUsed);
73 ok(ret == 1, "failed with %ld\n", GetLastError());
74 ok(cbUsed == 4, "cbUsed=%ld\n", cbUsed);
77 static void test_EnumProcessModules(void)
79 char buffer[200] = "C:\\windows\\system32\\notepad.exe";
80 PROCESS_INFORMATION pi = {0};
81 STARTUPINFOA si = {0};
82 void *cookie;
83 HMODULE hMod;
84 DWORD ret, cbNeeded = 0xdeadbeef;
86 SetLastError(0xdeadbeef);
87 EnumProcessModules(NULL, NULL, 0, &cbNeeded);
88 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
90 SetLastError(0xdeadbeef);
91 EnumProcessModules(hpQI, NULL, 0, &cbNeeded);
92 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
94 SetLastError(0xdeadbeef);
95 hMod = (void *)0xdeadbeef;
96 ret = EnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
97 ok(!ret, "succeeded\n");
98 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
100 SetLastError(0xdeadbeef);
101 hMod = (void *)0xdeadbeef;
102 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
103 ok(!ret, "succeeded\n");
104 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %ld\n", GetLastError());
105 ok(hMod == GetModuleHandleA(NULL),
106 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
108 SetLastError(0xdeadbeef);
109 ret = EnumProcessModules(hpQV, NULL, 0, &cbNeeded);
110 ok(ret == 1, "failed with %ld\n", GetLastError());
112 SetLastError(0xdeadbeef);
113 ret = EnumProcessModules(hpQV, NULL, sizeof(HMODULE), &cbNeeded);
114 ok(!ret, "succeeded\n");
115 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %ld\n", GetLastError());
117 SetLastError(0xdeadbeef);
118 hMod = (void *)0xdeadbeef;
119 ret = EnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
120 ok(ret == 1, "got %ld, failed with %ld\n", ret, GetLastError());
121 ok(hMod == GetModuleHandleA(NULL),
122 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
123 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%ld\n", cbNeeded);
125 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
126 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
128 ret = WaitForInputIdle(pi.hProcess, 5000);
129 ok(!ret, "wait timed out\n");
131 SetLastError(0xdeadbeef);
132 hMod = NULL;
133 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
134 ok(ret == 1, "got %ld, error %lu\n", ret, GetLastError());
135 ok(!!hMod, "expected non-NULL module\n");
136 ok(cbNeeded % sizeof(hMod) == 0, "got %lu\n", cbNeeded);
138 TerminateProcess(pi.hProcess, 0);
140 if (sizeof(void *) == 8)
142 MODULEINFO info;
143 char name[40];
145 strcpy(buffer, "C:\\windows\\syswow64\\notepad.exe");
146 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
147 if (ret)
149 ret = WaitForInputIdle(pi.hProcess, 5000);
150 ok(!ret, "wait timed out\n");
152 SetLastError(0xdeadbeef);
153 hMod = NULL;
154 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
155 ok(ret == 1, "got %ld, error %lu\n", ret, GetLastError());
156 ok(!!hMod, "expected non-NULL module\n");
157 ok(cbNeeded % sizeof(hMod) == 0, "got %lu\n", cbNeeded);
159 ret = GetModuleBaseNameA(pi.hProcess, hMod, name, sizeof(name));
160 ok(ret, "got error %lu\n", GetLastError());
161 ok(!strcmp(name, "notepad.exe"), "got %s\n", name);
163 ret = GetModuleFileNameExA(pi.hProcess, hMod, name, sizeof(name));
164 ok(ret, "got error %lu\n", GetLastError());
165 ok(!strcmp(name, buffer), "got %s\n", name);
167 ret = GetModuleInformation(pi.hProcess, hMod, &info, sizeof(info));
168 ok(ret, "got error %lu\n", GetLastError());
169 ok(info.lpBaseOfDll == hMod, "expected %p, got %p\n", hMod, info.lpBaseOfDll);
170 ok(info.SizeOfImage, "image size was 0\n");
171 ok(info.EntryPoint >= info.lpBaseOfDll, "got entry point %p\n", info.EntryPoint);
173 TerminateProcess(pi.hProcess, 0);
175 else
177 if (GetLastError() == ERROR_FILE_NOT_FOUND)
178 skip("Skip wow64 test on non compatible platform\n");
179 else
180 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
183 else if (wow64)
185 pWow64DisableWow64FsRedirection(&cookie);
186 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
187 pWow64RevertWow64FsRedirection(cookie);
188 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
190 ret = WaitForInputIdle(pi.hProcess, 5000);
191 ok(!ret, "wait timed out\n");
193 SetLastError(0xdeadbeef);
194 ret = EnumProcessModules(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded);
195 ok(!ret, "got %ld\n", ret);
196 todo_wine
197 ok(GetLastError() == ERROR_PARTIAL_COPY, "got error %lu\n", GetLastError());
199 TerminateProcess(pi.hProcess, 0);
203 struct moduleex_snapshot
205 unsigned list;
206 DWORD num_modules;
207 HMODULE modules[128];
210 static BOOL test_EnumProcessModulesEx_snapshot(HANDLE proc, struct moduleex_snapshot* mxsnap,
211 unsigned numsnap)
213 DWORD needed;
214 char buffer[80];
215 MODULEINFO info;
216 int i, j;
217 BOOL ret;
218 BOOL fail, wow64;
220 ret = IsWow64Process(proc, &wow64);
221 ok(ret, "IsWow64Process failed: %lu\n", GetLastError());
223 for (i = 0; i < numsnap; i++)
225 winetest_push_context("%d", mxsnap[i].list);
226 SetLastError(0xdeadbeef);
227 mxsnap[i].modules[0] = (void *)0xdeadbeef;
228 ret = EnumProcessModulesEx(proc, mxsnap[i].modules, sizeof(mxsnap[i].modules), &needed, mxsnap[i].list);
229 ok(ret, "didn't succeed %lu\n", GetLastError());
230 ok(needed % sizeof(HMODULE) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%ld\n", needed);
231 mxsnap[i].num_modules = min(needed, sizeof(mxsnap[i].modules)) / sizeof(HMODULE);
232 for (j = 0; j < mxsnap[i].num_modules; j++)
234 /* temporary todo until GetModuleBaseName and friends are fixed */
235 if ((fail = sizeof(void*) == 8 && wow64))
236 switch (mxsnap[i].list)
238 case LIST_MODULES_32BIT: fail = FALSE; break;
239 case LIST_MODULES_DEFAULT: fail = j >= 1; break;
240 case LIST_MODULES_ALL: fail = j >= mxsnap[0].num_modules; break;
241 case LIST_MODULES_64BIT: break;
243 ret = GetModuleBaseNameA(proc, mxsnap[i].modules[j], buffer, sizeof(buffer));
244 ok(ret, "GetModuleBaseName failed: %lu (%u/%lu=%p)\n", GetLastError(), j, mxsnap[i].num_modules, mxsnap[i].modules[j]);
245 ret = GetModuleFileNameExA(proc, mxsnap[i].modules[j], buffer, sizeof(buffer));
246 todo_wine_if(fail)
247 ok(ret, "GetModuleFileNameEx failed: %lu (%u/%lu=%p)\n", GetLastError(), j, mxsnap[i].num_modules, mxsnap[i].modules[j]);
248 memset(&info, 0, sizeof(info));
249 ret = GetModuleInformation(proc, mxsnap[i].modules[j], &info, sizeof(info));
250 todo_wine_if(fail) {
251 ok(ret, "GetModuleInformation failed: %lu\n", GetLastError());
252 ok(info.lpBaseOfDll == mxsnap[i].modules[j], "expected %p, got %p\n", mxsnap[i].modules[j], info.lpBaseOfDll);
253 ok(info.SizeOfImage, "image size was 0\n");
254 /* info.EntryPoint to be checked */
257 winetest_pop_context();
259 return ret;
262 static BOOL snapshot_is_empty(const struct moduleex_snapshot* snap)
264 return snap->num_modules == 0;
267 static BOOL snapshot_contains(const struct moduleex_snapshot* snap, HMODULE mod)
269 int i;
271 for (i = 0; i < snap->num_modules; i++)
272 if (snap->modules[i] == mod) return TRUE;
273 return FALSE;
276 /* It happens (experienced on Windows) that the considered process still loads modules,
277 * meaning that the number of loaded modules can increase between consecutive calls to EnumProcessModulesEx.
278 * In order to cope with this, we're testing for modules list being included into the next one (instead of
279 * equality)
281 static BOOL snapshot_is_subset(const struct moduleex_snapshot* subset, const struct moduleex_snapshot* superset)
283 int i;
285 for (i = 0; i < subset->num_modules; i++)
286 if (!snapshot_contains(superset, subset->modules[i])) return FALSE;
287 return TRUE;
290 static BOOL snapshot_is_equal(const struct moduleex_snapshot* seta, const struct moduleex_snapshot* setb)
292 return snapshot_is_subset(seta, setb) && seta->num_modules == setb->num_modules;
295 static BOOL snapshot_are_disjoint(const struct moduleex_snapshot* seta, const struct moduleex_snapshot* setb, unsigned start)
297 int i, j;
298 for (i = start; i < seta->num_modules; i++)
299 for (j = start; j < setb->num_modules; j++)
300 if (seta->modules[i] == setb->modules[j]) return FALSE;
301 return TRUE;
304 static void snapshot_check_first_main_module(const struct moduleex_snapshot* snap, HANDLE proc,
305 const char* filename)
307 char buffer[80];
308 MODULEINFO info;
309 const char* modname;
310 BOOL ret;
312 if (!(modname = strrchr(filename, '\\'))) modname = filename; else modname++;
313 winetest_push_context("%d", snap->list);
314 ret = GetModuleBaseNameA(proc, snap->modules[0], buffer, sizeof(buffer));
315 ok(ret, "got error %lu\n", GetLastError());
316 ok(!strcasecmp(buffer, modname), "expecting %s but got %s\n", modname, buffer);
317 ret = GetModuleFileNameExA(proc, snap->modules[0], buffer, sizeof(buffer));
318 ok(ret, "got error %lu\n", GetLastError());
319 ok(!strcasecmp(filename, buffer), "expecting %s but got %s\n", filename, buffer);
321 ret = GetModuleInformation(proc, snap->modules[0], &info, sizeof(info));
322 ok(ret, "got error %lu\n", GetLastError());
323 ok(info.lpBaseOfDll == snap->modules[0], "expected %p, got %p\n", snap->modules[0], info.lpBaseOfDll);
324 ok(info.SizeOfImage, "image size was 0\n");
325 ok(info.EntryPoint >= info.lpBaseOfDll, "got entry point %p\n", info.EntryPoint);
326 winetest_pop_context();
329 static void test_EnumProcessModulesEx(void)
331 char buffer[200] = "C:\\windows\\system32\\notepad.exe";
332 PROCESS_INFORMATION pi = {0};
333 STARTUPINFOA si = {0};
334 void *cookie;
335 HMODULE hMod;
336 DWORD ret, cbNeeded = 0xdeadbeef;
337 struct moduleex_snapshot snap[4] = {{LIST_MODULES_32BIT}, {LIST_MODULES_64BIT}, {LIST_MODULES_DEFAULT}, {LIST_MODULES_ALL}};
338 int i;
340 SetLastError(0xdeadbeef);
341 EnumProcessModulesEx(NULL, NULL, 0, &cbNeeded, LIST_MODULES_ALL);
342 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
344 SetLastError(0xdeadbeef);
345 EnumProcessModulesEx(hpQI, NULL, 0, &cbNeeded, LIST_MODULES_ALL);
346 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
348 SetLastError(0xdeadbeef);
349 hMod = (void *)0xdeadbeef;
350 ret = EnumProcessModulesEx(hpQI, &hMod, sizeof(HMODULE), NULL, LIST_MODULES_ALL);
351 ok(!ret, "succeeded\n");
352 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 hMod = (void *)0xdeadbeef;
356 ret = EnumProcessModulesEx(hpQV, &hMod, sizeof(HMODULE), NULL, LIST_MODULES_ALL);
357 ok(!ret, "succeeded\n");
358 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %ld\n", GetLastError());
359 ok(hMod == GetModuleHandleA(NULL),
360 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod, GetModuleHandleA(NULL));
362 SetLastError(0xdeadbeef);
363 ret = EnumProcessModulesEx(hpQV, NULL, 0, &cbNeeded, LIST_MODULES_ALL);
364 ok(ret == 1, "failed with %ld\n", GetLastError());
366 SetLastError(0xdeadbeef);
367 ret = EnumProcessModulesEx(hpQV, NULL, sizeof(HMODULE), &cbNeeded, LIST_MODULES_ALL);
368 ok(!ret, "succeeded\n");
369 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %ld\n", GetLastError());
371 winetest_push_context("self");
372 if (sizeof(void *) == 8)
374 test_EnumProcessModulesEx_snapshot(hpQV, snap, ARRAY_SIZE(snap));
375 ok(snapshot_is_empty(&snap[0]), "didn't expect 32bit module\n");
376 ok(snapshot_is_equal(&snap[1], &snap[2]), "mismatch in modules count\n");
377 ok(snapshot_is_equal(&snap[2], &snap[3]), "mismatch in modules count\n");
378 snapshot_check_first_main_module(&snap[1], hpQV, main_argv[0]);
379 snapshot_check_first_main_module(&snap[2], hpQV, main_argv[0]);
380 snapshot_check_first_main_module(&snap[3], hpQV, main_argv[0]);
382 /* in fact, this error is only returned when (list & 3 == 0), otherwise the corresponding
383 * list is returned without errors
385 SetLastError(0xdeadbeef);
386 ret = EnumProcessModulesEx(hpQV, &hMod, sizeof(HMODULE), &cbNeeded, 0x400);
387 ok(!ret, "succeeded\n");
388 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected error=ERROR_INVALID_PARAMETER but got %ld\n", GetLastError());
390 else if (wow64)
392 test_EnumProcessModulesEx_snapshot(hpQV, snap, ARRAY_SIZE(snap));
393 ok(snapshot_is_equal(&snap[0], &snap[1]), "mismatch in modules count\n");
394 ok(snapshot_is_equal(&snap[1], &snap[2]), "mismatch in modules count\n");
395 ok(snapshot_is_equal(&snap[2], &snap[3]), "mismatch in modules count\n");
396 snapshot_check_first_main_module(&snap[0], hpQV, main_argv[0]);
397 snapshot_check_first_main_module(&snap[1], hpQV, main_argv[0]);
398 snapshot_check_first_main_module(&snap[2], hpQV, main_argv[0]);
399 snapshot_check_first_main_module(&snap[3], hpQV, main_argv[0]);
401 winetest_pop_context();
403 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
404 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
406 ret = WaitForInputIdle(pi.hProcess, 5000);
407 ok(!ret, "wait timed out\n");
409 if (sizeof(void *) == 8)
411 winetest_push_context("pcs-6464");
412 test_EnumProcessModulesEx_snapshot(pi.hProcess, snap, ARRAY_SIZE(snap));
413 ok(snapshot_is_empty(&snap[0]), "didn't expect 32bit module\n");
414 ok(snapshot_is_subset(&snap[1], &snap[2]), "64bit and default module lists should match\n");
415 ok(snapshot_is_subset(&snap[2], &snap[3]), "default and all module lists should match\n");
416 snapshot_check_first_main_module(&snap[1], pi.hProcess, buffer);
417 snapshot_check_first_main_module(&snap[2], pi.hProcess, buffer);
418 snapshot_check_first_main_module(&snap[3], pi.hProcess, buffer);
419 winetest_pop_context();
421 /* in fact, this error is only returned when (list & 3 == 0), otherwise the corresponding
422 * list is returned without errors
424 SetLastError(0xdeadbeef);
425 ret = EnumProcessModulesEx(hpQV, &hMod, sizeof(HMODULE), &cbNeeded, 0x400);
426 ok(!ret, "succeeded\n");
427 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected error=ERROR_INVALID_PARAMETER but got %ld\n", GetLastError());
429 else if (wow64)
431 winetest_push_context("pcs-3232");
432 test_EnumProcessModulesEx_snapshot(pi.hProcess, snap, ARRAY_SIZE(snap));
433 /* some windows version return 64bit modules, others don't... */
434 /* ok(snapshot_is_empty(&snap[1]), "didn't expect 64bit module\n"); */
435 ok(snapshot_is_subset(&snap[1], &snap[3]), "64 and all module lists should match\n");
437 ok(snapshot_is_subset(&snap[0], &snap[2]), "32bit and default module lists should match\n");
438 ok(snapshot_is_subset(&snap[2], &snap[3]), "default and all module lists should match\n");
439 snapshot_check_first_main_module(&snap[0], pi.hProcess, "c:\\windows\\syswow64\\notepad.exe");
440 snapshot_check_first_main_module(&snap[2], pi.hProcess, "c:\\windows\\syswow64\\notepad.exe");
441 snapshot_check_first_main_module(&snap[3], pi.hProcess, "c:\\windows\\syswow64\\notepad.exe");
442 winetest_pop_context();
445 TerminateProcess(pi.hProcess, 0);
447 if (sizeof(void *) == 8)
449 strcpy(buffer, "C:\\windows\\syswow64\\notepad.exe");
450 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
451 if (ret)
453 ret = WaitForInputIdle(pi.hProcess, 5000);
454 ok(!ret, "wait timed out\n");
456 winetest_push_context("pcs-6432");
457 test_EnumProcessModulesEx_snapshot(pi.hProcess, snap, ARRAY_SIZE(snap));
458 ok(!snapshot_is_empty(&snap[0]), "expecting 32bit modules\n");
459 /* FIXME: this tests fails on Wine "old" wow configuration, but succceeds in "multi-arch" and Windows */
460 todo_wine_if(snapshot_is_empty(&snap[1]))
461 ok(!snapshot_is_empty(&snap[1]), "expecting 64bit modules\n");
462 ok(snapshot_is_subset(&snap[1], &snap[2]), "64bit and default module lists should match\n");
463 ok(snapshot_are_disjoint(&snap[0], &snap[1], 0), "32bit and 64bit list should be disjoint\n");
464 /* Main module (even 32bit) is present in both 32bit (makes sense) but also default
465 * (even if all the other modules are 64bit)
467 ok(snapshot_are_disjoint(&snap[0], &snap[2], 1), "32bit and default list should be disjoint\n");
468 ok(snapshot_is_subset(&snap[0], &snap[3]), "32bit and all module lists should match\n");
469 ok(snapshot_is_subset(&snap[1], &snap[3]), "64bit and all module lists should match\n");
470 ok(snapshot_is_subset(&snap[2], &snap[3]), "default and all module list should match\n");
471 snapshot_check_first_main_module(&snap[0], pi.hProcess, buffer);
472 ok(!snapshot_contains(&snap[1], snap[0].modules[0]), "main module shouldn't be present in 64bit list\n");
473 snapshot_check_first_main_module(&snap[2], pi.hProcess, buffer);
474 snapshot_check_first_main_module(&snap[3], pi.hProcess, buffer);
476 /* in fact, this error is only returned when (list & 3 == 0), otherwise the corresponding
477 * list is returned without errors.
479 SetLastError(0xdeadbeef);
480 ret = EnumProcessModulesEx(hpQV, &hMod, sizeof(HMODULE), &cbNeeded, 0x400);
481 ok(!ret, "succeeded\n");
482 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected error=ERROR_INVALID_PARAMETER but got %ld\n", GetLastError());
484 winetest_pop_context();
486 TerminateProcess(pi.hProcess, 0);
488 else
490 if (GetLastError() == ERROR_FILE_NOT_FOUND)
491 skip("Skip wow64 test on non compatible platform\n");
492 else
493 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
497 else if (wow64)
499 pWow64DisableWow64FsRedirection(&cookie);
500 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
501 pWow64RevertWow64FsRedirection(cookie);
502 ok(ret, "CreateProcess failed: %lu\n", GetLastError());
504 ret = WaitForInputIdle(pi.hProcess, 5000);
505 ok(!ret, "wait timed out\n");
507 winetest_push_context("pcs-3264");
508 for (i = 0; i < ARRAY_SIZE(snap); i++)
510 SetLastError(0xdeadbeef);
511 ret = EnumProcessModulesEx(pi.hProcess, &hMod, sizeof(HMODULE), &cbNeeded, snap[i].list);
512 ok(!ret, "succeeded\n");
513 ok(GetLastError() == ERROR_PARTIAL_COPY, "expected error=ERROR_PARTIAL_COPY but got %ld\n", GetLastError());
515 winetest_pop_context();
517 TerminateProcess(pi.hProcess, 0);
521 static void test_GetModuleInformation(void)
523 HMODULE hMod = GetModuleHandleA(NULL);
524 MODULEINFO info;
525 DWORD ret;
527 SetLastError(0xdeadbeef);
528 GetModuleInformation(NULL, hMod, &info, sizeof(info));
529 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
531 SetLastError(0xdeadbeef);
532 GetModuleInformation(hpQI, hMod, &info, sizeof(info));
533 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
535 SetLastError(0xdeadbeef);
536 GetModuleInformation(hpQV, hBad, &info, sizeof(info));
537 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
539 SetLastError(0xdeadbeef);
540 GetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
541 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
543 SetLastError(0xdeadbeef);
544 ret = GetModuleInformation(hpQV, hMod, &info, sizeof(info));
545 ok(ret == 1, "failed with %ld\n", GetLastError());
546 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
549 static BOOL check_with_margin(SIZE_T perf, SIZE_T sysperf, int margin)
551 return (perf >= max(sysperf, margin) - margin && perf <= sysperf + margin);
554 static void test_GetPerformanceInfo(void)
556 PERFORMANCE_INFORMATION info;
557 NTSTATUS status;
558 DWORD size;
559 BOOL ret;
561 SetLastError(0xdeadbeef);
562 ret = GetPerformanceInfo(&info, sizeof(info)-1);
563 ok(!ret, "GetPerformanceInfo unexpectedly succeeded\n");
564 ok(GetLastError() == ERROR_BAD_LENGTH, "expected error=ERROR_BAD_LENGTH but got %ld\n", GetLastError());
566 if (!pNtQuerySystemInformation)
567 win_skip("NtQuerySystemInformation not found, skipping tests\n");
568 else
570 char performance_buffer[sizeof(SYSTEM_PERFORMANCE_INFORMATION) + 16]; /* larger on w2k8/win7 */
571 SYSTEM_PERFORMANCE_INFORMATION *sys_performance_info = (SYSTEM_PERFORMANCE_INFORMATION *)performance_buffer;
572 SYSTEM_PROCESS_INFORMATION *sys_process_info = NULL, *spi;
573 SYSTEM_BASIC_INFORMATION sys_basic_info;
574 DWORD process_count, handle_count, thread_count;
576 /* compare with values from SYSTEM_PERFORMANCE_INFORMATION */
577 size = 0;
578 status = pNtQuerySystemInformation(SystemPerformanceInformation, sys_performance_info, sizeof(performance_buffer), &size);
579 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08lx\n", status);
580 ok(size >= sizeof(SYSTEM_PERFORMANCE_INFORMATION), "incorrect length %ld\n", size);
582 SetLastError(0xdeadbeef);
583 ret = GetPerformanceInfo(&info, sizeof(info));
584 ok(ret, "GetPerformanceInfo failed with %ld\n", GetLastError());
585 ok(info.cb == sizeof(PERFORMANCE_INFORMATION), "got %ld\n", info.cb);
587 ok(check_with_margin(info.CommitTotal, sys_performance_info->TotalCommittedPages, 2048),
588 "expected approximately %Id but got %ld\n", info.CommitTotal, sys_performance_info->TotalCommittedPages);
590 ok(check_with_margin(info.CommitLimit, sys_performance_info->TotalCommitLimit, 32),
591 "expected approximately %Id but got %ld\n", info.CommitLimit, sys_performance_info->TotalCommitLimit);
593 ok(check_with_margin(info.CommitPeak, sys_performance_info->PeakCommitment, 32),
594 "expected approximately %Id but got %ld\n", info.CommitPeak, sys_performance_info->PeakCommitment);
596 ok(check_with_margin(info.PhysicalAvailable, sys_performance_info->AvailablePages, 2048),
597 "expected approximately %Id but got %ld\n", info.PhysicalAvailable, sys_performance_info->AvailablePages);
599 /* TODO: info.SystemCache not checked yet - to which field(s) does this value correspond to? */
601 ok(check_with_margin(info.KernelTotal, sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage, 256),
602 "expected approximately %Id but got %ld\n", info.KernelTotal,
603 sys_performance_info->PagedPoolUsage + sys_performance_info->NonPagedPoolUsage);
605 ok(check_with_margin(info.KernelPaged, sys_performance_info->PagedPoolUsage, 256),
606 "expected approximately %Id but got %ld\n", info.KernelPaged, sys_performance_info->PagedPoolUsage);
608 ok(check_with_margin(info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage, 16),
609 "expected approximately %Id but got %ld\n", info.KernelNonpaged, sys_performance_info->NonPagedPoolUsage);
611 /* compare with values from SYSTEM_BASIC_INFORMATION */
612 size = 0;
613 status = pNtQuerySystemInformation(SystemBasicInformation, &sys_basic_info, sizeof(sys_basic_info), &size);
614 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08lx\n", status);
615 ok(size >= sizeof(SYSTEM_BASIC_INFORMATION), "incorrect length %ld\n", size);
617 ok(info.PhysicalTotal == sys_basic_info.MmNumberOfPhysicalPages,
618 "expected info.PhysicalTotal=%lu but got %lu\n",
619 sys_basic_info.MmNumberOfPhysicalPages, (ULONG)info.PhysicalTotal);
621 ok(info.PageSize == sys_basic_info.PageSize,
622 "expected info.PageSize=%lu but got %lu\n",
623 sys_basic_info.PageSize, (ULONG)info.PageSize);
625 /* compare with values from SYSTEM_PROCESS_INFORMATION */
626 size = 0;
627 status = pNtQuerySystemInformation(SystemProcessInformation, NULL, 0, &size);
628 ok(status == STATUS_INFO_LENGTH_MISMATCH, "expected STATUS_INFO_LENGTH_MISMATCH, got %08lx\n", status);
629 ok(size > 0, "incorrect length %ld\n", size);
630 while (status == STATUS_INFO_LENGTH_MISMATCH)
632 sys_process_info = HeapAlloc(GetProcessHeap(), 0, size);
633 ok(sys_process_info != NULL, "failed to allocate memory\n");
634 status = pNtQuerySystemInformation(SystemProcessInformation, sys_process_info, size, &size);
635 if (status == STATUS_SUCCESS) break;
636 HeapFree(GetProcessHeap(), 0, sys_process_info);
638 ok(status == STATUS_SUCCESS, "expected STATUS_SUCCESS, got %08lx\n", status);
640 process_count = handle_count = thread_count = 0;
641 for (spi = sys_process_info;; spi = (SYSTEM_PROCESS_INFORMATION *)(((char *)spi) + spi->NextEntryOffset))
643 process_count++;
644 handle_count += spi->HandleCount;
645 thread_count += spi->dwThreadCount;
646 if (spi->NextEntryOffset == 0) break;
648 HeapFree(GetProcessHeap(), 0, sys_process_info);
650 ok(check_with_margin(info.HandleCount, handle_count, 512),
651 "expected approximately %ld but got %ld\n", info.HandleCount, handle_count);
653 ok(check_with_margin(info.ProcessCount, process_count, 4),
654 "expected approximately %ld but got %ld\n", info.ProcessCount, process_count);
656 ok(check_with_margin(info.ThreadCount, thread_count, 4),
657 "expected approximately %ld but got %ld\n", info.ThreadCount, thread_count);
662 static void test_GetProcessMemoryInfo(void)
664 PROCESS_MEMORY_COUNTERS pmc;
665 DWORD ret;
667 SetLastError(0xdeadbeef);
668 ret = GetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
669 ok(!ret, "GetProcessMemoryInfo should fail\n");
670 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
672 SetLastError(0xdeadbeef);
673 ret = GetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
674 ok(!ret, "GetProcessMemoryInfo should fail\n");
675 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
677 SetLastError(0xdeadbeef);
678 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
679 ok(!ret, "GetProcessMemoryInfo should fail\n");
680 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
682 SetLastError(0xdeadbeef);
683 ret = GetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
684 ok(ret == 1, "failed with %ld\n", GetLastError());
687 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
689 MEMORY_SECTION_NAME *section_name;
690 WCHAR *buf;
691 SIZE_T buf_len, ret_len;
692 NTSTATUS status;
694 if (!pNtQueryVirtualMemory) return FALSE;
696 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
697 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
699 ret_len = 0xdeadbeef;
700 status = pNtQueryVirtualMemory(process, addr, MemoryMappedFilenameInformation, buf, buf_len, &ret_len);
701 ok(!status, "NtQueryVirtualMemory error %lx\n", status);
703 section_name = (MEMORY_SECTION_NAME *)buf;
704 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %Iu, %u\n",
705 ret_len, section_name->SectionFileName.MaximumLength);
706 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
707 section_name, section_name->SectionFileName.Buffer);
708 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
709 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
710 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
711 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
713 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
714 HeapFree(GetProcessHeap(), 0, buf);
715 return TRUE;
718 static void test_GetMappedFileName(void)
720 HMODULE hMod = GetModuleHandleA(NULL);
721 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
722 DWORD ret;
723 char *base;
724 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
725 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
726 HANDLE hfile, hmap;
728 SetLastError(0xdeadbeef);
729 ret = GetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
730 ok(!ret, "GetMappedFileName should fail\n");
731 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
733 SetLastError(0xdeadbeef);
734 ret = GetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
735 ok(!ret, "GetMappedFileName should fail\n");
736 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
738 SetLastError( 0xdeadbeef );
739 ret = GetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
740 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
741 "GetMappedFileNameA failed with error %lu\n", GetLastError() );
742 if (ret)
744 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%ld\n", szMapPath, ret);
745 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
746 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
747 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
748 if (szMapBaseName)
750 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
751 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
752 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
756 GetTempPathA(MAX_PATH, temp_path);
757 GetTempFileNameA(temp_path, "map", 0, file_name);
759 drive[0] = file_name[0];
760 drive[1] = ':';
761 drive[2] = 0;
762 SetLastError(0xdeadbeef);
763 ret = QueryDosDeviceA(drive, device_name, sizeof(device_name));
764 ok(ret, "QueryDosDeviceA error %ld\n", GetLastError());
765 trace("%s -> %s\n", drive, device_name);
767 SetLastError(0xdeadbeef);
768 hfile = CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
769 ok(hfile != INVALID_HANDLE_VALUE, "CreateFileA(%s) error %ld\n", file_name, GetLastError());
770 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
771 SetEndOfFile(hfile);
773 SetLastError(0xdeadbeef);
774 hmap = CreateFileMappingA(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
775 ok(hmap != 0, "CreateFileMappingA error %ld\n", GetLastError());
777 SetLastError(0xdeadbeef);
778 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
779 ok(base != NULL, "MapViewOfFile error %ld\n", GetLastError());
781 SetLastError(0xdeadbeef);
782 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
783 ok(!ret, "GetMappedFileName should fail\n");
784 ok(GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
785 "wrong error %ld\n", GetLastError());
787 SetLastError(0xdeadbeef);
788 ret = GetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
789 ok(!ret, "GetMappedFileName should fail\n");
790 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
792 SetLastError(0xdeadbeef);
793 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
794 ok(ret == 1, "GetMappedFileName error %ld\n", GetLastError());
795 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
797 SetLastError(0xdeadbeef);
798 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
799 ok(ret, "GetMappedFileName error %ld\n", GetLastError());
800 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
801 todo_wine
802 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
804 SetLastError(0xdeadbeef);
805 ret = GetMappedFileNameW(GetCurrentProcess(), base, map_nameW, ARRAY_SIZE(map_nameW));
806 ok(ret, "GetMappedFileNameW error %ld\n", GetLastError());
807 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
808 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, ARRAY_SIZE(nt_map_name)))
810 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
811 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
812 todo_wine
813 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
816 SetLastError(0xdeadbeef);
817 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
818 ok(ret, "GetMappedFileName error %ld\n", GetLastError());
819 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
820 todo_wine
821 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
823 SetLastError(0xdeadbeef);
824 ret = GetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
825 ok(!ret, "GetMappedFileName should fail\n");
826 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %ld\n", GetLastError());
828 SetLastError(0xdeadbeef);
829 ret = GetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
830 ok(!ret, "GetMappedFileName should fail\n");
831 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %ld\n", GetLastError());
833 SetLastError(0xdeadbeef);
834 ret = GetMappedFileNameA(0, base, map_name, sizeof(map_name));
835 ok(!ret, "GetMappedFileName should fail\n");
836 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %ld\n", GetLastError());
838 UnmapViewOfFile(base);
839 CloseHandle(hmap);
840 CloseHandle(hfile);
841 DeleteFileA(file_name);
843 SetLastError(0xdeadbeef);
844 hmap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
845 ok(hmap != 0, "CreateFileMappingA error %ld\n", GetLastError());
847 SetLastError(0xdeadbeef);
848 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
849 ok(base != NULL, "MapViewOfFile error %ld\n", GetLastError());
851 SetLastError(0xdeadbeef);
852 ret = GetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
853 ok(!ret, "GetMappedFileName should fail\n");
854 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %ld\n", GetLastError());
856 UnmapViewOfFile(base);
857 CloseHandle(hmap);
860 static void test_GetProcessImageFileName(void)
862 HMODULE hMod = GetModuleHandleA(NULL);
863 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
864 WCHAR szImgPathW[MAX_PATH];
865 DWORD ret, ret1;
867 /* This function is available on WinXP+ only */
868 SetLastError(0xdeadbeef);
869 if(!GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
871 if(GetLastError() == ERROR_INVALID_FUNCTION) {
872 win_skip("GetProcessImageFileName not implemented\n");
873 return;
875 ok(0, "failed with %ld\n", GetLastError());
878 SetLastError(0xdeadbeef);
879 GetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
880 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
882 SetLastError(0xdeadbeef);
883 GetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
884 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
886 SetLastError(0xdeadbeef);
887 GetProcessImageFileNameA(hpQI, szImgPath, 0);
888 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
890 ret = GetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
891 ret1 = GetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
892 if(ret && ret1)
894 /* Windows returns 2*strlen-1 */
895 ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%ld\n", szImgPath, ret);
896 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
899 SetLastError(0xdeadbeef);
900 GetProcessImageFileNameW(NULL, szImgPathW, ARRAY_SIZE(szImgPathW));
901 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
903 /* no information about correct buffer size returned: */
904 SetLastError(0xdeadbeef);
905 GetProcessImageFileNameW(hpQI, szImgPathW, 0);
906 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
908 SetLastError(0xdeadbeef);
909 GetProcessImageFileNameW(hpQI, NULL, 0);
910 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
912 /* correct call */
913 memset(szImgPathW, 0xff, sizeof(szImgPathW));
914 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ARRAY_SIZE(szImgPathW));
915 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
916 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
917 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %ld, got %d\n", ret, lstrlenW(szImgPathW));
919 /* boundary values of 'size' */
920 SetLastError(0xdeadbeef);
921 GetProcessImageFileNameW(hpQI, szImgPathW, ret);
922 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %ld\n", GetLastError());
924 memset(szImgPathW, 0xff, sizeof(szImgPathW));
925 ret = GetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
926 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
927 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
928 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %ld, got %d\n", ret, lstrlenW(szImgPathW));
931 static void test_GetModuleFileNameEx(void)
933 HMODULE hMod = GetModuleHandleA(NULL);
934 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
935 WCHAR buffer[MAX_PATH];
936 DWORD ret;
938 SetLastError(0xdeadbeef);
939 ret = GetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
940 ok( !ret, "GetModuleFileNameExA succeeded\n" );
941 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
943 SetLastError(0xdeadbeef);
944 ret = GetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
945 ok( !ret, "GetModuleFileNameExA succeeded\n" );
946 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
948 SetLastError(0xdeadbeef);
949 ret = GetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
950 ok( !ret, "GetModuleFileNameExA succeeded\n" );
951 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
953 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
954 if(!ret)
955 return;
956 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%ld\n", szModExPath, ret);
957 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
958 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
959 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
961 SetLastError(0xdeadbeef);
962 memset( szModExPath, 0xcc, sizeof(szModExPath) );
963 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
964 ok( ret == 4 || ret == strlen(szModExPath), "wrong length %lu\n", ret );
965 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
966 "szModExPath=\"%s\" ret=%ld\n", szModExPath, ret );
967 ok(GetLastError() == 0xdeadbeef, "got error %ld\n", GetLastError());
969 if (0) /* crashes on Windows 10 */
971 SetLastError(0xdeadbeef);
972 ret = GetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
973 ok( ret == 0, "wrong length %lu\n", ret );
974 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %ld\n", GetLastError());
977 SetLastError(0xdeadbeef);
978 memset( buffer, 0xcc, sizeof(buffer) );
979 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 4 );
980 ok( ret == 4 || ret == lstrlenW(buffer), "wrong length %lu\n", ret );
981 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
982 "buffer=%s ret=%ld\n", wine_dbgstr_w(buffer), ret );
983 ok(GetLastError() == 0xdeadbeef, "got error %ld\n", GetLastError());
985 if (0) /* crashes on Windows 10 */
987 SetLastError(0xdeadbeef);
988 buffer[0] = 0xcc;
989 ret = GetModuleFileNameExW(hpQV, NULL, buffer, 0 );
990 ok( ret == 0, "wrong length %lu\n", ret );
991 ok(GetLastError() == 0xdeadbeef, "got error %ld\n", GetLastError());
992 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
996 static void test_GetModuleBaseName(void)
998 HMODULE hMod = GetModuleHandleA(NULL);
999 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
1000 DWORD ret;
1002 SetLastError(0xdeadbeef);
1003 GetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
1004 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
1006 SetLastError(0xdeadbeef);
1007 GetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
1008 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
1010 SetLastError(0xdeadbeef);
1011 GetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
1012 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
1014 ret = GetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
1015 if(!ret)
1016 return;
1017 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%ld\n", szModBaseName, ret);
1018 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
1019 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
1020 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
1023 static void test_ws_functions(void)
1025 PSAPI_WS_WATCH_INFORMATION wswi[4096];
1026 HANDLE ws_handle;
1027 char *addr;
1028 unsigned int i;
1029 BOOL ret;
1031 ws_handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA |
1032 PROCESS_SET_INFORMATION, FALSE, GetCurrentProcessId());
1033 ok(!!ws_handle, "got error %lu\n", GetLastError());
1035 SetLastError(0xdeadbeef);
1036 EmptyWorkingSet(NULL);
1037 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %ld\n", GetLastError());
1039 SetLastError(0xdeadbeef);
1040 EmptyWorkingSet(hpSR);
1041 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %ld\n", GetLastError());
1043 SetLastError(0xdeadbeef);
1044 ret = EmptyWorkingSet(ws_handle);
1045 ok(ret == 1, "failed with %ld\n", GetLastError());
1047 SetLastError( 0xdeadbeef );
1048 ret = InitializeProcessForWsWatch( NULL );
1049 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
1050 if (!ret)
1052 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
1054 trace( "InitializeProcessForWsWatch not supported\n" );
1055 return;
1057 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %lu\n", GetLastError() );
1059 SetLastError(0xdeadbeef);
1060 ret = InitializeProcessForWsWatch(ws_handle);
1061 ok(ret == 1, "failed with %ld\n", GetLastError());
1063 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
1064 if(!addr)
1065 return;
1067 *addr = 0; /* make sure it's paged in (needed on wow64) */
1068 if(!VirtualLock(addr, 1))
1070 trace("locking failed (error=%ld) - skipping test\n", GetLastError());
1071 goto free_page;
1074 SetLastError(0xdeadbeef);
1075 ret = GetWsChanges(hpQI, wswi, sizeof(wswi));
1076 todo_wine ok(ret == 1, "failed with %ld\n", GetLastError());
1077 if(ret == 1)
1079 for(i = 0; wswi[i].FaultingVa; i++)
1080 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
1082 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
1083 goto free_page;
1086 todo_wine ok(0, "GetWsChanges didn't find our page\n");
1089 free_page:
1090 VirtualFree(addr, 0, MEM_RELEASE);
1093 static void check_QueryWorkingSetEx(PVOID addr, const char *desc, DWORD expected_valid,
1094 DWORD expected_protection, DWORD expected_shared, BOOL todo)
1096 PSAPI_WORKING_SET_EX_INFORMATION info;
1097 BOOL ret;
1099 memset(&info, 0x41, sizeof(info));
1100 info.VirtualAddress = addr;
1101 ret = pQueryWorkingSetEx(GetCurrentProcess(), &info, sizeof(info));
1102 ok(ret, "QueryWorkingSetEx failed with %ld\n", GetLastError());
1104 todo_wine_if(todo)
1105 ok(info.VirtualAttributes.Valid == expected_valid, "%s expected Valid=%lu but got %u\n",
1106 desc, expected_valid, info.VirtualAttributes.Valid);
1108 todo_wine_if(todo)
1109 ok(info.VirtualAttributes.Win32Protection == expected_protection, "%s expected Win32Protection=%lu but got %u\n",
1110 desc, expected_protection, info.VirtualAttributes.Win32Protection);
1112 ok(info.VirtualAttributes.Node == 0, "%s expected Node=0 but got %u\n",
1113 desc, info.VirtualAttributes.Node);
1114 ok(info.VirtualAttributes.LargePage == 0, "%s expected LargePage=0 but got %u\n",
1115 desc, info.VirtualAttributes.LargePage);
1117 ok(info.VirtualAttributes.Shared == expected_shared || broken(!info.VirtualAttributes.Valid) /* w2003 */,
1118 "%s expected Shared=%lu but got %u\n", desc, expected_shared, info.VirtualAttributes.Shared);
1119 if (info.VirtualAttributes.Valid && info.VirtualAttributes.Shared)
1120 ok(info.VirtualAttributes.ShareCount > 0, "%s expected ShareCount > 0 but got %u\n",
1121 desc, info.VirtualAttributes.ShareCount);
1122 else
1123 ok(info.VirtualAttributes.ShareCount == 0, "%s expected ShareCount == 0 but got %u\n",
1124 desc, info.VirtualAttributes.ShareCount);
1127 static void test_QueryWorkingSetEx(void)
1129 PVOID addr;
1130 DWORD prot;
1131 BOOL ret;
1133 if (pQueryWorkingSetEx == NULL)
1135 win_skip("QueryWorkingSetEx not found, skipping tests\n");
1136 return;
1139 addr = GetModuleHandleA(NULL);
1140 check_QueryWorkingSetEx(addr, "exe", 1, PAGE_READONLY, 1, FALSE);
1142 ret = VirtualProtect(addr, 0x1000, PAGE_NOACCESS, &prot);
1143 ok(ret, "VirtualProtect failed with %ld\n", GetLastError());
1144 check_QueryWorkingSetEx(addr, "exe,noaccess", 0, 0, 1, FALSE);
1146 ret = VirtualProtect(addr, 0x1000, prot, &prot);
1147 ok(ret, "VirtualProtect failed with %ld\n", GetLastError());
1148 check_QueryWorkingSetEx(addr, "exe,readonly1", 0, 0, 1, TRUE);
1150 *(volatile char *)addr;
1151 ok(ret, "VirtualProtect failed with %ld\n", GetLastError());
1152 check_QueryWorkingSetEx(addr, "exe,readonly2", 1, PAGE_READONLY, 1, FALSE);
1154 addr = VirtualAlloc(NULL, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
1155 ok(addr != NULL, "VirtualAlloc failed with %ld\n", GetLastError());
1156 check_QueryWorkingSetEx(addr, "valloc", 0, 0, 0, FALSE);
1158 *(volatile char *)addr;
1159 check_QueryWorkingSetEx(addr, "valloc,read", 1, PAGE_READWRITE, 0, FALSE);
1161 *(volatile char *)addr = 0x42;
1162 check_QueryWorkingSetEx(addr, "valloc,write", 1, PAGE_READWRITE, 0, FALSE);
1164 ret = VirtualProtect(addr, 0x1000, PAGE_NOACCESS, &prot);
1165 ok(ret, "VirtualProtect failed with %ld\n", GetLastError());
1166 check_QueryWorkingSetEx(addr, "valloc,noaccess", 0, 0, 0, FALSE);
1168 ret = VirtualProtect(addr, 0x1000, prot, &prot);
1169 ok(ret, "VirtualProtect failed with %ld\n", GetLastError());
1170 check_QueryWorkingSetEx(addr, "valloc,readwrite1", 0, 0, 0, TRUE);
1172 *(volatile char *)addr;
1173 check_QueryWorkingSetEx(addr, "valloc,readwrite2", 1, PAGE_READWRITE, 0, FALSE);
1175 ret = VirtualFree(addr, 0, MEM_RELEASE);
1176 ok(ret, "VirtualFree failed with %ld\n", GetLastError());
1177 check_QueryWorkingSetEx(addr, "valloc,free", FALSE, 0, 0, FALSE);
1180 START_TEST(psapi_main)
1182 DWORD pid = GetCurrentProcessId();
1184 winetest_get_mainargs(&main_argv);
1185 init_func_ptrs();
1187 if (pIsWow64Process)
1188 IsWow64Process(GetCurrentProcess(), &wow64);
1190 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
1191 ok(!!hpSR, "got error %lu\n", GetLastError());
1192 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
1193 ok(!!hpQI, "got error %lu\n", GetLastError());
1194 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
1195 ok(!!hpVR, "got error %lu\n", GetLastError());
1196 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
1197 ok(!!hpQV, "got error %lu\n", GetLastError());
1199 test_EnumProcesses();
1200 test_EnumProcessModules();
1201 test_EnumProcessModulesEx();
1202 test_GetModuleInformation();
1203 test_GetPerformanceInfo();
1204 test_GetProcessMemoryInfo();
1205 test_GetMappedFileName();
1206 test_GetProcessImageFileName();
1207 test_GetModuleFileNameEx();
1208 test_GetModuleBaseName();
1209 test_QueryWorkingSetEx();
1210 test_ws_functions();
1212 CloseHandle(hpSR);
1213 CloseHandle(hpQI);
1214 CloseHandle(hpVR);
1215 CloseHandle(hpQV);