415c47ac544379bd180bb952755a9a4a08d2c49e
[reactos.git] / rostests / winetests / psapi / psapi_main.c
1 /*
2 * Unit test suite for PSAPI
3 *
4 * Copyright (C) 2005 Felix Nawothnig
5 * Copyright (C) 2012 Dmitry Timoshkov
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26
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 "psapi.h"
34 #include "wine/test.h"
35
36 #define PSAPI_GET_PROC(func) \
37 p ## func = (void*)GetProcAddress(hpsapi, #func); \
38 if(!p ## func) { \
39 ok(0, "GetProcAddress(%s) failed\n", #func); \
40 FreeLibrary(hpsapi); \
41 return FALSE; \
42 }
43
44 static BOOL (WINAPI *pEmptyWorkingSet)(HANDLE);
45 static BOOL (WINAPI *pEnumProcesses)(DWORD*, DWORD, DWORD*);
46 static BOOL (WINAPI *pEnumProcessModules)(HANDLE, HMODULE*, DWORD, LPDWORD);
47 static DWORD (WINAPI *pGetModuleBaseNameA)(HANDLE, HMODULE, LPSTR, DWORD);
48 static DWORD (WINAPI *pGetModuleFileNameExA)(HANDLE, HMODULE, LPSTR, DWORD);
49 static DWORD (WINAPI *pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD);
50 static BOOL (WINAPI *pGetModuleInformation)(HANDLE, HMODULE, LPMODULEINFO, DWORD);
51 static DWORD (WINAPI *pGetMappedFileNameA)(HANDLE, LPVOID, LPSTR, DWORD);
52 static DWORD (WINAPI *pGetMappedFileNameW)(HANDLE, LPVOID, LPWSTR, DWORD);
53 static DWORD (WINAPI *pGetProcessImageFileNameA)(HANDLE, LPSTR, DWORD);
54 static DWORD (WINAPI *pGetProcessImageFileNameW)(HANDLE, LPWSTR, DWORD);
55 static BOOL (WINAPI *pGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
56 static BOOL (WINAPI *pGetWsChanges)(HANDLE, PPSAPI_WS_WATCH_INFORMATION, DWORD);
57 static BOOL (WINAPI *pInitializeProcessForWsWatch)(HANDLE);
58 static BOOL (WINAPI *pQueryWorkingSet)(HANDLE, PVOID, DWORD);
59 static NTSTATUS (WINAPI *pNtQueryVirtualMemory)(HANDLE, LPCVOID, ULONG, PVOID, SIZE_T, SIZE_T *);
60
61 static BOOL InitFunctionPtrs(HMODULE hpsapi)
62 {
63 PSAPI_GET_PROC(EmptyWorkingSet);
64 PSAPI_GET_PROC(EnumProcessModules);
65 PSAPI_GET_PROC(EnumProcesses);
66 PSAPI_GET_PROC(GetModuleBaseNameA);
67 PSAPI_GET_PROC(GetModuleFileNameExA);
68 PSAPI_GET_PROC(GetModuleFileNameExW);
69 PSAPI_GET_PROC(GetModuleInformation);
70 PSAPI_GET_PROC(GetMappedFileNameA);
71 PSAPI_GET_PROC(GetMappedFileNameW);
72 PSAPI_GET_PROC(GetProcessMemoryInfo);
73 PSAPI_GET_PROC(GetWsChanges);
74 PSAPI_GET_PROC(InitializeProcessForWsWatch);
75 PSAPI_GET_PROC(QueryWorkingSet);
76 /* GetProcessImageFileName is not exported on NT4 */
77 pGetProcessImageFileNameA =
78 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameA");
79 pGetProcessImageFileNameW =
80 (void *)GetProcAddress(hpsapi, "GetProcessImageFileNameW");
81 pNtQueryVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryVirtualMemory");
82 return TRUE;
83 }
84
85 static HANDLE hpSR, hpQI, hpVR, hpQV, hpAA;
86 static const HANDLE hBad = (HANDLE)0xdeadbeef;
87
88 static void test_EnumProcesses(void)
89 {
90 DWORD pid, ret, cbUsed = 0xdeadbeef;
91
92 SetLastError(0xdeadbeef);
93 ret = pEnumProcesses(NULL, 0, &cbUsed);
94 ok(ret == 1, "failed with %d\n", GetLastError());
95 ok(cbUsed == 0, "cbUsed=%d\n", cbUsed);
96
97 SetLastError(0xdeadbeef);
98 ret = pEnumProcesses(&pid, 4, &cbUsed);
99 ok(ret == 1, "failed with %d\n", GetLastError());
100 ok(cbUsed == 4, "cbUsed=%d\n", cbUsed);
101 }
102
103 static void test_EnumProcessModules(void)
104 {
105 HMODULE hMod = GetModuleHandle(NULL);
106 DWORD ret, cbNeeded = 0xdeadbeef;
107
108 win_skip("Stack corruption - ROSTEST-122\n");
109 return;
110
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());
114
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());
118
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());
123
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());
128
129 SetLastError(0xdeadbeef);
130 ret = pEnumProcessModules(hpQV, NULL, 0, &cbNeeded);
131 ok(ret == 1, "failed with %d\n", GetLastError());
132
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.
142 */
143 if (cbNeeded < 4 * sizeof(HMODULE) || cbNeeded > 30 * sizeof(HMODULE))
144 {
145 HMODULE hmods[100];
146 int i;
147 ok(0, "cbNeeded=%d\n", cbNeeded);
148
149 pEnumProcessModules(hpQV, hmods, sizeof(hmods), &cbNeeded);
150 for (i = 0 ; i < cbNeeded/sizeof(*hmods); i++)
151 {
152 char path[1024];
153 GetModuleFileNameA(hmods[i], path, sizeof(path));
154 trace("i=%d hmod=%p path=[%s]\n", i, hmods[i], path);
155 }
156 }
157 }
158
159 static void test_GetModuleInformation(void)
160 {
161 HMODULE hMod = GetModuleHandle(NULL);
162 MODULEINFO info;
163 DWORD ret;
164
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());
168
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());
172
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());
176
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());
180
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);
185 }
186
187 static void test_GetProcessMemoryInfo(void)
188 {
189 PROCESS_MEMORY_COUNTERS pmc;
190 DWORD ret;
191
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());
196
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());
203
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());
208
209 SetLastError(0xdeadbeef);
210 ret = pGetProcessMemoryInfo(hpQI, &pmc, sizeof(pmc));
211 ok(ret == 1, "failed with %d\n", GetLastError());
212 }
213
214 static BOOL nt_get_mapped_file_name(HANDLE process, LPVOID addr, LPWSTR name, DWORD len)
215 {
216 MEMORY_SECTION_NAME *section_name;
217 WCHAR *buf;
218 SIZE_T buf_len, ret_len;
219 NTSTATUS status;
220
221 if (!pNtQueryVirtualMemory) return FALSE;
222
223 buf_len = len * sizeof(WCHAR) + sizeof(MEMORY_SECTION_NAME);
224 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buf_len);
225
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;
232
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));
242
243 memcpy(name, section_name->SectionFileName.Buffer, section_name->SectionFileName.MaximumLength);
244 HeapFree(GetProcessHeap(), 0, buf);
245 return TRUE;
246 }
247
248 static void test_GetMappedFileName(void)
249 {
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;
257
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());
263
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());
269
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)
276 {
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)
284 {
285 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
286 ok(!strcmp(strrchr(szModPath, '\\'), szMapBaseName),
287 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath, szMapBaseName);
288 }
289 }
290
291 GetTempPath(MAX_PATH, temp_path);
292 GetTempFileName(temp_path, "map", 0, file_name);
293
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);
301
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);
307
308 SetLastError(0xdeadbeef);
309 hmap = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_COMMIT, 0, 0, NULL);
310 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
311
312 SetLastError(0xdeadbeef);
313 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
314 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
315
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 || GetLastError() == ERROR_INSUFFICIENT_BUFFER,
321 "wrong error %d\n", GetLastError());
322
323 SetLastError(0xdeadbeef);
324 ret = pGetMappedFileNameA(GetCurrentProcess(), base, 0, sizeof(map_name));
325 ok(!ret, "GetMappedFileName should fail\n");
326 todo_wine
327 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
328
329 SetLastError(0xdeadbeef);
330 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, 1);
331 todo_wine
332 ok(ret == 1, "GetMappedFileName error %d\n", GetLastError());
333 ok(!map_name[0] || broken(map_name[0] == device_name[0]) /* before win2k */, "expected 0, got %c\n", map_name[0]);
334
335 SetLastError(0xdeadbeef);
336 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
337 todo_wine {
338 ok(ret, "GetMappedFileName error %d\n", GetLastError());
339 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
340 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
341 }
342
343 SetLastError(0xdeadbeef);
344 ret = pGetMappedFileNameW(GetCurrentProcess(), base, map_nameW, sizeof(map_nameW)/sizeof(map_nameW[0]));
345 todo_wine {
346 ok(ret, "GetMappedFileNameW error %d\n", GetLastError());
347 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
348 }
349 if (nt_get_mapped_file_name(GetCurrentProcess(), base, nt_map_name, sizeof(nt_map_name)/sizeof(nt_map_name[0])))
350 {
351 ok(memcmp(map_nameW, nt_map_name, lstrlenW(map_nameW)) == 0, "map name does not start with a device name: %s\n", map_name);
352 WideCharToMultiByte(CP_ACP, 0, map_nameW, -1, map_name, MAX_PATH, NULL, NULL);
353 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
354 }
355
356 SetLastError(0xdeadbeef);
357 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x2000, map_name, sizeof(map_name));
358 todo_wine {
359 ok(ret, "GetMappedFileName error %d\n", GetLastError());
360 ok(ret > strlen(device_name), "map_name should be longer than device_name\n");
361 ok(memcmp(map_name, device_name, strlen(device_name)) == 0, "map name does not start with a device name: %s\n", map_name);
362 }
363
364 SetLastError(0xdeadbeef);
365 ret = pGetMappedFileNameA(GetCurrentProcess(), base + 0x4000, map_name, sizeof(map_name));
366 ok(!ret, "GetMappedFileName should fail\n");
367 todo_wine
368 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
369
370 SetLastError(0xdeadbeef);
371 ret = pGetMappedFileNameA(GetCurrentProcess(), NULL, map_name, sizeof(map_name));
372 ok(!ret, "GetMappedFileName should fail\n");
373 todo_wine
374 ok(GetLastError() == ERROR_UNEXP_NET_ERR, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
375
376 SetLastError(0xdeadbeef);
377 ret = pGetMappedFileNameA(0, base, map_name, sizeof(map_name));
378 ok(!ret, "GetMappedFileName should fail\n");
379 todo_wine
380 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
381
382 UnmapViewOfFile(base);
383 CloseHandle(hmap);
384 CloseHandle(hfile);
385 DeleteFile(file_name);
386
387 SetLastError(0xdeadbeef);
388 hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READONLY | SEC_COMMIT, 0, 4096, NULL);
389 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
390
391 SetLastError(0xdeadbeef);
392 base = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0);
393 ok(base != NULL, "MapViewOfFile error %d\n", GetLastError());
394
395 SetLastError(0xdeadbeef);
396 ret = pGetMappedFileNameA(GetCurrentProcess(), base, map_name, sizeof(map_name));
397 ok(!ret, "GetMappedFileName should fail\n");
398 todo_wine
399 ok(GetLastError() == ERROR_FILE_INVALID, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
400
401 UnmapViewOfFile(base);
402 CloseHandle(hmap);
403 }
404
405 static void test_GetProcessImageFileName(void)
406 {
407 HMODULE hMod = GetModuleHandle(NULL);
408 char szImgPath[MAX_PATH], szMapPath[MAX_PATH];
409 WCHAR szImgPathW[MAX_PATH];
410 DWORD ret, ret1;
411
412 if(pGetProcessImageFileNameA == NULL)
413 return;
414
415 /* This function is available on WinXP+ only */
416 SetLastError(0xdeadbeef);
417 if(!pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath)))
418 {
419 if(GetLastError() == ERROR_INVALID_FUNCTION) {
420 win_skip("GetProcessImageFileName not implemented\n");
421 return;
422 }
423
424 if(GetLastError() == 0xdeadbeef)
425 todo_wine ok(0, "failed without error code\n");
426 else
427 todo_wine ok(0, "failed with %d\n", GetLastError());
428 }
429
430 SetLastError(0xdeadbeef);
431 pGetProcessImageFileNameA(NULL, szImgPath, sizeof(szImgPath));
432 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
433
434 SetLastError(0xdeadbeef);
435 pGetProcessImageFileNameA(hpSR, szImgPath, sizeof(szImgPath));
436 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
437
438 SetLastError(0xdeadbeef);
439 pGetProcessImageFileNameA(hpQI, szImgPath, 0);
440 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
441
442 ret = pGetProcessImageFileNameA(hpQI, szImgPath, sizeof(szImgPath));
443 ret1 = pGetMappedFileNameA(hpQV, hMod, szMapPath, sizeof(szMapPath));
444 if(ret && ret1)
445 {
446 /* Windows returns 2*strlen-1 */
447 todo_wine ok(ret >= strlen(szImgPath), "szImgPath=\"%s\" ret=%d\n", szImgPath, ret);
448 todo_wine ok(!strcmp(szImgPath, szMapPath), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath, szMapPath);
449 }
450
451 SetLastError(0xdeadbeef);
452 pGetProcessImageFileNameW(NULL, szImgPathW, sizeof(szImgPathW));
453 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
454
455 /* no information about correct buffer size returned: */
456 SetLastError(0xdeadbeef);
457 pGetProcessImageFileNameW(hpQI, szImgPathW, 0);
458 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
459
460 SetLastError(0xdeadbeef);
461 pGetProcessImageFileNameW(hpQI, NULL, 0);
462 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
463
464 /* correct call */
465 memset(szImgPathW, 0xff, sizeof(szImgPathW));
466 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, sizeof(szImgPathW)/sizeof(WCHAR));
467 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
468 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
469 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
470
471 /* boundary values of 'size' */
472 SetLastError(0xdeadbeef);
473 pGetProcessImageFileNameW(hpQI, szImgPathW, ret);
474 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
475
476 memset(szImgPathW, 0xff, sizeof(szImgPathW));
477 ret = pGetProcessImageFileNameW(hpQI, szImgPathW, ret + 1);
478 ok(ret > 0, "GetProcessImageFileNameW should have succeeded.\n");
479 ok(szImgPathW[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
480 ok(lstrlenW(szImgPathW) == ret, "Expected length to be %d, got %d\n", ret, lstrlenW(szImgPathW));
481 }
482
483 static void test_GetModuleFileNameEx(void)
484 {
485 HMODULE hMod = GetModuleHandle(NULL);
486 char szModExPath[MAX_PATH+1], szModPath[MAX_PATH+1];
487 WCHAR buffer[MAX_PATH];
488 DWORD ret;
489
490 SetLastError(0xdeadbeef);
491 ret = pGetModuleFileNameExA(NULL, hMod, szModExPath, sizeof(szModExPath));
492 ok( !ret, "GetModuleFileNameExA succeeded\n" );
493 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
494
495 SetLastError(0xdeadbeef);
496 ret = pGetModuleFileNameExA(hpQI, hMod, szModExPath, sizeof(szModExPath));
497 ok( !ret, "GetModuleFileNameExA succeeded\n" );
498 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
499
500 SetLastError(0xdeadbeef);
501 ret = pGetModuleFileNameExA(hpQV, hBad, szModExPath, sizeof(szModExPath));
502 ok( !ret, "GetModuleFileNameExA succeeded\n" );
503 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
504
505 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, sizeof(szModExPath));
506 if(!ret)
507 return;
508 ok(ret == strlen(szModExPath), "szModExPath=\"%s\" ret=%d\n", szModExPath, ret);
509 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
510 ok(!strncmp(szModExPath, szModPath, MAX_PATH),
511 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath, szModPath);
512
513 SetLastError(0xdeadbeef);
514 memset( szModExPath, 0xcc, sizeof(szModExPath) );
515 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 4 );
516 ok( ret == 4, "wrong length %u\n", ret );
517 ok( broken(szModExPath[3]) /*w2kpro*/ || strlen(szModExPath) == 3,
518 "szModExPath=\"%s\" ret=%d\n", szModExPath, ret );
519 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
520
521 SetLastError(0xdeadbeef);
522 ret = pGetModuleFileNameExA(hpQV, NULL, szModExPath, 0 );
523 ok( ret == 0, "wrong length %u\n", ret );
524 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %d\n", GetLastError());
525
526 SetLastError(0xdeadbeef);
527 memset( buffer, 0xcc, sizeof(buffer) );
528 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 4 );
529 ok( ret == 4, "wrong length %u\n", ret );
530 ok( broken(buffer[3]) /*w2kpro*/ || lstrlenW(buffer) == 3,
531 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer), ret );
532 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
533
534 SetLastError(0xdeadbeef);
535 buffer[0] = 0xcc;
536 ret = pGetModuleFileNameExW(hpQV, NULL, buffer, 0 );
537 ok( ret == 0, "wrong length %u\n", ret );
538 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
539 ok( buffer[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer) );
540 }
541
542 static void test_GetModuleBaseName(void)
543 {
544 HMODULE hMod = GetModuleHandle(NULL);
545 char szModPath[MAX_PATH], szModBaseName[MAX_PATH];
546 DWORD ret;
547
548 SetLastError(0xdeadbeef);
549 pGetModuleBaseNameA(NULL, hMod, szModBaseName, sizeof(szModBaseName));
550 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
551
552 SetLastError(0xdeadbeef);
553 pGetModuleBaseNameA(hpQI, hMod, szModBaseName, sizeof(szModBaseName));
554 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
555
556 SetLastError(0xdeadbeef);
557 pGetModuleBaseNameA(hpQV, hBad, szModBaseName, sizeof(szModBaseName));
558 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
559
560 ret = pGetModuleBaseNameA(hpQV, NULL, szModBaseName, sizeof(szModBaseName));
561 if(!ret)
562 return;
563 ok(ret == strlen(szModBaseName), "szModBaseName=\"%s\" ret=%d\n", szModBaseName, ret);
564 GetModuleFileNameA(NULL, szModPath, sizeof(szModPath));
565 ok(!strcmp(strrchr(szModPath, '\\') + 1, szModBaseName),
566 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath, szModBaseName);
567 }
568
569 static void test_ws_functions(void)
570 {
571 PSAPI_WS_WATCH_INFORMATION wswi[4096];
572 ULONG_PTR pages[4096];
573 char *addr;
574 unsigned int i;
575 BOOL ret;
576
577 SetLastError(0xdeadbeef);
578 pEmptyWorkingSet(NULL);
579 todo_wine ok(GetLastError() == ERROR_INVALID_HANDLE, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
580
581 SetLastError(0xdeadbeef);
582 pEmptyWorkingSet(hpSR);
583 todo_wine ok(GetLastError() == ERROR_ACCESS_DENIED, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
584
585 SetLastError(0xdeadbeef);
586 ret = pEmptyWorkingSet(hpAA);
587 ok(ret == 1, "failed with %d\n", GetLastError());
588
589 SetLastError( 0xdeadbeef );
590 ret = pInitializeProcessForWsWatch( NULL );
591 todo_wine ok( !ret, "InitializeProcessForWsWatch succeeded\n" );
592 if (!ret)
593 {
594 if (GetLastError() == ERROR_INVALID_FUNCTION) /* not supported on xp in wow64 mode */
595 {
596 trace( "InitializeProcessForWsWatch not supported\n" );
597 return;
598 }
599 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
600 }
601 SetLastError(0xdeadbeef);
602 ret = pInitializeProcessForWsWatch(hpAA);
603 ok(ret == 1, "failed with %d\n", GetLastError());
604
605 addr = VirtualAlloc(NULL, 1, MEM_COMMIT, PAGE_READWRITE);
606 if(!addr)
607 return;
608
609 *addr = 0; /* make sure it's paged in (needed on wow64) */
610 if(!VirtualLock(addr, 1))
611 {
612 trace("locking failed (error=%d) - skipping test\n", GetLastError());
613 goto free_page;
614 }
615
616 SetLastError(0xdeadbeef);
617 ret = pQueryWorkingSet(hpQI, pages, 4096 * sizeof(ULONG_PTR));
618 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
619 if(ret == 1)
620 {
621 for(i = 0; i < pages[0]; i++)
622 if((pages[i+1] & ~0xfffL) == (ULONG_PTR)addr)
623 {
624 todo_wine ok(ret == 1, "QueryWorkingSet found our page\n");
625 goto test_gwsc;
626 }
627
628 todo_wine ok(0, "QueryWorkingSet didn't find our page\n");
629 }
630
631 test_gwsc:
632 SetLastError(0xdeadbeef);
633 ret = pGetWsChanges(hpQI, wswi, sizeof(wswi));
634 todo_wine ok(ret == 1, "failed with %d\n", GetLastError());
635 if(ret == 1)
636 {
637 for(i = 0; wswi[i].FaultingVa; i++)
638 if(((ULONG_PTR)wswi[i].FaultingVa & ~0xfffL) == (ULONG_PTR)addr)
639 {
640 todo_wine ok(ret == 1, "GetWsChanges found our page\n");
641 goto free_page;
642 }
643
644 todo_wine ok(0, "GetWsChanges didn't find our page\n");
645 }
646
647 free_page:
648 VirtualFree(addr, 0, MEM_RELEASE);
649 }
650
651 START_TEST(psapi_main)
652 {
653 HMODULE hpsapi = LoadLibraryA("psapi.dll");
654
655 if(!hpsapi)
656 {
657 win_skip("Could not load psapi.dll\n");
658 return;
659 }
660
661 if(InitFunctionPtrs(hpsapi))
662 {
663 DWORD pid = GetCurrentProcessId();
664
665 hpSR = OpenProcess(STANDARD_RIGHTS_REQUIRED, FALSE, pid);
666 hpQI = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
667 hpVR = OpenProcess(PROCESS_VM_READ, FALSE, pid);
668 hpQV = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
669 hpAA = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
670
671 if(hpSR && hpQI && hpVR && hpQV && hpAA)
672 {
673 test_EnumProcesses();
674 test_EnumProcessModules();
675 test_GetModuleInformation();
676 test_GetProcessMemoryInfo();
677 test_GetMappedFileName();
678 test_GetProcessImageFileName();
679 test_GetModuleFileNameEx();
680 test_GetModuleBaseName();
681 test_ws_functions();
682 }
683 CloseHandle(hpSR);
684 CloseHandle(hpQI);
685 CloseHandle(hpVR);
686 CloseHandle(hpQV);
687 CloseHandle(hpAA);
688 }
689
690 FreeLibrary(hpsapi);
691 }