wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / dlls / kernel32 / tests / module.c
blob35c7c552dd5493b48138ad04e283a740e194d26e
1 /*
2 * Unit tests for module/DLL/library API
4 * Copyright (c) 2004 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include "ntstatus.h"
23 #define WIN32_NO_STATUS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "winternl.h"
28 #include <psapi.h>
29 #include "wine/test.h"
31 static DWORD (WINAPI *pGetDllDirectoryA)(DWORD,LPSTR);
32 static DWORD (WINAPI *pGetDllDirectoryW)(DWORD,LPWSTR);
33 static BOOL (WINAPI *pSetDllDirectoryA)(LPCSTR);
34 static DLL_DIRECTORY_COOKIE (WINAPI *pAddDllDirectory)(const WCHAR*);
35 static BOOL (WINAPI *pRemoveDllDirectory)(DLL_DIRECTORY_COOKIE);
36 static BOOL (WINAPI *pSetDefaultDllDirectories)(DWORD);
37 static BOOL (WINAPI *pK32GetModuleInformation)(HANDLE process, HMODULE module,
38 MODULEINFO *modinfo, DWORD cb);
40 static NTSTATUS (WINAPI *pApiSetQueryApiSetPresence)(const UNICODE_STRING*,BOOLEAN*);
41 static NTSTATUS (WINAPI *pApiSetQueryApiSetPresenceEx)(const UNICODE_STRING*,BOOLEAN*,BOOLEAN*);
42 static NTSTATUS (WINAPI *pLdrGetDllDirectory)(UNICODE_STRING*);
43 static NTSTATUS (WINAPI *pLdrSetDllDirectory)(UNICODE_STRING*);
44 static NTSTATUS (WINAPI *pLdrGetDllHandle)( LPCWSTR load_path, ULONG flags, const UNICODE_STRING *name, HMODULE *base );
45 static NTSTATUS (WINAPI *pLdrGetDllHandleEx)( ULONG flags, LPCWSTR load_path, ULONG *dll_characteristics,
46 const UNICODE_STRING *name, HMODULE *base );
47 static NTSTATUS (WINAPI *pLdrGetDllFullName)( HMODULE module, UNICODE_STRING *name );
49 static BOOL (WINAPI *pIsApiSetImplemented)(LPCSTR);
51 static BOOL is_unicode_enabled = TRUE;
53 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
55 WCHAR aw[1024];
57 DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
58 a, lenA, aw, ARRAY_SIZE(aw));
59 if (len != lenB) return FALSE;
60 return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
63 static const struct
65 IMAGE_DOS_HEADER dos;
66 IMAGE_NT_HEADERS nt;
67 IMAGE_SECTION_HEADER section;
68 } dll_image =
70 { IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 }, 0, 0, { 0 },
71 sizeof(IMAGE_DOS_HEADER) },
73 IMAGE_NT_SIGNATURE, /* Signature */
75 #if defined __i386__
76 IMAGE_FILE_MACHINE_I386, /* Machine */
77 #elif defined __x86_64__
78 IMAGE_FILE_MACHINE_AMD64, /* Machine */
79 #elif defined __arm__
80 IMAGE_FILE_MACHINE_ARMNT, /* Machine */
81 #elif defined __aarch64__
82 IMAGE_FILE_MACHINE_ARM64, /* Machine */
83 #else
84 # error You must specify the machine type
85 #endif
86 1, /* NumberOfSections */
87 0, /* TimeDateStamp */
88 0, /* PointerToSymbolTable */
89 0, /* NumberOfSymbols */
90 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
91 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
93 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
94 1, /* MajorLinkerVersion */
95 0, /* MinorLinkerVersion */
96 0, /* SizeOfCode */
97 0, /* SizeOfInitializedData */
98 0, /* SizeOfUninitializedData */
99 0, /* AddressOfEntryPoint */
100 0x1000, /* BaseOfCode */
101 #ifndef _WIN64
102 0, /* BaseOfData */
103 #endif
104 0x10000000, /* ImageBase */
105 0x1000, /* SectionAlignment */
106 0x1000, /* FileAlignment */
107 4, /* MajorOperatingSystemVersion */
108 0, /* MinorOperatingSystemVersion */
109 1, /* MajorImageVersion */
110 0, /* MinorImageVersion */
111 4, /* MajorSubsystemVersion */
112 0, /* MinorSubsystemVersion */
113 0, /* Win32VersionValue */
114 0x2000, /* SizeOfImage */
115 sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS), /* SizeOfHeaders */
116 0, /* CheckSum */
117 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
118 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE | IMAGE_DLLCHARACTERISTICS_NX_COMPAT, /* DllCharacteristics */
119 0, /* SizeOfStackReserve */
120 0, /* SizeOfStackCommit */
121 0, /* SizeOfHeapReserve */
122 0, /* SizeOfHeapCommit */
123 0, /* LoaderFlags */
124 0, /* NumberOfRvaAndSizes */
125 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
128 { ".rodata", { 0 }, 0x1000, 0x1000, 0, 0, 0, 0, 0,
129 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ }
132 static void create_test_dll( const char *name )
134 DWORD dummy;
135 HANDLE handle = CreateFileA( name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0 );
137 ok( handle != INVALID_HANDLE_VALUE, "failed to create file err %lu\n", GetLastError() );
138 WriteFile( handle, &dll_image, sizeof(dll_image), &dummy, NULL );
139 SetFilePointer( handle, dll_image.nt.OptionalHeader.SizeOfImage, NULL, FILE_BEGIN );
140 SetEndOfFile( handle );
141 CloseHandle( handle );
144 static void testGetModuleFileName(const char* name)
146 HMODULE hMod;
147 char bufA[MAX_PATH];
148 WCHAR bufW[MAX_PATH];
149 DWORD len1A, len1W = 0, len2A, len2W = 0;
151 hMod = (name) ? GetModuleHandleA(name) : NULL;
153 /* first test, with enough space in buffer */
154 memset(bufA, '-', sizeof(bufA));
155 SetLastError(0xdeadbeef);
156 len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
157 ok(GetLastError() == ERROR_SUCCESS ||
158 broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
159 "LastError was not reset: %lu\n", GetLastError());
160 ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
162 if (is_unicode_enabled)
164 memset(bufW, '-', sizeof(bufW));
165 SetLastError(0xdeadbeef);
166 len1W = GetModuleFileNameW(hMod, bufW, ARRAY_SIZE(bufW));
167 ok(GetLastError() == ERROR_SUCCESS ||
168 broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
169 "LastError was not reset: %lu\n", GetLastError());
170 ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
173 ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%ld/%d)\n", len1A, lstrlenA(bufA));
175 if (is_unicode_enabled)
177 ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%ld/%d)\n", len1W, lstrlenW(bufW));
178 ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
181 /* second test with a buffer too small */
182 memset(bufA, '-', sizeof(bufA));
183 len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
184 ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
186 if (is_unicode_enabled)
188 memset(bufW, '-', sizeof(bufW));
189 len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
190 ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
191 ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
192 ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%ld/%ld)\n", len1W / 2, len2W);
195 ok(len1A / 2 == len2A,
196 "Correct length in GetModuleFilenameA with buffer too small (%ld/%ld)\n", len1A / 2, len2A);
198 len1A = GetModuleFileNameA(hMod, bufA, 0x10000);
199 ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
200 len1W = GetModuleFileNameW(hMod, bufW, 0x10000);
201 ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
204 static void testGetModuleFileName_Wrong(void)
206 char bufA[MAX_PATH];
207 WCHAR bufW[MAX_PATH];
209 /* test wrong handle */
210 if (is_unicode_enabled)
212 bufW[0] = '*';
213 ok(GetModuleFileNameW((void*)0xffffffff, bufW, ARRAY_SIZE(bufW)) == 0,
214 "Unexpected success in module handle\n");
215 ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
218 bufA[0] = '*';
219 ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
220 ok(bufA[0] == '*', "When failing, buffer shouldn't be written to\n");
223 static void testLoadLibraryA(void)
225 HMODULE hModule, hModule1;
226 FARPROC fp;
228 SetLastError(0xdeadbeef);
229 hModule = LoadLibraryA("kernel32.dll");
230 ok( hModule != NULL, "kernel32.dll should be loadable\n");
231 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %ld\n", GetLastError());
233 fp = GetProcAddress(hModule, "CreateFileA");
234 ok( fp != NULL, "CreateFileA should be there\n");
235 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %ld\n", GetLastError());
237 SetLastError(0xdeadbeef);
238 hModule1 = LoadLibraryA("kernel32 ");
239 ok( hModule1 != NULL, "\"kernel32 \" should be loadable\n" );
240 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %ld\n", GetLastError() );
241 ok( hModule == hModule1, "Loaded wrong module\n" );
242 FreeLibrary(hModule1);
243 FreeLibrary(hModule);
246 static void testNestedLoadLibraryA(void)
248 static const char dllname[] = "shell32.dll";
249 char path1[MAX_PATH], path2[MAX_PATH];
250 HMODULE hModule1, hModule2, hModule3;
252 /* This is not really a Windows conformance test, but more a Wine
253 * regression test. Wine's builtin dlls can be loaded from multiple paths,
254 * and this test tries to make sure that Wine does not get confused and
255 * really unloads the Unix .so file at the right time. Failure to do so
256 * will result in the dll being unloadable.
257 * This test must be done with a dll that can be unloaded, which means:
258 * - it must not already be loaded
259 * - it must not have a 16-bit counterpart
261 GetWindowsDirectoryA(path1, sizeof(path1));
262 strcat(path1, "\\system\\");
263 strcat(path1, dllname);
264 hModule1 = LoadLibraryA(path1);
265 if (!hModule1)
267 /* We must be on Windows, so we cannot test */
268 return;
271 GetWindowsDirectoryA(path2, sizeof(path2));
272 strcat(path2, "\\system32\\");
273 strcat(path2, dllname);
274 hModule2 = LoadLibraryA(path2);
275 ok(hModule2 != NULL, "LoadLibrary(%s) failed\n", path2);
277 /* The first LoadLibrary() call may have registered the dll under the
278 * system32 path. So load it, again, under the '...\system\...' path so
279 * Wine does not immediately notice that it is already loaded.
281 hModule3 = LoadLibraryA(path1);
282 ok(hModule3 != NULL, "LoadLibrary(%s) failed\n", path1);
284 /* Now fully unload the dll */
285 ok(FreeLibrary(hModule3), "FreeLibrary() failed\n");
286 ok(FreeLibrary(hModule2), "FreeLibrary() failed\n");
287 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
288 ok(GetModuleHandleA(dllname) == NULL, "%s was not fully unloaded\n", dllname);
290 /* Try to load the dll again, if refcounting is ok, this should work */
291 hModule1 = LoadLibraryA(path1);
292 ok(hModule1 != NULL, "LoadLibrary(%s) failed\n", path1);
293 if (hModule1 != NULL)
294 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
297 static void testLoadLibraryA_Wrong(void)
299 HMODULE hModule;
301 /* Try to load a nonexistent dll */
302 SetLastError(0xdeadbeef);
303 hModule = LoadLibraryA("non_ex_pv.dll");
304 ok( !hModule, "non_ex_pv.dll should be not loadable\n");
305 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %ld\n", GetLastError() );
307 /* Just in case */
308 FreeLibrary(hModule);
311 static void testGetProcAddress_Wrong(void)
313 FARPROC fp;
315 SetLastError(0xdeadbeef);
316 fp = GetProcAddress(NULL, "non_ex_call");
317 ok( !fp, "non_ex_call should not be found\n");
318 ok( GetLastError() == ERROR_PROC_NOT_FOUND, "Expected ERROR_PROC_NOT_FOUND, got %ld\n", GetLastError() );
320 SetLastError(0xdeadbeef);
321 fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
322 ok( !fp, "non_ex_call should not be found\n");
323 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %ld\n", GetLastError() );
326 static void testLoadLibraryEx(void)
328 CHAR path[MAX_PATH];
329 HMODULE hmodule;
330 HANDLE hfile;
331 BOOL ret;
333 hfile = CreateFileA("testfile.dll", GENERIC_READ | GENERIC_WRITE,
334 FILE_SHARE_READ | FILE_SHARE_WRITE,
335 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
336 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
338 /* NULL lpFileName */
339 SetLastError(0xdeadbeef);
340 hmodule = LoadLibraryExA(NULL, NULL, 0);
341 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
342 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
343 GetLastError() == ERROR_INVALID_PARAMETER,
344 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
346 /* empty lpFileName */
347 SetLastError(0xdeadbeef);
348 hmodule = LoadLibraryExA("", NULL, 0);
349 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
350 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
351 GetLastError() == ERROR_INVALID_PARAMETER /* win8 */,
352 "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND, got %ld\n", GetLastError());
354 /* hFile is non-NULL */
355 SetLastError(0xdeadbeef);
356 hmodule = LoadLibraryExA("testfile.dll", hfile, 0);
357 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
358 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
359 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
360 "Unexpected last error, got %ld\n", GetLastError());
362 SetLastError(0xdeadbeef);
363 hmodule = LoadLibraryExA("testfile.dll", (HANDLE)0xdeadbeef, 0);
364 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
365 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
366 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
367 "Unexpected last error, got %ld\n", GetLastError());
369 /* try to open a file that is locked */
370 SetLastError(0xdeadbeef);
371 hmodule = LoadLibraryExA("testfile.dll", NULL, 0);
372 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
373 ok(GetLastError() == ERROR_SHARING_VIOLATION,
374 "Expected ERROR_SHARING_VIOLATION, got %ld\n", GetLastError());
376 /* lpFileName does not matter */
377 if (is_unicode_enabled)
379 SetLastError(0xdeadbeef);
380 hmodule = LoadLibraryExA(NULL, hfile, 0);
381 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
382 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
383 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
384 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
387 CloseHandle(hfile);
389 /* load empty file */
390 SetLastError(0xdeadbeef);
391 hmodule = LoadLibraryExA("testfile.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
392 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
393 ok(GetLastError() == ERROR_FILE_INVALID, "Expected ERROR_FILE_INVALID, got %ld\n", GetLastError());
395 DeleteFileA("testfile.dll");
397 GetSystemDirectoryA(path, MAX_PATH);
398 if (path[lstrlenA(path) - 1] != '\\')
399 lstrcatA(path, "\\");
400 lstrcatA(path, "kernel32.dll");
402 /* load kernel32.dll with an absolute path */
403 SetLastError(0xdeadbeef);
404 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
405 ok(hmodule != 0, "Expected valid module handle\n");
406 ok(GetLastError() == 0xdeadbeef ||
407 GetLastError() == ERROR_SUCCESS,
408 "Expected 0xdeadbeef or ERROR_SUCCESS, got %ld\n", GetLastError());
410 /* try invalid file handle */
411 SetLastError(0xdeadbeef);
412 hmodule = LoadLibraryExA(path, (HANDLE)0xdeadbeef, 0);
413 if (!hmodule) /* succeeds on xp and older */
414 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError());
416 FreeLibrary(hmodule);
418 /* load kernel32.dll with no path */
419 SetLastError(0xdeadbeef);
420 hmodule = LoadLibraryExA("kernel32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
421 ok(hmodule != 0, "Expected valid module handle\n");
422 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %ld\n", GetLastError());
424 FreeLibrary(hmodule);
426 GetCurrentDirectoryA(MAX_PATH, path);
427 if (path[lstrlenA(path) - 1] != '\\')
428 lstrcatA(path, "\\");
429 lstrcatA(path, "kernel32.dll");
431 /* load kernel32.dll with an absolute path that does not exist */
432 SetLastError(0xdeadbeef);
433 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
434 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
435 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
436 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
438 /* Free the loaded dll when it's the first time this dll is loaded
439 in process - First time should pass, second fail */
440 SetLastError(0xdeadbeef);
441 hmodule = LoadLibraryExA("comctl32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
442 ok(hmodule != 0, "Expected valid module handle\n");
444 SetLastError(0xdeadbeef);
445 ret = FreeLibrary( (HMODULE)((ULONG_PTR)hmodule + 0x1230));
446 ok(!ret, "Free succeeded on wrong handle\n");
447 ok(GetLastError() == ERROR_BAD_EXE_FORMAT, "wrong error %lu\n", GetLastError());
449 SetLastError(0xdeadbeef);
450 ret = FreeLibrary(hmodule);
451 ok(ret, "Expected to be able to free the module, failed with %ld\n", GetLastError());
452 SetLastError(0xdeadbeef);
453 ret = FreeLibrary(hmodule);
454 ok(!ret, "Unexpected ability to free the module, failed with %ld\n", GetLastError());
456 /* load with full path, name without extension */
457 GetSystemDirectoryA(path, MAX_PATH);
458 if (path[lstrlenA(path) - 1] != '\\')
459 lstrcatA(path, "\\");
460 lstrcatA(path, "kernel32");
461 hmodule = LoadLibraryExA(path, NULL, 0);
462 ok(hmodule != NULL, "got %p\n", hmodule);
463 FreeLibrary(hmodule);
465 /* same with alterate search path */
466 hmodule = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
467 ok(hmodule != NULL, "got %p\n", hmodule);
468 FreeLibrary(hmodule);
471 static void test_LoadLibraryEx_search_flags(void)
473 static const char apiset_dll[] = "api-ms-win-shcore-obsolete-l1-1-0.dll";
474 static const struct
476 int add_dirs[4];
477 int dll_dir;
478 int expect;
479 } tests[] =
481 { { 1, 2, 3 }, 4, 3 }, /* 0 */
482 { { 1, 3, 2 }, 4, 2 },
483 { { 3, 1 }, 4, 1 },
484 { { 5, 6 }, 4, 4 },
485 { { 5, 2 }, 4, 2 },
486 { { 0 }, 4, 4 }, /* 5 */
487 { { 0 }, 0, 0 },
488 { { 6, 5 }, 5, 0 },
489 { { 1, 1, 2 }, 0, 2 },
491 char *p, path[MAX_PATH], buf[MAX_PATH], curdir[MAX_PATH];
492 WCHAR bufW[MAX_PATH];
493 DLL_DIRECTORY_COOKIE cookies[4];
494 unsigned int i, j, k;
495 BOOL ret;
496 HMODULE mod;
498 GetTempPathA( sizeof(path), path );
499 GetTempFileNameA( path, "tmp", 0, buf );
500 DeleteFileA( buf );
501 ret = CreateDirectoryA( buf, NULL );
502 ok( ret, "CreateDirectory failed err %lu\n", GetLastError() );
503 p = buf + strlen( buf );
504 for (i = 1; i <= 6; i++)
506 sprintf( p, "\\%u", i );
507 ret = CreateDirectoryA( buf, NULL );
508 ok( ret, "CreateDirectory failed err %lu\n", GetLastError() );
509 if (i >= 5) continue; /* dirs 5 and 6 are left empty */
510 sprintf( p, "\\%u\\winetestdll.dll", i );
511 create_test_dll( buf );
514 GetCurrentDirectoryA( MAX_PATH, curdir );
515 *p = 0;
516 SetCurrentDirectoryA( buf );
518 SetLastError( 0xdeadbeef );
519 mod = LoadLibraryA( "1\\winetestdll.dll" );
520 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
521 FreeLibrary( mod );
523 SetLastError( 0xdeadbeef );
524 sprintf( path, "%c:1\\winetestdll.dll", buf[0] );
525 mod = LoadLibraryA( path );
526 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
527 FreeLibrary( mod );
529 if (pAddDllDirectory)
531 SetLastError( 0xdeadbeef );
532 mod = LoadLibraryExA( "1\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
533 ok( !mod, "LoadLibrary succeeded\n" );
534 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
536 SetLastError( 0xdeadbeef );
537 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
538 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
539 FreeLibrary( mod );
542 strcpy( p, "\\1" );
543 SetCurrentDirectoryA( buf );
545 SetLastError( 0xdeadbeef );
546 mod = LoadLibraryA( "winetestdll.dll" );
547 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
548 FreeLibrary( mod );
550 SetLastError( 0xdeadbeef );
551 sprintf( path, "%c:winetestdll.dll", buf[0] );
552 mod = LoadLibraryA( path );
553 ok( mod != NULL || broken(!mod), /* win10 disallows this but allows c:1\\winetestdll.dll */
554 "LoadLibrary failed err %lu\n", GetLastError() );
555 if (!mod) ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
556 else FreeLibrary( mod );
558 SetLastError( 0xdeadbeef );
559 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
560 mod = LoadLibraryA( path );
561 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
562 FreeLibrary( mod );
564 if (pAddDllDirectory)
566 SetLastError( 0xdeadbeef );
567 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
568 ok( !mod, "LoadLibrary succeeded\n" );
569 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
571 SetLastError( 0xdeadbeef );
572 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
573 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
574 FreeLibrary( mod );
576 SetLastError( 0xdeadbeef );
577 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
578 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
579 ok( mod != NULL, "LoadLibrary failed err %lu\n", GetLastError() );
580 FreeLibrary( mod );
582 SetLastError( 0xdeadbeef );
583 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
584 ok( !mod, "LoadLibrary succeeded\n" );
585 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
587 SetLastError( 0xdeadbeef );
588 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
589 ok( !mod, "LoadLibrary succeeded\n" );
590 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
592 SetLastError( 0xdeadbeef );
593 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_WITH_ALTERED_SEARCH_PATH );
594 ok( !mod, "LoadLibrary succeeded\n" );
595 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
597 SetLastError( 0xdeadbeef );
598 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
599 ok( !mod, "LoadLibrary succeeded\n" );
600 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
602 SetLastError( 0xdeadbeef );
603 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
604 ok( !mod, "LoadLibrary succeeded\n" );
605 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
608 SetCurrentDirectoryA( curdir );
610 if (!pAddDllDirectory || !pSetDllDirectoryA) goto done;
612 SetLastError( 0xdeadbeef );
613 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
614 ok( !mod, "LoadLibrary succeeded\n" );
615 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
617 if (0) /* crashes on win10 */
619 SetLastError( 0xdeadbeef );
620 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
621 ok( !mod, "LoadLibrary succeeded\n" );
622 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
623 "wrong error %lu\n", GetLastError() );
626 SetLastError( 0xdeadbeef );
627 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
628 ok( !mod, "LoadLibrary succeeded\n" );
629 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
631 SetLastError( 0xdeadbeef );
632 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
633 ok( !mod, "LoadLibrary succeeded\n" );
634 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
636 SetLastError( 0xdeadbeef );
637 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 );
638 ok( !mod, "LoadLibrary succeeded\n" );
639 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
641 SetLastError( 0xdeadbeef );
642 mod = LoadLibraryExA( "foo\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
643 ok( !mod, "LoadLibrary succeeded\n" );
644 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
646 SetLastError( 0xdeadbeef );
647 mod = LoadLibraryExA( "\\windows\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
648 ok( !mod, "LoadLibrary succeeded\n" );
649 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
651 SetLastError( 0xdeadbeef );
652 mod = LoadLibraryA( "1\\winetestdll.dll" );
653 ok( !mod, "LoadLibrary succeeded\n" );
654 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
656 for (j = 0; j < ARRAY_SIZE(tests); j++)
658 for (k = 0; tests[j].add_dirs[k]; k++)
660 sprintf( p, "\\%u", tests[j].add_dirs[k] );
661 MultiByteToWideChar( CP_ACP, 0, buf, -1, bufW, MAX_PATH );
662 cookies[k] = pAddDllDirectory( bufW );
663 ok( cookies[k] != NULL, "failed to add %s\n", buf );
665 if (tests[j].dll_dir)
667 sprintf( p, "\\%u", tests[j].dll_dir );
668 pSetDllDirectoryA( buf );
670 else pSetDllDirectoryA( NULL );
672 SetLastError( 0xdeadbeef );
673 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
674 if (tests[j].expect)
676 ok( mod != NULL, "%u: LoadLibrary failed err %lu\n", j, GetLastError() );
677 GetModuleFileNameA( mod, path, MAX_PATH );
678 sprintf( p, "\\%u\\winetestdll.dll", tests[j].expect );
679 ok( !lstrcmpiA( path, buf ), "%u: wrong module %s expected %s\n", j, path, buf );
681 else
683 ok( !mod, "%u: LoadLibrary succeeded\n", j );
684 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
685 "%u: wrong error %lu\n", j, GetLastError() );
687 FreeLibrary( mod );
689 for (k = 0; tests[j].add_dirs[k]; k++) pRemoveDllDirectory( cookies[k] );
692 mod = GetModuleHandleA( apiset_dll );
693 if (mod)
695 win_skip( "%s already referenced, skipping test.\n", apiset_dll );
697 else
699 LoadLibraryA( "ole32.dll" ); /* FIXME: make sure the dependencies are loaded */
700 mod = LoadLibraryA( apiset_dll );
701 if (mod)
703 HMODULE shcore;
704 char buffer[MAX_PATH];
706 FreeLibrary(mod);
707 mod = LoadLibraryExA( apiset_dll, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
708 ok( !!mod, "Got NULL module, error %lu.\n", GetLastError() );
709 ok( !!GetModuleHandleA( apiset_dll ), "Got NULL handle.\n" );
710 shcore = GetModuleHandleA( "shcore.dll" );
711 ok( mod == shcore, "wrong module %p/%p\n", mod, shcore );
712 ret = FreeLibrary( mod );
713 ok( ret, "FreeLibrary failed, error %lu.\n", GetLastError() );
714 shcore = GetModuleHandleA( "shcore.dll" );
715 ok( !shcore, "shcore not unloaded\n" );
717 /* api set without .dll */
718 strcpy( buffer, apiset_dll );
719 buffer[strlen(buffer) - 4] = 0;
720 mod = LoadLibraryExA( buffer, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
721 ok( !!mod, "Got NULL module, error %lu.\n", GetLastError() );
722 ok( !!GetModuleHandleA( apiset_dll ), "Got NULL handle.\n" );
723 shcore = GetModuleHandleA( "shcore.dll" );
724 ok( mod == shcore, "wrong module %p/%p\n", mod, shcore );
725 ret = FreeLibrary( mod );
726 ok( ret, "FreeLibrary failed, error %lu.\n", GetLastError() );
727 shcore = GetModuleHandleA( "shcore.dll" );
728 ok( !shcore, "shcore not unloaded\n" );
730 /* api set with different version */
731 strcpy( buffer, apiset_dll );
732 buffer[strlen(buffer) - 5] = '9';
733 mod = LoadLibraryExA( buffer, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
734 ok( !!mod || broken(!mod) /* win8 */, "Got NULL module, error %lu.\n", GetLastError() );
735 if (mod)
737 ok( !!GetModuleHandleA( apiset_dll ), "Got NULL handle.\n" );
738 shcore = GetModuleHandleA( "shcore.dll" );
739 ok( mod == shcore, "wrong module %p/%p\n", mod, shcore );
740 ret = FreeLibrary( mod );
741 ok( ret, "FreeLibrary failed, error %lu.\n", GetLastError() );
743 shcore = GetModuleHandleA( "shcore.dll" );
744 ok( !shcore, "shcore not unloaded\n" );
746 /* api set with full path */
747 GetWindowsDirectoryA( buffer, MAX_PATH );
748 strcat( buffer, "\\" );
749 strcat( buffer, apiset_dll );
750 SetLastError( 0xdeadbeef );
751 mod = LoadLibraryExA( buffer, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
752 ok( !mod, "Loaded %s\n", debugstr_a(buffer) );
753 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
754 SetLastError( 0xdeadbeef );
755 mod = LoadLibraryA( buffer );
756 ok( !mod, "Loaded %s\n", debugstr_a(buffer) );
757 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
759 else
761 win_skip( "%s not found, skipping test.\n", apiset_dll );
764 /* try a library with dependencies */
765 mod = GetModuleHandleA( "rasapi32.dll" );
766 ok( !mod, "rasapi32 already loaded\n" );
767 mod = LoadLibraryA( "rasapi32.dll" );
768 ok( !!mod, "rasapi32 not found %lu\n", GetLastError() );
769 FreeLibrary( mod );
770 SetLastError( 0xdeadbeef );
771 mod = LoadLibraryExA( "rasapi32.dll", NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
772 ok( !mod, "rasapi32 loaded\n" );
773 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
774 SetLastError( 0xdeadbeef );
775 mod = LoadLibraryExA( "ext-ms-win-ras-rasapi32-l1-1-0.dll", NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
776 todo_wine /* rasapi32 doesn't have interesting dependencies on wine */
777 ok( !mod, "rasapi32 loaded\n" );
778 if (mod) FreeLibrary( mod );
779 else ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %lu\n", GetLastError() );
780 mod = LoadLibraryA( "ext-ms-win-ras-rasapi32-l1-1-0.dll" );
781 ok( !!mod || broken(!mod) /* win7 */, "rasapi32 not found %lu\n", GetLastError() );
782 if (mod) FreeLibrary( mod );
783 mod = GetModuleHandleA( "rasapi32.dll" );
784 ok( !mod, "rasapi32 still loaded\n" );
786 done:
787 for (i = 1; i <= 6; i++)
789 sprintf( p, "\\%u\\winetestdll.dll", i );
790 DeleteFileA( buf );
791 sprintf( p, "\\%u", i );
792 RemoveDirectoryA( buf );
794 *p = 0;
795 RemoveDirectoryA( buf );
798 static void testGetDllDirectory(void)
800 CHAR bufferA[MAX_PATH];
801 WCHAR bufferW[MAX_PATH];
802 DWORD length, ret;
803 int i;
804 static const char *dll_directories[] =
807 "C:\\Some\\Path",
808 "C:\\Some\\Path\\",
809 "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
811 const int test_count = ARRAY_SIZE(dll_directories);
813 if (!pGetDllDirectoryA || !pGetDllDirectoryW)
815 win_skip("GetDllDirectory not available\n");
816 return;
818 if (!pSetDllDirectoryA)
820 win_skip("SetDllDirectoryA not available\n");
821 return;
824 for (i = 0; i < test_count; i++)
826 length = strlen(dll_directories[i]);
827 if (!pSetDllDirectoryA(dll_directories[i]))
829 skip("i=%d, SetDllDirectoryA failed\n", i);
830 continue;
833 /* no buffer, determine length */
834 ret = pGetDllDirectoryA(0, NULL);
835 ok(ret == length + 1, "Expected %lu, got %lu\n", length + 1, ret);
837 ret = pGetDllDirectoryW(0, NULL);
838 ok(ret == length + 1, "Expected %lu, got %lu\n", length + 1, ret);
840 /* buffer of exactly the right size */
841 bufferA[length] = 'A';
842 bufferA[length + 1] = 'A';
843 ret = pGetDllDirectoryA(length + 1, bufferA);
844 ok(ret == length || broken(ret + 1 == length) /* win8 */,
845 "i=%d, Expected %lu(+1), got %lu\n", i, length, ret);
846 ok(bufferA[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
847 ok(strcmp(bufferA, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, bufferA);
849 bufferW[length] = 'A';
850 bufferW[length + 1] = 'A';
851 ret = pGetDllDirectoryW(length + 1, bufferW);
852 ok(ret == length, "i=%d, Expected %lu, got %lu\n", i, length, ret);
853 ok(bufferW[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
854 ok(cmpStrAW(dll_directories[i], bufferW, length, length),
855 "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(bufferW));
857 /* Zero size buffer. The buffer may or may not be terminated depending
858 * on the Windows version and whether the A or W API is called. */
859 bufferA[0] = 'A';
860 ret = pGetDllDirectoryA(0, bufferA);
861 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
863 bufferW[0] = 'A';
864 ret = pGetDllDirectoryW(0, bufferW);
865 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
866 ok(bufferW[0] == 'A' || broken(bufferW[0] == 0), /* XP, 2003 */
867 "i=%d, Buffer overflow\n", i);
869 /* buffer just one too short */
870 bufferA[0] = 'A';
871 ret = pGetDllDirectoryA(length, bufferA);
872 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
873 if (length != 0)
874 ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
876 bufferW[0] = 'A';
877 ret = pGetDllDirectoryW(length, bufferW);
878 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
879 if (length != 0)
880 ok(bufferW[0] == 0, "i=%d, Buffer overflow\n", i);
882 if (0)
884 /* crashes on win8 */
885 /* no buffer, but too short length */
886 ret = pGetDllDirectoryA(length, NULL);
887 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
889 ret = pGetDllDirectoryW(length, NULL);
890 ok(ret == length + 1, "i=%d, Expected %lu, got %lu\n", i, length + 1, ret);
893 if (pLdrGetDllDirectory)
895 UNICODE_STRING str;
896 NTSTATUS status;
897 str.Buffer = bufferW;
898 str.MaximumLength = sizeof(bufferW);
899 status = pLdrGetDllDirectory( &str );
900 ok( !status, "LdrGetDllDirectory failed %lx\n", status );
901 ok( cmpStrAW( dll_directories[i], bufferW, strlen(dll_directories[i]),
902 str.Length / sizeof(WCHAR) ), "%u: got %s instead of %s\n",
903 i, wine_dbgstr_w(bufferW), dll_directories[i] );
904 if (dll_directories[i][0])
906 memset( bufferW, 0xcc, sizeof(bufferW) );
907 str.MaximumLength = (strlen( dll_directories[i] ) - 1) * sizeof(WCHAR);
908 status = pLdrGetDllDirectory( &str );
909 ok( status == STATUS_BUFFER_TOO_SMALL, "%u: LdrGetDllDirectory failed %lx\n", i, status );
910 ok( bufferW[0] == 0 && bufferW[1] == 0xcccc,
911 "%u: buffer %x %x\n", i, bufferW[0], bufferW[1] );
912 length = (strlen( dll_directories[i] ) + 1) * sizeof(WCHAR);
913 ok( str.Length == length, "%u: wrong len %u / %lu\n", i, str.Length, length );
918 /* unset whatever we did so following tests won't be affected */
919 pSetDllDirectoryA(NULL);
922 static void init_pointers(void)
924 HMODULE mod = GetModuleHandleA("kernel32.dll");
926 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(mod, #f))
927 MAKEFUNC(GetDllDirectoryA);
928 MAKEFUNC(GetDllDirectoryW);
929 MAKEFUNC(SetDllDirectoryA);
930 MAKEFUNC(AddDllDirectory);
931 MAKEFUNC(RemoveDllDirectory);
932 MAKEFUNC(SetDefaultDllDirectories);
933 MAKEFUNC(K32GetModuleInformation);
934 mod = GetModuleHandleA( "ntdll.dll" );
935 MAKEFUNC(ApiSetQueryApiSetPresence);
936 MAKEFUNC(ApiSetQueryApiSetPresenceEx);
937 MAKEFUNC(LdrGetDllDirectory);
938 MAKEFUNC(LdrSetDllDirectory);
939 MAKEFUNC(LdrGetDllHandle);
940 MAKEFUNC(LdrGetDllHandleEx);
941 MAKEFUNC(LdrGetDllFullName);
942 mod = GetModuleHandleA( "kernelbase.dll" );
943 MAKEFUNC(IsApiSetImplemented);
944 #undef MAKEFUNC
946 /* before Windows 7 this was not exported in kernel32 */
947 if (!pK32GetModuleInformation)
949 HMODULE hPsapi = LoadLibraryA("psapi.dll");
950 pK32GetModuleInformation = (void *)GetProcAddress(hPsapi, "GetModuleInformation");
954 static void testGetModuleHandleEx(void)
956 static const char longname[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
957 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
958 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
959 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
960 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
961 static const WCHAR nosuchmodW[] = {'n','o','s','u','c','h','m','o','d',0};
962 BOOL ret;
963 DWORD error;
964 HMODULE mod, mod_kernel32;
966 SetLastError( 0xdeadbeef );
967 ret = GetModuleHandleExA( 0, NULL, NULL );
968 error = GetLastError();
969 ok( !ret, "unexpected success\n" );
970 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
972 SetLastError( 0xdeadbeef );
973 ret = GetModuleHandleExA( 0, "kernel32", NULL );
974 error = GetLastError();
975 ok( !ret, "unexpected success\n" );
976 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
978 SetLastError( 0xdeadbeef );
979 mod = (HMODULE)0xdeadbeef;
980 ret = GetModuleHandleExA( 0, "kernel32", &mod );
981 ok( ret, "unexpected failure %lu\n", GetLastError() );
982 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
983 FreeLibrary( mod );
985 SetLastError( 0xdeadbeef );
986 mod = (HMODULE)0xdeadbeef;
987 ret = GetModuleHandleExA( 0, "nosuchmod", &mod );
988 error = GetLastError();
989 ok( !ret, "unexpected success\n" );
990 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
991 ok( mod == NULL, "got %p\n", mod );
993 SetLastError( 0xdeadbeef );
994 ret = GetModuleHandleExA( 0, longname, NULL );
995 error = GetLastError();
996 ok( !ret, "unexpected success\n" );
997 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
999 SetLastError( 0xdeadbeef );
1000 mod = (HMODULE)0xdeadbeef;
1001 ret = GetModuleHandleExA( 0, longname, &mod );
1002 error = GetLastError();
1003 ok( !ret, "unexpected success\n" );
1004 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1005 ok( mod == NULL, "got %p\n", mod );
1007 SetLastError( 0xdeadbeef );
1008 ret = GetModuleHandleExW( 0, NULL, NULL );
1009 error = GetLastError();
1010 ok( !ret, "unexpected success\n" );
1011 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1013 SetLastError( 0xdeadbeef );
1014 ret = GetModuleHandleExW( 0, kernel32W, NULL );
1015 error = GetLastError();
1016 ok( !ret, "unexpected success\n" );
1017 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1019 SetLastError( 0xdeadbeef );
1020 mod = (HMODULE)0xdeadbeef;
1021 ret = GetModuleHandleExW( 0, kernel32W, &mod );
1022 ok( ret, "unexpected failure %lu\n", GetLastError() );
1023 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
1024 FreeLibrary( mod );
1026 SetLastError( 0xdeadbeef );
1027 mod = (HMODULE)0xdeadbeef;
1028 ret = GetModuleHandleExW( 0, nosuchmodW, &mod );
1029 error = GetLastError();
1030 ok( !ret, "unexpected success\n" );
1031 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1032 ok( mod == NULL, "got %p\n", mod );
1034 SetLastError( 0xdeadbeef );
1035 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
1036 error = GetLastError();
1037 ok( !ret, "unexpected success\n" );
1038 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1040 SetLastError( 0xdeadbeef );
1041 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", NULL );
1042 error = GetLastError();
1043 ok( !ret, "unexpected success\n" );
1044 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1046 SetLastError( 0xdeadbeef );
1047 mod = (HMODULE)0xdeadbeef;
1048 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", &mod );
1049 ok( ret, "unexpected failure %lu\n", GetLastError() );
1050 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
1052 SetLastError( 0xdeadbeef );
1053 mod = (HMODULE)0xdeadbeef;
1054 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "nosuchmod", &mod );
1055 error = GetLastError();
1056 ok( !ret, "unexpected success\n" );
1057 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1058 ok( mod == NULL, "got %p\n", mod );
1060 SetLastError( 0xdeadbeef );
1061 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
1062 error = GetLastError();
1063 ok( !ret, "unexpected success\n" );
1064 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1066 SetLastError( 0xdeadbeef );
1067 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, NULL );
1068 error = GetLastError();
1069 ok( !ret, "unexpected success\n" );
1070 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1072 SetLastError( 0xdeadbeef );
1073 mod = (HMODULE)0xdeadbeef;
1074 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, &mod );
1075 ok( ret, "unexpected failure %lu\n", GetLastError() );
1076 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
1078 SetLastError( 0xdeadbeef );
1079 mod = (HMODULE)0xdeadbeef;
1080 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nosuchmodW, &mod );
1081 error = GetLastError();
1082 ok( !ret, "unexpected success\n" );
1083 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1084 ok( mod == NULL, "got %p\n", mod );
1086 mod_kernel32 = LoadLibraryA( "kernel32" );
1088 SetLastError( 0xdeadbeef );
1089 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
1090 error = GetLastError();
1091 ok( !ret, "unexpected success\n" );
1092 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1094 SetLastError( 0xdeadbeef );
1095 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, NULL );
1096 error = GetLastError();
1097 ok( !ret, "unexpected success\n" );
1098 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1100 SetLastError( 0xdeadbeef );
1101 mod = (HMODULE)0xdeadbeef;
1102 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, &mod );
1103 ok( ret, "unexpected failure %lu\n", GetLastError() );
1104 ok( mod == mod_kernel32, "got %p\n", mod );
1105 FreeLibrary( mod );
1107 SetLastError( 0xdeadbeef );
1108 mod = (HMODULE)0xdeadbeef;
1109 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)0xbeefdead, &mod );
1110 error = GetLastError();
1111 ok( !ret, "unexpected success\n" );
1112 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1113 ok( mod == NULL, "got %p\n", mod );
1115 SetLastError( 0xdeadbeef );
1116 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
1117 error = GetLastError();
1118 ok( !ret, "unexpected success\n" );
1119 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1121 SetLastError( 0xdeadbeef );
1122 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, NULL );
1123 error = GetLastError();
1124 ok( !ret, "unexpected success\n" );
1125 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1127 SetLastError( 0xdeadbeef );
1128 mod = (HMODULE)0xdeadbeef;
1129 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, &mod );
1130 ok( ret, "unexpected failure %lu\n", GetLastError() );
1131 ok( mod == mod_kernel32, "got %p\n", mod );
1132 FreeLibrary( mod );
1134 SetLastError( 0xdeadbeef );
1135 mod = (HMODULE)0xdeadbeef;
1136 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)0xbeefdead, &mod );
1137 error = GetLastError();
1138 ok( !ret, "unexpected success\n" );
1139 ok( error == ERROR_MOD_NOT_FOUND, "got %lu\n", error );
1140 ok( mod == NULL, "got %p\n", mod );
1142 SetLastError( 0xdeadbeef );
1143 mod = (HMODULE)0xdeadbeef;
1144 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
1145 | GET_MODULE_HANDLE_EX_FLAG_PIN, (LPCWSTR)mod_kernel32, &mod );
1146 error = GetLastError();
1147 ok( !ret, "unexpected success\n" );
1148 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1149 ok( mod == NULL, "got %p\n", mod );
1151 SetLastError( 0xdeadbeef );
1152 mod = (HMODULE)0xdeadbeef;
1153 ret = GetModuleHandleExW( 8, kernel32W, &mod );
1154 error = GetLastError();
1155 ok( !ret, "unexpected success\n" );
1156 ok( error == ERROR_INVALID_PARAMETER, "got %lu\n", error );
1157 ok( mod == NULL, "got %p\n", mod );
1159 FreeLibrary( mod_kernel32 );
1162 static void testK32GetModuleInformation(void)
1164 MODULEINFO info;
1165 HMODULE mod;
1166 BOOL ret;
1168 mod = GetModuleHandleA(NULL);
1169 memset(&info, 0xAA, sizeof(info));
1170 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1171 ok(ret, "K32GetModuleInformation failed for main module\n");
1172 ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
1173 ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
1175 mod = GetModuleHandleA("kernel32.dll");
1176 memset(&info, 0xAA, sizeof(info));
1177 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1178 ok(ret, "K32GetModuleInformation failed for kernel32 module\n");
1179 ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
1180 ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
1183 static void test_AddDllDirectory(void)
1185 static const WCHAR tmpW[] = {'t','m','p',0};
1186 static const WCHAR dotW[] = {'.','\\','.',0};
1187 static const WCHAR rootW[] = {'\\',0};
1188 WCHAR path[MAX_PATH], buf[MAX_PATH];
1189 DLL_DIRECTORY_COOKIE cookie;
1190 BOOL ret;
1192 if (!pAddDllDirectory || !pRemoveDllDirectory)
1194 win_skip( "AddDllDirectory not available\n" );
1195 return;
1198 buf[0] = '\0';
1199 GetTempPathW(ARRAY_SIZE(path), path );
1200 ret = GetTempFileNameW( path, tmpW, 0, buf );
1201 ok( ret, "GetTempFileName failed err %lu\n", GetLastError() );
1202 SetLastError( 0xdeadbeef );
1203 cookie = pAddDllDirectory( buf );
1204 ok( cookie != NULL, "AddDllDirectory failed err %lu\n", GetLastError() );
1205 SetLastError( 0xdeadbeef );
1206 ret = pRemoveDllDirectory( cookie );
1207 ok( ret, "RemoveDllDirectory failed err %lu\n", GetLastError() );
1209 DeleteFileW( buf );
1210 SetLastError( 0xdeadbeef );
1211 cookie = pAddDllDirectory( buf );
1212 ok( !cookie, "AddDllDirectory succeeded\n" );
1213 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %lu\n", GetLastError() );
1214 cookie = pAddDllDirectory( dotW );
1215 ok( !cookie, "AddDllDirectory succeeded\n" );
1216 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1217 cookie = pAddDllDirectory( rootW );
1218 ok( cookie != NULL, "AddDllDirectory failed err %lu\n", GetLastError() );
1219 SetLastError( 0xdeadbeef );
1220 ret = pRemoveDllDirectory( cookie );
1221 ok( ret, "RemoveDllDirectory failed err %lu\n", GetLastError() );
1222 GetWindowsDirectoryW( buf, MAX_PATH );
1223 lstrcpyW( buf + 2, tmpW );
1224 cookie = pAddDllDirectory( buf );
1225 ok( !cookie, "AddDllDirectory succeeded\n" );
1226 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1229 static void test_SetDefaultDllDirectories(void)
1231 HMODULE mod;
1232 BOOL ret;
1234 if (!pSetDefaultDllDirectories)
1236 win_skip( "SetDefaultDllDirectories not available\n" );
1237 return;
1240 mod = LoadLibraryA( "authz.dll" );
1241 ok( mod != NULL, "loading authz failed\n" );
1242 FreeLibrary( mod );
1243 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_USER_DIRS );
1244 ok( ret, "SetDefaultDllDirectories failed err %lu\n", GetLastError() );
1245 mod = LoadLibraryA( "authz.dll" );
1246 ok( !mod, "loading authz succeeded\n" );
1247 FreeLibrary( mod );
1248 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_SYSTEM32 );
1249 ok( ret, "SetDefaultDllDirectories failed err %lu\n", GetLastError() );
1250 mod = LoadLibraryA( "authz.dll" );
1251 ok( mod != NULL, "loading authz failed\n" );
1252 FreeLibrary( mod );
1253 mod = LoadLibraryExA( "authz.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1254 ok( !mod, "loading authz succeeded\n" );
1255 FreeLibrary( mod );
1256 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1257 ok( ret, "SetDefaultDllDirectories failed err %lu\n", GetLastError() );
1258 mod = LoadLibraryA( "authz.dll" );
1259 ok( !mod, "loading authz succeeded\n" );
1260 FreeLibrary( mod );
1261 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1262 ok( ret, "SetDefaultDllDirectories failed err %lu\n", GetLastError() );
1263 mod = LoadLibraryA( "authz.dll" );
1264 ok( mod != NULL, "loading authz failed\n" );
1265 FreeLibrary( mod );
1267 SetLastError( 0xdeadbeef );
1268 ret = pSetDefaultDllDirectories( 0 );
1269 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1270 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1272 SetLastError( 0xdeadbeef );
1273 ret = pSetDefaultDllDirectories( 3 );
1274 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1275 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1277 SetLastError( 0xdeadbeef );
1278 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR | 0x8000 );
1279 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1280 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1282 SetLastError( 0xdeadbeef );
1283 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
1284 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1285 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1287 SetLastError( 0xdeadbeef );
1288 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_USER_DIRS );
1289 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1290 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1292 /* restore some sane defaults */
1293 pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1296 static void check_refcount( HMODULE mod, unsigned int refcount )
1298 unsigned int i;
1299 BOOL ret;
1301 for (i = 0; i < min( refcount, 10 ); ++i)
1303 ret = FreeLibrary( mod );
1304 ok( ret || broken( refcount == ~0u && GetLastError() == ERROR_MOD_NOT_FOUND && i == 2 ) /* Win8 */,
1305 "Refcount test failed, i %u, error %lu.\n", i, GetLastError() );
1306 if (!ret) return;
1308 if (refcount != ~0u)
1310 ret = FreeLibrary( mod );
1311 ok( !ret && GetLastError() == ERROR_MOD_NOT_FOUND, "Refcount test failed, ret %d, error %lu.\n",
1312 ret, GetLastError() );
1316 static void test_LdrGetDllHandleEx(void)
1318 HMODULE mod, loaded_mod;
1319 UNICODE_STRING name;
1320 NTSTATUS status;
1321 unsigned int i;
1323 if (!pLdrGetDllHandleEx)
1325 win_skip( "LdrGetDllHandleEx is not available.\n" );
1326 return;
1329 RtlInitUnicodeString( &name, L"unknown.dll" );
1330 status = pLdrGetDllHandleEx( 0, NULL, NULL, &name, &mod );
1331 ok( status == STATUS_DLL_NOT_FOUND, "Got unexpected status %#lx.\n", status );
1333 RtlInitUnicodeString( &name, L"authz.dll" );
1334 loaded_mod = LoadLibraryW( name.Buffer );
1335 ok( !!loaded_mod, "Failed to load module.\n" );
1336 status = pLdrGetDllHandleEx( 0, NULL, NULL, &name, &mod );
1337 ok( !status, "Got unexpected status %#lx.\n", status );
1338 ok( mod == loaded_mod, "got %p\n", mod );
1339 winetest_push_context( "Flags 0" );
1340 check_refcount( loaded_mod, 2 );
1341 winetest_pop_context();
1343 loaded_mod = LoadLibraryW( name.Buffer );
1344 ok( !!loaded_mod, "Failed to load module.\n" );
1345 status = pLdrGetDllHandleEx( LDR_GET_DLL_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL,
1346 NULL, &name, &mod );
1347 ok( !status, "Got unexpected status %#lx.\n", status );
1348 ok( mod == loaded_mod, "got %p\n", mod );
1349 winetest_push_context( "LDR_GET_DLL_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT" );
1350 check_refcount( loaded_mod, 1 );
1351 winetest_pop_context();
1353 loaded_mod = LoadLibraryW( name.Buffer );
1354 ok( !!loaded_mod, "Failed to load module.\n" );
1355 status = pLdrGetDllHandle( NULL, ~0u, &name, &mod );
1356 ok( !status, "Got unexpected status %#lx.\n", status );
1357 ok( mod == loaded_mod, "got %p\n", mod );
1358 winetest_push_context( "LdrGetDllHandle" );
1359 check_refcount( loaded_mod, 1 );
1360 winetest_pop_context();
1362 loaded_mod = LoadLibraryW( name.Buffer );
1363 ok( !!loaded_mod, "Failed to load module.\n" );
1364 status = pLdrGetDllHandleEx( 4, NULL, NULL, (void *)&name, &mod );
1365 ok( !status, "Got unexpected status %#lx.\n", status );
1366 ok( mod == loaded_mod, "got %p\n", mod );
1367 winetest_push_context( "Flag 4" );
1368 check_refcount( loaded_mod, 2 );
1369 winetest_pop_context();
1371 for (i = 3; i < 32; ++i)
1373 loaded_mod = LoadLibraryW( name.Buffer );
1374 ok( !!loaded_mod, "Failed to load module.\n" );
1375 status = pLdrGetDllHandleEx( 1 << i, NULL, NULL, &name, &mod );
1376 ok( status == STATUS_INVALID_PARAMETER, "Got unexpected status %#lx.\n", status );
1377 winetest_push_context( "Invalid flags, i %u", i );
1378 check_refcount( loaded_mod, 1 );
1379 winetest_pop_context();
1382 status = pLdrGetDllHandleEx( LDR_GET_DLL_HANDLE_EX_FLAG_PIN | LDR_GET_DLL_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
1383 NULL, NULL, &name, &mod );
1384 ok( status == STATUS_INVALID_PARAMETER, "Got unexpected status %#lx.\n", status );
1386 loaded_mod = LoadLibraryW( name.Buffer );
1387 ok( !!loaded_mod, "Failed to load module.\n" );
1388 status = pLdrGetDllHandleEx( LDR_GET_DLL_HANDLE_EX_FLAG_PIN, NULL,
1389 NULL, &name, &mod );
1390 ok( !status, "Got unexpected status %#lx.\n", status );
1391 ok( mod == loaded_mod, "got %p\n", mod );
1392 winetest_push_context( "LDR_GET_DLL_HANDLE_EX_FLAG_PIN" );
1393 check_refcount( loaded_mod, ~0u );
1394 winetest_pop_context();
1397 static void test_LdrGetDllFullName(void)
1399 WCHAR expected_path[MAX_PATH], path_buffer[MAX_PATH];
1400 UNICODE_STRING path = {0, 0, path_buffer};
1401 WCHAR expected_terminator;
1402 NTSTATUS status;
1403 HMODULE ntdll;
1405 if (!pLdrGetDllFullName)
1407 win_skip( "LdrGetDllFullName not available.\n" );
1408 return;
1411 if (0) /* crashes on Windows */
1412 pLdrGetDllFullName( ntdll, NULL );
1414 ntdll = GetModuleHandleW( L"ntdll.dll" );
1416 memset( path_buffer, 0x23, sizeof(path_buffer) );
1418 status = pLdrGetDllFullName( ntdll, &path );
1419 ok( status == STATUS_BUFFER_TOO_SMALL, "Got unexpected status %#lx.\n", status );
1420 ok( path.Length == 0, "Expected length 0, got %d.\n", path.Length );
1421 ok( path_buffer[0] == 0x2323, "Expected 0x2323, got 0x%x.\n", path_buffer[0] );
1423 GetSystemDirectoryW( expected_path, ARRAY_SIZE(expected_path) );
1424 path.MaximumLength = 5; /* odd numbers produce partially copied characters */
1426 status = pLdrGetDllFullName( ntdll, &path );
1427 ok( status == STATUS_BUFFER_TOO_SMALL, "Got unexpected status %#lx.\n", status );
1428 ok( path.Length == path.MaximumLength, "Expected length %u, got %u.\n", path.MaximumLength, path.Length );
1429 expected_terminator = 0x2300 | (expected_path[path.MaximumLength / sizeof(WCHAR)] & 0xFF);
1430 ok( path_buffer[path.MaximumLength / sizeof(WCHAR)] == expected_terminator,
1431 "Expected 0x%x, got 0x%x.\n", expected_terminator, path_buffer[path.MaximumLength / 2] );
1432 path_buffer[path.MaximumLength / sizeof(WCHAR)] = 0;
1433 expected_path[path.MaximumLength / sizeof(WCHAR)] = 0;
1434 ok( lstrcmpW(path_buffer, expected_path) == 0, "Expected %s, got %s.\n",
1435 wine_dbgstr_w(expected_path), wine_dbgstr_w(path_buffer) );
1437 GetSystemDirectoryW( expected_path, ARRAY_SIZE(expected_path) );
1438 lstrcatW( expected_path, L"\\ntdll.dll" );
1439 path.MaximumLength = sizeof(path_buffer);
1441 status = pLdrGetDllFullName( ntdll, &path );
1442 ok( status == STATUS_SUCCESS, "Got unexpected status %#lx.\n", status );
1443 ok( !lstrcmpiW(path_buffer, expected_path), "Expected %s, got %s\n",
1444 wine_dbgstr_w(expected_path), wine_dbgstr_w(path_buffer) );
1446 status = pLdrGetDllFullName( NULL, &path );
1447 ok( status == STATUS_SUCCESS, "Got unexpected status %#lx.\n", status );
1448 GetModuleFileNameW( NULL, expected_path, ARRAY_SIZE(expected_path) );
1449 ok( !lstrcmpiW(path_buffer, expected_path), "Expected %s, got %s.\n",
1450 wine_dbgstr_w(expected_path), wine_dbgstr_w(path_buffer) );
1453 static void test_apisets(void)
1455 static const struct
1457 const char *name;
1458 BOOLEAN present;
1459 NTSTATUS status;
1460 BOOLEAN present_ex, in_schema, broken;
1462 tests[] =
1464 { "api-ms-win-core-console-l1-1-0", TRUE, STATUS_SUCCESS, TRUE, TRUE },
1465 { "api-ms-win-core-console-l1-1-0.dll", TRUE, STATUS_INVALID_PARAMETER },
1466 { "api-ms-win-core-console-l1-1-9", TRUE, STATUS_SUCCESS, FALSE, FALSE, TRUE },
1467 { "api-ms-win-core-console-l1-1-9.dll", TRUE, STATUS_INVALID_PARAMETER, FALSE, FALSE, TRUE },
1468 { "api-ms-win-core-console-l1-1", FALSE, STATUS_SUCCESS },
1469 { "api-ms-win-core-console-l1-1-0.fake", TRUE, STATUS_INVALID_PARAMETER, FALSE, FALSE, TRUE },
1470 { "api-ms-win-foo-bar-l1-1-0", FALSE, STATUS_SUCCESS },
1471 { "api-ms-win-foo-bar-l1-1-0.dll", FALSE, STATUS_INVALID_PARAMETER },
1472 { "ext-ms-win-gdi-draw-l1-1-1", TRUE, STATUS_SUCCESS, TRUE, TRUE },
1473 { "ext-ms-win-gdi-draw-l1-1-1.dll", TRUE, STATUS_INVALID_PARAMETER },
1474 { "api-ms-win-deprecated-apis-advapi-l1-1-0", FALSE, STATUS_SUCCESS, FALSE, TRUE },
1475 { "foo", FALSE, STATUS_INVALID_PARAMETER },
1476 { "foo.dll", FALSE, STATUS_INVALID_PARAMETER },
1477 { "", FALSE, STATUS_INVALID_PARAMETER },
1479 unsigned int i;
1480 NTSTATUS status;
1481 BOOLEAN present, in_schema;
1482 UNICODE_STRING name;
1484 if (!pApiSetQueryApiSetPresence)
1486 win_skip( "ApiSetQueryApiSetPresence not implemented\n" );
1487 return;
1489 if (!pApiSetQueryApiSetPresenceEx) win_skip( "ApiSetQueryApiSetPresenceEx not implemented\n" );
1490 if (!pIsApiSetImplemented) win_skip( "IsApiSetImplemented not implemented\n" );
1492 for (i = 0; i < ARRAY_SIZE(tests); i++)
1494 RtlCreateUnicodeStringFromAsciiz( &name, tests[i].name );
1495 name.Buffer[name.Length / sizeof(WCHAR)] = 0xcccc; /* test without null termination */
1496 winetest_push_context( "%u:%s", i, tests[i].name );
1497 present = 0xff;
1498 status = pApiSetQueryApiSetPresence( &name, &present );
1499 ok( status == STATUS_SUCCESS, "wrong ret %lx\n", status );
1500 ok( present == tests[i].present || broken(!present && tests[i].broken) /* win8 */,
1501 "wrong present %u\n", present );
1502 if (pApiSetQueryApiSetPresenceEx)
1504 present = in_schema = 0xff;
1505 status = pApiSetQueryApiSetPresenceEx( &name, &in_schema, &present );
1506 ok( status == tests[i].status, "wrong ret %lx\n", status );
1507 if (!status)
1509 ok( in_schema == tests[i].in_schema, "wrong in_schema %u\n", in_schema );
1510 ok( present == tests[i].present_ex, "wrong present %u\n", present );
1512 else
1514 ok( in_schema == 0xff, "wrong in_schema %u\n", in_schema );
1515 ok( present == 0xff, "wrong present %u\n", present );
1518 if (pIsApiSetImplemented)
1520 BOOL ret = pIsApiSetImplemented( tests[i].name );
1521 ok( ret == (!tests[i].status && tests[i].present_ex), "wrong result %u\n", ret );
1523 winetest_pop_context();
1524 RtlFreeUnicodeString( &name );
1528 static void test_ddag_node(void)
1530 static const struct
1532 const WCHAR *dllname;
1533 BOOL optional;
1535 expected_exe_dependencies[] =
1537 { L"advapi32.dll" },
1538 { L"msvcrt.dll", TRUE },
1539 { L"user32.dll", TRUE },
1541 LDR_DDAG_NODE *node, *dep_node, *prev_node;
1542 LDR_DATA_TABLE_ENTRY *mod, *mod2;
1543 SINGLE_LIST_ENTRY *se, *se2;
1544 LDR_DEPENDENCY *dep, *dep2;
1545 NTSTATUS status;
1546 unsigned int i;
1547 HMODULE hexe;
1549 hexe = GetModuleHandleW( NULL );
1550 ok( !!hexe, "Got NULL exe handle.\n" );
1552 status = LdrFindEntryForAddress( hexe, &mod );
1553 ok( !status, "Got unexpected status %#lx.\n", status );
1555 if (!(node = mod->DdagNode))
1557 win_skip( "DdagNode is NULL, skipping tests.\n" );
1558 return;
1561 ok( !!node->Modules.Flink, "Got NULL module link.\n" );
1562 mod2 = CONTAINING_RECORD(node->Modules.Flink, LDR_DATA_TABLE_ENTRY, NodeModuleLink);
1563 ok( mod2 == mod || broken( (void **)mod2 == (void **)mod - 1 ), "Got unexpected mod2 %p, expected %p.\n",
1564 mod2, mod );
1565 if (mod2 != mod)
1567 win_skip( "Old LDR_DATA_TABLE_ENTRY structure, skipping tests.\n" );
1568 return;
1570 ok( node->Modules.Flink->Flink == &node->Modules, "Expected one element in list.\n" );
1572 ok( !node->IncomingDependencies.Tail, "Expected empty incoming dependencies list.\n" );
1574 /* node->Dependencies.Tail is NULL on Windows 10 1507-1607 32 bit test, maybe due to broken structure layout. */
1575 ok( !!node->Dependencies.Tail || broken( sizeof(void *) == 4 && !node->Dependencies.Tail ),
1576 "Expected nonempty dependencies list.\n" );
1577 if (!node->Dependencies.Tail)
1579 win_skip( "Empty dependencies list.\n" );
1580 return;
1582 todo_wine ok( node->LoadCount == -1, "Got unexpected LoadCount %ld.\n", node->LoadCount );
1584 prev_node = NULL;
1585 se = node->Dependencies.Tail;
1586 for (i = 0; i < ARRAY_SIZE(expected_exe_dependencies); ++i)
1588 winetest_push_context( "Dep %u (%s)", i, debugstr_w(expected_exe_dependencies[i].dllname) );
1590 se = se->Next;
1592 ok( !!se, "Got NULL list element.\n" );
1593 dep = CONTAINING_RECORD(se, LDR_DEPENDENCY, dependency_to_entry);
1594 ok( dep->dependency_from == node, "Got unexpected dependency_from %p.\n", dep->dependency_from );
1595 ok( !!dep->dependency_to, "Got null dependency_to.\n" );
1596 dep_node = dep->dependency_to;
1597 ok( !!dep_node, "Got NULL dep_node.\n" );
1599 if (dep_node == prev_node && expected_exe_dependencies[i].optional)
1601 win_skip( "Module is not directly referenced.\n" );
1602 winetest_pop_context();
1603 prev_node = dep_node;
1604 continue;
1607 mod2 = CONTAINING_RECORD(dep_node->Modules.Flink, LDR_DATA_TABLE_ENTRY, NodeModuleLink);
1608 ok( !lstrcmpW( mod2->BaseDllName.Buffer, expected_exe_dependencies[i].dllname ),
1609 "Got unexpected module %s.\n", debugstr_w(mod2->BaseDllName.Buffer));
1611 se2 = dep_node->IncomingDependencies.Tail;
1612 ok( !!se2, "Got empty incoming dependencies list.\n" );
1615 se2 = se2->Next;
1616 dep2 = CONTAINING_RECORD(se2, LDR_DEPENDENCY, dependency_from_entry);
1618 while (dep2 != dep && se2 != dep_node->IncomingDependencies.Tail);
1619 ok( dep2 == dep, "Dependency not found in incoming deps list.\n" );
1621 todo_wine ok( dep_node->LoadCount > 0 || broken(!dep_node->LoadCount) /* Win8 */,
1622 "Got unexpected LoadCount %ld.\n", dep_node->LoadCount );
1624 winetest_pop_context();
1625 prev_node = dep_node;
1627 ok( se == node->Dependencies.Tail, "Expected end of the list.\n" );
1630 START_TEST(module)
1632 WCHAR filenameW[MAX_PATH];
1634 /* Test if we can use GetModuleFileNameW */
1636 SetLastError(0xdeadbeef);
1637 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
1638 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1640 win_skip("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
1641 is_unicode_enabled = FALSE;
1644 init_pointers();
1646 testGetModuleFileName(NULL);
1647 testGetModuleFileName("kernel32.dll");
1648 testGetModuleFileName_Wrong();
1650 testGetDllDirectory();
1652 testLoadLibraryA();
1653 testNestedLoadLibraryA();
1654 testLoadLibraryA_Wrong();
1655 testGetProcAddress_Wrong();
1656 testLoadLibraryEx();
1657 test_LoadLibraryEx_search_flags();
1658 testGetModuleHandleEx();
1659 testK32GetModuleInformation();
1660 test_AddDllDirectory();
1661 test_SetDefaultDllDirectories();
1662 test_LdrGetDllHandleEx();
1663 test_LdrGetDllFullName();
1664 test_apisets();
1665 test_ddag_node();