01fa448a33360fdd77f1c5354a70eeda7c4114cd
[reactos.git] / rostests / winetests / kernel32 / module.c
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
24 static BOOL is_unicode_enabled = TRUE;
25
26 static BOOL cmpStrAW(const char* a, const WCHAR* b, DWORD lenA, DWORD lenB)
27 {
28 WCHAR aw[1024];
29
30 DWORD len = MultiByteToWideChar( AreFileApisANSI() ? CP_ACP : CP_OEMCP, 0,
31 a, lenA, aw, sizeof(aw) / sizeof(aw[0]) );
32 if (len != lenB) return FALSE;
33 return memcmp(aw, b, len * sizeof(WCHAR)) == 0;
34 }
35
36 static void testGetModuleFileName(const char* name)
37 {
38 HMODULE hMod;
39 char bufA[MAX_PATH];
40 WCHAR bufW[MAX_PATH];
41 DWORD len1A, len1W = 0, len2A, len2W = 0;
42
43 hMod = (name) ? GetModuleHandle(name) : NULL;
44
45 /* first test, with enough space in buffer */
46 memset(bufA, '-', sizeof(bufA));
47 len1A = GetModuleFileNameA(hMod, bufA, sizeof(bufA));
48 ok(len1A > 0, "Getting module filename for handle %p\n", hMod);
49
50 if (is_unicode_enabled)
51 {
52 memset(bufW, '-', sizeof(bufW));
53 len1W = GetModuleFileNameW(hMod, bufW, sizeof(bufW) / sizeof(WCHAR));
54 ok(len1W > 0, "Getting module filename for handle %p\n", hMod);
55 }
56
57 ok(len1A == strlen(bufA), "Unexpected length of GetModuleFilenameA (%d/%d)\n", len1A, lstrlenA(bufA));
58
59 if (is_unicode_enabled)
60 {
61 ok(len1W == lstrlenW(bufW), "Unexpected length of GetModuleFilenameW (%d/%d)\n", len1W, lstrlenW(bufW));
62 ok(cmpStrAW(bufA, bufW, len1A, len1W), "Comparing GetModuleFilenameAW results\n");
63 }
64
65 /* second test with a buffer too small */
66 memset(bufA, '-', sizeof(bufA));
67 len2A = GetModuleFileNameA(hMod, bufA, len1A / 2);
68 ok(len2A > 0, "Getting module filename for handle %p\n", hMod);
69
70 if (is_unicode_enabled)
71 {
72 memset(bufW, '-', sizeof(bufW));
73 len2W = GetModuleFileNameW(hMod, bufW, len1W / 2);
74 ok(len2W > 0, "Getting module filename for handle %p\n", hMod);
75 ok(cmpStrAW(bufA, bufW, len2A, len2W), "Comparing GetModuleFilenameAW results with buffer too small\n" );
76 ok(len1W / 2 == len2W, "Correct length in GetModuleFilenameW with buffer too small (%d/%d)\n", len1W / 2, len2W);
77 }
78
79 ok(len1A / 2 == len2A ||
80 len1A / 2 == len2A + 1, /* Win9x */
81 "Correct length in GetModuleFilenameA with buffer too small (%d/%d)\n", len1A / 2, len2A);
82 }
83
84 static void testGetModuleFileName_Wrong(void)
85 {
86 char bufA[MAX_PATH];
87 WCHAR bufW[MAX_PATH];
88
89 /* test wrong handle */
90 if (is_unicode_enabled)
91 {
92 bufW[0] = '*';
93 ok(GetModuleFileNameW((void*)0xffffffff, bufW, sizeof(bufW) / sizeof(WCHAR)) == 0, "Unexpected success in module handle\n");
94 ok(bufW[0] == '*', "When failing, buffer shouldn't be written to\n");
95 }
96
97 bufA[0] = '*';
98 ok(GetModuleFileNameA((void*)0xffffffff, bufA, sizeof(bufA)) == 0, "Unexpected success in module handle\n");
99 ok(bufA[0] == '*' ||
100 bufA[0] == 0 /* Win9x */,
101 "When failing, buffer shouldn't be written to\n");
102 }
103
104 static void testLoadLibraryA(void)
105 {
106 HMODULE hModule, hModule1;
107 FARPROC fp;
108
109 SetLastError(0xdeadbeef);
110 hModule = LoadLibraryA("kernel32.dll");
111 ok( hModule != NULL, "kernel32.dll should be loadable\n");
112 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
113
114 fp = GetProcAddress(hModule, "CreateFileA");
115 ok( fp != NULL, "CreateFileA should be there\n");
116 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
117
118 SetLastError(0xdeadbeef);
119 hModule1 = LoadLibraryA("kernel32 ");
120 /* Only winNT does this */
121 if (GetLastError() != ERROR_DLL_NOT_FOUND)
122 {
123 ok( hModule1 != NULL, "\"kernel32 \" should be loadable\n");
124 ok( GetLastError() == 0xdeadbeef, "GetLastError should be 0xdeadbeef but is %d\n", GetLastError());
125 ok( hModule == hModule1, "Loaded wrong module\n");
126 FreeLibrary(hModule1);
127 }
128 FreeLibrary(hModule);
129 }
130
131 static void testNestedLoadLibraryA(void)
132 {
133 static const char dllname[] = "shell32.dll";
134 char path1[MAX_PATH], path2[MAX_PATH];
135 HMODULE hModule1, hModule2, hModule3;
136
137 /* This is not really a Windows conformance test, but more a Wine
138 * regression test. Wine's builtin dlls can be loaded from multiple paths,
139 * and this test tries to make sure that Wine does not get confused and
140 * really unloads the Unix .so file at the right time. Failure to do so
141 * will result in the dll being unloadable.
142 * This test must be done with a dll that can be unloaded, which means:
143 * - it must not already be loaded
144 * - it must not have a 16-bit counterpart
145 */
146 GetWindowsDirectory(path1, sizeof(path1));
147 strcat(path1, "\\system\\");
148 strcat(path1, dllname);
149 hModule1 = LoadLibraryA(path1);
150 if (!hModule1)
151 {
152 /* We must be on Windows NT, so we cannot test */
153 return;
154 }
155
156 GetWindowsDirectory(path2, sizeof(path2));
157 strcat(path2, "\\system32\\");
158 strcat(path2, dllname);
159 hModule2 = LoadLibraryA(path2);
160 if (!hModule2)
161 {
162 /* We must be on Windows 9x, so we cannot test */
163 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
164 return;
165 }
166
167 /* The first LoadLibrary() call may have registered the dll under the
168 * system32 path. So load it, again, under the '...\system\...' path so
169 * Wine does not immediately notice that it is already loaded.
170 */
171 hModule3 = LoadLibraryA(path1);
172 ok(hModule3 != NULL, "LoadLibrary(%s) failed\n", path1);
173
174 /* Now fully unload the dll */
175 ok(FreeLibrary(hModule3), "FreeLibrary() failed\n");
176 ok(FreeLibrary(hModule2), "FreeLibrary() failed\n");
177 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
178 ok(GetModuleHandle(dllname) == NULL, "%s was not fully unloaded\n", dllname);
179
180 /* Try to load the dll again, if refcounting is ok, this should work */
181 hModule1 = LoadLibraryA(path1);
182 ok(hModule1 != NULL, "LoadLibrary(%s) failed\n", path1);
183 if (hModule1 != NULL)
184 ok(FreeLibrary(hModule1), "FreeLibrary() failed\n");
185 }
186
187 static void testLoadLibraryA_Wrong(void)
188 {
189 HMODULE hModule;
190
191 /* Try to load a nonexistent dll */
192 SetLastError(0xdeadbeef);
193 hModule = LoadLibraryA("non_ex_pv.dll");
194 ok( !hModule, "non_ex_pv.dll should be not loadable\n");
195 ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_DLL_NOT_FOUND,
196 "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND (win9x), got %d\n", GetLastError());
197
198 /* Just in case */
199 FreeLibrary(hModule);
200 }
201
202 static void testGetProcAddress_Wrong(void)
203 {
204 FARPROC fp;
205
206 SetLastError(0xdeadbeef);
207 fp = GetProcAddress(NULL, "non_ex_call");
208 ok( !fp, "non_ex_call should not be found\n");
209 ok( GetLastError() == ERROR_PROC_NOT_FOUND || GetLastError() == ERROR_INVALID_HANDLE,
210 "Expected ERROR_PROC_NOT_FOUND or ERROR_INVALID_HANDLE(win9x), got %d\n", GetLastError());
211
212 SetLastError(0xdeadbeef);
213 fp = GetProcAddress((HMODULE)0xdeadbeef, "non_ex_call");
214 ok( !fp, "non_ex_call should not be found\n");
215 ok( GetLastError() == ERROR_MOD_NOT_FOUND || GetLastError() == ERROR_INVALID_HANDLE,
216 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_HANDLE(win9x), got %d\n", GetLastError());
217 }
218
219 static void testLoadLibraryEx(void)
220 {
221 CHAR path[MAX_PATH];
222 HMODULE hmodule;
223 HANDLE hfile;
224 BOOL ret;
225
226 hfile = CreateFileA("testfile.dll", GENERIC_READ | GENERIC_WRITE,
227 FILE_SHARE_READ | FILE_SHARE_WRITE,
228 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
229 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
230
231 /* NULL lpFileName */
232 if (is_unicode_enabled)
233 {
234 SetLastError(0xdeadbeef);
235 hmodule = LoadLibraryExA(NULL, NULL, 0);
236 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
237 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
238 GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */
239 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
240 GetLastError());
241 }
242 else
243 win_skip("NULL filename crashes on WinMe\n");
244
245 /* empty lpFileName */
246 SetLastError(0xdeadbeef);
247 hmodule = LoadLibraryExA("", NULL, 0);
248 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
249 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
250 GetLastError() == ERROR_DLL_NOT_FOUND, /* win9x */
251 "Expected ERROR_MOD_NOT_FOUND or ERROR_DLL_NOT_FOUND, got %d\n",
252 GetLastError());
253
254 /* hFile is non-NULL */
255 SetLastError(0xdeadbeef);
256 hmodule = LoadLibraryExA("testfile.dll", hfile, 0);
257 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
258 todo_wine
259 {
260 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
261 GetLastError() == ERROR_INVALID_PARAMETER || /* win2k3 */
262 GetLastError() == ERROR_FILE_NOT_FOUND, /* win9x */
263 "Unexpected last error, got %d\n", GetLastError());
264 }
265
266 SetLastError(0xdeadbeef);
267 hmodule = LoadLibraryExA("testfile.dll", (HANDLE)0xdeadbeef, 0);
268 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
269 todo_wine
270 {
271 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
272 GetLastError() == ERROR_INVALID_PARAMETER || /* win2k3 */
273 GetLastError() == ERROR_FILE_NOT_FOUND, /* win9x */
274 "Unexpected last error, got %d\n", GetLastError());
275 }
276
277 /* try to open a file that is locked */
278 SetLastError(0xdeadbeef);
279 hmodule = LoadLibraryExA("testfile.dll", NULL, 0);
280 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
281 todo_wine
282 {
283 ok(GetLastError() == ERROR_SHARING_VIOLATION ||
284 GetLastError() == ERROR_FILE_NOT_FOUND, /* win9x */
285 "Expected ERROR_SHARING_VIOLATION or ERROR_FILE_NOT_FOUND, got %d\n",
286 GetLastError());
287 }
288
289 /* lpFileName does not matter */
290 if (is_unicode_enabled)
291 {
292 SetLastError(0xdeadbeef);
293 hmodule = LoadLibraryExA(NULL, hfile, 0);
294 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
295 ok(GetLastError() == ERROR_MOD_NOT_FOUND ||
296 GetLastError() == ERROR_INVALID_PARAMETER, /* win2k3 */
297 "Expected ERROR_MOD_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
298 GetLastError());
299 }
300
301 CloseHandle(hfile);
302
303 /* load empty file */
304 SetLastError(0xdeadbeef);
305 hmodule = LoadLibraryExA("testfile.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
306 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
307 todo_wine
308 {
309 ok(GetLastError() == ERROR_FILE_INVALID ||
310 GetLastError() == ERROR_BAD_FORMAT, /* win9x */
311 "Expected ERROR_FILE_INVALID or ERROR_BAD_FORMAT, got %d\n",
312 GetLastError());
313 }
314
315 DeleteFileA("testfile.dll");
316
317 GetSystemDirectoryA(path, MAX_PATH);
318 if (path[lstrlenA(path) - 1] != '\\')
319 lstrcatA(path, "\\");
320 lstrcatA(path, "kernel32.dll");
321
322 /* load kernel32.dll with an absolute path */
323 SetLastError(0xdeadbeef);
324 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
325 ok(hmodule != 0, "Expected valid module handle\n");
326 ok(GetLastError() == 0xdeadbeef ||
327 GetLastError() == ERROR_SUCCESS, /* win9x */
328 "Expected 0xdeadbeef or ERROR_SUCCESS, got %d\n", GetLastError());
329
330 /* try invalid file handle */
331 SetLastError(0xdeadbeef);
332 hmodule = LoadLibraryExA(path, (HANDLE)0xdeadbeef, 0);
333 if (!hmodule) /* succeeds on xp and older */
334 ok(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError());
335
336 CloseHandle(hmodule);
337
338 /* load kernel32.dll with no path */
339 SetLastError(0xdeadbeef);
340 hmodule = LoadLibraryExA("kernel32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
341 ok(hmodule != 0, "Expected valid module handle\n");
342 ok(GetLastError() == 0xdeadbeef ||
343 GetLastError() == ERROR_SUCCESS, /* win9x */
344 "Expected 0xdeadbeef or ERROR_SUCCESS, got %d\n", GetLastError());
345
346 CloseHandle(hmodule);
347
348 GetCurrentDirectoryA(MAX_PATH, path);
349 if (path[lstrlenA(path) - 1] != '\\')
350 lstrcatA(path, "\\");
351 lstrcatA(path, "kernel32.dll");
352
353 /* load kernel32.dll with an absolute path that does not exist */
354 SetLastError(0xdeadbeef);
355 hmodule = LoadLibraryExA(path, NULL, LOAD_LIBRARY_AS_DATAFILE);
356 todo_wine
357 {
358 ok(hmodule == 0, "Expected 0, got %p\n", hmodule);
359 }
360 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
361 broken(GetLastError() == ERROR_INVALID_HANDLE), /* nt4 */
362 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
363
364 /* Free the loaded dll when its the first time this dll is loaded
365 in process - First time should pass, second fail */
366 SetLastError(0xdeadbeef);
367 hmodule = LoadLibraryExA("comctl32.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
368 ok(hmodule != 0, "Expected valid module handle\n");
369
370 SetLastError(0xdeadbeef);
371 ret = FreeLibrary(hmodule);
372 ok(ret, "Expected to be able to free the module, failed with %d\n", GetLastError());
373 SetLastError(0xdeadbeef);
374 ret = FreeLibrary(hmodule);
375 ok(!ret, "Unexpected ability to free the module, failed with %d\n", GetLastError());
376
377 CloseHandle(hmodule);
378
379 }
380
381 START_TEST(module)
382 {
383 WCHAR filenameW[MAX_PATH];
384
385 /* Test if we can use GetModuleFileNameW */
386
387 SetLastError(0xdeadbeef);
388 GetModuleFileNameW(NULL, filenameW, MAX_PATH);
389 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
390 {
391 win_skip("GetModuleFileNameW not existing on this platform, skipping W-calls\n");
392 is_unicode_enabled = FALSE;
393 }
394
395 testGetModuleFileName(NULL);
396 testGetModuleFileName("kernel32.dll");
397 testGetModuleFileName_Wrong();
398
399 testLoadLibraryA();
400 testNestedLoadLibraryA();
401 testLoadLibraryA_Wrong();
402 testGetProcAddress_Wrong();
403 testLoadLibraryEx();
404 }