4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
28 #include <sys/types.h>
32 #include "wine/winbase16.h"
40 #include "kernel_private.h"
42 #include "wine/exception.h"
43 #include "wine/debug.h"
44 #include "wine/unicode.h"
45 #include "wine/server.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(module);
48 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
51 static WCHAR *dll_directory; /* extra path for SetDllDirectoryW */
53 static CRITICAL_SECTION dlldir_section;
54 static CRITICAL_SECTION_DEBUG critsect_debug =
56 0, 0, &dlldir_section,
57 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
58 0, 0, { 0, (DWORD)(__FILE__ ": dlldir_section") }
60 static CRITICAL_SECTION dlldir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
63 /****************************************************************************
64 * GetDllDirectoryA (KERNEL32.@)
66 DWORD WINAPI GetDllDirectoryA( DWORD buf_len, LPSTR buffer )
70 RtlEnterCriticalSection( &dlldir_section );
71 len = dll_directory ? FILE_name_WtoA( dll_directory, strlenW(dll_directory), NULL, 0 ) : 0;
72 if (buffer && buf_len > len)
74 if (dll_directory) FILE_name_WtoA( dll_directory, -1, buffer, buf_len );
79 len++; /* for terminating null */
80 if (buffer) *buffer = 0;
82 RtlLeaveCriticalSection( &dlldir_section );
87 /****************************************************************************
88 * GetDllDirectoryW (KERNEL32.@)
90 DWORD WINAPI GetDllDirectoryW( DWORD buf_len, LPWSTR buffer )
94 RtlEnterCriticalSection( &dlldir_section );
95 len = dll_directory ? strlenW( dll_directory ) : 0;
96 if (buffer && buf_len > len)
98 if (dll_directory) memcpy( buffer, dll_directory, (len + 1) * sizeof(WCHAR) );
103 len++; /* for terminating null */
104 if (buffer) *buffer = 0;
106 RtlLeaveCriticalSection( &dlldir_section );
111 /****************************************************************************
112 * SetDllDirectoryA (KERNEL32.@)
114 BOOL WINAPI SetDllDirectoryA( LPCSTR dir )
119 if (!(dirW = FILE_name_AtoW( dir, TRUE ))) return FALSE;
120 ret = SetDllDirectoryW( dirW );
121 HeapFree( GetProcessHeap(), 0, dirW );
126 /****************************************************************************
127 * SetDllDirectoryW (KERNEL32.@)
129 BOOL WINAPI SetDllDirectoryW( LPCWSTR dir )
131 WCHAR *newdir = NULL;
135 DWORD len = (strlenW(dir) + 1) * sizeof(WCHAR);
136 if (!(newdir = HeapAlloc( GetProcessHeap(), 0, len )))
138 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
141 memcpy( newdir, dir, len );
144 RtlEnterCriticalSection( &dlldir_section );
145 HeapFree( GetProcessHeap(), 0, dll_directory );
146 dll_directory = newdir;
147 RtlLeaveCriticalSection( &dlldir_section );
152 /****************************************************************************
153 * DisableThreadLibraryCalls (KERNEL32.@)
155 * Inform the module loader that thread notifications are not required for a dll.
158 * hModule [I] Module handle to skip calls for
161 * Success: TRUE. Thread attach and detach notifications will not be sent
163 * Failure: FALSE. Use GetLastError() to determine the cause.
166 * This is typically called from the dll entry point of a dll during process
167 * attachment, for dlls that do not need to process thread notifications.
169 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
171 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
172 if (nts == STATUS_SUCCESS) return TRUE;
174 SetLastError( RtlNtStatusToDosError( nts ) );
179 /* Check whether a file is an OS/2 or a very old Windows executable
180 * by testing on import of KERNEL.
182 * FIXME: is reading the module imports the only way of discerning
183 * old Windows binaries from OS/2 ones ? At least it seems so...
185 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
186 const IMAGE_OS2_HEADER *ne)
188 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
189 enum binary_type ret = BINARY_OS216;
190 LPWORD modtab = NULL;
191 LPSTR nametab = NULL;
195 /* read modref table */
196 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
197 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
198 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
199 || (len != ne->ne_cmod*sizeof(WORD)) )
202 /* read imported names table */
203 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
204 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
205 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
206 || (len != ne->ne_enttab - ne->ne_imptab) )
209 for (i=0; i < ne->ne_cmod; i++)
211 LPSTR module = &nametab[modtab[i]];
212 TRACE("modref: %.*s\n", module[0], &module[1]);
213 if (!(strncmp(&module[1], "KERNEL", module[0])))
214 { /* very old Windows file */
215 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
222 ERR("Hmm, an error occurred. Is this binary file broken?\n");
225 HeapFree( GetProcessHeap(), 0, modtab);
226 HeapFree( GetProcessHeap(), 0, nametab);
227 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
231 /***********************************************************************
232 * MODULE_GetBinaryType
234 enum binary_type MODULE_GetBinaryType( HANDLE hfile, void **res_start, void **res_end )
240 unsigned char magic[4];
241 unsigned char ignored[12];
247 unsigned long cputype;
248 unsigned long cpusubtype;
249 unsigned long filetype;
256 /* Seek to the start of the file and read the header information. */
257 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
258 return BINARY_UNKNOWN;
259 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
260 return BINARY_UNKNOWN;
262 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
264 /* FIXME: we don't bother to check byte order, architecture, etc. */
265 switch(header.elf.type)
267 case 2: return BINARY_UNIX_EXE;
268 case 3: return BINARY_UNIX_LIB;
270 return BINARY_UNKNOWN;
273 /* Mach-o File with Endian set to Big Endian or Little Endian */
274 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
276 switch(header.macho.filetype)
278 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
280 return BINARY_UNKNOWN;
283 /* Not ELF, try DOS */
285 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
289 IMAGE_OS2_HEADER os2;
293 /* We do have a DOS image so we will now try to seek into
294 * the file by the amount indicated by the field
295 * "Offset to extended header" and read in the
296 * "magic" field information at that location.
297 * This will tell us if there is more header information
300 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
302 if (!ReadFile( hfile, &ext_header, sizeof(ext_header), &len, NULL ) || len < 4)
305 /* Reading the magic field succeeded so
306 * we will try to determine what type it is.
308 if (!memcmp( &ext_header.nt.Signature, "PE\0\0", 4 ))
310 if (len >= sizeof(ext_header.nt.FileHeader))
312 if (len < sizeof(ext_header.nt)) /* clear remaining part of header if missing */
313 memset( (char *)&ext_header.nt + len, 0, sizeof(ext_header.nt) - len );
314 if (res_start) *res_start = (void *)ext_header.nt.OptionalHeader.ImageBase;
315 if (res_end) *res_end = (void *)(ext_header.nt.OptionalHeader.ImageBase +
316 ext_header.nt.OptionalHeader.SizeOfImage);
317 if (ext_header.nt.FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
318 return BINARY_PE_EXE;
323 if (!memcmp( &ext_header.os2.ne_magic, "NE", 2 ))
325 /* This is a Windows executable (NE) header. This can
326 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
327 * DOS program (running under a DOS extender). To decide
328 * which, we'll have to read the NE header.
330 if (len >= sizeof(ext_header.os2))
332 switch ( ext_header.os2.ne_exetyp )
334 case 1: return BINARY_OS216; /* OS/2 */
335 case 2: return BINARY_WIN16; /* Windows */
336 case 3: return BINARY_DOS; /* European MS-DOS 4.x */
337 case 4: return BINARY_WIN16; /* Windows 386; FIXME: is this 32bit??? */
338 case 5: return BINARY_DOS; /* BOSS, Borland Operating System Services */
339 /* other types, e.g. 0 is: "unknown" */
340 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ext_header.os2);
343 /* Couldn't read header, so abort. */
347 /* Unknown extended header, but this file is nonetheless DOS-executable. */
351 return BINARY_UNKNOWN;
354 /***********************************************************************
355 * GetBinaryTypeW [KERNEL32.@]
357 * Determine whether a file is executable, and if so, what kind.
360 * lpApplicationName [I] Path of the file to check
361 * lpBinaryType [O] Destination for the binary type
364 * TRUE, if the file is an executable, in which case lpBinaryType is set.
365 * FALSE, if the file is not an executable or if the function fails.
368 * The type of executable is a property that determines which subsytem an
369 * executable file runs under. lpBinaryType can be set to one of the following
371 * SCS_32BIT_BINARY: A Win32 based application
372 * SCS_DOS_BINARY: An MS-Dos based application
373 * SCS_WOW_BINARY: A Win16 based application
374 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
375 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
376 * SCS_OS216_BINARY: A 16bit OS/2 based application
378 * To find the binary type, this function reads in the files header information.
379 * If extended header information is not present it will assume that the file
380 * is a DOS executable. If extended header information is present it will
381 * determine if the file is a 16 or 32 bit Windows executable by checking the
382 * flags in the header.
384 * ".com" and ".pif" files are only recognized by their file name extension,
385 * as per native Windows.
387 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
392 TRACE("%s\n", debugstr_w(lpApplicationName) );
396 if ( lpApplicationName == NULL || lpBinaryType == NULL )
399 /* Open the file indicated by lpApplicationName for reading.
401 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
402 NULL, OPEN_EXISTING, 0, 0 );
403 if ( hfile == INVALID_HANDLE_VALUE )
408 switch(MODULE_GetBinaryType( hfile, NULL, NULL ))
412 static const WCHAR comW[] = { '.','C','O','M',0 };
413 static const WCHAR pifW[] = { '.','P','I','F',0 };
416 /* try to determine from file name */
417 ptr = strrchrW( lpApplicationName, '.' );
419 if (!strcmpiW( ptr, comW ))
421 *lpBinaryType = SCS_DOS_BINARY;
424 else if (!strcmpiW( ptr, pifW ))
426 *lpBinaryType = SCS_PIF_BINARY;
433 *lpBinaryType = SCS_32BIT_BINARY;
437 *lpBinaryType = SCS_WOW_BINARY;
441 *lpBinaryType = SCS_OS216_BINARY;
445 *lpBinaryType = SCS_DOS_BINARY;
448 case BINARY_UNIX_EXE:
449 case BINARY_UNIX_LIB:
454 CloseHandle( hfile );
458 /***********************************************************************
459 * GetBinaryTypeA [KERNEL32.@]
460 * GetBinaryType [KERNEL32.@]
462 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
464 ANSI_STRING app_nameA;
467 TRACE("%s\n", debugstr_a(lpApplicationName));
471 if ( lpApplicationName == NULL || lpBinaryType == NULL )
474 RtlInitAnsiString(&app_nameA, lpApplicationName);
475 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
478 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
480 SetLastError(RtlNtStatusToDosError(status));
485 /***********************************************************************
486 * GetModuleHandleA (KERNEL32.@)
487 * GetModuleHandle32 (KERNEL.488)
489 * Get the handle of a dll loaded into the process address space.
492 * module [I] Name of the dll
495 * Success: A handle to the loaded dll.
496 * Failure: A NULL handle. Use GetLastError() to determine the cause.
498 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
502 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
503 if (!(moduleW = FILE_name_AtoW( module, FALSE ))) return 0;
504 return GetModuleHandleW( moduleW );
507 /***********************************************************************
508 * GetModuleHandleW (KERNEL32.@)
510 * Unicode version of GetModuleHandleA.
512 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
518 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
520 RtlInitUnicodeString( &wstr, module );
521 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
522 if (nts != STATUS_SUCCESS)
524 SetLastError( RtlNtStatusToDosError( nts ) );
531 /***********************************************************************
532 * GetModuleFileNameA (KERNEL32.@)
533 * GetModuleFileName32 (KERNEL.487)
535 * Get the file name of a loaded module from its handle.
538 * Success: The length of the file name, excluding the terminating NUL.
539 * Failure: 0. Use GetLastError() to determine the cause.
542 * This function always returns the long path of hModule (as opposed to
543 * GetModuleFileName16() which returns short paths when the modules version
545 * The function doesn't write a terminating '\0' if the buffer is too
548 DWORD WINAPI GetModuleFileNameA(
549 HMODULE hModule, /* [in] Module handle (32 bit) */
550 LPSTR lpFileName, /* [out] Destination for file name */
551 DWORD size ) /* [in] Size of lpFileName in characters */
553 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
558 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
561 if ((len = GetModuleFileNameW( hModule, filenameW, size )))
563 len = FILE_name_WtoA( filenameW, len, lpFileName, size );
564 if (len < size) lpFileName[len] = '\0';
566 HeapFree( GetProcessHeap(), 0, filenameW );
570 /***********************************************************************
571 * GetModuleFileNameW (KERNEL32.@)
573 * Unicode version of GetModuleFileNameA.
575 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
577 ULONG magic, len = 0;
580 WIN16_SUBSYSTEM_TIB *win16_tib;
582 if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
584 len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR));
585 memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
586 if (len < size) lpFileName[len] = '\0';
590 LdrLockLoaderLock( 0, NULL, &magic );
592 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
593 nts = LdrFindEntryForAddress( hModule, &pldr );
594 if (nts == STATUS_SUCCESS)
596 len = min(size, pldr->FullDllName.Length / sizeof(WCHAR));
597 memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR));
598 if (len < size) lpFileName[len] = '\0';
600 else SetLastError( RtlNtStatusToDosError( nts ) );
602 LdrUnlockLoaderLock( 0, magic );
604 TRACE( "%s\n", debugstr_wn(lpFileName, len) );
609 /***********************************************************************
610 * get_dll_system_path
612 static const WCHAR *get_dll_system_path(void)
614 static WCHAR *cached_path;
621 len += 2 * GetSystemDirectoryW( NULL, 0 );
622 len += GetWindowsDirectoryW( NULL, 0 );
623 p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
626 GetSystemDirectoryW( p, path + len - p);
628 /* if system directory ends in "32" add 16-bit version too */
629 if (p[-2] == '3' && p[-1] == '2')
632 GetSystemDirectoryW( p, path + len - p);
636 GetWindowsDirectoryW( p, path + len - p);
643 /******************************************************************
644 * MODULE_get_dll_load_path
646 * Compute the load path to use for a given dll.
647 * Returned pointer must be freed by caller.
649 WCHAR *MODULE_get_dll_load_path( LPCWSTR module )
651 static const WCHAR pathW[] = {'P','A','T','H',0};
653 const WCHAR *system_path = get_dll_system_path();
654 const WCHAR *mod_end = NULL;
655 UNICODE_STRING name, value;
657 int len = 0, path_len = 0;
659 /* adjust length for module name */
661 if (!module) module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
665 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
666 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
667 if (mod_end == module + 2 && module[1] == ':') mod_end++;
668 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
669 len += (mod_end - module) + 1;
671 len += strlenW( system_path ) + 2;
673 /* get the PATH variable */
675 RtlInitUnicodeString( &name, pathW );
677 value.MaximumLength = 0;
679 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
680 path_len = value.Length;
682 RtlEnterCriticalSection( &dlldir_section );
683 if (dll_directory) len += strlenW(dll_directory) + 1;
684 if ((p = ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) )))
688 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
689 p += (mod_end - module);
694 strcpyW( p, dll_directory );
699 RtlLeaveCriticalSection( &dlldir_section );
700 if (!ret) return NULL;
702 strcpyW( p, system_path );
706 value.MaximumLength = path_len;
708 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
712 /* grow the buffer and retry */
713 path_len = value.Length;
714 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
716 HeapFree( GetProcessHeap(), 0, ret );
719 value.Buffer = new_ptr + (value.Buffer - ret);
720 value.MaximumLength = path_len;
723 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
728 /******************************************************************
729 * load_library_as_datafile
731 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
733 static const WCHAR dotDLL[] = {'.','d','l','l',0};
735 WCHAR filenameW[MAX_PATH];
736 HANDLE hFile = INVALID_HANDLE_VALUE;
742 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
745 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
746 NULL, OPEN_EXISTING, 0, 0 );
748 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
750 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
751 CloseHandle( hFile );
752 if (!mapping) return FALSE;
754 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
755 CloseHandle( mapping );
756 if (!module) return FALSE;
758 /* make sure it's a valid PE file */
759 if (!RtlImageNtHeader(module))
761 UnmapViewOfFile( module );
764 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
769 /******************************************************************
772 * Helper for LoadLibraryExA/W.
774 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
780 if (flags & LOAD_LIBRARY_AS_DATAFILE)
782 /* The method in load_library_as_datafile allows searching for the
783 * 'native' libraries only
785 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
786 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
787 /* Fallback to normal behaviour */
790 load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
791 nts = LdrLoadDll( load_path, flags, libname, &hModule );
792 HeapFree( GetProcessHeap(), 0, load_path );
793 if (nts != STATUS_SUCCESS)
796 SetLastError( RtlNtStatusToDosError( nts ) );
802 /******************************************************************
803 * LoadLibraryExA (KERNEL32.@)
805 * Load a dll file into the process address space.
808 * libname [I] Name of the file to load
809 * hfile [I] Reserved, must be 0.
810 * flags [I] Flags for loading the dll
813 * Success: A handle to the loaded dll.
814 * Failure: A NULL handle. Use GetLastError() to determine the cause.
817 * The HFILE parameter is not used and marked reserved in the SDK. I can
818 * only guess that it should force a file to be mapped, but I rather
819 * ignore the parameter because it would be extremely difficult to
820 * integrate this with different types of module representations.
822 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
826 if (!(libnameW = FILE_name_AtoW( libname, FALSE ))) return 0;
827 return LoadLibraryExW( libnameW, hfile, flags );
830 /***********************************************************************
831 * LoadLibraryExW (KERNEL32.@)
833 * Unicode version of LoadLibraryExA.
835 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
841 SetLastError(ERROR_INVALID_PARAMETER);
844 RtlInitUnicodeString( &wstr, libnameW );
845 return load_library( &wstr, flags );
848 /***********************************************************************
849 * LoadLibraryA (KERNEL32.@)
851 * Load a dll file into the process address space.
854 * libname [I] Name of the file to load
857 * Success: A handle to the loaded dll.
858 * Failure: A NULL handle. Use GetLastError() to determine the cause.
861 * See LoadLibraryExA().
863 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
865 return LoadLibraryExA(libname, 0, 0);
868 /***********************************************************************
869 * LoadLibraryW (KERNEL32.@)
871 * Unicode version of LoadLibraryA.
873 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
875 return LoadLibraryExW(libnameW, 0, 0);
878 /***********************************************************************
879 * FreeLibrary (KERNEL32.@)
880 * FreeLibrary32 (KERNEL.486)
882 * Free a dll loaded into the process address space.
885 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
888 * Success: TRUE. The dll is removed if it is not still in use.
889 * Failure: FALSE. Use GetLastError() to determine the cause.
891 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
898 SetLastError( ERROR_INVALID_HANDLE );
902 if ((ULONG_PTR)hLibModule & 1)
904 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
905 char *ptr = (char *)hLibModule - 1;
906 UnmapViewOfFile( ptr );
910 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
911 else SetLastError( RtlNtStatusToDosError( nts ) );
916 /***********************************************************************
917 * GetProcAddress (KERNEL32.@)
919 * Find the address of an exported symbol in a loaded dll.
922 * hModule [I] Handle to the dll returned by LoadLibraryA().
923 * function [I] Name of the symbol, or an integer ordinal number < 16384
926 * Success: A pointer to the symbol in the process address space.
927 * Failure: NULL. Use GetLastError() to determine the cause.
929 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
934 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
936 if (HIWORD(function))
940 RtlInitAnsiString( &str, function );
941 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
944 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
945 if (nts != STATUS_SUCCESS)
947 SetLastError( RtlNtStatusToDosError( nts ) );
953 /***********************************************************************
954 * GetProcAddress32 (KERNEL.453)
956 * Find the address of an exported symbol in a loaded dll.
959 * hModule [I] Handle to the dll returned by LoadLibraryA().
960 * function [I] Name of the symbol, or an integer ordinal number < 16384
963 * Success: A pointer to the symbol in the process address space.
964 * Failure: NULL. Use GetLastError() to determine the cause.
966 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
968 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
969 return GetProcAddress( hModule, function );
973 /***********************************************************************
974 * DelayLoadFailureHook (KERNEL32.@)
976 FARPROC WINAPI DelayLoadFailureHook( LPCSTR name, LPCSTR function )
980 ERR( "failed to delay load %s.%s\n", name, function );
981 args[0] = (ULONG_PTR)name;
982 args[1] = (ULONG_PTR)function;
983 RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );