Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / psapi / tests / psapi_main.c
blob312d544c491e8b2d5ab90368b517f8454f49c9e8
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 /* 0x0600 makes PROCESS_ALL_ACCESS not compatible with pre-Vista versions */
25 #define _WIN32_WINNT 0x0500
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winnt.h"
34 #include "winternl.h"
35 #include "winnls.h"
36 #include "psapi.h"
37 #include "wine/test.h"
39 #define PSAPI_GET_PROC(func) \
40 p ## func = (void*)GetProcAddress(hpsapi, #func); \
41 if(!p ## func) { \
42 ok(0, "GetProcAddress(%s) failed\n", #func); \
43 FreeLibrary(hpsapi); \
44 return FALSE; \
47 static BOOL (WINAPI *pEmptyWorkingSet)(HANDLE);
48 static BOOL (WINAPI *pEnumProcesses)(DWORD*, DWORD, DWORD*);
49 static BOOL (WINAPI *pEnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
50 static DWORD (WINAPI *pGetModuleBaseNameA)(HANDLE, HMODULE, LPSTR, DWORD);
51 static DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE, HMODULE, LPSTR, DWORD);
52 static DWORD (WINAPI *pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD);
53 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
54 static DWORD (WINAPI *pGetMappedFileNameA)(HANDLE, LPVOID, LPSTR, DWORD);
55 static DWORD (WINAPI *pGetMappedFileNameW)(HANDLE, LPVOID, LPWSTR, DWORD);
56 static DWORD (WINAPI *pGetProcessImageFileNameA)(HANDLE, LPSTR, DWORD);
57 static DWORD (WINAPI *pGetProcessImageFileNameW)(HANDLE, LPWSTR, DWORD);
58 static BOOL (WINAPI *pGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
59 static BOOL (WINAPI *pGetWsChanges)(HANDLE, PPSAPI_WS_WATCH_INFORMATION, DWORD);
60 static BOOL (WINAPI *pInitializeProcessForWsWatch)(HANDLE);
61 static BOOL (WINAPI *pQueryWorkingSet)(HANDLE, PVOID, DWORD);
62 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, ULONG, PVOID, SIZE_T, SIZE_T *);
64 static BOOL InitFunctionPtrs(HMODULE hpsapi)
66 PSAPI_GET_PROC(EmptyWorkingSet);
67 PSAPI_GET_PROC(EnumProcessModules);
68 PSAPI_GET_PROC(EnumProcesses);
69 PSAPI_GET_PROC(GetModuleBaseNameA);
70 PSAPI_GET_PROC(GetModuleFileNameExA);
71 PSAPI_GET_PROC(GetModuleFileNameExW);
72 PSAPI_GET_PROC(GetModuleInformation);
73 PSAPI_GET_PROC(GetMappedFileNameA);
74 PSAPI_GET_PROC(GetMappedFileNameW);
75 PSAPI_GET_PROC(GetProcessMemoryInfo);
76 PSAPI_GET_PROC(GetWsChanges);
77 PSAPI_GET_PROC(InitializeProcessForWsWatch);
78 PSAPI_GET_PROC(QueryWorkingSet);
79 /* GetProcessImageFileName is not exported on NT4 */
80 pGetProcessImageFileNameA =
81 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameA");
82 pGetProcessImageFileNameW =
83 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameW");
84 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryVirtualMemory");
85 return TRUE;
88 static HANDLE hpSR, hpQI, hpVR, hpQV, hpAA;
89 static const HANDLE hBad = (HANDLE)0xdeadbeef;
91 static void test_EnumProcesses(void)
93 DWORD pid, ret, cbUsed = 0xdeadbeef;
95 SetLastError(0xdeadbeef);
96 ret = pEnumProcesses(NULL, 0, &cbUsed);
97 ok(ret == 1, "failed with %d\n", GetLastError());
98 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
100 SetLastError(0xdeadbeef);
101 ret = pEnumProcesses(&pid, 4, &cbUsed);
102 ok(ret == 1, "failed with %d\n", GetLastError());
103 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
106 static void test_EnumProcessModules(void)
108 HMODULE hMod = GetModuleHandle(NULL);
109 DWORD ret, cbNeeded = 0xdeadbeef;
111 SetLastError(0xdeadbeef);
112 pEnumProcessModules(NULL, NULL, 0, &cbNeeded);
113 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
115 SetLastError(0xdeadbeef);
116 pEnumProcessModules(hpQI, NULL, 0, &cbNeeded);
117 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
119 SetLastError(0xdeadbeef);
120 ret = pEnumProcessModules(hpQI, &hMod, sizeof(HMODULE), NULL);
121 ok(!ret, "succeeded\n");
122 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
124 SetLastError(0xdeadbeef);
125 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), NULL);
126 ok(!ret, "succeeded\n");
127 ok(GetLastError() == ERROR_NOACCESS, "expected error=ERROR_NOACCESS but got %d\n", GetLastError());
129 SetLastError(0xdeadbeef);
130 ret = pEnumProcessModules(hpQV, NULL, 0, &cbNeeded);
131 ok(ret == 1, "failed with %d\n", GetLastError());
133 SetLastError(0xdeadbeef);
134 ret = pEnumProcessModules(hpQV, &hMod, sizeof(HMODULE), &cbNeeded);
135 if(ret != 1)
136 return;
137 ok(hMod == GetModuleHandle(NULL),
138 "hMod=%p GetModuleHandle(NULL)=%p\n", hMod, GetModuleHandle(NULL));
139 ok(cbNeeded % sizeof(hMod) == 0, "not a multiple of sizeof(HMODULE) cbNeeded=%d\n", cbNeeded);
140 /* Windows sometimes has a bunch of extra dlls, presumably brought in by
141 * aclayers.dll.
143 if (cbNeeded < 4 * sizeof(HMODULE) || cbNeeded > 30 * sizeof(HMODULE))
145 HMODULE hmods[100];
146 int i;
147 ok(0, "cbNeeded=%d\n", cbNeeded);
149 pEnumProcessModules(hpQV, hmods, sizeof(hmods), &cbNeeded);
150 for (i = 0 ; i < cbNeeded/sizeof(*hmods); i++)
152 char path[1024];
153 GetModuleFileNameA(hmods[i], path, sizeof(path));
154 trace("i=%d hmod=%p path=[%s]\n", i, hmods[i], path);
159 static void test_GetModuleInformation(void)
161 HMODULE hMod = GetModuleHandle(NULL);
162 MODULEINFO info;
163 DWORD ret;
165 SetLastError(0xdeadbeef);
166 pGetModuleInformation(NULL, hMod, &info, sizeof(info));
167 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
169 SetLastError(0xdeadbeef);
170 pGetModuleInformation(hpQI, hMod, &info, sizeof(info));
171 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
173 SetLastError(0xdeadbeef);
174 pGetModuleInformation(hpQV, hBad, &info, sizeof(info));
175 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
177 SetLastError(0xdeadbeef);
178 pGetModuleInformation(hpQV, hMod, &info, sizeof(info)-1);
179 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
181 SetLastError(0xdeadbeef);
182 ret = pGetModuleInformation(hpQV, hMod, &info, sizeof(info));
183 ok(ret == 1, "failed with %d\n", GetLastError());
184 ok(info.lpBaseOfDll == hMod, "lpBaseOfDll=%p hMod=%p\n", info.lpBaseOfDll, hMod);
187 static void test_GetProcessMemoryInfo(void)
189 PROCESS_MEMORY_COUNTERS pmc;
190 DWORD ret;
192 SetLastError(0xdeadbeef);
193 ret = pGetProcessMemoryInfo(NULL, &pmc, sizeof(pmc));
194 ok(!ret, "GetProcessMemoryInfo should fail\n");
195 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
197 SetLastError(0xdeadbeef);
198 ret = pGetProcessMemoryInfo(hpSR, &pmc, sizeof(pmc));
199 todo_wine
200 ok(!ret, "GetProcessMemoryInfo should fail\n");
201 todo_wine
202 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
204 SetLastError(0xdeadbeef);
205 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc)-1);
206 ok(!ret, "GetProcessMemoryInfo should fail\n");
207 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
209 SetLastError(0xdeadbeef);
210 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
211 ok(ret == 1, "failed with %d\n", GetLastError());
214 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
216 MEMORY_SECTION_NAME *section_name;
217 WCHAR *buf;
218 SIZE_T buf_len, ret_len;
219 NTSTATUS status;
221 if (!pNtQueryVirtualMemory) return FALSE;
223 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
224 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
226 ret_len = 0xdeadbeef;
227 status = pNtQueryVirtualMemory(process, addr, MemorySectionName, buf, buf_len, &ret_len);
228 todo_wine
229 ok(!status, "NtQueryVirtualMemory error %x\n", status);
230 /* FIXME: remove once Wine is fixed */
231 if (status) return FALSE;
233 section_name = (MEMORY_SECTION_NAME *)buf;
234 ok(ret_len == section_name->SectionFileName.MaximumLength + sizeof(*section_name), "got %lu, %u\n",
235 ret_len, section_name->SectionFileName.MaximumLength);
236 ok((char *)section_name->SectionFileName.Buffer == (char *)section_name + sizeof(*section_name), "got %p, %p\n",
237 section_name, section_name->SectionFileName.Buffer);
238 ok(section_name->SectionFileName.MaximumLength == section_name->SectionFileName.Length + sizeof(WCHAR), "got %u, %u\n",
239 section_name->SectionFileName.MaximumLength, section_name->SectionFileName.Length);
240 ok(section_name->SectionFileName.Length == lstrlenW(section_name->SectionFileName.Buffer) * sizeof(WCHAR), "got %u, %u\n",
241 section_name->SectionFileName.Length, lstrlenW(section_name->SectionFileName.Buffer));
243 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
244 HeapFree(GetProcessHeap(), 0, buf);
245 return TRUE;
248 static void test_GetMappedFileName(void)
250 HMODULE hMod = GetModuleHandle(NULL);
251 char szMapPath[MAX_PATH], szModPath[MAX_PATH], *szMapBaseName;
252 DWORD ret;
253 char *base;
254 char temp_path[MAX_PATH], file_name[MAX_PATH], map_name[MAX_PATH], device_name[MAX_PATH], drive[3];
255 WCHAR map_nameW[MAX_PATH], nt_map_name[MAX_PATH];
256 HANDLE hfile, hmap;
258 SetLastError(0xdeadbeef);
259 ret = pGetMappedFileNameA(NULL, hMod, szMapPath, sizeof(szMapPath));
260 ok(!ret, "GetMappedFileName should fail\n");
261 todo_wine
262 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
264 SetLastError(0xdeadbeef);
265 ret = pGetMappedFileNameA(hpSR, hMod, szMapPath, sizeof(szMapPath));
266 ok(!ret, "GetMappedFileName should fail\n");
267 todo_wine
268 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
270 SetLastError( 0xdeadbeef );
271 ret = pGetMappedFileNameA(hpQI, hMod, szMapPath, sizeof(szMapPath));
272 todo_wine
273 ok( ret || broken(GetLastError() == ERROR_UNEXP_NET_ERR), /* win2k */
274 "GetMappedFileNameA failed with error %u\n", GetLastError() );
275 if (ret)
277 ok(ret == strlen(szMapPath), "szMapPath=\"%s\" ret=%d\n", szMapPath, ret);
278 todo_wine
279 ok(szMapPath[0] == '\\', "szMapPath=\"%s\"\n", szMapPath);
280 szMapBaseName = strrchr(szMapPath, '\\'); /* That's close enough for us */
281 todo_wine
282 ok(szMapBaseName && *szMapBaseName, "szMapPath=\"%s\"\n", szMapPath);
283 if (szMapBaseName)
285 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
286 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
287 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
291 GetTempPath(MAX_PATH, temp_path);
292 GetTempFileName(temp_path, "map", 0, file_name);
294 drive[0] = file_name[0];
295 drive[1] = ':';
296 drive[2] = 0;
297 SetLastError(0xdeadbeef);
298 ret = QueryDosDevice(drive, device_name, sizeof(device_name));
299 ok(ret, "QueryDosDevice error %d\n", GetLastError());
300 trace("%s -> %s\n", drive, device_name);
302 SetLastError(0xdeadbeef);
303 hfile = CreateFile(file_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
304 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile(%s) error %d\n", file_name, GetLastError());
305 SetFilePointer(hfile, 0x4000, NULL, FILE_BEGIN);
306 SetEndOfFile(hfile);
308 SetLastError(0xdeadbeef);
309 hmap = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
310 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
312 SetLastError(0xdeadbeef);
313 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
314 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
316 SetLastError(0xdeadbeef);
317 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 0);
318 ok(!ret, "GetMappedFileName should fail\n");
319 todo_wine
320 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
322 SetLastError(0xdeadbeef);
323 ret = pGetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
324 ok(!ret, "GetMappedFileName should fail\n");
325 todo_wine
326 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
328 SetLastError(0xdeadbeef);
329 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
330 todo_wine
331 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
332 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
334 SetLastError(0xdeadbeef);
335 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
336 todo_wine {
337 ok(ret, "GetMappedFileName error %d\n", GetLastError());
338 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
339 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
342 SetLastError(0xdeadbeef);
343 ret = pGetMappedFileNameW(GetCurrentProcess(), base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0]));
344 todo_wine {
345 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
346 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
348 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0])))
350 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
351 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
352 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
355 SetLastError(0xdeadbeef);
356 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
357 todo_wine {
358 ok(ret, "GetMappedFileName error %d\n", GetLastError());
359 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
360 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
363 SetLastError(0xdeadbeef);
364 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
365 ok(!ret, "GetMappedFileName should fail\n");
366 todo_wine
367 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
369 SetLastError(0xdeadbeef);
370 ret = pGetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
371 ok(!ret, "GetMappedFileName should fail\n");
372 todo_wine
373 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
375 SetLastError(0xdeadbeef);
376 ret = pGetMappedFileNameA(0, base, map_name, sizeof(map_name));
377 ok(!ret, "GetMappedFileName should fail\n");
378 todo_wine
379 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
381 UnmapViewOfFile(base);
382 CloseHandle(hmap);
383 CloseHandle(hfile);
384 DeleteFile(file_name);
386 SetLastError(0xdeadbeef);
387 hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
388 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
390 SetLastError(0xdeadbeef);
391 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
392 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
394 SetLastError(0xdeadbeef);
395 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
396 ok(!ret, "GetMappedFileName should fail\n");
397 todo_wine
398 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
400 UnmapViewOfFile(base);
401 CloseHandle(hmap);
404 static void test_GetProcessImageFileName(void)
406 HMODULE hMod = GetModuleHandle(NULL);
407 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
408 WCHAR szImgPathW[MAX_PATH];
409 DWORD ret, ret1;
411 if(pGetProcessImageFileNameA == NULL)
412 return;
414 /* This function is available on WinXP+ only */
415 SetLastError(0xdeadbeef);
416 if(!pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
418 if(GetLastError() == ERROR_INVALID_FUNCTION) {
419 win_skip("GetProcessImageFileName not implemented\n");
420 return;
423 if(GetLastError() == 0xdeadbeef)
424 todo_wine ok(0, "failed without error code\n");
425 else
426 todo_wine ok(0, "failed with %d\n", GetLastError());
429 SetLastError(0xdeadbeef);
430 pGetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
431 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
433 SetLastError(0xdeadbeef);
434 pGetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
435 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
437 SetLastError(0xdeadbeef);
438 pGetProcessImageFileNameA(hpQI, szImgPath, 0);
439 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
441 ret = pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
442 ret1 = pGetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
443 if(ret && ret1)
445 /* Windows returns 2*strlen-1 */
446 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
447 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
450 SetLastError(0xdeadbeef);
451 pGetProcessImageFileNameW(NULL, szImgPathW, sizeof(szImgPathW));
452 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
454 /* no information about correct buffer size returned: */
455 SetLastError(0xdeadbeef);
456 pGetProcessImageFileNameW(hpQI, szImgPathW, 0);
457 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
459 SetLastError(0xdeadbeef);
460 pGetProcessImageFileNameW(hpQI, NULL, 0);
461 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
463 /* correct call */
464 memset(szImgPathW, 0xff, sizeof(szImgPathW));
465 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, sizeof(szImgPathW)/sizeof(WCHAR));
466 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
467 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
468 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
470 /* boundary values of 'size' */
471 SetLastError(0xdeadbeef);
472 pGetProcessImageFileNameW(hpQI, szImgPathW, ret);
473 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
475 memset(szImgPathW, 0xff, sizeof(szImgPathW));
476 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
477 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
478 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
479 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
482 static void test_GetModuleFileNameEx(void)
484 HMODULE hMod = GetModuleHandle(NULL);
485 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
486 WCHAR buffer[MAX_PATH];
487 DWORD ret;
489 SetLastError(0xdeadbeef);
490 ret = pGetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
491 ok( !ret, "GetModuleFileNameExA succeeded\n" );
492 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
494 SetLastError(0xdeadbeef);
495 ret = pGetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
496 ok( !ret, "GetModuleFileNameExA succeeded\n" );
497 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
499 SetLastError(0xdeadbeef);
500 ret = pGetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
501 ok( !ret, "GetModuleFileNameExA succeeded\n" );
502 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
504 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
505 if(!ret)
506 return;
507 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
508 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
509 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
510 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
512 SetLastError(0xdeadbeef);
513 memset( szModExPath, 0xcc, sizeof(szModExPath) );
514 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
515 ok( ret == 4, "wrong length %u\n", ret );
516 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
517 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
518 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
520 SetLastError(0xdeadbeef);
521 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
522 ok( ret == 0, "wrong length %u\n", ret );
523 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
525 SetLastError(0xdeadbeef);
526 memset( buffer, 0xcc, sizeof(buffer) );
527 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 4 );
528 ok( ret == 4, "wrong length %u\n", ret );
529 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
530 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
531 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
533 SetLastError(0xdeadbeef);
534 buffer[0] = 0xcc;
535 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 0 );
536 ok( ret == 0, "wrong length %u\n", ret );
537 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
538 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
541 static void test_GetModuleBaseName(void)
543 HMODULE hMod = GetModuleHandle(NULL);
544 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
545 DWORD ret;
547 SetLastError(0xdeadbeef);
548 pGetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
549 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
551 SetLastError(0xdeadbeef);
552 pGetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
553 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
555 SetLastError(0xdeadbeef);
556 pGetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
557 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
559 ret = pGetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
560 if(!ret)
561 return;
562 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
563 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
564 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
565 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
568 static void test_ws_functions(void)
570 PSAPI_WS_WATCH_INFORMATION wswi[4096];
571 ULONG_PTR pages[4096];
572 char *addr;
573 unsigned int i;
574 BOOL ret;
576 SetLastError(0xdeadbeef);
577 pEmptyWorkingSet(NULL);
578 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
580 SetLastError(0xdeadbeef);
581 pEmptyWorkingSet(hpSR);
582 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
584 SetLastError(0xdeadbeef);
585 ret = pEmptyWorkingSet(hpAA);
586 ok(ret == 1, "failed with %d\n", GetLastError());
588 SetLastError( 0xdeadbeef );
589 ret = pInitializeProcessForWsWatch( NULL );
590 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
591 if (!ret)
593 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
595 trace( "InitializeProcessForWsWatch not supported\n" );
596 return;
598 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
600 SetLastError(0xdeadbeef);
601 ret = pInitializeProcessForWsWatch(hpAA);
602 ok(ret == 1, "failed with %d\n", GetLastError());
604 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
605 if(!addr)
606 return;
608 *addr = 0; /* make sure it's paged in (needed on wow64) */
609 if(!VirtualLock(addr, 1))
611 trace("locking failed (error=%d) - skipping test\n", GetLastError());
612 goto free_page;
615 SetLastError(0xdeadbeef);
616 ret = pQueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
617 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
618 if(ret == 1)
620 for(i = 0; i < pages[0]; i++)
621 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
623 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
624 goto test_gwsc;
627 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
630 test_gwsc:
631 SetLastError(0xdeadbeef);
632 ret = pGetWsChanges(hpQI, wswi, sizeof(wswi));
633 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
634 if(ret == 1)
636 for(i = 0; wswi[i].FaultingVa; i++)
637 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
639 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
640 goto free_page;
643 todo_wine ok(0, "GetWsChanges didn't find our page\n");
646 free_page:
647 VirtualFree(addr, 0, MEM_RELEASE);
650 START_TEST(psapi_main)
652 HMODULE hpsapi = LoadLibraryA("psapi.dll");
654 if(!hpsapi)
656 win_skip("Could not load psapi.dll\n");
657 return;
660 if(InitFunctionPtrs(hpsapi))
662 DWORD pid = GetCurrentProcessId();
664 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
665 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
666 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
667 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
668 hpAA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
670 if(hpSR && hpQI && hpVR && hpQV && hpAA)
672 test_EnumProcesses();
673 test_EnumProcessModules();
674 test_GetModuleInformation();
675 test_GetProcessMemoryInfo();
676 test_GetMappedFileName();
677 test_GetProcessImageFileName();
678 test_GetModuleFileNameEx();
679 test_GetModuleBaseName();
680 test_ws_functions();
682 CloseHandle(hpSR);
683 CloseHandle(hpQI);
684 CloseHandle(hpVR);
685 CloseHandle(hpQV);
686 CloseHandle(hpAA);
689 FreeLibrary(hpsapi);