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 /* 0x0600 makes PROCESS_ALL_ACCESS not compatible with pre-Vista versions */
23 #define _WIN32_WINNT 0x0500
28 #define WIN32_NO_STATUS
37 #include "wine/test.h"
39 #define PSAPI_GET_PROC(func) \
40 p ## func = (void*)GetProcAddress(hpsapi, #func); \
42 ok(0, "GetProcAddress(%s) failed\n", #func); \
43 FreeLibrary(hpsapi); \
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(GetModuleHandleA("ntdll.dll"), "NtQueryVirtualMemory");
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
= GetModuleHandleA(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
);
137 ok(hMod
== GetModuleHandleA(NULL
),
138 "hMod=%p GetModuleHandleA(NULL)=%p\n", hMod
, GetModuleHandleA(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
143 if (cbNeeded
< 4 * sizeof(HMODULE
) || cbNeeded
> 30 * sizeof(HMODULE
))
147 ok(0, "cbNeeded=%d\n", cbNeeded
);
149 pEnumProcessModules(hpQV
, hmods
, sizeof(hmods
), &cbNeeded
);
150 for (i
= 0 ; i
< cbNeeded
/sizeof(*hmods
); i
++)
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
= GetModuleHandleA(NULL
);
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
;
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
));
200 ok(!ret
, "GetProcessMemoryInfo should fail\n");
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
;
218 SIZE_T buf_len
, ret_len
;
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
);
229 ok(!status
, "NtQueryVirtualMemory error %x\n", status
);
230 /* FIXME: remove once Wine is fixed */
233 HeapFree(GetProcessHeap(), 0, buf
);
237 section_name
= (MEMORY_SECTION_NAME
*)buf
;
238 ok(ret_len
== section_name
->SectionFileName
.MaximumLength
+ sizeof(*section_name
), "got %lu, %u\n",
239 ret_len
, section_name
->SectionFileName
.MaximumLength
);
240 ok((char *)section_name
->SectionFileName
.Buffer
== (char *)section_name
+ sizeof(*section_name
), "got %p, %p\n",
241 section_name
, section_name
->SectionFileName
.Buffer
);
242 ok(section_name
->SectionFileName
.MaximumLength
== section_name
->SectionFileName
.Length
+ sizeof(WCHAR
), "got %u, %u\n",
243 section_name
->SectionFileName
.MaximumLength
, section_name
->SectionFileName
.Length
);
244 ok(section_name
->SectionFileName
.Length
== lstrlenW(section_name
->SectionFileName
.Buffer
) * sizeof(WCHAR
), "got %u, %u\n",
245 section_name
->SectionFileName
.Length
, lstrlenW(section_name
->SectionFileName
.Buffer
));
247 memcpy(name
, section_name
->SectionFileName
.Buffer
, section_name
->SectionFileName
.MaximumLength
);
248 HeapFree(GetProcessHeap(), 0, buf
);
252 static void test_GetMappedFileName(void)
254 HMODULE hMod
= GetModuleHandleA(NULL
);
255 char szMapPath
[MAX_PATH
], szModPath
[MAX_PATH
], *szMapBaseName
;
258 char temp_path
[MAX_PATH
], file_name
[MAX_PATH
], map_name
[MAX_PATH
], device_name
[MAX_PATH
], drive
[3];
259 WCHAR map_nameW
[MAX_PATH
], nt_map_name
[MAX_PATH
];
262 SetLastError(0xdeadbeef);
263 ret
= pGetMappedFileNameA(NULL
, hMod
, szMapPath
, sizeof(szMapPath
));
264 ok(!ret
, "GetMappedFileName should fail\n");
266 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
268 SetLastError(0xdeadbeef);
269 ret
= pGetMappedFileNameA(hpSR
, hMod
, szMapPath
, sizeof(szMapPath
));
270 ok(!ret
, "GetMappedFileName should fail\n");
272 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
274 SetLastError( 0xdeadbeef );
275 ret
= pGetMappedFileNameA(hpQI
, hMod
, szMapPath
, sizeof(szMapPath
));
277 ok( ret
|| broken(GetLastError() == ERROR_UNEXP_NET_ERR
), /* win2k */
278 "GetMappedFileNameA failed with error %u\n", GetLastError() );
281 ok(ret
== strlen(szMapPath
), "szMapPath=\"%s\" ret=%d\n", szMapPath
, ret
);
283 ok(szMapPath
[0] == '\\', "szMapPath=\"%s\"\n", szMapPath
);
284 szMapBaseName
= strrchr(szMapPath
, '\\'); /* That's close enough for us */
286 ok(szMapBaseName
&& *szMapBaseName
, "szMapPath=\"%s\"\n", szMapPath
);
289 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
290 ok(!strcmp(strrchr(szModPath
, '\\'), szMapBaseName
),
291 "szModPath=\"%s\" szMapBaseName=\"%s\"\n", szModPath
, szMapBaseName
);
295 GetTempPathA(MAX_PATH
, temp_path
);
296 GetTempFileNameA(temp_path
, "map", 0, file_name
);
298 drive
[0] = file_name
[0];
301 SetLastError(0xdeadbeef);
302 ret
= QueryDosDeviceA(drive
, device_name
, sizeof(device_name
));
303 ok(ret
, "QueryDosDeviceA error %d\n", GetLastError());
304 trace("%s -> %s\n", drive
, device_name
);
306 SetLastError(0xdeadbeef);
307 hfile
= CreateFileA(file_name
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
308 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFileA(%s) error %d\n", file_name
, GetLastError());
309 SetFilePointer(hfile
, 0x4000, NULL
, FILE_BEGIN
);
312 SetLastError(0xdeadbeef);
313 hmap
= CreateFileMappingA(hfile
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 0, NULL
);
314 ok(hmap
!= 0, "CreateFileMappingA error %d\n", GetLastError());
316 SetLastError(0xdeadbeef);
317 base
= MapViewOfFile(hmap
, FILE_MAP_READ
, 0, 0, 0);
318 ok(base
!= NULL
, "MapViewOfFile error %d\n", GetLastError());
320 SetLastError(0xdeadbeef);
321 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
, map_name
, 0);
322 ok(!ret
, "GetMappedFileName should fail\n");
324 ok(GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_INSUFFICIENT_BUFFER
,
325 "wrong error %d\n", GetLastError());
327 SetLastError(0xdeadbeef);
328 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
, 0, sizeof(map_name
));
329 ok(!ret
, "GetMappedFileName should fail\n");
331 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
333 SetLastError(0xdeadbeef);
334 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
, map_name
, 1);
336 ok(ret
== 1, "GetMappedFileName error %d\n", GetLastError());
337 ok(!map_name
[0] || broken(map_name
[0] == device_name
[0]) /* before win2k */, "expected 0, got %c\n", map_name
[0]);
339 SetLastError(0xdeadbeef);
340 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
, map_name
, sizeof(map_name
));
342 ok(ret
, "GetMappedFileName error %d\n", GetLastError());
343 ok(ret
> strlen(device_name
), "map_name should be longer than device_name\n");
344 ok(memcmp(map_name
, device_name
, strlen(device_name
)) == 0, "map name does not start with a device name: %s\n", map_name
);
347 SetLastError(0xdeadbeef);
348 ret
= pGetMappedFileNameW(GetCurrentProcess(), base
, map_nameW
, sizeof(map_nameW
)/sizeof(map_nameW
[0]));
350 ok(ret
, "GetMappedFileNameW error %d\n", GetLastError());
351 ok(ret
> strlen(device_name
), "map_name should be longer than device_name\n");
353 if (nt_get_mapped_file_name(GetCurrentProcess(), base
, nt_map_name
, sizeof(nt_map_name
)/sizeof(nt_map_name
[0])))
355 ok(memcmp(map_nameW
, nt_map_name
, lstrlenW(map_nameW
)) == 0, "map name does not start with a device name: %s\n", map_name
);
356 WideCharToMultiByte(CP_ACP
, 0, map_nameW
, -1, map_name
, MAX_PATH
, NULL
, NULL
);
357 ok(memcmp(map_name
, device_name
, strlen(device_name
)) == 0, "map name does not start with a device name: %s\n", map_name
);
360 SetLastError(0xdeadbeef);
361 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
+ 0x2000, map_name
, sizeof(map_name
));
363 ok(ret
, "GetMappedFileName error %d\n", GetLastError());
364 ok(ret
> strlen(device_name
), "map_name should be longer than device_name\n");
365 ok(memcmp(map_name
, device_name
, strlen(device_name
)) == 0, "map name does not start with a device name: %s\n", map_name
);
368 SetLastError(0xdeadbeef);
369 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
+ 0x4000, map_name
, sizeof(map_name
));
370 ok(!ret
, "GetMappedFileName should fail\n");
372 ok(GetLastError() == ERROR_UNEXP_NET_ERR
, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
374 SetLastError(0xdeadbeef);
375 ret
= pGetMappedFileNameA(GetCurrentProcess(), NULL
, map_name
, sizeof(map_name
));
376 ok(!ret
, "GetMappedFileName should fail\n");
378 ok(GetLastError() == ERROR_UNEXP_NET_ERR
, "expected ERROR_UNEXP_NET_ERR, got %d\n", GetLastError());
380 SetLastError(0xdeadbeef);
381 ret
= pGetMappedFileNameA(0, base
, map_name
, sizeof(map_name
));
382 ok(!ret
, "GetMappedFileName should fail\n");
384 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
386 UnmapViewOfFile(base
);
389 DeleteFileA(file_name
);
391 SetLastError(0xdeadbeef);
392 hmap
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READONLY
| SEC_COMMIT
, 0, 4096, NULL
);
393 ok(hmap
!= 0, "CreateFileMappingA error %d\n", GetLastError());
395 SetLastError(0xdeadbeef);
396 base
= MapViewOfFile(hmap
, FILE_MAP_READ
, 0, 0, 0);
397 ok(base
!= NULL
, "MapViewOfFile error %d\n", GetLastError());
399 SetLastError(0xdeadbeef);
400 ret
= pGetMappedFileNameA(GetCurrentProcess(), base
, map_name
, sizeof(map_name
));
401 ok(!ret
, "GetMappedFileName should fail\n");
403 ok(GetLastError() == ERROR_FILE_INVALID
, "expected ERROR_FILE_INVALID, got %d\n", GetLastError());
405 UnmapViewOfFile(base
);
409 static void test_GetProcessImageFileName(void)
411 HMODULE hMod
= GetModuleHandleA(NULL
);
412 char szImgPath
[MAX_PATH
], szMapPath
[MAX_PATH
];
413 WCHAR szImgPathW
[MAX_PATH
];
416 if(pGetProcessImageFileNameA
== NULL
)
419 /* This function is available on WinXP+ only */
420 SetLastError(0xdeadbeef);
421 if(!pGetProcessImageFileNameA(hpQI
, szImgPath
, sizeof(szImgPath
)))
423 if(GetLastError() == ERROR_INVALID_FUNCTION
) {
424 win_skip("GetProcessImageFileName not implemented\n");
428 if(GetLastError() == 0xdeadbeef)
429 todo_wine
ok(0, "failed without error code\n");
431 todo_wine
ok(0, "failed with %d\n", GetLastError());
434 SetLastError(0xdeadbeef);
435 pGetProcessImageFileNameA(NULL
, szImgPath
, sizeof(szImgPath
));
436 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
438 SetLastError(0xdeadbeef);
439 pGetProcessImageFileNameA(hpSR
, szImgPath
, sizeof(szImgPath
));
440 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
442 SetLastError(0xdeadbeef);
443 pGetProcessImageFileNameA(hpQI
, szImgPath
, 0);
444 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
446 ret
= pGetProcessImageFileNameA(hpQI
, szImgPath
, sizeof(szImgPath
));
447 ret1
= pGetMappedFileNameA(hpQV
, hMod
, szMapPath
, sizeof(szMapPath
));
450 /* Windows returns 2*strlen-1 */
451 todo_wine
ok(ret
>= strlen(szImgPath
), "szImgPath=\"%s\" ret=%d\n", szImgPath
, ret
);
452 todo_wine
ok(!strcmp(szImgPath
, szMapPath
), "szImgPath=\"%s\" szMapPath=\"%s\"\n", szImgPath
, szMapPath
);
455 SetLastError(0xdeadbeef);
456 pGetProcessImageFileNameW(NULL
, szImgPathW
, sizeof(szImgPathW
));
457 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
459 /* no information about correct buffer size returned: */
460 SetLastError(0xdeadbeef);
461 pGetProcessImageFileNameW(hpQI
, szImgPathW
, 0);
462 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
464 SetLastError(0xdeadbeef);
465 pGetProcessImageFileNameW(hpQI
, NULL
, 0);
466 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
469 memset(szImgPathW
, 0xff, sizeof(szImgPathW
));
470 ret
= pGetProcessImageFileNameW(hpQI
, szImgPathW
, sizeof(szImgPathW
)/sizeof(WCHAR
));
471 ok(ret
> 0, "GetProcessImageFileNameW should have succeeded.\n");
472 ok(szImgPathW
[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
473 ok(lstrlenW(szImgPathW
) == ret
, "Expected length to be %d, got %d\n", ret
, lstrlenW(szImgPathW
));
475 /* boundary values of 'size' */
476 SetLastError(0xdeadbeef);
477 pGetProcessImageFileNameW(hpQI
, szImgPathW
, ret
);
478 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER
, "expected error=ERROR_INSUFFICIENT_BUFFER but got %d\n", GetLastError());
480 memset(szImgPathW
, 0xff, sizeof(szImgPathW
));
481 ret
= pGetProcessImageFileNameW(hpQI
, szImgPathW
, ret
+ 1);
482 ok(ret
> 0, "GetProcessImageFileNameW should have succeeded.\n");
483 ok(szImgPathW
[0] == '\\', "GetProcessImageFileNameW should have returned an NT path.\n");
484 ok(lstrlenW(szImgPathW
) == ret
, "Expected length to be %d, got %d\n", ret
, lstrlenW(szImgPathW
));
487 static void test_GetModuleFileNameEx(void)
489 HMODULE hMod
= GetModuleHandleA(NULL
);
490 char szModExPath
[MAX_PATH
+1], szModPath
[MAX_PATH
+1];
491 WCHAR buffer
[MAX_PATH
];
494 SetLastError(0xdeadbeef);
495 ret
= pGetModuleFileNameExA(NULL
, hMod
, szModExPath
, sizeof(szModExPath
));
496 ok( !ret
, "GetModuleFileNameExA succeeded\n" );
497 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
499 SetLastError(0xdeadbeef);
500 ret
= pGetModuleFileNameExA(hpQI
, hMod
, szModExPath
, sizeof(szModExPath
));
501 ok( !ret
, "GetModuleFileNameExA succeeded\n" );
502 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
504 SetLastError(0xdeadbeef);
505 ret
= pGetModuleFileNameExA(hpQV
, hBad
, szModExPath
, sizeof(szModExPath
));
506 ok( !ret
, "GetModuleFileNameExA succeeded\n" );
507 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
509 ret
= pGetModuleFileNameExA(hpQV
, NULL
, szModExPath
, sizeof(szModExPath
));
512 ok(ret
== strlen(szModExPath
), "szModExPath=\"%s\" ret=%d\n", szModExPath
, ret
);
513 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
514 ok(!strncmp(szModExPath
, szModPath
, MAX_PATH
),
515 "szModExPath=\"%s\" szModPath=\"%s\"\n", szModExPath
, szModPath
);
517 SetLastError(0xdeadbeef);
518 memset( szModExPath
, 0xcc, sizeof(szModExPath
) );
519 ret
= pGetModuleFileNameExA(hpQV
, NULL
, szModExPath
, 4 );
520 ok( ret
== 4, "wrong length %u\n", ret
);
521 ok( broken(szModExPath
[3]) /*w2kpro*/ || strlen(szModExPath
) == 3,
522 "szModExPath=\"%s\" ret=%d\n", szModExPath
, ret
);
523 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
525 SetLastError(0xdeadbeef);
526 ret
= pGetModuleFileNameExA(hpQV
, NULL
, szModExPath
, 0 );
527 ok( ret
== 0, "wrong length %u\n", ret
);
528 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "got error %d\n", GetLastError());
530 SetLastError(0xdeadbeef);
531 memset( buffer
, 0xcc, sizeof(buffer
) );
532 ret
= pGetModuleFileNameExW(hpQV
, NULL
, buffer
, 4 );
533 ok( ret
== 4, "wrong length %u\n", ret
);
534 ok( broken(buffer
[3]) /*w2kpro*/ || lstrlenW(buffer
) == 3,
535 "buffer=%s ret=%d\n", wine_dbgstr_w(buffer
), ret
);
536 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
538 SetLastError(0xdeadbeef);
540 ret
= pGetModuleFileNameExW(hpQV
, NULL
, buffer
, 0 );
541 ok( ret
== 0, "wrong length %u\n", ret
);
542 ok(GetLastError() == 0xdeadbeef, "got error %d\n", GetLastError());
543 ok( buffer
[0] == 0xcc, "buffer modified %s\n", wine_dbgstr_w(buffer
) );
546 static void test_GetModuleBaseName(void)
548 HMODULE hMod
= GetModuleHandleA(NULL
);
549 char szModPath
[MAX_PATH
], szModBaseName
[MAX_PATH
];
552 SetLastError(0xdeadbeef);
553 pGetModuleBaseNameA(NULL
, hMod
, szModBaseName
, sizeof(szModBaseName
));
554 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
556 SetLastError(0xdeadbeef);
557 pGetModuleBaseNameA(hpQI
, hMod
, szModBaseName
, sizeof(szModBaseName
));
558 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
560 SetLastError(0xdeadbeef);
561 pGetModuleBaseNameA(hpQV
, hBad
, szModBaseName
, sizeof(szModBaseName
));
562 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
564 ret
= pGetModuleBaseNameA(hpQV
, NULL
, szModBaseName
, sizeof(szModBaseName
));
567 ok(ret
== strlen(szModBaseName
), "szModBaseName=\"%s\" ret=%d\n", szModBaseName
, ret
);
568 GetModuleFileNameA(NULL
, szModPath
, sizeof(szModPath
));
569 ok(!strcmp(strrchr(szModPath
, '\\') + 1, szModBaseName
),
570 "szModPath=\"%s\" szModBaseName=\"%s\"\n", szModPath
, szModBaseName
);
573 static void test_ws_functions(void)
575 PSAPI_WS_WATCH_INFORMATION wswi
[4096];
576 ULONG_PTR pages
[4096];
581 SetLastError(0xdeadbeef);
582 pEmptyWorkingSet(NULL
);
583 todo_wine
ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected error=ERROR_INVALID_HANDLE but got %d\n", GetLastError());
585 SetLastError(0xdeadbeef);
586 pEmptyWorkingSet(hpSR
);
587 todo_wine
ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected error=ERROR_ACCESS_DENIED but got %d\n", GetLastError());
589 SetLastError(0xdeadbeef);
590 ret
= pEmptyWorkingSet(hpAA
);
591 ok(ret
== 1, "failed with %d\n", GetLastError());
593 SetLastError( 0xdeadbeef );
594 ret
= pInitializeProcessForWsWatch( NULL
);
595 todo_wine
ok( !ret
, "InitializeProcessForWsWatch succeeded\n" );
598 if (GetLastError() == ERROR_INVALID_FUNCTION
) /* not supported on xp in wow64 mode */
600 trace( "InitializeProcessForWsWatch not supported\n" );
603 ok( GetLastError() == ERROR_INVALID_HANDLE
, "wrong error %u\n", GetLastError() );
605 SetLastError(0xdeadbeef);
606 ret
= pInitializeProcessForWsWatch(hpAA
);
607 ok(ret
== 1, "failed with %d\n", GetLastError());
609 addr
= VirtualAlloc(NULL
, 1, MEM_COMMIT
, PAGE_READWRITE
);
613 *addr
= 0; /* make sure it's paged in (needed on wow64) */
614 if(!VirtualLock(addr
, 1))
616 trace("locking failed (error=%d) - skipping test\n", GetLastError());
620 SetLastError(0xdeadbeef);
621 ret
= pQueryWorkingSet(hpQI
, pages
, 4096 * sizeof(ULONG_PTR
));
622 todo_wine
ok(ret
== 1, "failed with %d\n", GetLastError());
625 for(i
= 0; i
< pages
[0]; i
++)
626 if((pages
[i
+1] & ~0xfffL
) == (ULONG_PTR
)addr
)
628 todo_wine
ok(ret
== 1, "QueryWorkingSet found our page\n");
632 todo_wine
ok(0, "QueryWorkingSet didn't find our page\n");
636 SetLastError(0xdeadbeef);
637 ret
= pGetWsChanges(hpQI
, wswi
, sizeof(wswi
));
638 todo_wine
ok(ret
== 1, "failed with %d\n", GetLastError());
641 for(i
= 0; wswi
[i
].FaultingVa
; i
++)
642 if(((ULONG_PTR
)wswi
[i
].FaultingVa
& ~0xfffL
) == (ULONG_PTR
)addr
)
644 todo_wine
ok(ret
== 1, "GetWsChanges found our page\n");
648 todo_wine
ok(0, "GetWsChanges didn't find our page\n");
652 VirtualFree(addr
, 0, MEM_RELEASE
);
655 START_TEST(psapi_main
)
657 HMODULE hpsapi
= LoadLibraryA("psapi.dll");
661 win_skip("Could not load psapi.dll\n");
665 if(InitFunctionPtrs(hpsapi
))
667 DWORD pid
= GetCurrentProcessId();
669 hpSR
= OpenProcess(STANDARD_RIGHTS_REQUIRED
, FALSE
, pid
);
670 hpQI
= OpenProcess(PROCESS_QUERY_INFORMATION
, FALSE
, pid
);
671 hpVR
= OpenProcess(PROCESS_VM_READ
, FALSE
, pid
);
672 hpQV
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
673 hpAA
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
, pid
);
675 if(hpSR
&& hpQI
&& hpVR
&& hpQV
&& hpAA
)
677 test_EnumProcesses();
678 test_EnumProcessModules();
679 test_GetModuleInformation();
680 test_GetProcessMemoryInfo();
681 test_GetMappedFileName();
682 test_GetProcessImageFileName();
683 test_GetModuleFileNameEx();
684 test_GetModuleBaseName();