include: Make sure __int64 is correctly defined on PPC64.
[wine.git] / dlls / kernel32 / tests / module.c
blob6eb2fc804d1bcd31ed3a5ff6f7a640de05e6f550
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 *pLdrGetDllDirectory)(UNICODE_STRING*);
41 static NTSTATUS (WINAPI *pLdrSetDllDirectory)(UNICODE_STRING*);
43 static BOOL is_unicode_enabled = TRUE;
45 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
47 WCHAR aw[1024];
49 DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
50 a, lenA, aw, ARRAY_SIZE(aw));
51 if (len != lenB) return FALSE;
52 return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
55 static const struct
57 IMAGE_DOS_HEADER dos;
58 IMAGE_NT_HEADERS nt;
59 IMAGE_SECTION_HEADER section;
60 } dll_image =
62 { IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 }, 0, 0, { 0 },
63 sizeof(IMAGE_DOS_HEADER) },
65 IMAGE_NT_SIGNATURE, /* Signature */
67 #if defined __i386__
68 IMAGE_FILE_MACHINE_I386, /* Machine */
69 #elif defined __x86_64__
70 IMAGE_FILE_MACHINE_AMD64, /* Machine */
71 #elif defined __arm__
72 IMAGE_FILE_MACHINE_ARMNT, /* Machine */
73 #elif defined __aarch64__
74 IMAGE_FILE_MACHINE_ARM64, /* Machine */
75 #else
76 # error You must specify the machine type
77 #endif
78 1, /* NumberOfSections */
79 0, /* TimeDateStamp */
80 0, /* PointerToSymbolTable */
81 0, /* NumberOfSymbols */
82 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
83 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
85 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
86 1, /* MajorLinkerVersion */
87 0, /* MinorLinkerVersion */
88 0, /* SizeOfCode */
89 0, /* SizeOfInitializedData */
90 0, /* SizeOfUninitializedData */
91 0, /* AddressOfEntryPoint */
92 0x1000, /* BaseOfCode */
93 #ifndef _WIN64
94 0, /* BaseOfData */
95 #endif
96 0x10000000, /* ImageBase */
97 0x1000, /* SectionAlignment */
98 0x1000, /* FileAlignment */
99 4, /* MajorOperatingSystemVersion */
100 0, /* MinorOperatingSystemVersion */
101 1, /* MajorImageVersion */
102 0, /* MinorImageVersion */
103 4, /* MajorSubsystemVersion */
104 0, /* MinorSubsystemVersion */
105 0, /* Win32VersionValue */
106 0x2000, /* SizeOfImage */
107 sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS), /* SizeOfHeaders */
108 0, /* CheckSum */
109 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
110 0, /* DllCharacteristics */
111 0, /* SizeOfStackReserve */
112 0, /* SizeOfStackCommit */
113 0, /* SizeOfHeapReserve */
114 0, /* SizeOfHeapCommit */
115 0, /* LoaderFlags */
116 0, /* NumberOfRvaAndSizes */
117 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
120 { ".rodata", { 0 }, 0x1000, 0x1000, 0, 0, 0, 0, 0,
121 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ }
124 static void create_test_dll( const char *name )
126 DWORD dummy;
127 HANDLE handle = CreateFileA( name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0 );
129 ok( handle != INVALID_HANDLE_VALUE, "failed to create file err %u\n", GetLastError() );
130 WriteFile( handle, &dll_image, sizeof(dll_image), &dummy, NULL );
131 SetFilePointer( handle, dll_image.nt.OptionalHeader.SizeOfImage, NULL, FILE_BEGIN );
132 SetEndOfFile( handle );
133 CloseHandle( handle );
136 static void testGetModuleFileName(const char* name)
138 HMODULE hMod;
139 char bufA[MAX_PATH];
140 WCHAR bufW[MAX_PATH];
141 DWORD len1A, len1W = 0, len2A, len2W = 0;
143 hMod = (name) ? GetModuleHandleA(name) : NULL;
145 /* first test, with enough space in buffer */
146 memset(bufA, '-', sizeof(bufA));
147 SetLastError(0xdeadbeef);
148 len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
149 ok(GetLastError() == ERROR_SUCCESS ||
150 broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
151 "LastError was not reset: %u\n", GetLastError());
152 ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
154 if (is_unicode_enabled)
156 memset(bufW, '-', sizeof(bufW));
157 SetLastError(0xdeadbeef);
158 len1W = GetModuleFileNameW(hMod, bufW, ARRAY_SIZE(bufW));
159 ok(GetLastError() == ERROR_SUCCESS ||
160 broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
161 "LastError was not reset: %u\n", GetLastError());
162 ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
165 ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%d/%d)\n", len1A, lstrlenA(bufA));
167 if (is_unicode_enabled)
169 ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%d/%d)\n", len1W, lstrlenW(bufW));
170 ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
173 /* second test with a buffer too small */
174 memset(bufA, '-', sizeof(bufA));
175 len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
176 ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
178 if (is_unicode_enabled)
180 memset(bufW, '-', sizeof(bufW));
181 len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
182 ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
183 ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
184 ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%d/%d)\n", len1W / 2, len2W);
187 ok(len1A / 2 == len2A,
188 "Correct length in GetModuleFilenameA with buffer too small (%d/%d)\n", len1A / 2, len2A);
191 static void testGetModuleFileName_Wrong(void)
193 char bufA[MAX_PATH];
194 WCHAR bufW[MAX_PATH];
196 /* test wrong handle */
197 if (is_unicode_enabled)
199 bufW[0] = '*';
200 ok(GetModuleFileNameW((void*)0xffffffff, bufW, ARRAY_SIZE(bufW)) == 0,
201 "Unexpected success in module handle\n");
202 ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
205 bufA[0] = '*';
206 ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
207 ok(bufA[0] == '*', "When failing, buffer shouldn't be written to\n");
210 static void testLoadLibraryA(void)
212 HMODULE hModule, hModule1;
213 FARPROC fp;
215 SetLastError(0xdeadbeef);
216 hModule = LoadLibraryA("kernel32.dll");
217 ok( hModule != NULL, "kernel32.dll should be loadable\n");
218 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
220 fp = GetProcAddress(hModule, "CreateFileA");
221 ok( fp != NULL, "CreateFileA should be there\n");
222 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
224 SetLastError(0xdeadbeef);
225 hModule1 = LoadLibraryA("kernel32 ");
226 ok( hModule1 != NULL, "\"kernel32 \" should be loadable\n" );
227 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError() );
228 ok( hModule == hModule1, "Loaded wrong module\n" );
229 FreeLibrary(hModule1);
230 FreeLibrary(hModule);
233 static void testNestedLoadLibraryA(void)
235 static const char dllname[] = "shell32.dll";
236 char path1[MAX_PATH], path2[MAX_PATH];
237 HMODULE hModule1, hModule2, hModule3;
239 /* This is not really a Windows conformance test, but more a Wine
240 * regression test. Wine's builtin dlls can be loaded from multiple paths,
241 * and this test tries to make sure that Wine does not get confused and
242 * really unloads the Unix .so file at the right time. Failure to do so
243 * will result in the dll being unloadable.
244 * This test must be done with a dll that can be unloaded, which means:
245 * - it must not already be loaded
246 * - it must not have a 16-bit counterpart
248 GetWindowsDirectoryA(path1, sizeof(path1));
249 strcat(path1, "\\system\\");
250 strcat(path1, dllname);
251 hModule1 = LoadLibraryA(path1);
252 if (!hModule1)
254 /* We must be on Windows, so we cannot test */
255 return;
258 GetWindowsDirectoryA(path2, sizeof(path2));
259 strcat(path2, "\\system32\\");
260 strcat(path2, dllname);
261 hModule2 = LoadLibraryA(path2);
262 ok(hModule2 != NULL, "LoadLibrary(%s) failed\n", path2);
264 /* The first LoadLibrary() call may have registered the dll under the
265 * system32 path. So load it, again, under the '...\system\...' path so
266 * Wine does not immediately notice that it is already loaded.
268 hModule3 = LoadLibraryA(path1);
269 ok(hModule3 != NULL, "LoadLibrary(%s) failed\n", path1);
271 /* Now fully unload the dll */
272 ok(FreeLibrary(hModule3), "FreeLibrary() failed\n");
273 ok(FreeLibrary(hModule2), "FreeLibrary() failed\n");
274 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
275 ok(GetModuleHandleA(dllname) == NULL, "%s was not fully unloaded\n", dllname);
277 /* Try to load the dll again, if refcounting is ok, this should work */
278 hModule1 = LoadLibraryA(path1);
279 ok(hModule1 != NULL, "LoadLibrary(%s) failed\n", path1);
280 if (hModule1 != NULL)
281 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
284 static void testLoadLibraryA_Wrong(void)
286 HMODULE hModule;
288 /* Try to load a nonexistent dll */
289 SetLastError(0xdeadbeef);
290 hModule = LoadLibraryA("non_ex_pv.dll");
291 ok( !hModule, "non_ex_pv.dll should be not loadable\n");
292 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %d\n", GetLastError() );
294 /* Just in case */
295 FreeLibrary(hModule);
298 static void testGetProcAddress_Wrong(void)
300 FARPROC fp;
302 SetLastError(0xdeadbeef);
303 fp = GetProcAddress(NULL, "non_ex_call");
304 ok( !fp, "non_ex_call should not be found\n");
305 ok( GetLastError() == ERROR_PROC_NOT_FOUND, "Expected ERROR_PROC_NOT_FOUND, got %d\n", GetLastError() );
307 SetLastError(0xdeadbeef);
308 fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
309 ok( !fp, "non_ex_call should not be found\n");
310 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %d\n", GetLastError() );
313 static void testLoadLibraryEx(void)
315 CHAR path[MAX_PATH];
316 HMODULE hmodule;
317 HANDLE hfile;
318 BOOL ret;
320 hfile = CreateFileA("testfile.dll", GENERIC_READ | GENERIC_WRITE,
321 FILE_SHARE_READ | FILE_SHARE_WRITE,
322 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
323 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
325 /* NULL lpFileName */
326 SetLastError(0xdeadbeef);
327 hmodule = LoadLibraryExA(NULL, NULL, 0);
328 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
329 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
330 GetLastError() == ERROR_INVALID_PARAMETER,
331 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
333 /* empty lpFileName */
334 SetLastError(0xdeadbeef);
335 hmodule = LoadLibraryExA("", NULL, 0);
336 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
337 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
338 GetLastError() == ERROR_INVALID_PARAMETER /* win8 */,
339 "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND, got %d\n", GetLastError());
341 /* hFile is non-NULL */
342 SetLastError(0xdeadbeef);
343 hmodule = LoadLibraryExA("testfile.dll", hfile, 0);
344 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
345 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
346 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
347 "Unexpected last error, got %d\n", GetLastError());
349 SetLastError(0xdeadbeef);
350 hmodule = LoadLibraryExA("testfile.dll", (HANDLE)0xdeadbeef, 0);
351 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
352 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
353 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
354 "Unexpected last error, got %d\n", GetLastError());
356 /* try to open a file that is locked */
357 SetLastError(0xdeadbeef);
358 hmodule = LoadLibraryExA("testfile.dll", NULL, 0);
359 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
360 ok(GetLastError() == ERROR_SHARING_VIOLATION,
361 "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
363 /* lpFileName does not matter */
364 if (is_unicode_enabled)
366 SetLastError(0xdeadbeef);
367 hmodule = LoadLibraryExA(NULL, hfile, 0);
368 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
369 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
370 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
371 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
374 CloseHandle(hfile);
376 /* load empty file */
377 SetLastError(0xdeadbeef);
378 hmodule = LoadLibraryExA("testfile.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
379 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
380 todo_wine
382 ok(GetLastError() == ERROR_FILE_INVALID,
383 "Expected ERROR_FILE_INVALID, got %d\n", GetLastError());
386 DeleteFileA("testfile.dll");
388 GetSystemDirectoryA(path, MAX_PATH);
389 if (path[lstrlenA(path) - 1] != '\\')
390 lstrcatA(path, "\\");
391 lstrcatA(path, "kernel32.dll");
393 /* load kernel32.dll with an absolute path */
394 SetLastError(0xdeadbeef);
395 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
396 ok(hmodule != 0, "Expected valid module handle\n");
397 ok(GetLastError() == 0xdeadbeef ||
398 GetLastError() == ERROR_SUCCESS,
399 "Expected 0xdeadbeef or ERROR_SUCCESS, got %d\n", GetLastError());
401 /* try invalid file handle */
402 SetLastError(0xdeadbeef);
403 hmodule = LoadLibraryExA(path, (HANDLE)0xdeadbeef, 0);
404 if (!hmodule) /* succeeds on xp and older */
405 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
407 FreeLibrary(hmodule);
409 /* load kernel32.dll with no path */
410 SetLastError(0xdeadbeef);
411 hmodule = LoadLibraryExA("kernel32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
412 ok(hmodule != 0, "Expected valid module handle\n");
413 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
415 FreeLibrary(hmodule);
417 GetCurrentDirectoryA(MAX_PATH, path);
418 if (path[lstrlenA(path) - 1] != '\\')
419 lstrcatA(path, "\\");
420 lstrcatA(path, "kernel32.dll");
422 /* load kernel32.dll with an absolute path that does not exist */
423 SetLastError(0xdeadbeef);
424 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
425 todo_wine
427 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
429 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
430 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
432 /* Free the loaded dll when it's the first time this dll is loaded
433 in process - First time should pass, second fail */
434 SetLastError(0xdeadbeef);
435 hmodule = LoadLibraryExA("comctl32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
436 ok(hmodule != 0, "Expected valid module handle\n");
438 SetLastError(0xdeadbeef);
439 ret = FreeLibrary( (HMODULE)((ULONG_PTR)hmodule + 0x1230));
440 ok(!ret, "Free succeeded on wrong handle\n");
441 ok(GetLastError() == ERROR_BAD_EXE_FORMAT, "wrong error %u\n", GetLastError());
443 SetLastError(0xdeadbeef);
444 ret = FreeLibrary(hmodule);
445 ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError());
446 SetLastError(0xdeadbeef);
447 ret = FreeLibrary(hmodule);
448 ok(!ret, "Unexpected ability to free the module, failed with %d\n", GetLastError());
450 /* load with full path, name without extension */
451 GetSystemDirectoryA(path, MAX_PATH);
452 if (path[lstrlenA(path) - 1] != '\\')
453 lstrcatA(path, "\\");
454 lstrcatA(path, "kernel32");
455 hmodule = LoadLibraryExA(path, NULL, 0);
456 ok(hmodule != NULL, "got %p\n", hmodule);
457 FreeLibrary(hmodule);
459 /* same with alterate search path */
460 hmodule = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
461 ok(hmodule != NULL, "got %p\n", hmodule);
462 FreeLibrary(hmodule);
465 static void test_LoadLibraryEx_search_flags(void)
467 static const struct
469 int add_dirs[4];
470 int dll_dir;
471 int expect;
472 } tests[] =
474 { { 1, 2, 3 }, 4, 3 }, /* 0 */
475 { { 1, 3, 2 }, 4, 2 },
476 { { 3, 1 }, 4, 1 },
477 { { 5, 6 }, 4, 4 },
478 { { 5, 2 }, 4, 2 },
479 { { 0 }, 4, 4 }, /* 5 */
480 { { 0 }, 0, 0 },
481 { { 6, 5 }, 5, 0 },
482 { { 1, 1, 2 }, 0, 2 },
484 char *p, path[MAX_PATH], buf[MAX_PATH], curdir[MAX_PATH];
485 WCHAR bufW[MAX_PATH];
486 DLL_DIRECTORY_COOKIE cookies[4];
487 unsigned int i, j, k;
488 BOOL ret;
489 HMODULE mod;
491 GetTempPathA( sizeof(path), path );
492 GetTempFileNameA( path, "tmp", 0, buf );
493 DeleteFileA( buf );
494 ret = CreateDirectoryA( buf, NULL );
495 ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
496 p = buf + strlen( buf );
497 for (i = 1; i <= 6; i++)
499 sprintf( p, "\\%u", i );
500 ret = CreateDirectoryA( buf, NULL );
501 ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
502 if (i >= 5) continue; /* dirs 5 and 6 are left empty */
503 sprintf( p, "\\%u\\winetestdll.dll", i );
504 create_test_dll( buf );
507 GetCurrentDirectoryA( MAX_PATH, curdir );
508 *p = 0;
509 SetCurrentDirectoryA( buf );
511 SetLastError( 0xdeadbeef );
512 mod = LoadLibraryA( "1\\winetestdll.dll" );
513 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
514 FreeLibrary( mod );
516 SetLastError( 0xdeadbeef );
517 sprintf( path, "%c:1\\winetestdll.dll", buf[0] );
518 mod = LoadLibraryA( path );
519 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
520 FreeLibrary( mod );
522 if (pAddDllDirectory)
524 SetLastError( 0xdeadbeef );
525 mod = LoadLibraryExA( "1\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
526 ok( !mod, "LoadLibrary succeeded\n" );
527 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
529 SetLastError( 0xdeadbeef );
530 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
531 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
532 FreeLibrary( mod );
535 strcpy( p, "\\1" );
536 SetCurrentDirectoryA( buf );
538 SetLastError( 0xdeadbeef );
539 mod = LoadLibraryA( "winetestdll.dll" );
540 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
541 FreeLibrary( mod );
543 SetLastError( 0xdeadbeef );
544 sprintf( path, "%c:winetestdll.dll", buf[0] );
545 mod = LoadLibraryA( path );
546 ok( mod != NULL || broken(!mod), /* win10 disallows this but allows c:1\\winetestdll.dll */
547 "LoadLibrary failed err %u\n", GetLastError() );
548 if (!mod) ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
549 else FreeLibrary( mod );
551 SetLastError( 0xdeadbeef );
552 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
553 mod = LoadLibraryA( path );
554 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
555 FreeLibrary( mod );
557 if (pAddDllDirectory)
559 SetLastError( 0xdeadbeef );
560 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
561 ok( !mod, "LoadLibrary succeeded\n" );
562 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
564 SetLastError( 0xdeadbeef );
565 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
566 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
567 FreeLibrary( mod );
569 SetLastError( 0xdeadbeef );
570 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
571 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
572 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
573 FreeLibrary( mod );
575 SetLastError( 0xdeadbeef );
576 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
577 ok( !mod, "LoadLibrary succeeded\n" );
578 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
580 SetLastError( 0xdeadbeef );
581 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
582 ok( !mod, "LoadLibrary succeeded\n" );
583 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
585 SetLastError( 0xdeadbeef );
586 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_WITH_ALTERED_SEARCH_PATH );
587 ok( !mod, "LoadLibrary succeeded\n" );
588 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
590 SetLastError( 0xdeadbeef );
591 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
592 ok( !mod, "LoadLibrary succeeded\n" );
593 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
595 SetLastError( 0xdeadbeef );
596 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
597 ok( !mod, "LoadLibrary succeeded\n" );
598 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
601 SetCurrentDirectoryA( curdir );
603 if (!pAddDllDirectory || !pSetDllDirectoryA) goto done;
605 SetLastError( 0xdeadbeef );
606 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
607 ok( !mod, "LoadLibrary succeeded\n" );
608 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
610 if (0) /* crashes on win10 */
612 SetLastError( 0xdeadbeef );
613 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
614 ok( !mod, "LoadLibrary succeeded\n" );
615 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
616 "wrong error %u\n", GetLastError() );
619 SetLastError( 0xdeadbeef );
620 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
621 ok( !mod, "LoadLibrary succeeded\n" );
622 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
624 SetLastError( 0xdeadbeef );
625 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
626 ok( !mod, "LoadLibrary succeeded\n" );
627 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
629 SetLastError( 0xdeadbeef );
630 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 );
631 ok( !mod, "LoadLibrary succeeded\n" );
632 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
634 SetLastError( 0xdeadbeef );
635 mod = LoadLibraryExA( "foo\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
636 ok( !mod, "LoadLibrary succeeded\n" );
637 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
639 SetLastError( 0xdeadbeef );
640 mod = LoadLibraryExA( "\\windows\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
641 ok( !mod, "LoadLibrary succeeded\n" );
642 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
644 SetLastError( 0xdeadbeef );
645 mod = LoadLibraryA( "1\\winetestdll.dll" );
646 ok( !mod, "LoadLibrary succeeded\n" );
647 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
649 for (j = 0; j < ARRAY_SIZE(tests); j++)
651 for (k = 0; tests[j].add_dirs[k]; k++)
653 sprintf( p, "\\%u", tests[j].add_dirs[k] );
654 MultiByteToWideChar( CP_ACP, 0, buf, -1, bufW, MAX_PATH );
655 cookies[k] = pAddDllDirectory( bufW );
656 ok( cookies[k] != NULL, "failed to add %s\n", buf );
658 if (tests[j].dll_dir)
660 sprintf( p, "\\%u", tests[j].dll_dir );
661 pSetDllDirectoryA( buf );
663 else pSetDllDirectoryA( NULL );
665 SetLastError( 0xdeadbeef );
666 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
667 if (tests[j].expect)
669 ok( mod != NULL, "%u: LoadLibrary failed err %u\n", j, GetLastError() );
670 GetModuleFileNameA( mod, path, MAX_PATH );
671 sprintf( p, "\\%u\\winetestdll.dll", tests[j].expect );
672 ok( !lstrcmpiA( path, buf ), "%u: wrong module %s expected %s\n", j, path, buf );
674 else
676 ok( !mod, "%u: LoadLibrary succeeded\n", j );
677 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
678 "%u: wrong error %u\n", j, GetLastError() );
680 FreeLibrary( mod );
682 for (k = 0; tests[j].add_dirs[k]; k++) pRemoveDllDirectory( cookies[k] );
685 done:
686 for (i = 1; i <= 6; i++)
688 sprintf( p, "\\%u\\winetestdll.dll", i );
689 DeleteFileA( buf );
690 sprintf( p, "\\%u", i );
691 RemoveDirectoryA( buf );
693 *p = 0;
694 RemoveDirectoryA( buf );
697 static void testGetDllDirectory(void)
699 CHAR bufferA[MAX_PATH];
700 WCHAR bufferW[MAX_PATH];
701 DWORD length, ret;
702 int i;
703 static const char *dll_directories[] =
706 "C:\\Some\\Path",
707 "C:\\Some\\Path\\",
708 "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
710 const int test_count = ARRAY_SIZE(dll_directories);
712 if (!pGetDllDirectoryA || !pGetDllDirectoryW)
714 win_skip("GetDllDirectory not available\n");
715 return;
717 if (!pSetDllDirectoryA)
719 win_skip("SetDllDirectoryA not available\n");
720 return;
723 for (i = 0; i < test_count; i++)
725 length = strlen(dll_directories[i]);
726 if (!pSetDllDirectoryA(dll_directories[i]))
728 skip("i=%d, SetDllDirectoryA failed\n", i);
729 continue;
732 /* no buffer, determine length */
733 ret = pGetDllDirectoryA(0, NULL);
734 ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
736 ret = pGetDllDirectoryW(0, NULL);
737 ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
739 /* buffer of exactly the right size */
740 bufferA[length] = 'A';
741 bufferA[length + 1] = 'A';
742 ret = pGetDllDirectoryA(length + 1, bufferA);
743 ok(ret == length || broken(ret + 1 == length) /* win8 */,
744 "i=%d, Expected %u(+1), got %u\n", i, length, ret);
745 ok(bufferA[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
746 ok(strcmp(bufferA, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, bufferA);
748 bufferW[length] = 'A';
749 bufferW[length + 1] = 'A';
750 ret = pGetDllDirectoryW(length + 1, bufferW);
751 ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
752 ok(bufferW[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
753 ok(cmpStrAW(dll_directories[i], bufferW, length, length),
754 "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(bufferW));
756 /* Zero size buffer. The buffer may or may not be terminated depending
757 * on the Windows version and whether the A or W API is called. */
758 bufferA[0] = 'A';
759 ret = pGetDllDirectoryA(0, bufferA);
760 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
762 bufferW[0] = 'A';
763 ret = pGetDllDirectoryW(0, bufferW);
764 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
765 ok(bufferW[0] == 'A' || broken(bufferW[0] == 0), /* XP, 2003 */
766 "i=%d, Buffer overflow\n", i);
768 /* buffer just one too short */
769 bufferA[0] = 'A';
770 ret = pGetDllDirectoryA(length, bufferA);
771 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
772 if (length != 0)
773 ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
775 bufferW[0] = 'A';
776 ret = pGetDllDirectoryW(length, bufferW);
777 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
778 if (length != 0)
779 ok(bufferW[0] == 0, "i=%d, Buffer overflow\n", i);
781 if (0)
783 /* crashes on win8 */
784 /* no buffer, but too short length */
785 ret = pGetDllDirectoryA(length, NULL);
786 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
788 ret = pGetDllDirectoryW(length, NULL);
789 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
792 if (pLdrGetDllDirectory)
794 UNICODE_STRING str;
795 NTSTATUS status;
796 str.Buffer = bufferW;
797 str.MaximumLength = sizeof(bufferW);
798 status = pLdrGetDllDirectory( &str );
799 ok( !status, "LdrGetDllDirectory failed %x\n", status );
800 ok( cmpStrAW( dll_directories[i], bufferW, strlen(dll_directories[i]),
801 str.Length / sizeof(WCHAR) ), "%u: got %s instead of %s\n",
802 i, wine_dbgstr_w(bufferW), dll_directories[i] );
803 if (dll_directories[i][0])
805 memset( bufferW, 0xcc, sizeof(bufferW) );
806 str.MaximumLength = (strlen( dll_directories[i] ) - 1) * sizeof(WCHAR);
807 status = pLdrGetDllDirectory( &str );
808 ok( status == STATUS_BUFFER_TOO_SMALL, "%u: LdrGetDllDirectory failed %x\n", i, status );
809 ok( bufferW[0] == 0 && bufferW[1] == 0xcccc,
810 "%u: buffer %x %x\n", i, bufferW[0], bufferW[1] );
811 length = (strlen( dll_directories[i] ) + 1) * sizeof(WCHAR);
812 ok( str.Length == length, "%u: wrong len %u / %u\n", i, str.Length, length );
817 /* unset whatever we did so following tests won't be affected */
818 pSetDllDirectoryA(NULL);
821 static void init_pointers(void)
823 HMODULE mod = GetModuleHandleA("kernel32.dll");
825 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(mod, #f))
826 MAKEFUNC(GetDllDirectoryA);
827 MAKEFUNC(GetDllDirectoryW);
828 MAKEFUNC(SetDllDirectoryA);
829 MAKEFUNC(AddDllDirectory);
830 MAKEFUNC(RemoveDllDirectory);
831 MAKEFUNC(SetDefaultDllDirectories);
832 MAKEFUNC(K32GetModuleInformation);
833 mod = GetModuleHandleA( "ntdll.dll" );
834 MAKEFUNC(LdrGetDllDirectory);
835 MAKEFUNC(LdrSetDllDirectory);
836 #undef MAKEFUNC
838 /* before Windows 7 this was not exported in kernel32 */
839 if (!pK32GetModuleInformation)
841 HMODULE hPsapi = LoadLibraryA("psapi.dll");
842 pK32GetModuleInformation = (void *)GetProcAddress(hPsapi, "GetModuleInformation");
846 static void testGetModuleHandleEx(void)
848 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
849 static const WCHAR nosuchmodW[] = {'n','o','s','u','c','h','m','o','d',0};
850 BOOL ret;
851 DWORD error;
852 HMODULE mod, mod_kernel32;
854 SetLastError( 0xdeadbeef );
855 ret = GetModuleHandleExA( 0, NULL, NULL );
856 error = GetLastError();
857 ok( !ret, "unexpected success\n" );
858 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
860 SetLastError( 0xdeadbeef );
861 ret = GetModuleHandleExA( 0, "kernel32", NULL );
862 error = GetLastError();
863 ok( !ret, "unexpected success\n" );
864 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
866 SetLastError( 0xdeadbeef );
867 mod = (HMODULE)0xdeadbeef;
868 ret = GetModuleHandleExA( 0, "kernel32", &mod );
869 ok( ret, "unexpected failure %u\n", GetLastError() );
870 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
871 FreeLibrary( mod );
873 SetLastError( 0xdeadbeef );
874 mod = (HMODULE)0xdeadbeef;
875 ret = GetModuleHandleExA( 0, "nosuchmod", &mod );
876 error = GetLastError();
877 ok( !ret, "unexpected success\n" );
878 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
879 ok( mod == NULL, "got %p\n", mod );
881 SetLastError( 0xdeadbeef );
882 ret = GetModuleHandleExW( 0, NULL, NULL );
883 error = GetLastError();
884 ok( !ret, "unexpected success\n" );
885 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
887 SetLastError( 0xdeadbeef );
888 ret = GetModuleHandleExW( 0, kernel32W, NULL );
889 error = GetLastError();
890 ok( !ret, "unexpected success\n" );
891 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
893 SetLastError( 0xdeadbeef );
894 mod = (HMODULE)0xdeadbeef;
895 ret = GetModuleHandleExW( 0, kernel32W, &mod );
896 ok( ret, "unexpected failure %u\n", GetLastError() );
897 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
898 FreeLibrary( mod );
900 SetLastError( 0xdeadbeef );
901 mod = (HMODULE)0xdeadbeef;
902 ret = GetModuleHandleExW( 0, nosuchmodW, &mod );
903 error = GetLastError();
904 ok( !ret, "unexpected success\n" );
905 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
906 ok( mod == NULL, "got %p\n", mod );
908 SetLastError( 0xdeadbeef );
909 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
910 error = GetLastError();
911 ok( !ret, "unexpected success\n" );
912 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
914 SetLastError( 0xdeadbeef );
915 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", NULL );
916 error = GetLastError();
917 ok( !ret, "unexpected success\n" );
918 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
920 SetLastError( 0xdeadbeef );
921 mod = (HMODULE)0xdeadbeef;
922 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", &mod );
923 ok( ret, "unexpected failure %u\n", GetLastError() );
924 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
926 SetLastError( 0xdeadbeef );
927 mod = (HMODULE)0xdeadbeef;
928 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "nosuchmod", &mod );
929 error = GetLastError();
930 ok( !ret, "unexpected success\n" );
931 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
932 ok( mod == NULL, "got %p\n", mod );
934 SetLastError( 0xdeadbeef );
935 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
936 error = GetLastError();
937 ok( !ret, "unexpected success\n" );
938 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
940 SetLastError( 0xdeadbeef );
941 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, NULL );
942 error = GetLastError();
943 ok( !ret, "unexpected success\n" );
944 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
946 SetLastError( 0xdeadbeef );
947 mod = (HMODULE)0xdeadbeef;
948 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, &mod );
949 ok( ret, "unexpected failure %u\n", GetLastError() );
950 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
952 SetLastError( 0xdeadbeef );
953 mod = (HMODULE)0xdeadbeef;
954 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nosuchmodW, &mod );
955 error = GetLastError();
956 ok( !ret, "unexpected success\n" );
957 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
958 ok( mod == NULL, "got %p\n", mod );
960 mod_kernel32 = LoadLibraryA( "kernel32" );
962 SetLastError( 0xdeadbeef );
963 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
964 error = GetLastError();
965 ok( !ret, "unexpected success\n" );
966 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
968 SetLastError( 0xdeadbeef );
969 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, NULL );
970 error = GetLastError();
971 ok( !ret, "unexpected success\n" );
972 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
974 SetLastError( 0xdeadbeef );
975 mod = (HMODULE)0xdeadbeef;
976 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, &mod );
977 ok( ret, "unexpected failure %u\n", GetLastError() );
978 ok( mod == mod_kernel32, "got %p\n", mod );
979 FreeLibrary( mod );
981 SetLastError( 0xdeadbeef );
982 mod = (HMODULE)0xdeadbeef;
983 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)0xbeefdead, &mod );
984 error = GetLastError();
985 ok( !ret, "unexpected success\n" );
986 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
987 ok( mod == NULL, "got %p\n", mod );
989 SetLastError( 0xdeadbeef );
990 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
991 error = GetLastError();
992 ok( !ret, "unexpected success\n" );
993 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
995 SetLastError( 0xdeadbeef );
996 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, NULL );
997 error = GetLastError();
998 ok( !ret, "unexpected success\n" );
999 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
1001 SetLastError( 0xdeadbeef );
1002 mod = (HMODULE)0xdeadbeef;
1003 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, &mod );
1004 ok( ret, "unexpected failure %u\n", GetLastError() );
1005 ok( mod == mod_kernel32, "got %p\n", mod );
1006 FreeLibrary( mod );
1008 SetLastError( 0xdeadbeef );
1009 mod = (HMODULE)0xdeadbeef;
1010 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)0xbeefdead, &mod );
1011 error = GetLastError();
1012 ok( !ret, "unexpected success\n" );
1013 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
1014 ok( mod == NULL, "got %p\n", mod );
1016 FreeLibrary( mod_kernel32 );
1019 static void testK32GetModuleInformation(void)
1021 MODULEINFO info;
1022 HMODULE mod;
1023 BOOL ret;
1025 mod = GetModuleHandleA(NULL);
1026 memset(&info, 0xAA, sizeof(info));
1027 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1028 ok(ret, "K32GetModuleInformation failed for main module\n");
1029 ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
1030 ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
1032 mod = GetModuleHandleA("kernel32.dll");
1033 memset(&info, 0xAA, sizeof(info));
1034 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1035 ok(ret, "K32GetModuleInformation failed for kernel32 module\n");
1036 ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
1037 ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
1040 static void test_AddDllDirectory(void)
1042 static const WCHAR tmpW[] = {'t','m','p',0};
1043 static const WCHAR dotW[] = {'.','\\','.',0};
1044 static const WCHAR rootW[] = {'\\',0};
1045 WCHAR path[MAX_PATH], buf[MAX_PATH];
1046 DLL_DIRECTORY_COOKIE cookie;
1047 BOOL ret;
1049 if (!pAddDllDirectory || !pRemoveDllDirectory)
1051 win_skip( "AddDllDirectory not available\n" );
1052 return;
1055 buf[0] = '\0';
1056 GetTempPathW(ARRAY_SIZE(path), path );
1057 ret = GetTempFileNameW( path, tmpW, 0, buf );
1058 ok( ret, "GetTempFileName failed err %u\n", GetLastError() );
1059 SetLastError( 0xdeadbeef );
1060 cookie = pAddDllDirectory( buf );
1061 ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
1062 SetLastError( 0xdeadbeef );
1063 ret = pRemoveDllDirectory( cookie );
1064 ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
1066 DeleteFileW( buf );
1067 SetLastError( 0xdeadbeef );
1068 cookie = pAddDllDirectory( buf );
1069 ok( !cookie, "AddDllDirectory succeeded\n" );
1070 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError() );
1071 cookie = pAddDllDirectory( dotW );
1072 ok( !cookie, "AddDllDirectory succeeded\n" );
1073 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1074 cookie = pAddDllDirectory( rootW );
1075 ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
1076 SetLastError( 0xdeadbeef );
1077 ret = pRemoveDllDirectory( cookie );
1078 ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
1079 GetWindowsDirectoryW( buf, MAX_PATH );
1080 lstrcpyW( buf + 2, tmpW );
1081 cookie = pAddDllDirectory( buf );
1082 ok( !cookie, "AddDllDirectory succeeded\n" );
1083 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1086 static void test_SetDefaultDllDirectories(void)
1088 HMODULE mod;
1089 BOOL ret;
1091 if (!pSetDefaultDllDirectories)
1093 win_skip( "SetDefaultDllDirectories not available\n" );
1094 return;
1097 mod = LoadLibraryA( "authz.dll" );
1098 ok( mod != NULL, "loading authz failed\n" );
1099 FreeLibrary( mod );
1100 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_USER_DIRS );
1101 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1102 mod = LoadLibraryA( "authz.dll" );
1103 todo_wine ok( !mod, "loading authz succeeded\n" );
1104 FreeLibrary( mod );
1105 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_SYSTEM32 );
1106 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1107 mod = LoadLibraryA( "authz.dll" );
1108 ok( mod != NULL, "loading authz failed\n" );
1109 FreeLibrary( mod );
1110 mod = LoadLibraryExA( "authz.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1111 todo_wine ok( !mod, "loading authz succeeded\n" );
1112 FreeLibrary( mod );
1113 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1114 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1115 mod = LoadLibraryA( "authz.dll" );
1116 todo_wine ok( !mod, "loading authz succeeded\n" );
1117 FreeLibrary( mod );
1118 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1119 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1120 mod = LoadLibraryA( "authz.dll" );
1121 ok( mod != NULL, "loading authz failed\n" );
1122 FreeLibrary( mod );
1124 SetLastError( 0xdeadbeef );
1125 ret = pSetDefaultDllDirectories( 0 );
1126 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1127 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1129 SetLastError( 0xdeadbeef );
1130 ret = pSetDefaultDllDirectories( 3 );
1131 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1132 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1134 SetLastError( 0xdeadbeef );
1135 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR | 0x8000 );
1136 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1137 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1139 SetLastError( 0xdeadbeef );
1140 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
1141 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1142 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1144 SetLastError( 0xdeadbeef );
1145 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_USER_DIRS );
1146 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1147 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1149 /* restore some sane defaults */
1150 pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1153 START_TEST(module)
1155 WCHAR filenameW[MAX_PATH];
1157 /* Test if we can use GetModuleFileNameW */
1159 SetLastError(0xdeadbeef);
1160 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
1161 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1163 win_skip("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
1164 is_unicode_enabled = FALSE;
1167 init_pointers();
1169 testGetModuleFileName(NULL);
1170 testGetModuleFileName("kernel32.dll");
1171 testGetModuleFileName_Wrong();
1173 testGetDllDirectory();
1175 testLoadLibraryA();
1176 testNestedLoadLibraryA();
1177 testLoadLibraryA_Wrong();
1178 testGetProcAddress_Wrong();
1179 testLoadLibraryEx();
1180 test_LoadLibraryEx_search_flags();
1181 testGetModuleHandleEx();
1182 testK32GetModuleInformation();
1183 test_AddDllDirectory();
1184 test_SetDefaultDllDirectories();