1 /*
2  * Unit tests for module/DLL/library API
3  *
4  * Copyright (c) 2004 Eric Pouech
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "wine/test.h"
22 #include <windows.h>
23 #include <stdio.h>
24 #include <psapi.h>
25 
26 static DWORD (WINAPI *pGetDllDirectoryA)(DWORD,LPSTR);
27 static DWORD (WINAPI *pGetDllDirectoryW)(DWORD,LPWSTR);
28 static BOOL (WINAPI *pSetDllDirectoryA)(LPCSTR);
29 static DLL_DIRECTORY_COOKIE (WINAPI *pAddDllDirectory)(const WCHAR*);
30 static BOOL (WINAPI *pRemoveDllDirectory)(DLL_DIRECTORY_COOKIE);
31 static BOOL (WINAPI *pSetDefaultDllDirectories)(DWORD);
32 static BOOL (WINAPI *pK32GetModuleInformation)(HANDLE process, HMODULE module,
33                                                MODULEINFO *modinfo, DWORD cb);
34 
35 static BOOL is_unicode_enabled = TRUE;
36 
37 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
38 {
39     WCHAR       aw[1024];
40 
41     DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
42                                      a, lenA, aw, sizeof(aw) / sizeof(aw[0]) );
43     if (len != lenB) return FALSE;
44     return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
45 }
46 
47 static const struct
48 {
49     IMAGE_DOS_HEADER dos;
50     IMAGE_NT_HEADERS nt;
51     IMAGE_SECTION_HEADER section;
52 } dll_image =
53 {
54     { IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 }, 0, 0, { 0 },
55       sizeof(IMAGE_DOS_HEADER) },
56     {
57         IMAGE_NT_SIGNATURE, /* Signature */
58         {
59 #if defined __i386__
60             IMAGE_FILE_MACHINE_I386, /* Machine */
61 #elif defined __x86_64__
62             IMAGE_FILE_MACHINE_AMD64, /* Machine */
63 #elif defined __powerpc__
64             IMAGE_FILE_MACHINE_POWERPC, /* Machine */
65 #elif defined __arm__
66             IMAGE_FILE_MACHINE_ARMNT, /* Machine */
67 #elif defined __aarch64__
68             IMAGE_FILE_MACHINE_ARM64, /* Machine */
69 #else
70 # error You must specify the machine type
71 #endif
72             1, /* NumberOfSections */
73             0, /* TimeDateStamp */
74             0, /* PointerToSymbolTable */
75             0, /* NumberOfSymbols */
76             sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
77             IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
78         },
79         { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
80           1, /* MajorLinkerVersion */
81           0, /* MinorLinkerVersion */
82           0, /* SizeOfCode */
83           0, /* SizeOfInitializedData */
84           0, /* SizeOfUninitializedData */
85           0, /* AddressOfEntryPoint */
86           0x1000, /* BaseOfCode */
87 #ifndef _WIN64
88           0, /* BaseOfData */
89 #endif
90           0x10000000, /* ImageBase */
91           0x1000, /* SectionAlignment */
92           0x1000, /* FileAlignment */
93           4, /* MajorOperatingSystemVersion */
94           0, /* MinorOperatingSystemVersion */
95           1, /* MajorImageVersion */
96           0, /* MinorImageVersion */
97           4, /* MajorSubsystemVersion */
98           0, /* MinorSubsystemVersion */
99           0, /* Win32VersionValue */
100           0x2000, /* SizeOfImage */
101           sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS), /* SizeOfHeaders */
102           0, /* CheckSum */
103           IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
104           0, /* DllCharacteristics */
105           0, /* SizeOfStackReserve */
106           0, /* SizeOfStackCommit */
107           0, /* SizeOfHeapReserve */
108           0, /* SizeOfHeapCommit */
109           0, /* LoaderFlags */
110           0, /* NumberOfRvaAndSizes */
111           { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
112         }
113     },
114     { ".rodata", { 0 }, 0x1000, 0x1000, 0, 0, 0, 0, 0,
115       IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ }
116 };
117 
118 static void create_test_dll( const char *name )
119 {
120     DWORD dummy;
121     HANDLE handle = CreateFileA( name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0 );
122 
123     ok( handle != INVALID_HANDLE_VALUE, "failed to create file err %u\n", GetLastError() );
124     WriteFile( handle, &dll_image, sizeof(dll_image), &dummy, NULL );
125     SetFilePointer( handle, dll_image.nt.OptionalHeader.SizeOfImage, NULL, FILE_BEGIN );
126     SetEndOfFile( handle );
127     CloseHandle( handle );
128 }
129 
130 static void testGetModuleFileName(const char* name)
131 {
132     HMODULE     hMod;
133     char        bufA[MAX_PATH];
134     WCHAR       bufW[MAX_PATH];
135     DWORD       len1A, len1W = 0, len2A, len2W = 0;
136 
137     hMod = (name) ? GetModuleHandleA(name) : NULL;
138 
139     /* first test, with enough space in buffer */
140     memset(bufA, '-', sizeof(bufA));
141     SetLastError(0xdeadbeef);
142     len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
143     ok(GetLastError() == ERROR_SUCCESS ||
144        broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
145        "LastError was not reset: %u\n", GetLastError());
146     ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
147 
148     if (is_unicode_enabled)
149     {
150         memset(bufW, '-', sizeof(bufW));
151         SetLastError(0xdeadbeef);
152         len1W = GetModuleFileNameW(hMod, bufW, sizeof(bufW) / sizeof(WCHAR));
153         ok(GetLastError() == ERROR_SUCCESS ||
154            broken(GetLastError() == 0xdeadbeef), /* <= XP SP3 */
155            "LastError was not reset: %u\n", GetLastError());
156         ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
157     }
158 
159     ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%d/%d)\n", len1A, lstrlenA(bufA));
160 
161     if (is_unicode_enabled)
162     {
163         ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%d/%d)\n", len1W, lstrlenW(bufW));
164         ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
165     }
166 
167     /* second test with a buffer too small */
168     memset(bufA, '-', sizeof(bufA));
169     len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
170     ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
171 
172     if (is_unicode_enabled)
173     {
174         memset(bufW, '-', sizeof(bufW));
175         len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
176         ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
177         ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
178         ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%d/%d)\n", len1W / 2, len2W);
179     }
180 
181     ok(len1A / 2 == len2A,
182        "Correct length in GetModuleFilenameA with buffer too small (%d/%d)\n", len1A / 2, len2A);
183 }
184 
185 static void testGetModuleFileName_Wrong(void)
186 {
187     char        bufA[MAX_PATH];
188     WCHAR       bufW[MAX_PATH];
189 
190     /* test wrong handle */
191     if (is_unicode_enabled)
192     {
193         bufW[0] = '*';
194         ok(GetModuleFileNameW((void*)0xffffffff, bufW, sizeof(bufW) / sizeof(WCHAR)) == 0, "Unexpected success in module handle\n");
195         ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
196     }
197 
198     bufA[0] = '*';
199     ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
200     ok(bufA[0] == '*', "When failing, buffer shouldn't be written to\n");
201 }
202 
203 static void testLoadLibraryA(void)
204 {
205     HMODULE hModule, hModule1;
206     FARPROC fp;
207 
208     SetLastError(0xdeadbeef);
209     hModule = LoadLibraryA("kernel32.dll");
210     ok( hModule != NULL, "kernel32.dll should be loadable\n");
211     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
212 
213     fp = GetProcAddress(hModule, "CreateFileA");
214     ok( fp != NULL, "CreateFileA should be there\n");
215     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
216 
217     SetLastError(0xdeadbeef);
218     hModule1 = LoadLibraryA("kernel32   ");
219     ok( hModule1 != NULL, "\"kernel32   \" should be loadable\n" );
220     ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError() );
221     ok( hModule == hModule1, "Loaded wrong module\n" );
222     FreeLibrary(hModule1);
223     FreeLibrary(hModule);
224 }
225 
226 static void testNestedLoadLibraryA(void)
227 {
228     static const char dllname[] = "shell32.dll";
229     char path1[MAX_PATH], path2[MAX_PATH];
230     HMODULE hModule1, hModule2, hModule3;
231 
232     /* This is not really a Windows conformance test, but more a Wine
233      * regression test. Wine's builtin dlls can be loaded from multiple paths,
234      * and this test tries to make sure that Wine does not get confused and
235      * really unloads the Unix .so file at the right time. Failure to do so
236      * will result in the dll being unloadable.
237      * This test must be done with a dll that can be unloaded, which means:
238      * - it must not already be loaded
239      * - it must not have a 16-bit counterpart
240      */
241     GetWindowsDirectoryA(path1, sizeof(path1));
242     strcat(path1, "\\system\\");
243     strcat(path1, dllname);
244     hModule1 = LoadLibraryA(path1);
245     if (!hModule1)
246     {
247         /* We must be on Windows, so we cannot test */
248         return;
249     }
250 
251     GetWindowsDirectoryA(path2, sizeof(path2));
252     strcat(path2, "\\system32\\");
253     strcat(path2, dllname);
254     hModule2 = LoadLibraryA(path2);
255     ok(hModule2 != NULL, "LoadLibrary(%s) failed\n", path2);
256 
257     /* The first LoadLibrary() call may have registered the dll under the
258      * system32 path. So load it, again, under the '...\system\...' path so
259      * Wine does not immediately notice that it is already loaded.
260      */
261     hModule3 = LoadLibraryA(path1);
262     ok(hModule3 != NULL, "LoadLibrary(%s) failed\n", path1);
263 
264     /* Now fully unload the dll */
265     ok(FreeLibrary(hModule3), "FreeLibrary() failed\n");
266     ok(FreeLibrary(hModule2), "FreeLibrary() failed\n");
267     ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
268     ok(GetModuleHandleA(dllname) == NULL, "%s was not fully unloaded\n", dllname);
269 
270     /* Try to load the dll again, if refcounting is ok, this should work */
271     hModule1 = LoadLibraryA(path1);
272     ok(hModule1 != NULL, "LoadLibrary(%s) failed\n", path1);
273     if (hModule1 != NULL)
274         ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
275 }
276 
277 static void testLoadLibraryA_Wrong(void)
278 {
279     HMODULE hModule;
280 
281     /* Try to load a nonexistent dll */
282     SetLastError(0xdeadbeef);
283     hModule = LoadLibraryA("non_ex_pv.dll");
284     ok( !hModule, "non_ex_pv.dll should be not loadable\n");
285     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %d\n", GetLastError() );
286 
287     /* Just in case */
288     FreeLibrary(hModule);
289 }
290 
291 static void testGetProcAddress_Wrong(void)
292 {
293     FARPROC fp;
294 
295     SetLastError(0xdeadbeef);
296     fp = GetProcAddress(NULL, "non_ex_call");
297     ok( !fp, "non_ex_call should not be found\n");
298     ok( GetLastError() == ERROR_PROC_NOT_FOUND, "Expected ERROR_PROC_NOT_FOUND, got %d\n", GetLastError() );
299 
300     SetLastError(0xdeadbeef);
301     fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
302     ok( !fp, "non_ex_call should not be found\n");
303     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "Expected ERROR_MOD_NOT_FOUND, got %d\n", GetLastError() );
304 }
305 
306 static void testLoadLibraryEx(void)
307 {
308     CHAR path[MAX_PATH];
309     HMODULE hmodule;
310     HANDLE hfile;
311     BOOL ret;
312 
313     hfile = CreateFileA("testfile.dll", GENERIC_READ | GENERIC_WRITE,
314                         FILE_SHARE_READ | FILE_SHARE_WRITE,
315                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
316     ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
317 
318     /* NULL lpFileName */
319     SetLastError(0xdeadbeef);
320     hmodule = LoadLibraryExA(NULL, NULL, 0);
321     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
322     ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
323        GetLastError() == ERROR_INVALID_PARAMETER,
324        "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
325 
326     /* empty lpFileName */
327     SetLastError(0xdeadbeef);
328     hmodule = LoadLibraryExA("", NULL, 0);
329     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
330     ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
331        GetLastError() == ERROR_INVALID_PARAMETER /* win8 */,
332        "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND, got %d\n", GetLastError());
333 
334     /* hFile is non-NULL */
335     SetLastError(0xdeadbeef);
336     hmodule = LoadLibraryExA("testfile.dll", hfile, 0);
337     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
338     todo_wine
339     {
340         ok(GetLastError() == ERROR_SHARING_VIOLATION ||
341            GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
342            "Unexpected last error, got %d\n", GetLastError());
343     }
344 
345     SetLastError(0xdeadbeef);
346     hmodule = LoadLibraryExA("testfile.dll", (HANDLE)0xdeadbeef, 0);
347     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
348     todo_wine
349     {
350         ok(GetLastError() == ERROR_SHARING_VIOLATION ||
351            GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
352            "Unexpected last error, got %d\n", GetLastError());
353     }
354 
355     /* try to open a file that is locked */
356     SetLastError(0xdeadbeef);
357     hmodule = LoadLibraryExA("testfile.dll", NULL, 0);
358     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
359     todo_wine
360     {
361         ok(GetLastError() == ERROR_SHARING_VIOLATION,
362            "Expected ERROR_SHARING_VIOLATION, got %d\n", GetLastError());
363     }
364 
365     /* lpFileName does not matter */
366     if (is_unicode_enabled)
367     {
368         SetLastError(0xdeadbeef);
369         hmodule = LoadLibraryExA(NULL, hfile, 0);
370         ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
371         ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
372            GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
373            "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
374     }
375 
376     CloseHandle(hfile);
377 
378     /* load empty file */
379     SetLastError(0xdeadbeef);
380     hmodule = LoadLibraryExA("testfile.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
381     ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
382     todo_wine
383     {
384         ok(GetLastError() == ERROR_FILE_INVALID,
385            "Expected ERROR_FILE_INVALID, got %d\n", GetLastError());
386     }
387 
388     DeleteFileA("testfile.dll");
389 
390     GetSystemDirectoryA(path, MAX_PATH);
391     if (path[lstrlenA(path) - 1] != '\\')
392         lstrcatA(path, "\\");
393     lstrcatA(path, "kernel32.dll");
394 
395     /* load kernel32.dll with an absolute path */
396     SetLastError(0xdeadbeef);
397     hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
398     ok(hmodule != 0, "Expected valid module handle\n");
399     ok(GetLastError() == 0xdeadbeef ||
400        GetLastError() == ERROR_SUCCESS,
401        "Expected 0xdeadbeef or ERROR_SUCCESS, got %d\n", GetLastError());
402 
403     /* try invalid file handle */
404     SetLastError(0xdeadbeef);
405     hmodule = LoadLibraryExA(path, (HANDLE)0xdeadbeef, 0);
406     if (!hmodule)  /* succeeds on xp and older */
407         ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
408 
409     FreeLibrary(hmodule);
410 
411     /* load kernel32.dll with no path */
412     SetLastError(0xdeadbeef);
413     hmodule = LoadLibraryExA("kernel32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
414     ok(hmodule != 0, "Expected valid module handle\n");
415     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
416 
417     FreeLibrary(hmodule);
418 
419     GetCurrentDirectoryA(MAX_PATH, path);
420     if (path[lstrlenA(path) - 1] != '\\')
421         lstrcatA(path, "\\");
422     lstrcatA(path, "kernel32.dll");
423 
424     /* load kernel32.dll with an absolute path that does not exist */
425     SetLastError(0xdeadbeef);
426     hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
427     todo_wine
428     {
429         ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
430     }
431     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
432        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
433 
434     /* Free the loaded dll when it's the first time this dll is loaded
435        in process - First time should pass, second fail */
436     SetLastError(0xdeadbeef);
437     hmodule = LoadLibraryExA("comctl32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
438     ok(hmodule != 0, "Expected valid module handle\n");
439 
440     SetLastError(0xdeadbeef);
441     ret = FreeLibrary(hmodule);
442     ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError());
443     SetLastError(0xdeadbeef);
444     ret = FreeLibrary(hmodule);
445     ok(!ret, "Unexpected ability to free the module, failed with %d\n", GetLastError());
446 
447     /* load with full path, name without extension */
448     GetSystemDirectoryA(path, MAX_PATH);
449     if (path[lstrlenA(path) - 1] != '\\')
450         lstrcatA(path, "\\");
451     lstrcatA(path, "kernel32");
452     hmodule = LoadLibraryExA(path, NULL, 0);
453     ok(hmodule != NULL, "got %p\n", hmodule);
454     FreeLibrary(hmodule);
455 
456     /* same with alterate search path */
457     hmodule = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
458     ok(hmodule != NULL, "got %p\n", hmodule);
459     FreeLibrary(hmodule);
460 }
461 
462 static void test_LoadLibraryEx_search_flags(void)
463 {
464     static const struct
465     {
466         int add_dirs[4];
467         int dll_dir;
468         int expect;
469     } tests[] =
470     {
471         { { 1, 2, 3 }, 4, 3 }, /* 0 */
472         { { 1, 3, 2 }, 4, 2 },
473         { { 3, 1 },    4, 1 },
474         { { 5, 6 },    4, 4 },
475         { { 5, 2 },    4, 2 },
476         { { 0 },       4, 4 }, /* 5 */
477         { { 0 },       0, 0 },
478         { { 6, 5 },    5, 0 },
479         { { 1, 1, 2 }, 0, 2 },
480     };
481     char *p, path[MAX_PATH], buf[MAX_PATH];
482     WCHAR bufW[MAX_PATH];
483     DLL_DIRECTORY_COOKIE cookies[4];
484     unsigned int i, j, k;
485     BOOL ret;
486     HMODULE mod;
487 
488     if (!pAddDllDirectory || !pSetDllDirectoryA) return;
489 
490     GetTempPathA( sizeof(path), path );
491     GetTempFileNameA( path, "tmp", 0, buf );
492     DeleteFileA( buf );
493     ret = CreateDirectoryA( buf, NULL );
494     ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
495     p = buf + strlen( buf );
496     for (i = 1; i <= 6; i++)
497     {
498         sprintf( p, "\\%u", i );
499         ret = CreateDirectoryA( buf, NULL );
500         ok( ret, "CreateDirectory failed err %u\n", GetLastError() );
501         if (i >= 5) continue;  /* dirs 5 and 6 are left empty */
502         sprintf( p, "\\%u\\winetestdll.dll", i );
503         create_test_dll( buf );
504     }
505     SetLastError( 0xdeadbeef );
506     mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
507     ok( !mod, "LoadLibrary succeeded\n" );
508     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
509 
510     SetLastError( 0xdeadbeef );
511     mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
512     ok( !mod, "LoadLibrary succeeded\n" );
513     ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
514         "wrong error %u\n", GetLastError() );
515 
516     SetLastError( 0xdeadbeef );
517     mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32 );
518     ok( !mod, "LoadLibrary succeeded\n" );
519     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
520 
521     SetLastError( 0xdeadbeef );
522     mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
523     ok( !mod, "LoadLibrary succeeded\n" );
524     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
525 
526     SetLastError( 0xdeadbeef );
527     mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 );
528     ok( !mod, "LoadLibrary succeeded\n" );
529     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
530 
531     SetLastError( 0xdeadbeef );
532     mod = LoadLibraryExA( "foo\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
533     ok( !mod, "LoadLibrary succeeded\n" );
534     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
535 
536     SetLastError( 0xdeadbeef );
537     mod = LoadLibraryExA( "\\windows\\winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
538     ok( !mod, "LoadLibrary succeeded\n" );
539     ok( GetLastError() == ERROR_MOD_NOT_FOUND, "wrong error %u\n", GetLastError() );
540 
541     for (j = 0; j < sizeof(tests) / sizeof(tests[0]); j++)
542     {
543         for (k = 0; tests[j].add_dirs[k]; k++)
544         {
545             sprintf( p, "\\%u", tests[j].add_dirs[k] );
546             MultiByteToWideChar( CP_ACP, 0, buf, -1, bufW, MAX_PATH );
547             cookies[k] = pAddDllDirectory( bufW );
548             ok( cookies[k] != NULL, "failed to add %s\n", buf );
549         }
550         if (tests[j].dll_dir)
551         {
552             sprintf( p, "\\%u", tests[j].dll_dir );
553             pSetDllDirectoryA( buf );
554         }
555         else pSetDllDirectoryA( NULL );
556 
557         SetLastError( 0xdeadbeef );
558         mod = LoadLibraryExA( "winetestdll.dll", 0, LOAD_LIBRARY_SEARCH_USER_DIRS );
559         if (tests[j].expect)
560         {
561             ok( mod != NULL, "%u: LoadLibrary failed err %u\n", j, GetLastError() );
562             GetModuleFileNameA( mod, path, MAX_PATH );
563             sprintf( p, "\\%u\\winetestdll.dll", tests[j].expect );
564             ok( !lstrcmpiA( path, buf ), "%u: wrong module %s expected %s\n", j, path, buf );
565         }
566         else
567         {
568             ok( !mod, "%u: LoadLibrary succeeded\n", j );
569             ok( GetLastError() == ERROR_MOD_NOT_FOUND || broken(GetLastError() == ERROR_NOT_ENOUGH_MEMORY),
570                 "%u: wrong error %u\n", j, GetLastError() );
571         }
572         FreeLibrary( mod );
573 
574         for (k = 0; tests[j].add_dirs[k]; k++) pRemoveDllDirectory( cookies[k] );
575     }
576 
577     for (i = 1; i <= 6; i++)
578     {
579         sprintf( p, "\\%u\\winetestdll.dll", i );
580         DeleteFileA( buf );
581         sprintf( p, "\\%u", i );
582         RemoveDirectoryA( buf );
583     }
584     *p = 0;
585     RemoveDirectoryA( buf );
586 }
587 
588 static void testGetDllDirectory(void)
589 {
590     CHAR bufferA[MAX_PATH];
591     WCHAR bufferW[MAX_PATH];
592     DWORD length, ret;
593     int i;
594     static const char *dll_directories[] =
595     {
596         "",
597         "C:\\Some\\Path",
598         "C:\\Some\\Path\\",
599         "Q:\\A\\Long\\Path with spaces that\\probably\\doesn't exist!",
600     };
601     const int test_count = sizeof(dll_directories) / sizeof(dll_directories[0]);
602 
603     if (!pGetDllDirectoryA || !pGetDllDirectoryW)
604     {
605         win_skip("GetDllDirectory not available\n");
606         return;
607     }
608     if (!pSetDllDirectoryA)
609     {
610         win_skip("SetDllDirectoryA not available\n");
611         return;
612     }
613 
614     for (i = 0; i < test_count; i++)
615     {
616         length = strlen(dll_directories[i]);
617         if (!pSetDllDirectoryA(dll_directories[i]))
618         {
619             skip("i=%d, SetDllDirectoryA failed\n", i);
620             continue;
621         }
622 
623         /* no buffer, determine length */
624         ret = pGetDllDirectoryA(0, NULL);
625         ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
626 
627         ret = pGetDllDirectoryW(0, NULL);
628         ok(ret == length + 1, "Expected %u, got %u\n", length + 1, ret);
629 
630         /* buffer of exactly the right size */
631         bufferA[length] = 'A';
632         bufferA[length + 1] = 'A';
633         ret = pGetDllDirectoryA(length + 1, bufferA);
634         ok(ret == length || broken(ret + 1 == length) /* win8 */,
635            "i=%d, Expected %u(+1), got %u\n", i, length, ret);
636         ok(bufferA[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
637         ok(strcmp(bufferA, dll_directories[i]) == 0, "i=%d, Wrong path returned: '%s'\n", i, bufferA);
638 
639         bufferW[length] = 'A';
640         bufferW[length + 1] = 'A';
641         ret = pGetDllDirectoryW(length + 1, bufferW);
642         ok(ret == length, "i=%d, Expected %u, got %u\n", i, length, ret);
643         ok(bufferW[length + 1] == 'A', "i=%d, Buffer overflow\n", i);
644         ok(cmpStrAW(dll_directories[i], bufferW, length, length),
645            "i=%d, Wrong path returned: %s\n", i, wine_dbgstr_w(bufferW));
646 
647         /* Zero size buffer. The buffer may or may not be terminated depending
648          * on the Windows version and whether the A or W API is called. */
649         bufferA[0] = 'A';
650         ret = pGetDllDirectoryA(0, bufferA);
651         ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
652 
653         bufferW[0] = 'A';
654         ret = pGetDllDirectoryW(0, bufferW);
655         ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
656         ok(bufferW[0] == 0 || /* XP, 2003 */
657            broken(bufferW[0] == 'A'), "i=%d, Buffer overflow\n", i);
658 
659         /* buffer just one too short */
660         bufferA[0] = 'A';
661         ret = pGetDllDirectoryA(length, bufferA);
662         ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
663         if (length != 0)
664             ok(bufferA[0] == 0, "i=%d, Buffer not null terminated\n", i);
665 
666         bufferW[0] = 'A';
667         ret = pGetDllDirectoryW(length, bufferW);
668         ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
669         ok(bufferW[0] == 0 || /* XP, 2003 */
670            broken(bufferW[0] == 'A'), "i=%d, Buffer overflow\n", i);
671 
672         if (0)
673         {
674             /* crashes on win8 */
675             /* no buffer, but too short length */
676             ret = pGetDllDirectoryA(length, NULL);
677             ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
678 
679             ret = pGetDllDirectoryW(length, NULL);
680             ok(ret == length + 1, "i=%d, Expected %u, got %u\n", i, length + 1, ret);
681         }
682     }
683 
684     /* unset whatever we did so following tests won't be affected */
685     pSetDllDirectoryA(NULL);
686 }
687 
688 static void init_pointers(void)
689 {
690     HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
691 
692 #define MAKEFUNC(f) (p##f = (void*)GetProcAddress(hKernel32, #f))
693     MAKEFUNC(GetDllDirectoryA);
694     MAKEFUNC(GetDllDirectoryW);
695     MAKEFUNC(SetDllDirectoryA);
696     MAKEFUNC(AddDllDirectory);
697     MAKEFUNC(RemoveDllDirectory);
698     MAKEFUNC(SetDefaultDllDirectories);
699     MAKEFUNC(K32GetModuleInformation);
700 #undef MAKEFUNC
701 
702     /* before Windows 7 this was not exported in kernel32 */
703     if (!pK32GetModuleInformation)
704     {
705         HMODULE hPsapi = LoadLibraryA("psapi.dll");
706         pK32GetModuleInformation = (void *)GetProcAddress(hPsapi, "GetModuleInformation");
707     }
708 }
709 
710 static void testGetModuleHandleEx(void)
711 {
712     static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
713     static const WCHAR nosuchmodW[] = {'n','o','s','u','c','h','m','o','d',0};
714     BOOL ret;
715     DWORD error;
716     HMODULE mod, mod_kernel32;
717 
718     SetLastError( 0xdeadbeef );
719     ret = GetModuleHandleExA( 0, NULL, NULL );
720     error = GetLastError();
721     ok( !ret, "unexpected success\n" );
722     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
723 
724     SetLastError( 0xdeadbeef );
725     ret = GetModuleHandleExA( 0, "kernel32", NULL );
726     error = GetLastError();
727     ok( !ret, "unexpected success\n" );
728     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
729 
730     SetLastError( 0xdeadbeef );
731     mod = (HMODULE)0xdeadbeef;
732     ret = GetModuleHandleExA( 0, "kernel32", &mod );
733     ok( ret, "unexpected failure %u\n", GetLastError() );
734     ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
735     FreeLibrary( mod );
736 
737     SetLastError( 0xdeadbeef );
738     mod = (HMODULE)0xdeadbeef;
739     ret = GetModuleHandleExA( 0, "nosuchmod", &mod );
740     error = GetLastError();
741     ok( !ret, "unexpected success\n" );
742     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
743     ok( mod == NULL, "got %p\n", mod );
744 
745     SetLastError( 0xdeadbeef );
746     ret = GetModuleHandleExW( 0, NULL, NULL );
747     error = GetLastError();
748     ok( !ret, "unexpected success\n" );
749     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
750 
751     SetLastError( 0xdeadbeef );
752     ret = GetModuleHandleExW( 0, kernel32W, NULL );
753     error = GetLastError();
754     ok( !ret, "unexpected success\n" );
755     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
756 
757     SetLastError( 0xdeadbeef );
758     mod = (HMODULE)0xdeadbeef;
759     ret = GetModuleHandleExW( 0, kernel32W, &mod );
760     ok( ret, "unexpected failure %u\n", GetLastError() );
761     ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
762     FreeLibrary( mod );
763 
764     SetLastError( 0xdeadbeef );
765     mod = (HMODULE)0xdeadbeef;
766     ret = GetModuleHandleExW( 0, nosuchmodW, &mod );
767     error = GetLastError();
768     ok( !ret, "unexpected success\n" );
769     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
770     ok( mod == NULL, "got %p\n", mod );
771 
772     SetLastError( 0xdeadbeef );
773     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
774     error = GetLastError();
775     ok( !ret, "unexpected success\n" );
776     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
777 
778     SetLastError( 0xdeadbeef );
779     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", NULL );
780     error = GetLastError();
781     ok( !ret, "unexpected success\n" );
782     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
783 
784     SetLastError( 0xdeadbeef );
785     mod = (HMODULE)0xdeadbeef;
786     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "kernel32", &mod );
787     ok( ret, "unexpected failure %u\n", GetLastError() );
788     ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
789 
790     SetLastError( 0xdeadbeef );
791     mod = (HMODULE)0xdeadbeef;
792     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, "nosuchmod", &mod );
793     error = GetLastError();
794     ok( !ret, "unexpected success\n" );
795     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
796     ok( mod == NULL, "got %p\n", mod );
797 
798     SetLastError( 0xdeadbeef );
799     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, NULL );
800     error = GetLastError();
801     ok( !ret, "unexpected success\n" );
802     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
803 
804     SetLastError( 0xdeadbeef );
805     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, NULL );
806     error = GetLastError();
807     ok( !ret, "unexpected success\n" );
808     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
809 
810     SetLastError( 0xdeadbeef );
811     mod = (HMODULE)0xdeadbeef;
812     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, kernel32W, &mod );
813     ok( ret, "unexpected failure %u\n", GetLastError() );
814     ok( mod != (HMODULE)0xdeadbeef, "got %p\n", mod );
815 
816     SetLastError( 0xdeadbeef );
817     mod = (HMODULE)0xdeadbeef;
818     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, nosuchmodW, &mod );
819     error = GetLastError();
820     ok( !ret, "unexpected success\n" );
821     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
822     ok( mod == NULL, "got %p\n", mod );
823 
824     mod_kernel32 = LoadLibraryA( "kernel32" );
825 
826     SetLastError( 0xdeadbeef );
827     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
828     error = GetLastError();
829     ok( !ret, "unexpected success\n" );
830     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
831 
832     SetLastError( 0xdeadbeef );
833     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, NULL );
834     error = GetLastError();
835     ok( !ret, "unexpected success\n" );
836     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
837 
838     SetLastError( 0xdeadbeef );
839     mod = (HMODULE)0xdeadbeef;
840     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)mod_kernel32, &mod );
841     ok( ret, "unexpected failure %u\n", GetLastError() );
842     ok( mod == mod_kernel32, "got %p\n", mod );
843     FreeLibrary( mod );
844 
845     SetLastError( 0xdeadbeef );
846     mod = (HMODULE)0xdeadbeef;
847     ret = GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)0xbeefdead, &mod );
848     error = GetLastError();
849     ok( !ret, "unexpected success\n" );
850     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
851     ok( mod == NULL, "got %p\n", mod );
852 
853     SetLastError( 0xdeadbeef );
854     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, NULL, NULL );
855     error = GetLastError();
856     ok( !ret, "unexpected success\n" );
857     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
858 
859     SetLastError( 0xdeadbeef );
860     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, NULL );
861     error = GetLastError();
862     ok( !ret, "unexpected success\n" );
863     ok( error == ERROR_INVALID_PARAMETER, "got %u\n", error );
864 
865     SetLastError( 0xdeadbeef );
866     mod = (HMODULE)0xdeadbeef;
867     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)mod_kernel32, &mod );
868     ok( ret, "unexpected failure %u\n", GetLastError() );
869     ok( mod == mod_kernel32, "got %p\n", mod );
870     FreeLibrary( mod );
871 
872     SetLastError( 0xdeadbeef );
873     mod = (HMODULE)0xdeadbeef;
874     ret = GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)0xbeefdead, &mod );
875     error = GetLastError();
876     ok( !ret, "unexpected success\n" );
877     ok( error == ERROR_MOD_NOT_FOUND, "got %u\n", error );
878     ok( mod == NULL, "got %p\n", mod );
879 
880     FreeLibrary( mod_kernel32 );
881 }
882 
883 static void testK32GetModuleInformation(void)
884 {
885     MODULEINFO info;
886     HMODULE mod;
887     BOOL ret;
888 
889     mod = GetModuleHandleA(NULL);
890     memset(&info, 0xAA, sizeof(info));
891     ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
892     ok(ret, "K32GetModuleInformation failed for main module\n");
893     ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
894     ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
895 
896     mod = GetModuleHandleA("kernel32.dll");
897     memset(&info, 0xAA, sizeof(info));
898     ret = pK32GetModuleInformation(GetCurrentProcess(), mod, &info, sizeof(info));
899     ok(ret, "K32GetModuleInformation failed for kernel32 module\n");
900     ok(info.lpBaseOfDll == mod, "Wrong info.lpBaseOfDll = %p, expected %p\n", info.lpBaseOfDll, mod);
901     ok(info.EntryPoint != NULL, "Expected nonzero entrypoint\n");
902 }
903 
904 static void test_AddDllDirectory(void)
905 {
906     static const WCHAR tmpW[] = {'t','m','p',0};
907     static const WCHAR dotW[] = {'.','\\','.',0};
908     static const WCHAR rootW[] = {'\\',0};
909     WCHAR path[MAX_PATH], buf[MAX_PATH];
910     DLL_DIRECTORY_COOKIE cookie;
911     BOOL ret;
912 
913     if (!pAddDllDirectory || !pRemoveDllDirectory)
914     {
915         win_skip( "AddDllDirectory not available\n" );
916         return;
917     }
918 
919     buf[0] = '\0';
920     GetTempPathW( sizeof(path)/sizeof(path[0]), path );
921     ret = GetTempFileNameW( path, tmpW, 0, buf );
922     ok( ret, "GetTempFileName failed err %u\n", GetLastError() );
923     SetLastError( 0xdeadbeef );
924     cookie = pAddDllDirectory( buf );
925     ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
926     SetLastError( 0xdeadbeef );
927     ret = pRemoveDllDirectory( cookie );
928     ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
929 
930     DeleteFileW( buf );
931     SetLastError( 0xdeadbeef );
932     cookie = pAddDllDirectory( buf );
933     ok( !cookie, "AddDllDirectory succeeded\n" );
934     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "wrong error %u\n", GetLastError() );
935     cookie = pAddDllDirectory( dotW );
936     ok( !cookie, "AddDllDirectory succeeded\n" );
937     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
938     cookie = pAddDllDirectory( rootW );
939     ok( cookie != NULL, "AddDllDirectory failed err %u\n", GetLastError() );
940     SetLastError( 0xdeadbeef );
941     ret = pRemoveDllDirectory( cookie );
942     ok( ret, "RemoveDllDirectory failed err %u\n", GetLastError() );
943     GetWindowsDirectoryW( buf, MAX_PATH );
944     lstrcpyW( buf + 2, tmpW );
945     cookie = pAddDllDirectory( buf );
946     ok( !cookie, "AddDllDirectory succeeded\n" );
947     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
948 }
949 
950 static void test_SetDefaultDllDirectories(void)
951 {
952     HMODULE mod;
953     BOOL ret;
954 
955     if (!pSetDefaultDllDirectories)
956     {
957         win_skip( "SetDefaultDllDirectories not available\n" );
958         return;
959     }
960 
961     mod = LoadLibraryA( "authz.dll" );
962     ok( mod != NULL, "loading authz failed\n" );
963     FreeLibrary( mod );
964     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_USER_DIRS );
965     ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
966     mod = LoadLibraryA( "authz.dll" );
967     todo_wine ok( !mod, "loading authz succeeded\n" );
968     FreeLibrary( mod );
969     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_SYSTEM32 );
970     ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
971     mod = LoadLibraryA( "authz.dll" );
972     ok( mod != NULL, "loading authz failed\n" );
973     FreeLibrary( mod );
974     mod = LoadLibraryExA( "authz.dll", 0, LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
975     todo_wine ok( !mod, "loading authz succeeded\n" );
976     FreeLibrary( mod );
977     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR );
978     ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
979     mod = LoadLibraryA( "authz.dll" );
980     todo_wine ok( !mod, "loading authz succeeded\n" );
981     FreeLibrary( mod );
982     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
983     ok( ret, "SetDefaultDllDirectories failed err %u\n", GetLastError() );
984     mod = LoadLibraryA( "authz.dll" );
985     ok( mod != NULL, "loading authz failed\n" );
986     FreeLibrary( mod );
987 
988     SetLastError( 0xdeadbeef );
989     ret = pSetDefaultDllDirectories( 0 );
990     ok( !ret, "SetDefaultDllDirectories succeeded\n" );
991     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
992 
993     SetLastError( 0xdeadbeef );
994     ret = pSetDefaultDllDirectories( 3 );
995     ok( !ret, "SetDefaultDllDirectories succeeded\n" );
996     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
997 
998     SetLastError( 0xdeadbeef );
999     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_APPLICATION_DIR | 0x8000 );
1000     ok( !ret, "SetDefaultDllDirectories succeeded\n" );
1001     ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1002 
1003     SetLastError( 0xdeadbeef );
1004     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR );
1005     ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1006     if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1007 
1008     SetLastError( 0xdeadbeef );
1009     ret = pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_USER_DIRS );
1010     ok( !ret || broken(ret) /* win7 */, "SetDefaultDllDirectories succeeded\n" );
1011     if (!ret) ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1012 
1013     /* restore some sane defaults */
1014     pSetDefaultDllDirectories( LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
1015 }
1016 
1017 START_TEST(module)
1018 {
1019     WCHAR filenameW[MAX_PATH];
1020 
1021     /* Test if we can use GetModuleFileNameW */
1022 
1023     SetLastError(0xdeadbeef);
1024     GetModuleFileNameW(NULL, filenameW, MAX_PATH);
1025     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1026     {
1027         win_skip("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
1028         is_unicode_enabled = FALSE;
1029     }
1030 
1031     init_pointers();
1032 
1033     testGetModuleFileName(NULL);
1034     testGetModuleFileName("kernel32.dll");
1035     testGetModuleFileName_Wrong();
1036 
1037     testGetDllDirectory();
1038 
1039     testLoadLibraryA();
1040     testNestedLoadLibraryA();
1041     testLoadLibraryA_Wrong();
1042     testGetProcAddress_Wrong();
1043     testLoadLibraryEx();
1044     test_LoadLibraryEx_search_flags();
1045     testGetModuleHandleEx();
1046     testK32GetModuleInformation();
1047     test_AddDllDirectory();
1048     test_SetDefaultDllDirectories();
1049 }
1050