winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / kernel32 / tests / module.c
blob0066f7de5a85c786a0c14704f584f7420d03255d
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 ok(GetLastError() == ERROR_FILE_INVALID, "Expected ERROR_FILE_INVALID, got %d\n", GetLastError());
382 DeleteFileA("testfile.dll");
384 GetSystemDirectoryA(path, MAX_PATH);
385 if (path[lstrlenA(path) - 1] != '\\')
386 lstrcatA(path, "\\");
387 lstrcatA(path, "kernel32.dll");
389 /* load kernel32.dll with an absolute path */
390 SetLastError(0xdeadbeef);
391 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
392 ok(hmodule != 0, "Expected valid module handle\n");
393 ok(GetLastError() == 0xdeadbeef ||
394 GetLastError() == ERROR_SUCCESS,
395 "Expected 0xdeadbeef or ERROR_SUCCESS, got %d\n", GetLastError());
397 /* try invalid file handle */
398 SetLastError(0xdeadbeef);
399 hmodule = LoadLibraryExA(path, (HANDLE)0xdeadbeef, 0);
400 if (!hmodule) /* succeeds on xp and older */
401 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
403 FreeLibrary(hmodule);
405 /* load kernel32.dll with no path */
406 SetLastError(0xdeadbeef);
407 hmodule = LoadLibraryExA("kernel32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
408 ok(hmodule != 0, "Expected valid module handle\n");
409 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
411 FreeLibrary(hmodule);
413 GetCurrentDirectoryA(MAX_PATH, path);
414 if (path[lstrlenA(path) - 1] != '\\')
415 lstrcatA(path, "\\");
416 lstrcatA(path, "kernel32.dll");
418 /* load kernel32.dll with an absolute path that does not exist */
419 SetLastError(0xdeadbeef);
420 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
421 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
422 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
423 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
425 /* Free the loaded dll when it's the first time this dll is loaded
426 in process - First time should pass, second fail */
427 SetLastError(0xdeadbeef);
428 hmodule = LoadLibraryExA("comctl32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
429 ok(hmodule != 0, "Expected valid module handle\n");
431 SetLastError(0xdeadbeef);
432 ret = FreeLibrary( (HMODULE)((ULONG_PTR)hmodule + 0x1230));
433 ok(!ret, "Free succeeded on wrong handle\n");
434 ok(GetLastError() == ERROR_BAD_EXE_FORMAT, "wrong error %u\n", GetLastError());
436 SetLastError(0xdeadbeef);
437 ret = FreeLibrary(hmodule);
438 ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError());
439 SetLastError(0xdeadbeef);
440 ret = FreeLibrary(hmodule);
441 ok(!ret, "Unexpected ability to free the module, failed with %d\n", GetLastError());
443 /* load with full path, name without extension */
444 GetSystemDirectoryA(path, MAX_PATH);
445 if (path[lstrlenA(path) - 1] != '\\')
446 lstrcatA(path, "\\");
447 lstrcatA(path, "kernel32");
448 hmodule = LoadLibraryExA(path, NULL, 0);
449 ok(hmodule != NULL, "got %p\n", hmodule);
450 FreeLibrary(hmodule);
452 /* same with alterate search path */
453 hmodule = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
454 ok(hmodule != NULL, "got %p\n", hmodule);
455 FreeLibrary(hmodule);
458 static void test_LoadLibraryEx_search_flags(void)
460 static const struct
462 int add_dirs[4];
463 int dll_dir;
464 int expect;
465 } tests[] =
467 { { 1, 2, 3 }, 4, 3 }, /* 0 */
468 { { 1, 3, 2 }, 4, 2 },
469 { { 3, 1 }, 4, 1 },
470 { { 5, 6 }, 4, 4 },
471 { { 5, 2 }, 4, 2 },
472 { { 0 }, 4, 4 }, /* 5 */
473 { { 0 }, 0, 0 },
474 { { 6, 5 }, 5, 0 },
475 { { 1, 1, 2 }, 0, 2 },
477 char *p, path[MAX_PATH], buf[MAX_PATH], curdir[MAX_PATH];
478 WCHAR bufW[MAX_PATH];
479 DLL_DIRECTORY_COOKIE cookies[4];
480 unsigned int i, j, k;
481 BOOL ret;
482 HMODULE mod;
484 GetTempPathA( sizeof(path), path );
485 GetTempFileNameA( path, "tmp", 0, buf );
486 DeleteFileA( buf );
487 ret = CreateDirectoryA( buf, NULL );
488 ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
489 p = buf + strlen( buf );
490 for (i = 1; i <= 6; i++)
492 sprintf( p, "\\%u", i );
493 ret = CreateDirectoryA( buf, NULL );
494 ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
495 if (i >= 5) continue; /* dirs 5 and 6 are left empty */
496 sprintf( p, "\\%u\\winetestdll.dll", i );
497 create_test_dll( buf );
500 GetCurrentDirectoryA( MAX_PATH, curdir );
501 *p = 0;
502 SetCurrentDirectoryA( buf );
504 SetLastError( 0xdeadbeef );
505 mod = LoadLibraryA( "1\\winetestdll.dll" );
506 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
507 FreeLibrary( mod );
509 SetLastError( 0xdeadbeef );
510 sprintf( path, "%c:1\\winetestdll.dll", buf[0] );
511 mod = LoadLibraryA( path );
512 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
513 FreeLibrary( mod );
515 if (pAddDllDirectory)
517 SetLastError( 0xdeadbeef );
518 mod = LoadLibraryExA( "1\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
519 ok( !mod, "LoadLibrary succeeded\n" );
520 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
522 SetLastError( 0xdeadbeef );
523 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
524 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
525 FreeLibrary( mod );
528 strcpy( p, "\\1" );
529 SetCurrentDirectoryA( buf );
531 SetLastError( 0xdeadbeef );
532 mod = LoadLibraryA( "winetestdll.dll" );
533 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
534 FreeLibrary( mod );
536 SetLastError( 0xdeadbeef );
537 sprintf( path, "%c:winetestdll.dll", buf[0] );
538 mod = LoadLibraryA( path );
539 ok( mod != NULL || broken(!mod), /* win10 disallows this but allows c:1\\winetestdll.dll */
540 "LoadLibrary failed err %u\n", GetLastError() );
541 if (!mod) ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
542 else FreeLibrary( mod );
544 SetLastError( 0xdeadbeef );
545 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
546 mod = LoadLibraryA( path );
547 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
548 FreeLibrary( mod );
550 if (pAddDllDirectory)
552 SetLastError( 0xdeadbeef );
553 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
554 ok( !mod, "LoadLibrary succeeded\n" );
555 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
557 SetLastError( 0xdeadbeef );
558 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
559 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
560 FreeLibrary( mod );
562 SetLastError( 0xdeadbeef );
563 sprintf( path, "%s\\winetestdll.dll", buf + 2 );
564 mod = LoadLibraryExA( path, 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
565 ok( mod != NULL, "LoadLibrary failed err %u\n", GetLastError() );
566 FreeLibrary( mod );
568 SetLastError( 0xdeadbeef );
569 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
570 ok( !mod, "LoadLibrary succeeded\n" );
571 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
573 SetLastError( 0xdeadbeef );
574 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_WITH_ALTERED_SEARCH_PATH );
575 ok( !mod, "LoadLibrary succeeded\n" );
576 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
578 SetLastError( 0xdeadbeef );
579 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_WITH_ALTERED_SEARCH_PATH );
580 ok( !mod, "LoadLibrary succeeded\n" );
581 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
583 SetLastError( 0xdeadbeef );
584 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
585 ok( !mod, "LoadLibrary succeeded\n" );
586 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
588 SetLastError( 0xdeadbeef );
589 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_WITH_ALTERED_SEARCH_PATH );
590 ok( !mod, "LoadLibrary succeeded\n" );
591 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
594 SetCurrentDirectoryA( curdir );
596 if (!pAddDllDirectory || !pSetDllDirectoryA) goto done;
598 SetLastError( 0xdeadbeef );
599 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
600 ok( !mod, "LoadLibrary succeeded\n" );
601 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
603 if (0) /* crashes on win10 */
605 SetLastError( 0xdeadbeef );
606 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
607 ok( !mod, "LoadLibrary succeeded\n" );
608 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
609 "wrong error %u\n", GetLastError() );
612 SetLastError( 0xdeadbeef );
613 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
614 ok( !mod, "LoadLibrary succeeded\n" );
615 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
617 SetLastError( 0xdeadbeef );
618 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
619 ok( !mod, "LoadLibrary succeeded\n" );
620 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
622 SetLastError( 0xdeadbeef );
623 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 );
624 ok( !mod, "LoadLibrary succeeded\n" );
625 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
627 SetLastError( 0xdeadbeef );
628 mod = LoadLibraryExA( "foo\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
629 ok( !mod, "LoadLibrary succeeded\n" );
630 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
632 SetLastError( 0xdeadbeef );
633 mod = LoadLibraryExA( "\\windows\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
634 ok( !mod, "LoadLibrary succeeded\n" );
635 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
637 SetLastError( 0xdeadbeef );
638 mod = LoadLibraryA( "1\\winetestdll.dll" );
639 ok( !mod, "LoadLibrary succeeded\n" );
640 ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
642 for (j = 0; j < ARRAY_SIZE(tests); j++)
644 for (k = 0; tests[j].add_dirs[k]; k++)
646 sprintf( p, "\\%u", tests[j].add_dirs[k] );
647 MultiByteToWideChar( CP_ACP, 0, buf, -1, bufW, MAX_PATH );
648 cookies[k] = pAddDllDirectory( bufW );
649 ok( cookies[k] != NULL, "failed to add %s\n", buf );
651 if (tests[j].dll_dir)
653 sprintf( p, "\\%u", tests[j].dll_dir );
654 pSetDllDirectoryA( buf );
656 else pSetDllDirectoryA( NULL );
658 SetLastError( 0xdeadbeef );
659 mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
660 if (tests[j].expect)
662 ok( mod != NULL, "%u: LoadLibrary failed err %u\n", j, GetLastError() );
663 GetModuleFileNameA( mod, path, MAX_PATH );
664 sprintf( p, "\\%u\\winetestdll.dll", tests[j].expect );
665 ok( !lstrcmpiA( path, buf ), "%u: wrong module %s expected %s\n", j, path, buf );
667 else
669 ok( !mod, "%u: LoadLibrary succeeded\n", j );
670 ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
671 "%u: wrong error %u\n", j, GetLastError() );
673 FreeLibrary( mod );
675 for (k = 0; tests[j].add_dirs[k]; k++) pRemoveDllDirectory( cookies[k] );
678 done:
679 for (i = 1; i <= 6; i++)
681 sprintf( p, "\\%u\\winetestdll.dll", i );
682 DeleteFileA( buf );
683 sprintf( p, "\\%u", i );
684 RemoveDirectoryA( buf );
686 *p = 0;
687 RemoveDirectoryA( buf );
690 static void testGetDllDirectory(void)
692 CHAR bufferA[MAX_PATH];
693 WCHAR bufferW[MAX_PATH];
694 DWORD length, ret;
695 int i;
696 static const char *dll_directories[] =
699 "C:\\Some\\Path",
700 "C:\\Some\\Path\\",
701 "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
703 const int test_count = ARRAY_SIZE(dll_directories);
705 if (!pGetDllDirectoryA || !pGetDllDirectoryW)
707 win_skip("GetDllDirectory not available\n");
708 return;
710 if (!pSetDllDirectoryA)
712 win_skip("SetDllDirectoryA not available\n");
713 return;
716 for (i = 0; i < test_count; i++)
718 length = strlen(dll_directories[i]);
719 if (!pSetDllDirectoryA(dll_directories[i]))
721 skip("i=%d, SetDllDirectoryA failed\n", i);
722 continue;
725 /* no buffer, determine length */
726 ret = pGetDllDirectoryA(0, NULL);
727 ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
729 ret = pGetDllDirectoryW(0, NULL);
730 ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
732 /* buffer of exactly the right size */
733 bufferA[length] = 'A';
734 bufferA[length + 1] = 'A';
735 ret = pGetDllDirectoryA(length + 1, bufferA);
736 ok(ret == length || broken(ret + 1 == length) /* win8 */,
737 "i=%d, Expected %u(+1), got %u\n", i, length, ret);
738 ok(bufferA[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
739 ok(strcmp(bufferA, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, bufferA);
741 bufferW[length] = 'A';
742 bufferW[length + 1] = 'A';
743 ret = pGetDllDirectoryW(length + 1, bufferW);
744 ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
745 ok(bufferW[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
746 ok(cmpStrAW(dll_directories[i], bufferW, length, length),
747 "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(bufferW));
749 /* Zero size buffer. The buffer may or may not be terminated depending
750 * on the Windows version and whether the A or W API is called. */
751 bufferA[0] = 'A';
752 ret = pGetDllDirectoryA(0, bufferA);
753 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
755 bufferW[0] = 'A';
756 ret = pGetDllDirectoryW(0, bufferW);
757 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
758 ok(bufferW[0] == 'A' || broken(bufferW[0] == 0), /* XP, 2003 */
759 "i=%d, Buffer overflow\n", i);
761 /* buffer just one too short */
762 bufferA[0] = 'A';
763 ret = pGetDllDirectoryA(length, bufferA);
764 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
765 if (length != 0)
766 ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
768 bufferW[0] = 'A';
769 ret = pGetDllDirectoryW(length, bufferW);
770 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
771 if (length != 0)
772 ok(bufferW[0] == 0, "i=%d, Buffer overflow\n", i);
774 if (0)
776 /* crashes on win8 */
777 /* no buffer, but too short length */
778 ret = pGetDllDirectoryA(length, NULL);
779 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
781 ret = pGetDllDirectoryW(length, NULL);
782 ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
785 if (pLdrGetDllDirectory)
787 UNICODE_STRING str;
788 NTSTATUS status;
789 str.Buffer = bufferW;
790 str.MaximumLength = sizeof(bufferW);
791 status = pLdrGetDllDirectory( &str );
792 ok( !status, "LdrGetDllDirectory failed %x\n", status );
793 ok( cmpStrAW( dll_directories[i], bufferW, strlen(dll_directories[i]),
794 str.Length / sizeof(WCHAR) ), "%u: got %s instead of %s\n",
795 i, wine_dbgstr_w(bufferW), dll_directories[i] );
796 if (dll_directories[i][0])
798 memset( bufferW, 0xcc, sizeof(bufferW) );
799 str.MaximumLength = (strlen( dll_directories[i] ) - 1) * sizeof(WCHAR);
800 status = pLdrGetDllDirectory( &str );
801 ok( status == STATUS_BUFFER_TOO_SMALL, "%u: LdrGetDllDirectory failed %x\n", i, status );
802 ok( bufferW[0] == 0 && bufferW[1] == 0xcccc,
803 "%u: buffer %x %x\n", i, bufferW[0], bufferW[1] );
804 length = (strlen( dll_directories[i] ) + 1) * sizeof(WCHAR);
805 ok( str.Length == length, "%u: wrong len %u / %u\n", i, str.Length, length );
810 /* unset whatever we did so following tests won't be affected */
811 pSetDllDirectoryA(NULL);
814 static void init_pointers(void)
816 HMODULE mod = GetModuleHandleA("kernel32.dll");
818 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(mod, #f))
819 MAKEFUNC(GetDllDirectoryA);
820 MAKEFUNC(GetDllDirectoryW);
821 MAKEFUNC(SetDllDirectoryA);
822 MAKEFUNC(AddDllDirectory);
823 MAKEFUNC(RemoveDllDirectory);
824 MAKEFUNC(SetDefaultDllDirectories);
825 MAKEFUNC(K32GetModuleInformation);
826 mod = GetModuleHandleA( "ntdll.dll" );
827 MAKEFUNC(LdrGetDllDirectory);
828 MAKEFUNC(LdrSetDllDirectory);
829 #undef MAKEFUNC
831 /* before Windows 7 this was not exported in kernel32 */
832 if (!pK32GetModuleInformation)
834 HMODULE hPsapi = LoadLibraryA("psapi.dll");
835 pK32GetModuleInformation = (void *)GetProcAddress(hPsapi, "GetModuleInformation");
839 static void testGetModuleHandleEx(void)
841 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
842 static const WCHAR nosuchmodW[] = {'n','o','s','u','c','h','m','o','d',0};
843 BOOL ret;
844 DWORD error;
845 HMODULE mod, mod_kernel32;
847 SetLastError( 0xdeadbeef );
848 ret = GetModuleHandleExA( 0, NULL, NULL );
849 error = GetLastError();
850 ok( !ret, "unexpected success\n" );
851 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
853 SetLastError( 0xdeadbeef );
854 ret = GetModuleHandleExA( 0, "kernel32", NULL );
855 error = GetLastError();
856 ok( !ret, "unexpected success\n" );
857 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
859 SetLastError( 0xdeadbeef );
860 mod = (HMODULE)0xdeadbeef;
861 ret = GetModuleHandleExA( 0, "kernel32", &mod );
862 ok( ret, "unexpected failure %u\n", GetLastError() );
863 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
864 FreeLibrary( mod );
866 SetLastError( 0xdeadbeef );
867 mod = (HMODULE)0xdeadbeef;
868 ret = GetModuleHandleExA( 0, "nosuchmod", &mod );
869 error = GetLastError();
870 ok( !ret, "unexpected success\n" );
871 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
872 ok( mod == NULL, "got %p\n", mod );
874 SetLastError( 0xdeadbeef );
875 ret = GetModuleHandleExW( 0, NULL, NULL );
876 error = GetLastError();
877 ok( !ret, "unexpected success\n" );
878 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
880 SetLastError( 0xdeadbeef );
881 ret = GetModuleHandleExW( 0, kernel32W, NULL );
882 error = GetLastError();
883 ok( !ret, "unexpected success\n" );
884 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
886 SetLastError( 0xdeadbeef );
887 mod = (HMODULE)0xdeadbeef;
888 ret = GetModuleHandleExW( 0, kernel32W, &mod );
889 ok( ret, "unexpected failure %u\n", GetLastError() );
890 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
891 FreeLibrary( mod );
893 SetLastError( 0xdeadbeef );
894 mod = (HMODULE)0xdeadbeef;
895 ret = GetModuleHandleExW( 0, nosuchmodW, &mod );
896 error = GetLastError();
897 ok( !ret, "unexpected success\n" );
898 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
899 ok( mod == NULL, "got %p\n", mod );
901 SetLastError( 0xdeadbeef );
902 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
903 error = GetLastError();
904 ok( !ret, "unexpected success\n" );
905 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
907 SetLastError( 0xdeadbeef );
908 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", NULL );
909 error = GetLastError();
910 ok( !ret, "unexpected success\n" );
911 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
913 SetLastError( 0xdeadbeef );
914 mod = (HMODULE)0xdeadbeef;
915 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", &mod );
916 ok( ret, "unexpected failure %u\n", GetLastError() );
917 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
919 SetLastError( 0xdeadbeef );
920 mod = (HMODULE)0xdeadbeef;
921 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "nosuchmod", &mod );
922 error = GetLastError();
923 ok( !ret, "unexpected success\n" );
924 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
925 ok( mod == NULL, "got %p\n", mod );
927 SetLastError( 0xdeadbeef );
928 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
929 error = GetLastError();
930 ok( !ret, "unexpected success\n" );
931 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
933 SetLastError( 0xdeadbeef );
934 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, NULL );
935 error = GetLastError();
936 ok( !ret, "unexpected success\n" );
937 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
939 SetLastError( 0xdeadbeef );
940 mod = (HMODULE)0xdeadbeef;
941 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, &mod );
942 ok( ret, "unexpected failure %u\n", GetLastError() );
943 ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
945 SetLastError( 0xdeadbeef );
946 mod = (HMODULE)0xdeadbeef;
947 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nosuchmodW, &mod );
948 error = GetLastError();
949 ok( !ret, "unexpected success\n" );
950 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
951 ok( mod == NULL, "got %p\n", mod );
953 mod_kernel32 = LoadLibraryA( "kernel32" );
955 SetLastError( 0xdeadbeef );
956 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
957 error = GetLastError();
958 ok( !ret, "unexpected success\n" );
959 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
961 SetLastError( 0xdeadbeef );
962 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, NULL );
963 error = GetLastError();
964 ok( !ret, "unexpected success\n" );
965 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
967 SetLastError( 0xdeadbeef );
968 mod = (HMODULE)0xdeadbeef;
969 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, &mod );
970 ok( ret, "unexpected failure %u\n", GetLastError() );
971 ok( mod == mod_kernel32, "got %p\n", mod );
972 FreeLibrary( mod );
974 SetLastError( 0xdeadbeef );
975 mod = (HMODULE)0xdeadbeef;
976 ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)0xbeefdead, &mod );
977 error = GetLastError();
978 ok( !ret, "unexpected success\n" );
979 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
980 ok( mod == NULL, "got %p\n", mod );
982 SetLastError( 0xdeadbeef );
983 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
984 error = GetLastError();
985 ok( !ret, "unexpected success\n" );
986 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
988 SetLastError( 0xdeadbeef );
989 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, NULL );
990 error = GetLastError();
991 ok( !ret, "unexpected success\n" );
992 ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
994 SetLastError( 0xdeadbeef );
995 mod = (HMODULE)0xdeadbeef;
996 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, &mod );
997 ok( ret, "unexpected failure %u\n", GetLastError() );
998 ok( mod == mod_kernel32, "got %p\n", mod );
999 FreeLibrary( mod );
1001 SetLastError( 0xdeadbeef );
1002 mod = (HMODULE)0xdeadbeef;
1003 ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)0xbeefdead, &mod );
1004 error = GetLastError();
1005 ok( !ret, "unexpected success\n" );
1006 ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
1007 ok( mod == NULL, "got %p\n", mod );
1009 FreeLibrary( mod_kernel32 );
1012 static void testK32GetModuleInformation(void)
1014 MODULEINFO info;
1015 HMODULE mod;
1016 BOOL ret;
1018 mod = GetModuleHandleA(NULL);
1019 memset(&info, 0xAA, sizeof(info));
1020 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1021 ok(ret, "K32GetModuleInformation failed for main module\n");
1022 ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
1023 ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
1025 mod = GetModuleHandleA("kernel32.dll");
1026 memset(&info, 0xAA, sizeof(info));
1027 ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
1028 ok(ret, "K32GetModuleInformation failed for kernel32 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");
1033 static void test_AddDllDirectory(void)
1035 static const WCHAR tmpW[] = {'t','m','p',0};
1036 static const WCHAR dotW[] = {'.','\\','.',0};
1037 static const WCHAR rootW[] = {'\\',0};
1038 WCHAR path[MAX_PATH], buf[MAX_PATH];
1039 DLL_DIRECTORY_COOKIE cookie;
1040 BOOL ret;
1042 if (!pAddDllDirectory || !pRemoveDllDirectory)
1044 win_skip( "AddDllDirectory not available\n" );
1045 return;
1048 buf[0] = '\0';
1049 GetTempPathW(ARRAY_SIZE(path), path );
1050 ret = GetTempFileNameW( path, tmpW, 0, buf );
1051 ok( ret, "GetTempFileName failed err %u\n", GetLastError() );
1052 SetLastError( 0xdeadbeef );
1053 cookie = pAddDllDirectory( buf );
1054 ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
1055 SetLastError( 0xdeadbeef );
1056 ret = pRemoveDllDirectory( cookie );
1057 ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
1059 DeleteFileW( buf );
1060 SetLastError( 0xdeadbeef );
1061 cookie = pAddDllDirectory( buf );
1062 ok( !cookie, "AddDllDirectory succeeded\n" );
1063 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError() );
1064 cookie = pAddDllDirectory( dotW );
1065 ok( !cookie, "AddDllDirectory succeeded\n" );
1066 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1067 cookie = pAddDllDirectory( rootW );
1068 ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
1069 SetLastError( 0xdeadbeef );
1070 ret = pRemoveDllDirectory( cookie );
1071 ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
1072 GetWindowsDirectoryW( buf, MAX_PATH );
1073 lstrcpyW( buf + 2, tmpW );
1074 cookie = pAddDllDirectory( buf );
1075 ok( !cookie, "AddDllDirectory succeeded\n" );
1076 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1079 static void test_SetDefaultDllDirectories(void)
1081 HMODULE mod;
1082 BOOL ret;
1084 if (!pSetDefaultDllDirectories)
1086 win_skip( "SetDefaultDllDirectories not available\n" );
1087 return;
1090 mod = LoadLibraryA( "authz.dll" );
1091 ok( mod != NULL, "loading authz failed\n" );
1092 FreeLibrary( mod );
1093 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_USER_DIRS );
1094 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1095 mod = LoadLibraryA( "authz.dll" );
1096 ok( !mod, "loading authz succeeded\n" );
1097 FreeLibrary( mod );
1098 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_SYSTEM32 );
1099 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1100 mod = LoadLibraryA( "authz.dll" );
1101 ok( mod != NULL, "loading authz failed\n" );
1102 FreeLibrary( mod );
1103 mod = LoadLibraryExA( "authz.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1104 ok( !mod, "loading authz succeeded\n" );
1105 FreeLibrary( mod );
1106 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
1107 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1108 mod = LoadLibraryA( "authz.dll" );
1109 ok( !mod, "loading authz succeeded\n" );
1110 FreeLibrary( mod );
1111 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1112 ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
1113 mod = LoadLibraryA( "authz.dll" );
1114 ok( mod != NULL, "loading authz failed\n" );
1115 FreeLibrary( mod );
1117 SetLastError( 0xdeadbeef );
1118 ret = pSetDefaultDllDirectories( 0 );
1119 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1120 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1122 SetLastError( 0xdeadbeef );
1123 ret = pSetDefaultDllDirectories( 3 );
1124 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1125 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1127 SetLastError( 0xdeadbeef );
1128 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR | 0x8000 );
1129 ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1130 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1132 SetLastError( 0xdeadbeef );
1133 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
1134 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1135 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1137 SetLastError( 0xdeadbeef );
1138 ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_USER_DIRS );
1139 ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1140 if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1142 /* restore some sane defaults */
1143 pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1146 START_TEST(module)
1148 WCHAR filenameW[MAX_PATH];
1150 /* Test if we can use GetModuleFileNameW */
1152 SetLastError(0xdeadbeef);
1153 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
1154 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1156 win_skip("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
1157 is_unicode_enabled = FALSE;
1160 init_pointers();
1162 testGetModuleFileName(NULL);
1163 testGetModuleFileName("kernel32.dll");
1164 testGetModuleFileName_Wrong();
1166 testGetDllDirectory();
1168 testLoadLibraryA();
1169 testNestedLoadLibraryA();
1170 testLoadLibraryA_Wrong();
1171 testGetProcAddress_Wrong();
1172 testLoadLibraryEx();
1173 test_LoadLibraryEx_search_flags();
1174 testGetModuleHandleEx();
1175 testK32GetModuleInformation();
1176 test_AddDllDirectory();
1177 test_SetDefaultDllDirectories();