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