Commit | Line | Data |
---|---|---|
594997c9 AJ |
1 | /* |
2 | * Modules | |
3 | * | |
4 | * Copyright 1995 Alexandre Julliard | |
0799c1a7 AJ |
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 | |
360a3f91 | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
594997c9 AJ |
19 | */ |
20 | ||
d016f819 | 21 | #include "config.h" |
9aab47ed | 22 | #include "wine/port.h" |
d016f819 | 23 | |
594997c9 | 24 | #include <fcntl.h> |
402b79a1 | 25 | #include <stdio.h> |
594997c9 | 26 | #include <stdlib.h> |
594997c9 | 27 | #include <string.h> |
8f9b09e9 | 28 | #include <sys/types.h> |
d016f819 PS |
29 | #ifdef HAVE_UNISTD_H |
30 | # include <unistd.h> | |
31 | #endif | |
1a1583a3 GG |
32 | #include "ntstatus.h" |
33 | #define WIN32_NO_STATUS | |
d3e22d9d | 34 | #include "wine/winbase16.h" |
85ed45e3 | 35 | #include "winerror.h" |
e37c6e18 AJ |
36 | #include "windef.h" |
37 | #include "winbase.h" | |
9c1de6de | 38 | #include "winternl.h" |
fba7149a | 39 | #include "kernel_private.h" |
cf07e100 | 40 | |
646f17f2 | 41 | #include "wine/exception.h" |
0799c1a7 | 42 | #include "wine/debug.h" |
cf07e100 | 43 | #include "wine/unicode.h" |
594997c9 | 44 | |
0799c1a7 | 45 | WINE_DEFAULT_DEBUG_CHANNEL(module); |
03e4ea17 | 46 | |
dab980e0 AJ |
47 | static WCHAR *dll_directory; /* extra path for SetDllDirectoryW */ |
48 | ||
49 | static CRITICAL_SECTION dlldir_section; | |
50 | static CRITICAL_SECTION_DEBUG critsect_debug = | |
51 | { | |
52 | 0, 0, &dlldir_section, | |
53 | { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList }, | |
20a1a203 | 54 | 0, 0, { (DWORD_PTR)(__FILE__ ": dlldir_section") } |
dab980e0 AJ |
55 | }; |
56 | static CRITICAL_SECTION dlldir_section = { &critsect_debug, -1, 0, 0, 0, 0 }; | |
57 | ||
58 | ||
59 | /**************************************************************************** | |
60 | * GetDllDirectoryA (KERNEL32.@) | |
61 | */ | |
62 | DWORD WINAPI GetDllDirectoryA( DWORD buf_len, LPSTR buffer ) | |
63 | { | |
64 | DWORD len; | |
65 | ||
66 | RtlEnterCriticalSection( &dlldir_section ); | |
67 | len = dll_directory ? FILE_name_WtoA( dll_directory, strlenW(dll_directory), NULL, 0 ) : 0; | |
68 | if (buffer && buf_len > len) | |
69 | { | |
70 | if (dll_directory) FILE_name_WtoA( dll_directory, -1, buffer, buf_len ); | |
71 | else *buffer = 0; | |
72 | } | |
73 | else | |
74 | { | |
75 | len++; /* for terminating null */ | |
76 | if (buffer) *buffer = 0; | |
77 | } | |
78 | RtlLeaveCriticalSection( &dlldir_section ); | |
79 | return len; | |
80 | } | |
81 | ||
82 | ||
83 | /**************************************************************************** | |
84 | * GetDllDirectoryW (KERNEL32.@) | |
85 | */ | |
86 | DWORD WINAPI GetDllDirectoryW( DWORD buf_len, LPWSTR buffer ) | |
87 | { | |
88 | DWORD len; | |
89 | ||
90 | RtlEnterCriticalSection( &dlldir_section ); | |
91 | len = dll_directory ? strlenW( dll_directory ) : 0; | |
92 | if (buffer && buf_len > len) | |
93 | { | |
94 | if (dll_directory) memcpy( buffer, dll_directory, (len + 1) * sizeof(WCHAR) ); | |
95 | else *buffer = 0; | |
96 | } | |
97 | else | |
98 | { | |
99 | len++; /* for terminating null */ | |
100 | if (buffer) *buffer = 0; | |
101 | } | |
102 | RtlLeaveCriticalSection( &dlldir_section ); | |
103 | return len; | |
104 | } | |
105 | ||
106 | ||
107 | /**************************************************************************** | |
108 | * SetDllDirectoryA (KERNEL32.@) | |
109 | */ | |
110 | BOOL WINAPI SetDllDirectoryA( LPCSTR dir ) | |
111 | { | |
112 | WCHAR *dirW; | |
113 | BOOL ret; | |
114 | ||
115 | if (!(dirW = FILE_name_AtoW( dir, TRUE ))) return FALSE; | |
116 | ret = SetDllDirectoryW( dirW ); | |
117 | HeapFree( GetProcessHeap(), 0, dirW ); | |
118 | return ret; | |
119 | } | |
120 | ||
121 | ||
122 | /**************************************************************************** | |
123 | * SetDllDirectoryW (KERNEL32.@) | |
124 | */ | |
125 | BOOL WINAPI SetDllDirectoryW( LPCWSTR dir ) | |
126 | { | |
127 | WCHAR *newdir = NULL; | |
128 | ||
129 | if (dir) | |
130 | { | |
131 | DWORD len = (strlenW(dir) + 1) * sizeof(WCHAR); | |
132 | if (!(newdir = HeapAlloc( GetProcessHeap(), 0, len ))) | |
133 | { | |
134 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); | |
135 | return FALSE; | |
136 | } | |
137 | memcpy( newdir, dir, len ); | |
138 | } | |
139 | ||
140 | RtlEnterCriticalSection( &dlldir_section ); | |
141 | HeapFree( GetProcessHeap(), 0, dll_directory ); | |
142 | dll_directory = newdir; | |
143 | RtlLeaveCriticalSection( &dlldir_section ); | |
144 | return TRUE; | |
145 | } | |
146 | ||
147 | ||
e469a583 | 148 | /**************************************************************************** |
dae8de69 | 149 | * DisableThreadLibraryCalls (KERNEL32.@) |
c1d1cfe9 | 150 | * |
4c1fa161 JG |
151 | * Inform the module loader that thread notifications are not required for a dll. |
152 | * | |
153 | * PARAMS | |
154 | * hModule [I] Module handle to skip calls for | |
155 | * | |
156 | * RETURNS | |
157 | * Success: TRUE. Thread attach and detach notifications will not be sent | |
158 | * to hModule. | |
159 | * Failure: FALSE. Use GetLastError() to determine the cause. | |
160 | * | |
161 | * NOTES | |
162 | * This is typically called from the dll entry point of a dll during process | |
163 | * attachment, for dlls that do not need to process thread notifications. | |
e469a583 | 164 | */ |
c1d1cfe9 | 165 | BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule ) |
e469a583 | 166 | { |
7d609648 EP |
167 | NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule ); |
168 | if (nts == STATUS_SUCCESS) return TRUE; | |
e469a583 | 169 | |
7d609648 EP |
170 | SetLastError( RtlNtStatusToDosError( nts ) ); |
171 | return FALSE; | |
e469a583 UW |
172 | } |
173 | ||
a2f2e019 | 174 | |
b021fe29 AM |
175 | /* Check whether a file is an OS/2 or a very old Windows executable |
176 | * by testing on import of KERNEL. | |
177 | * | |
178 | * FIXME: is reading the module imports the only way of discerning | |
179 | * old Windows binaries from OS/2 ones ? At least it seems so... | |
180 | */ | |
3536316a AJ |
181 | static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz, |
182 | const IMAGE_OS2_HEADER *ne) | |
b021fe29 AM |
183 | { |
184 | DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR); | |
3536316a | 185 | enum binary_type ret = BINARY_OS216; |
b021fe29 AM |
186 | LPWORD modtab = NULL; |
187 | LPSTR nametab = NULL; | |
188 | DWORD len; | |
189 | int i; | |
190 | ||
191 | /* read modref table */ | |
192 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1) | |
193 | || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD)))) | |
194 | || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL))) | |
195 | || (len != ne->ne_cmod*sizeof(WORD)) ) | |
196 | goto broken; | |
197 | ||
198 | /* read imported names table */ | |
199 | if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1) | |
200 | || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab))) | |
201 | || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL))) | |
202 | || (len != ne->ne_enttab - ne->ne_imptab) ) | |
203 | goto broken; | |
204 | ||
205 | for (i=0; i < ne->ne_cmod; i++) | |
206 | { | |
207 | LPSTR module = &nametab[modtab[i]]; | |
208 | TRACE("modref: %.*s\n", module[0], &module[1]); | |
209 | if (!(strncmp(&module[1], "KERNEL", module[0]))) | |
210 | { /* very old Windows file */ | |
211 | MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n"); | |
3536316a | 212 | ret = BINARY_WIN16; |
b021fe29 AM |
213 | goto good; |
214 | } | |
215 | } | |
216 | ||
217 | broken: | |
85b06836 | 218 | ERR("Hmm, an error occurred. Is this binary file broken?\n"); |
b021fe29 AM |
219 | |
220 | good: | |
221 | HeapFree( GetProcessHeap(), 0, modtab); | |
222 | HeapFree( GetProcessHeap(), 0, nametab); | |
223 | SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */ | |
3536316a | 224 | return ret; |
b021fe29 AM |
225 | } |
226 | ||
6e0d3860 UW |
227 | /*********************************************************************** |
228 | * MODULE_GetBinaryType | |
3536316a | 229 | */ |
10e6f57f | 230 | enum binary_type MODULE_GetBinaryType( HANDLE hfile, void **res_start, void **res_end ) |
3536316a AJ |
231 | { |
232 | union | |
233 | { | |
234 | struct | |
235 | { | |
236 | unsigned char magic[4]; | |
237 | unsigned char ignored[12]; | |
238 | unsigned short type; | |
239 | } elf; | |
89321f49 PH |
240 | struct |
241 | { | |
242 | unsigned long magic; | |
243 | unsigned long cputype; | |
244 | unsigned long cpusubtype; | |
245 | unsigned long filetype; | |
246 | } macho; | |
3536316a AJ |
247 | IMAGE_DOS_HEADER mz; |
248 | } header; | |
249 | ||
3536316a AJ |
250 | DWORD len; |
251 | ||
252 | /* Seek to the start of the file and read the header information. */ | |
253 | if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1) | |
254 | return BINARY_UNKNOWN; | |
255 | if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header)) | |
256 | return BINARY_UNKNOWN; | |
257 | ||
258 | if (!memcmp( header.elf.magic, "\177ELF", 4 )) | |
259 | { | |
260 | /* FIXME: we don't bother to check byte order, architecture, etc. */ | |
261 | switch(header.elf.type) | |
262 | { | |
263 | case 2: return BINARY_UNIX_EXE; | |
264 | case 3: return BINARY_UNIX_LIB; | |
265 | } | |
266 | return BINARY_UNKNOWN; | |
267 | } | |
268 | ||
85b06836 | 269 | /* Mach-o File with Endian set to Big Endian or Little Endian */ |
2f7480e5 | 270 | if (header.macho.magic == 0xfeedface || header.macho.magic == 0xcefaedfe) |
89321f49 PH |
271 | { |
272 | switch(header.macho.filetype) | |
273 | { | |
274 | case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB; | |
275 | } | |
276 | return BINARY_UNKNOWN; | |
277 | } | |
278 | ||
3536316a AJ |
279 | /* Not ELF, try DOS */ |
280 | ||
281 | if (header.mz.e_magic == IMAGE_DOS_SIGNATURE) | |
282 | { | |
10e6f57f AJ |
283 | union |
284 | { | |
285 | IMAGE_OS2_HEADER os2; | |
286 | IMAGE_NT_HEADERS nt; | |
287 | } ext_header; | |
288 | ||
3536316a AJ |
289 | /* We do have a DOS image so we will now try to seek into |
290 | * the file by the amount indicated by the field | |
291 | * "Offset to extended header" and read in the | |
292 | * "magic" field information at that location. | |
293 | * This will tell us if there is more header information | |
294 | * to read or not. | |
295 | */ | |
3536316a AJ |
296 | if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1) |
297 | return BINARY_DOS; | |
10e6f57f | 298 | if (!ReadFile( hfile, &ext_header, sizeof(ext_header), &len, NULL ) || len < 4) |
3536316a AJ |
299 | return BINARY_DOS; |
300 | ||
301 | /* Reading the magic field succeeded so | |
302 | * we will try to determine what type it is. | |
303 | */ | |
10e6f57f | 304 | if (!memcmp( &ext_header.nt.Signature, "PE\0\0", 4 )) |
3536316a | 305 | { |
10e6f57f | 306 | if (len >= sizeof(ext_header.nt.FileHeader)) |
3536316a | 307 | { |
10e6f57f AJ |
308 | if (len < sizeof(ext_header.nt)) /* clear remaining part of header if missing */ |
309 | memset( (char *)&ext_header.nt + len, 0, sizeof(ext_header.nt) - len ); | |
310 | if (res_start) *res_start = (void *)ext_header.nt.OptionalHeader.ImageBase; | |
311 | if (res_end) *res_end = (void *)(ext_header.nt.OptionalHeader.ImageBase + | |
312 | ext_header.nt.OptionalHeader.SizeOfImage); | |
313 | if (ext_header.nt.FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL; | |
3536316a AJ |
314 | return BINARY_PE_EXE; |
315 | } | |
1467bbd5 | 316 | return BINARY_DOS; |
3536316a AJ |
317 | } |
318 | ||
10e6f57f | 319 | if (!memcmp( &ext_header.os2.ne_magic, "NE", 2 )) |
3536316a AJ |
320 | { |
321 | /* This is a Windows executable (NE) header. This can | |
322 | * mean either a 16-bit OS/2 or a 16-bit Windows or even a | |
323 | * DOS program (running under a DOS extender). To decide | |
324 | * which, we'll have to read the NE header. | |
325 | */ | |
10e6f57f | 326 | if (len >= sizeof(ext_header.os2)) |
3536316a | 327 | { |
10e6f57f | 328 | switch ( ext_header.os2.ne_exetyp ) |
3536316a | 329 | { |
85b06836 AM |
330 | case 1: return BINARY_OS216; /* OS/2 */ |
331 | case 2: return BINARY_WIN16; /* Windows */ | |
332 | case 3: return BINARY_DOS; /* European MS-DOS 4.x */ | |
333 | case 4: return BINARY_WIN16; /* Windows 386; FIXME: is this 32bit??? */ | |
334 | case 5: return BINARY_DOS; /* BOSS, Borland Operating System Services */ | |
335 | /* other types, e.g. 0 is: "unknown" */ | |
10e6f57f | 336 | default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ext_header.os2); |
3536316a AJ |
337 | } |
338 | } | |
339 | /* Couldn't read header, so abort. */ | |
1467bbd5 | 340 | return BINARY_DOS; |
3536316a AJ |
341 | } |
342 | ||
343 | /* Unknown extended header, but this file is nonetheless DOS-executable. */ | |
344 | return BINARY_DOS; | |
345 | } | |
346 | ||
347 | return BINARY_UNKNOWN; | |
348 | } | |
349 | ||
350 | /*********************************************************************** | |
45ac9cd2 | 351 | * GetBinaryTypeW [KERNEL32.@] |
6e0d3860 | 352 | * |
4c1fa161 JG |
353 | * Determine whether a file is executable, and if so, what kind. |
354 | * | |
355 | * PARAMS | |
356 | * lpApplicationName [I] Path of the file to check | |
357 | * lpBinaryType [O] Destination for the binary type | |
358 | * | |
359 | * RETURNS | |
360 | * TRUE, if the file is an executable, in which case lpBinaryType is set. | |
361 | * FALSE, if the file is not an executable or if the function fails. | |
362 | * | |
363 | * NOTES | |
364 | * The type of executable is a property that determines which subsytem an | |
365 | * executable file runs under. lpBinaryType can be set to one of the following | |
366 | * values: | |
367 | * SCS_32BIT_BINARY: A Win32 based application | |
368 | * SCS_DOS_BINARY: An MS-Dos based application | |
369 | * SCS_WOW_BINARY: A Win16 based application | |
370 | * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app | |
371 | * SCS_POSIX_BINARY: A POSIX based application ( Not implemented ) | |
372 | * SCS_OS216_BINARY: A 16bit OS/2 based application | |
373 | * | |
374 | * To find the binary type, this function reads in the files header information. | |
375 | * If extended header information is not present it will assume that the file | |
376 | * is a DOS executable. If extended header information is present it will | |
377 | * determine if the file is a 16 or 32 bit Windows executable by checking the | |
378 | * flags in the header. | |
379 | * | |
380 | * ".com" and ".pif" files are only recognized by their file name extension, | |
381 | * as per native Windows. | |
594997c9 | 382 | */ |
45ac9cd2 | 383 | BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType ) |
6e0d3860 UW |
384 | { |
385 | BOOL ret = FALSE; | |
237e8e95 | 386 | HANDLE hfile; |
1e37a181 | 387 | |
45ac9cd2 | 388 | TRACE("%s\n", debugstr_w(lpApplicationName) ); |
a2f2e019 | 389 | |
6e0d3860 UW |
390 | /* Sanity check. |
391 | */ | |
392 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) | |
393 | return FALSE; | |
394 | ||
395 | /* Open the file indicated by lpApplicationName for reading. | |
396 | */ | |
45ac9cd2 | 397 | hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ, |
da2b6a9f | 398 | NULL, OPEN_EXISTING, 0, 0 ); |
237e8e95 | 399 | if ( hfile == INVALID_HANDLE_VALUE ) |
6e0d3860 UW |
400 | return FALSE; |
401 | ||
402 | /* Check binary type | |
403 | */ | |
10e6f57f | 404 | switch(MODULE_GetBinaryType( hfile, NULL, NULL )) |
3536316a AJ |
405 | { |
406 | case BINARY_UNKNOWN: | |
45ac9cd2 DT |
407 | { |
408 | static const WCHAR comW[] = { '.','C','O','M',0 }; | |
409 | static const WCHAR pifW[] = { '.','P','I','F',0 }; | |
410 | const WCHAR *ptr; | |
411 | ||
3536316a | 412 | /* try to determine from file name */ |
45ac9cd2 | 413 | ptr = strrchrW( lpApplicationName, '.' ); |
3536316a | 414 | if (!ptr) break; |
45ac9cd2 | 415 | if (!strcmpiW( ptr, comW )) |
3536316a AJ |
416 | { |
417 | *lpBinaryType = SCS_DOS_BINARY; | |
418 | ret = TRUE; | |
419 | } | |
45ac9cd2 | 420 | else if (!strcmpiW( ptr, pifW )) |
3536316a AJ |
421 | { |
422 | *lpBinaryType = SCS_PIF_BINARY; | |
423 | ret = TRUE; | |
424 | } | |
425 | break; | |
45ac9cd2 | 426 | } |
3536316a AJ |
427 | case BINARY_PE_EXE: |
428 | case BINARY_PE_DLL: | |
429 | *lpBinaryType = SCS_32BIT_BINARY; | |
430 | ret = TRUE; | |
431 | break; | |
432 | case BINARY_WIN16: | |
433 | *lpBinaryType = SCS_WOW_BINARY; | |
434 | ret = TRUE; | |
435 | break; | |
436 | case BINARY_OS216: | |
437 | *lpBinaryType = SCS_OS216_BINARY; | |
438 | ret = TRUE; | |
439 | break; | |
440 | case BINARY_DOS: | |
441 | *lpBinaryType = SCS_DOS_BINARY; | |
442 | ret = TRUE; | |
443 | break; | |
444 | case BINARY_UNIX_EXE: | |
445 | case BINARY_UNIX_LIB: | |
446 | ret = FALSE; | |
447 | break; | |
448 | } | |
6e0d3860 | 449 | |
6e0d3860 | 450 | CloseHandle( hfile ); |
6e0d3860 | 451 | return ret; |
2787be87 AJ |
452 | } |
453 | ||
6e0d3860 | 454 | /*********************************************************************** |
45ac9cd2 DT |
455 | * GetBinaryTypeA [KERNEL32.@] |
456 | * GetBinaryType [KERNEL32.@] | |
b8d9b619 MA |
457 | * |
458 | * See GetBinaryTypeW. | |
6e0d3860 | 459 | */ |
45ac9cd2 | 460 | BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType ) |
6e0d3860 | 461 | { |
45ac9cd2 DT |
462 | ANSI_STRING app_nameA; |
463 | NTSTATUS status; | |
6e0d3860 | 464 | |
45ac9cd2 | 465 | TRACE("%s\n", debugstr_a(lpApplicationName)); |
6e0d3860 UW |
466 | |
467 | /* Sanity check. | |
468 | */ | |
469 | if ( lpApplicationName == NULL || lpBinaryType == NULL ) | |
470 | return FALSE; | |
471 | ||
45ac9cd2 DT |
472 | RtlInitAnsiString(&app_nameA, lpApplicationName); |
473 | status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString, | |
474 | &app_nameA, FALSE); | |
475 | if (!status) | |
476 | return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType); | |
6e0d3860 | 477 | |
45ac9cd2 DT |
478 | SetLastError(RtlNtStatusToDosError(status)); |
479 | return FALSE; | |
6e0d3860 | 480 | } |
2787be87 | 481 | |
ab2349da AJ |
482 | /*********************************************************************** |
483 | * GetModuleHandleExA (KERNEL32.@) | |
484 | */ | |
485 | BOOL WINAPI GetModuleHandleExA( DWORD flags, LPCSTR name, HMODULE *module ) | |
486 | { | |
487 | WCHAR *nameW; | |
488 | ||
489 | if (!name || (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)) | |
490 | return GetModuleHandleExW( flags, (LPCWSTR)name, module ); | |
491 | ||
492 | if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE; | |
493 | return GetModuleHandleExW( flags, nameW, module ); | |
494 | } | |
495 | ||
496 | /*********************************************************************** | |
497 | * GetModuleHandleExW (KERNEL32.@) | |
498 | */ | |
499 | BOOL WINAPI GetModuleHandleExW( DWORD flags, LPCWSTR name, HMODULE *module ) | |
500 | { | |
501 | NTSTATUS status = STATUS_SUCCESS; | |
502 | HMODULE ret; | |
306d71c3 AJ |
503 | ULONG magic; |
504 | ||
505 | /* if we are messing with the refcount, grab the loader lock */ | |
506 | if ((flags & GET_MODULE_HANDLE_EX_FLAG_PIN) || | |
507 | !(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) | |
508 | LdrLockLoaderLock( 0, NULL, &magic ); | |
ab2349da AJ |
509 | |
510 | if (!name) | |
511 | { | |
512 | ret = NtCurrentTeb()->Peb->ImageBaseAddress; | |
513 | } | |
514 | else if (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) | |
515 | { | |
516 | void *dummy; | |
517 | if (!(ret = RtlPcToFileHeader( (void *)name, &dummy ))) status = STATUS_DLL_NOT_FOUND; | |
518 | } | |
519 | else | |
520 | { | |
521 | UNICODE_STRING wstr; | |
522 | RtlInitUnicodeString( &wstr, name ); | |
306d71c3 | 523 | status = LdrGetDllHandle( NULL, 0, &wstr, &ret ); |
ab2349da AJ |
524 | } |
525 | ||
306d71c3 | 526 | if (status == STATUS_SUCCESS) |
ab2349da | 527 | { |
306d71c3 AJ |
528 | if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) |
529 | FIXME( "should pin refcount for %p\n", ret ); | |
530 | else if (!(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) | |
531 | LdrAddRefDll( 0, ret ); | |
ab2349da | 532 | } |
306d71c3 | 533 | else SetLastError( RtlNtStatusToDosError( status ) ); |
ab2349da AJ |
534 | |
535 | if ((flags & GET_MODULE_HANDLE_EX_FLAG_PIN) || | |
536 | !(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) | |
306d71c3 | 537 | LdrUnlockLoaderLock( 0, magic ); |
ab2349da AJ |
538 | |
539 | if (module) *module = ret; | |
306d71c3 | 540 | return (status == STATUS_SUCCESS); |
ab2349da | 541 | } |
a0d77315 | 542 | |
77b9918e | 543 | /*********************************************************************** |
dae8de69 | 544 | * GetModuleHandleA (KERNEL32.@) |
044855c6 | 545 | * GetModuleHandle32 (KERNEL.488) |
4c1fa161 JG |
546 | * |
547 | * Get the handle of a dll loaded into the process address space. | |
548 | * | |
549 | * PARAMS | |
550 | * module [I] Name of the dll | |
551 | * | |
552 | * RETURNS | |
553 | * Success: A handle to the loaded dll. | |
554 | * Failure: A NULL handle. Use GetLastError() to determine the cause. | |
77b9918e | 555 | */ |
a3960292 | 556 | HMODULE WINAPI GetModuleHandleA(LPCSTR module) |
77b9918e | 557 | { |
ab2349da | 558 | HMODULE ret; |
c1d1cfe9 | 559 | |
ab2349da AJ |
560 | if (!GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret )) ret = 0; |
561 | return ret; | |
77b9918e AJ |
562 | } |
563 | ||
2d6457c1 | 564 | /*********************************************************************** |
3ca98239 | 565 | * GetModuleHandleW (KERNEL32.@) |
4c1fa161 JG |
566 | * |
567 | * Unicode version of GetModuleHandleA. | |
2d6457c1 | 568 | */ |
a3960292 | 569 | HMODULE WINAPI GetModuleHandleW(LPCWSTR module) |
77b9918e | 570 | { |
ab2349da | 571 | HMODULE ret; |
f3a73ef7 | 572 | |
ab2349da | 573 | if (!GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret )) ret = 0; |
f3a73ef7 | 574 | return ret; |
77b9918e AJ |
575 | } |
576 | ||
594997c9 | 577 | |
b1bac320 | 578 | /*********************************************************************** |
dae8de69 | 579 | * GetModuleFileNameA (KERNEL32.@) |
044855c6 | 580 | * GetModuleFileName32 (KERNEL.487) |
4654c321 | 581 | * |
4c1fa161 JG |
582 | * Get the file name of a loaded module from its handle. |
583 | * | |
584 | * RETURNS | |
585 | * Success: The length of the file name, excluding the terminating NUL. | |
586 | * Failure: 0. Use GetLastError() to determine the cause. | |
587 | * | |
588 | * NOTES | |
589 | * This function always returns the long path of hModule (as opposed to | |
590 | * GetModuleFileName16() which returns short paths when the modules version | |
591 | * field is < 4.0). | |
08383277 | 592 | * The function doesn't write a terminating '\0' if the buffer is too |
df93f2ee | 593 | * small. |
b1bac320 | 594 | */ |
9a624916 | 595 | DWORD WINAPI GetModuleFileNameA( |
4c1fa161 JG |
596 | HMODULE hModule, /* [in] Module handle (32 bit) */ |
597 | LPSTR lpFileName, /* [out] Destination for file name */ | |
598 | DWORD size ) /* [in] Size of lpFileName in characters */ | |
081ee941 | 599 | { |
03e4ea17 | 600 | LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ); |
df93f2ee | 601 | DWORD len; |
dadf78ff | 602 | |
03e4ea17 | 603 | if (!filenameW) |
1cef2974 | 604 | { |
03e4ea17 AJ |
605 | SetLastError( ERROR_NOT_ENOUGH_MEMORY ); |
606 | return 0; | |
1cef2974 | 607 | } |
df93f2ee EP |
608 | if ((len = GetModuleFileNameW( hModule, filenameW, size ))) |
609 | { | |
610 | len = FILE_name_WtoA( filenameW, len, lpFileName, size ); | |
611 | if (len < size) lpFileName[len] = '\0'; | |
612 | } | |
03e4ea17 | 613 | HeapFree( GetProcessHeap(), 0, filenameW ); |
df93f2ee | 614 | return len; |
9a624916 VB |
615 | } |
616 | ||
b1bac320 | 617 | /*********************************************************************** |
dae8de69 | 618 | * GetModuleFileNameW (KERNEL32.@) |
4c1fa161 JG |
619 | * |
620 | * Unicode version of GetModuleFileNameA. | |
b1bac320 | 621 | */ |
83886f23 | 622 | DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size ) |
b1bac320 | 623 | { |
df93f2ee | 624 | ULONG magic, len = 0; |
fba7149a AJ |
625 | LDR_MODULE *pldr; |
626 | NTSTATUS nts; | |
627 | WIN16_SUBSYSTEM_TIB *win16_tib; | |
500a2f95 | 628 | |
fba7149a | 629 | if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name) |
84d1a8ff | 630 | { |
df93f2ee EP |
631 | len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR)); |
632 | memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) ); | |
633 | if (len < size) lpFileName[len] = '\0'; | |
fba7149a | 634 | goto done; |
84d1a8ff | 635 | } |
84d1a8ff | 636 | |
fba7149a | 637 | LdrLockLoaderLock( 0, NULL, &magic ); |
84d1a8ff | 638 | |
fba7149a AJ |
639 | if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress; |
640 | nts = LdrFindEntryForAddress( hModule, &pldr ); | |
df93f2ee EP |
641 | if (nts == STATUS_SUCCESS) |
642 | { | |
643 | len = min(size, pldr->FullDllName.Length / sizeof(WCHAR)); | |
644 | memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR)); | |
645 | if (len < size) lpFileName[len] = '\0'; | |
646 | } | |
fba7149a | 647 | else SetLastError( RtlNtStatusToDosError( nts ) ); |
84d1a8ff | 648 | |
fba7149a AJ |
649 | LdrUnlockLoaderLock( 0, magic ); |
650 | done: | |
df93f2ee EP |
651 | TRACE( "%s\n", debugstr_wn(lpFileName, len) ); |
652 | return len; | |
b1bac320 AJ |
653 | } |
654 | ||
4137aeca AJ |
655 | |
656 | /*********************************************************************** | |
657 | * get_dll_system_path | |
658 | */ | |
659 | static const WCHAR *get_dll_system_path(void) | |
660 | { | |
af16aac7 | 661 | static WCHAR *cached_path; |
4137aeca | 662 | |
af16aac7 | 663 | if (!cached_path) |
4137aeca | 664 | { |
af16aac7 | 665 | WCHAR *p, *path; |
4137aeca AJ |
666 | int len = 3; |
667 | ||
d4441f28 | 668 | len += 2 * GetSystemDirectoryW( NULL, 0 ); |
4137aeca | 669 | len += GetWindowsDirectoryW( NULL, 0 ); |
af16aac7 | 670 | p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ); |
4137aeca AJ |
671 | *p++ = '.'; |
672 | *p++ = ';'; | |
673 | GetSystemDirectoryW( p, path + len - p); | |
674 | p += strlenW(p); | |
d4441f28 AJ |
675 | /* if system directory ends in "32" add 16-bit version too */ |
676 | if (p[-2] == '3' && p[-1] == '2') | |
677 | { | |
678 | *p++ = ';'; | |
679 | GetSystemDirectoryW( p, path + len - p); | |
680 | p += strlenW(p) - 2; | |
681 | } | |
4137aeca AJ |
682 | *p++ = ';'; |
683 | GetWindowsDirectoryW( p, path + len - p); | |
af16aac7 | 684 | cached_path = path; |
4137aeca | 685 | } |
af16aac7 | 686 | return cached_path; |
4137aeca AJ |
687 | } |
688 | ||
27412837 RS |
689 | /****************************************************************** |
690 | * get_module_path_end | |
691 | * | |
692 | * Returns the end of the directory component of the module path. | |
693 | */ | |
694 | static inline const WCHAR *get_module_path_end(const WCHAR *module) | |
695 | { | |
696 | const WCHAR *p; | |
697 | const WCHAR *mod_end = module; | |
698 | if (!module) return mod_end; | |
699 | ||
700 | if ((p = strrchrW( mod_end, '\\' ))) mod_end = p; | |
701 | if ((p = strrchrW( mod_end, '/' ))) mod_end = p; | |
702 | if (mod_end == module + 2 && module[1] == ':') mod_end++; | |
703 | if (mod_end == module && module[0] && module[1] == ':') mod_end += 2; | |
704 | ||
705 | return mod_end; | |
706 | } | |
4137aeca AJ |
707 | |
708 | /****************************************************************** | |
af16aac7 | 709 | * MODULE_get_dll_load_path |
4137aeca AJ |
710 | * |
711 | * Compute the load path to use for a given dll. | |
712 | * Returned pointer must be freed by caller. | |
713 | */ | |
af16aac7 | 714 | WCHAR *MODULE_get_dll_load_path( LPCWSTR module ) |
4137aeca AJ |
715 | { |
716 | static const WCHAR pathW[] = {'P','A','T','H',0}; | |
717 | ||
718 | const WCHAR *system_path = get_dll_system_path(); | |
719 | const WCHAR *mod_end = NULL; | |
720 | UNICODE_STRING name, value; | |
721 | WCHAR *p, *ret; | |
722 | int len = 0, path_len = 0; | |
723 | ||
724 | /* adjust length for module name */ | |
725 | ||
726 | if (module) | |
27412837 RS |
727 | mod_end = get_module_path_end( module ); |
728 | /* if module is NULL or doesn't contain a path, fall back to directory | |
729 | * process was loaded from */ | |
730 | if (module == mod_end) | |
4137aeca | 731 | { |
27412837 RS |
732 | module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer; |
733 | mod_end = get_module_path_end( module ); | |
4137aeca | 734 | } |
27412837 RS |
735 | len += (mod_end - module) + 1; |
736 | ||
4137aeca AJ |
737 | len += strlenW( system_path ) + 2; |
738 | ||
739 | /* get the PATH variable */ | |
740 | ||
741 | RtlInitUnicodeString( &name, pathW ); | |
742 | value.Length = 0; | |
743 | value.MaximumLength = 0; | |
744 | value.Buffer = NULL; | |
745 | if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL) | |
746 | path_len = value.Length; | |
747 | ||
dab980e0 AJ |
748 | RtlEnterCriticalSection( &dlldir_section ); |
749 | if (dll_directory) len += strlenW(dll_directory) + 1; | |
750 | if ((p = ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) | |
4137aeca | 751 | { |
dab980e0 AJ |
752 | if (module) |
753 | { | |
754 | memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) ); | |
755 | p += (mod_end - module); | |
756 | *p++ = ';'; | |
757 | } | |
758 | if (dll_directory) | |
759 | { | |
760 | strcpyW( p, dll_directory ); | |
761 | p += strlenW(p); | |
762 | *p++ = ';'; | |
763 | } | |
4137aeca | 764 | } |
dab980e0 AJ |
765 | RtlLeaveCriticalSection( &dlldir_section ); |
766 | if (!ret) return NULL; | |
767 | ||
4137aeca AJ |
768 | strcpyW( p, system_path ); |
769 | p += strlenW(p); | |
770 | *p++ = ';'; | |
771 | value.Buffer = p; | |
772 | value.MaximumLength = path_len; | |
773 | ||
774 | while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL) | |
775 | { | |
776 | WCHAR *new_ptr; | |
777 | ||
778 | /* grow the buffer and retry */ | |
779 | path_len = value.Length; | |
780 | if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) ))) | |
781 | { | |
782 | HeapFree( GetProcessHeap(), 0, ret ); | |
783 | return NULL; | |
784 | } | |
785 | value.Buffer = new_ptr + (value.Buffer - ret); | |
786 | value.MaximumLength = path_len; | |
787 | ret = new_ptr; | |
788 | } | |
789 | value.Buffer[value.Length / sizeof(WCHAR)] = 0; | |
790 | return ret; | |
791 | } | |
792 | ||
793 | ||
32872b1a EP |
794 | /****************************************************************** |
795 | * load_library_as_datafile | |
65fc1c92 | 796 | */ |
803b5686 | 797 | static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod) |
65fc1c92 | 798 | { |
803b5686 AJ |
799 | static const WCHAR dotDLL[] = {'.','d','l','l',0}; |
800 | ||
801 | WCHAR filenameW[MAX_PATH]; | |
32872b1a EP |
802 | HANDLE hFile = INVALID_HANDLE_VALUE; |
803 | HANDLE mapping; | |
a33f318f | 804 | HMODULE module; |
65fc1c92 | 805 | |
803b5686 | 806 | *hmod = 0; |
65fc1c92 | 807 | |
650b2733 | 808 | if (SearchPathW( NULL, name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]), |
803b5686 | 809 | filenameW, NULL )) |
32872b1a | 810 | { |
803b5686 AJ |
811 | hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ, |
812 | NULL, OPEN_EXISTING, 0, 0 ); | |
32872b1a | 813 | } |
32872b1a | 814 | if (hFile == INVALID_HANDLE_VALUE) return FALSE; |
a33f318f AJ |
815 | |
816 | mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL ); | |
32872b1a | 817 | CloseHandle( hFile ); |
a33f318f AJ |
818 | if (!mapping) return FALSE; |
819 | ||
820 | module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 ); | |
821 | CloseHandle( mapping ); | |
822 | if (!module) return FALSE; | |
65fc1c92 | 823 | |
a33f318f AJ |
824 | /* make sure it's a valid PE file */ |
825 | if (!RtlImageNtHeader(module)) | |
826 | { | |
827 | UnmapViewOfFile( module ); | |
828 | return FALSE; | |
829 | } | |
830 | *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */ | |
831 | return TRUE; | |
65fc1c92 BM |
832 | } |
833 | ||
4137aeca AJ |
834 | |
835 | /****************************************************************** | |
836 | * load_library | |
837 | * | |
838 | * Helper for LoadLibraryExA/W. | |
839 | */ | |
840 | static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags ) | |
841 | { | |
842 | NTSTATUS nts; | |
843 | HMODULE hModule; | |
844 | WCHAR *load_path; | |
845 | ||
8530cb0a AJ |
846 | load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL ); |
847 | ||
4137aeca AJ |
848 | if (flags & LOAD_LIBRARY_AS_DATAFILE) |
849 | { | |
8530cb0a AJ |
850 | ULONG magic; |
851 | ||
852 | LdrLockLoaderLock( 0, NULL, &magic ); | |
853 | if (!(nts = LdrGetDllHandle( load_path, flags, libname, &hModule ))) | |
854 | { | |
855 | LdrAddRefDll( 0, hModule ); | |
856 | LdrUnlockLoaderLock( 0, magic ); | |
857 | goto done; | |
858 | } | |
859 | LdrUnlockLoaderLock( 0, magic ); | |
860 | ||
4137aeca AJ |
861 | /* The method in load_library_as_datafile allows searching for the |
862 | * 'native' libraries only | |
863 | */ | |
8530cb0a | 864 | if (load_library_as_datafile( libname->Buffer, &hModule )) goto done; |
4137aeca AJ |
865 | flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */ |
866 | /* Fallback to normal behaviour */ | |
867 | } | |
868 | ||
4137aeca | 869 | nts = LdrLoadDll( load_path, flags, libname, &hModule ); |
4137aeca AJ |
870 | if (nts != STATUS_SUCCESS) |
871 | { | |
872 | hModule = 0; | |
873 | SetLastError( RtlNtStatusToDosError( nts ) ); | |
874 | } | |
8530cb0a AJ |
875 | done: |
876 | HeapFree( GetProcessHeap(), 0, load_path ); | |
4137aeca AJ |
877 | return hModule; |
878 | } | |
879 | ||
880 | ||
32872b1a EP |
881 | /****************************************************************** |
882 | * LoadLibraryExA (KERNEL32.@) | |
c1d1cfe9 | 883 | * |
4c1fa161 JG |
884 | * Load a dll file into the process address space. |
885 | * | |
886 | * PARAMS | |
887 | * libname [I] Name of the file to load | |
888 | * hfile [I] Reserved, must be 0. | |
889 | * flags [I] Flags for loading the dll | |
890 | * | |
891 | * RETURNS | |
892 | * Success: A handle to the loaded dll. | |
893 | * Failure: A NULL handle. Use GetLastError() to determine the cause. | |
894 | * | |
895 | * NOTES | |
c1d1cfe9 BS |
896 | * The HFILE parameter is not used and marked reserved in the SDK. I can |
897 | * only guess that it should force a file to be mapped, but I rather | |
898 | * ignore the parameter because it would be extremely difficult to | |
01c8ec3a | 899 | * integrate this with different types of module representations. |
c1d1cfe9 | 900 | */ |
32872b1a | 901 | HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags) |
77b9918e | 902 | { |
0d33e5e3 | 903 | WCHAR *libnameW; |
c7c4246a | 904 | |
0d33e5e3 AJ |
905 | if (!(libnameW = FILE_name_AtoW( libname, FALSE ))) return 0; |
906 | return LoadLibraryExW( libnameW, hfile, flags ); | |
32872b1a | 907 | } |
1b34697a | 908 | |
32872b1a EP |
909 | /*********************************************************************** |
910 | * LoadLibraryExW (KERNEL32.@) | |
4c1fa161 JG |
911 | * |
912 | * Unicode version of LoadLibraryExA. | |
32872b1a EP |
913 | */ |
914 | HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags) | |
915 | { | |
916 | UNICODE_STRING wstr; | |
64896241 | 917 | HMODULE res; |
081ee941 | 918 | |
32872b1a EP |
919 | if (!libnameW) |
920 | { | |
921 | SetLastError(ERROR_INVALID_PARAMETER); | |
922 | return 0; | |
923 | } | |
32872b1a | 924 | RtlInitUnicodeString( &wstr, libnameW ); |
64896241 VM |
925 | if (wstr.Buffer[wstr.Length/sizeof(WCHAR) - 1] != ' ') |
926 | return load_library( &wstr, flags ); | |
927 | ||
928 | /* Library name has trailing spaces */ | |
929 | RtlCreateUnicodeString( &wstr, libnameW ); | |
930 | while (wstr.Length > sizeof(WCHAR) && | |
931 | wstr.Buffer[wstr.Length/sizeof(WCHAR) - 1] == ' ') | |
932 | { | |
933 | wstr.Length -= sizeof(WCHAR); | |
934 | } | |
935 | wstr.Buffer[wstr.Length/sizeof(WCHAR)] = '\0'; | |
936 | res = load_library( &wstr, flags ); | |
937 | RtlFreeUnicodeString( &wstr ); | |
938 | return res; | |
77b9918e AJ |
939 | } |
940 | ||
941 | /*********************************************************************** | |
3ca98239 | 942 | * LoadLibraryA (KERNEL32.@) |
4c1fa161 JG |
943 | * |
944 | * Load a dll file into the process address space. | |
945 | * | |
946 | * PARAMS | |
947 | * libname [I] Name of the file to load | |
948 | * | |
949 | * RETURNS | |
950 | * Success: A handle to the loaded dll. | |
951 | * Failure: A NULL handle. Use GetLastError() to determine the cause. | |
952 | * | |
953 | * NOTES | |
954 | * See LoadLibraryExA(). | |
77b9918e | 955 | */ |
32872b1a EP |
956 | HMODULE WINAPI LoadLibraryA(LPCSTR libname) |
957 | { | |
958 | return LoadLibraryExA(libname, 0, 0); | |
77b9918e AJ |
959 | } |
960 | ||
961 | /*********************************************************************** | |
3ca98239 | 962 | * LoadLibraryW (KERNEL32.@) |
4c1fa161 JG |
963 | * |
964 | * Unicode version of LoadLibraryA. | |
77b9918e | 965 | */ |
a3960292 | 966 | HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW) |
77b9918e | 967 | { |
32872b1a | 968 | return LoadLibraryExW(libnameW, 0, 0); |
84c70f55 AJ |
969 | } |
970 | ||
c1d1cfe9 | 971 | /*********************************************************************** |
044855c6 PS |
972 | * FreeLibrary (KERNEL32.@) |
973 | * FreeLibrary32 (KERNEL.486) | |
4c1fa161 JG |
974 | * |
975 | * Free a dll loaded into the process address space. | |
976 | * | |
977 | * PARAMS | |
978 | * hLibModule [I] Handle to the dll returned by LoadLibraryA(). | |
979 | * | |
980 | * RETURNS | |
981 | * Success: TRUE. The dll is removed if it is not still in use. | |
982 | * Failure: FALSE. Use GetLastError() to determine the cause. | |
c1d1cfe9 BS |
983 | */ |
984 | BOOL WINAPI FreeLibrary(HINSTANCE hLibModule) | |
985 | { | |
1efa50e4 EP |
986 | BOOL retv = FALSE; |
987 | NTSTATUS nts; | |
8ff37b8f | 988 | |
32872b1a EP |
989 | if (!hLibModule) |
990 | { | |
991 | SetLastError( ERROR_INVALID_HANDLE ); | |
992 | return FALSE; | |
993 | } | |
994 | ||
8ff37b8f AJ |
995 | if ((ULONG_PTR)hLibModule & 1) |
996 | { | |
997 | /* this is a LOAD_LIBRARY_AS_DATAFILE module */ | |
998 | char *ptr = (char *)hLibModule - 1; | |
999 | UnmapViewOfFile( ptr ); | |
1000 | return TRUE; | |
1001 | } | |
1b34697a | 1002 | |
1efa50e4 EP |
1003 | if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE; |
1004 | else SetLastError( RtlNtStatusToDosError( nts ) ); | |
1b34697a | 1005 | |
c1d1cfe9 BS |
1006 | return retv; |
1007 | } | |
1b34697a | 1008 | |
ca22b33d | 1009 | /*********************************************************************** |
dae8de69 | 1010 | * GetProcAddress (KERNEL32.@) |
4c1fa161 JG |
1011 | * |
1012 | * Find the address of an exported symbol in a loaded dll. | |
1013 | * | |
1014 | * PARAMS | |
1015 | * hModule [I] Handle to the dll returned by LoadLibraryA(). | |
1016 | * function [I] Name of the symbol, or an integer ordinal number < 16384 | |
1017 | * | |
1018 | * RETURNS | |
1019 | * Success: A pointer to the symbol in the process address space. | |
1020 | * Failure: NULL. Use GetLastError() to determine the cause. | |
ca22b33d | 1021 | */ |
a3960292 | 1022 | FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function ) |
ca22b33d | 1023 | { |
f3a73ef7 EP |
1024 | NTSTATUS nts; |
1025 | FARPROC fp; | |
1026 | ||
f315d8b0 AJ |
1027 | if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress; |
1028 | ||
f3a73ef7 EP |
1029 | if (HIWORD(function)) |
1030 | { | |
1031 | ANSI_STRING str; | |
1032 | ||
1033 | RtlInitAnsiString( &str, function ); | |
1034 | nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp ); | |
1035 | } | |
1036 | else | |
ba41fe20 | 1037 | nts = LdrGetProcedureAddress( hModule, NULL, LOWORD(function), (void**)&fp ); |
f3a73ef7 EP |
1038 | if (nts != STATUS_SUCCESS) |
1039 | { | |
1040 | SetLastError( RtlNtStatusToDosError( nts ) ); | |
1041 | fp = NULL; | |
1042 | } | |
1043 | return fp; | |
46ea8b3f AJ |
1044 | } |
1045 | ||
a3527cf3 | 1046 | /*********************************************************************** |
54fe8380 | 1047 | * GetProcAddress32 (KERNEL.453) |
4c1fa161 JG |
1048 | * |
1049 | * Find the address of an exported symbol in a loaded dll. | |
1050 | * | |
1051 | * PARAMS | |
1052 | * hModule [I] Handle to the dll returned by LoadLibraryA(). | |
1053 | * function [I] Name of the symbol, or an integer ordinal number < 16384 | |
1054 | * | |
1055 | * RETURNS | |
1056 | * Success: A pointer to the symbol in the process address space. | |
1057 | * Failure: NULL. Use GetLastError() to determine the cause. | |
a3527cf3 | 1058 | */ |
a3960292 | 1059 | FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function ) |
a3527cf3 | 1060 | { |
f3a73ef7 EP |
1061 | /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */ |
1062 | return GetProcAddress( hModule, function ); | |
a3527cf3 | 1063 | } |
646f17f2 AJ |
1064 | |
1065 | ||
1066 | /*********************************************************************** | |
1067 | * DelayLoadFailureHook (KERNEL32.@) | |
1068 | */ | |
1069 | FARPROC WINAPI DelayLoadFailureHook( LPCSTR name, LPCSTR function ) | |
1070 | { | |
1071 | ULONG_PTR args[2]; | |
1072 | ||
6501d7b1 RR |
1073 | if ((ULONG_PTR)function >> 16) |
1074 | ERR( "failed to delay load %s.%s\n", name, function ); | |
1075 | else | |
1076 | ERR( "failed to delay load %s.%u\n", name, LOWORD(function) ); | |
646f17f2 AJ |
1077 | args[0] = (ULONG_PTR)name; |
1078 | args[1] = (ULONG_PTR)function; | |
1079 | RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args ); | |
1080 | return NULL; | |
1081 | } |