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);
49 static WCHAR *dll_directory; /* extra path for SetDllDirectoryW */
51 static CRITICAL_SECTION dlldir_section;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
54 0, 0, &dlldir_section,
55 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56 0, 0, { (DWORD_PTR)(__FILE__ ": dlldir_section") }
58 static CRITICAL_SECTION dlldir_section = { &critsect_debug, -1, 0, 0, 0, 0 };
61 /****************************************************************************
62 * GetDllDirectoryA (KERNEL32.@)
64 DWORD WINAPI GetDllDirectoryA( DWORD buf_len, LPSTR buffer )
68 RtlEnterCriticalSection( &dlldir_section );
69 len = dll_directory ? FILE_name_WtoA( dll_directory, strlenW(dll_directory), NULL, 0 ) : 0;
70 if (buffer && buf_len > len)
72 if (dll_directory) FILE_name_WtoA( dll_directory, -1, buffer, buf_len );
77 len++; /* for terminating null */
78 if (buffer) *buffer = 0;
80 RtlLeaveCriticalSection( &dlldir_section );
85 /****************************************************************************
86 * GetDllDirectoryW (KERNEL32.@)
88 DWORD WINAPI GetDllDirectoryW( DWORD buf_len, LPWSTR buffer )
92 RtlEnterCriticalSection( &dlldir_section );
93 len = dll_directory ? strlenW( dll_directory ) : 0;
94 if (buffer && buf_len > len)
96 if (dll_directory) memcpy( buffer, dll_directory, (len + 1) * sizeof(WCHAR) );
101 len++; /* for terminating null */
102 if (buffer) *buffer = 0;
104 RtlLeaveCriticalSection( &dlldir_section );
109 /****************************************************************************
110 * SetDllDirectoryA (KERNEL32.@)
112 BOOL WINAPI SetDllDirectoryA( LPCSTR dir )
117 if (!(dirW = FILE_name_AtoW( dir, TRUE ))) return FALSE;
118 ret = SetDllDirectoryW( dirW );
119 HeapFree( GetProcessHeap(), 0, dirW );
124 /****************************************************************************
125 * SetDllDirectoryW (KERNEL32.@)
127 BOOL WINAPI SetDllDirectoryW( LPCWSTR dir )
129 WCHAR *newdir = NULL;
133 DWORD len = (strlenW(dir) + 1) * sizeof(WCHAR);
134 if (!(newdir = HeapAlloc( GetProcessHeap(), 0, len )))
136 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
139 memcpy( newdir, dir, len );
142 RtlEnterCriticalSection( &dlldir_section );
143 HeapFree( GetProcessHeap(), 0, dll_directory );
144 dll_directory = newdir;
145 RtlLeaveCriticalSection( &dlldir_section );
150 /****************************************************************************
151 * DisableThreadLibraryCalls (KERNEL32.@)
153 * Inform the module loader that thread notifications are not required for a dll.
156 * hModule [I] Module handle to skip calls for
159 * Success: TRUE. Thread attach and detach notifications will not be sent
161 * Failure: FALSE. Use GetLastError() to determine the cause.
164 * This is typically called from the dll entry point of a dll during process
165 * attachment, for dlls that do not need to process thread notifications.
167 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
169 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
170 if (nts == STATUS_SUCCESS) return TRUE;
172 SetLastError( RtlNtStatusToDosError( nts ) );
177 /* Check whether a file is an OS/2 or a very old Windows executable
178 * by testing on import of KERNEL.
180 * FIXME: is reading the module imports the only way of discerning
181 * old Windows binaries from OS/2 ones ? At least it seems so...
183 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
184 const IMAGE_OS2_HEADER *ne)
186 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
187 enum binary_type ret = BINARY_OS216;
188 LPWORD modtab = NULL;
189 LPSTR nametab = NULL;
193 /* read modref table */
194 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
195 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
196 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
197 || (len != ne->ne_cmod*sizeof(WORD)) )
200 /* read imported names table */
201 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
202 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
203 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
204 || (len != ne->ne_enttab - ne->ne_imptab) )
207 for (i=0; i < ne->ne_cmod; i++)
209 LPSTR module = &nametab[modtab[i]];
210 TRACE("modref: %.*s\n", module[0], &module[1]);
211 if (!(strncmp(&module[1], "KERNEL", module[0])))
212 { /* very old Windows file */
213 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
220 ERR("Hmm, an error occurred. Is this binary file broken?\n");
223 HeapFree( GetProcessHeap(), 0, modtab);
224 HeapFree( GetProcessHeap(), 0, nametab);
225 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
229 /***********************************************************************
230 * MODULE_GetBinaryType
232 enum binary_type MODULE_GetBinaryType( HANDLE hfile, void **res_start, void **res_end )
238 unsigned char magic[4];
239 unsigned char ignored[12];
245 unsigned long cputype;
246 unsigned long cpusubtype;
247 unsigned long filetype;
254 /* Seek to the start of the file and read the header information. */
255 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
256 return BINARY_UNKNOWN;
257 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
258 return BINARY_UNKNOWN;
260 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
262 /* FIXME: we don't bother to check byte order, architecture, etc. */
263 switch(header.elf.type)
265 case 2: return BINARY_UNIX_EXE;
266 case 3: return BINARY_UNIX_LIB;
268 return BINARY_UNKNOWN;
271 /* Mach-o File with Endian set to Big Endian or Little Endian */
272 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
274 switch(header.macho.filetype)
276 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
278 return BINARY_UNKNOWN;
281 /* Not ELF, try DOS */
283 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
287 IMAGE_OS2_HEADER os2;
291 /* We do have a DOS image so we will now try to seek into
292 * the file by the amount indicated by the field
293 * "Offset to extended header" and read in the
294 * "magic" field information at that location.
295 * This will tell us if there is more header information
298 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
300 if (!ReadFile( hfile, &ext_header, sizeof(ext_header), &len, NULL ) || len < 4)
303 /* Reading the magic field succeeded so
304 * we will try to determine what type it is.
306 if (!memcmp( &ext_header.nt.Signature, "PE\0\0", 4 ))
308 if (len >= sizeof(ext_header.nt.FileHeader))
310 if (len < sizeof(ext_header.nt)) /* clear remaining part of header if missing */
311 memset( (char *)&ext_header.nt + len, 0, sizeof(ext_header.nt) - len );
312 if (res_start) *res_start = (void *)ext_header.nt.OptionalHeader.ImageBase;
313 if (res_end) *res_end = (void *)(ext_header.nt.OptionalHeader.ImageBase +
314 ext_header.nt.OptionalHeader.SizeOfImage);
315 if (ext_header.nt.FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
316 return BINARY_PE_EXE;
321 if (!memcmp( &ext_header.os2.ne_magic, "NE", 2 ))
323 /* This is a Windows executable (NE) header. This can
324 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
325 * DOS program (running under a DOS extender). To decide
326 * which, we'll have to read the NE header.
328 if (len >= sizeof(ext_header.os2))
330 switch ( ext_header.os2.ne_exetyp )
332 case 1: return BINARY_OS216; /* OS/2 */
333 case 2: return BINARY_WIN16; /* Windows */
334 case 3: return BINARY_DOS; /* European MS-DOS 4.x */
335 case 4: return BINARY_WIN16; /* Windows 386; FIXME: is this 32bit??? */
336 case 5: return BINARY_DOS; /* BOSS, Borland Operating System Services */
337 /* other types, e.g. 0 is: "unknown" */
338 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ext_header.os2);
341 /* Couldn't read header, so abort. */
345 /* Unknown extended header, but this file is nonetheless DOS-executable. */
349 return BINARY_UNKNOWN;
352 /***********************************************************************
353 * GetBinaryTypeW [KERNEL32.@]
355 * Determine whether a file is executable, and if so, what kind.
358 * lpApplicationName [I] Path of the file to check
359 * lpBinaryType [O] Destination for the binary type
362 * TRUE, if the file is an executable, in which case lpBinaryType is set.
363 * FALSE, if the file is not an executable or if the function fails.
366 * The type of executable is a property that determines which subsytem an
367 * executable file runs under. lpBinaryType can be set to one of the following
369 * SCS_32BIT_BINARY: A Win32 based application
370 * SCS_DOS_BINARY: An MS-Dos based application
371 * SCS_WOW_BINARY: A Win16 based application
372 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
373 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
374 * SCS_OS216_BINARY: A 16bit OS/2 based application
376 * To find the binary type, this function reads in the files header information.
377 * If extended header information is not present it will assume that the file
378 * is a DOS executable. If extended header information is present it will
379 * determine if the file is a 16 or 32 bit Windows executable by checking the
380 * flags in the header.
382 * ".com" and ".pif" files are only recognized by their file name extension,
383 * as per native Windows.
385 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
390 TRACE("%s\n", debugstr_w(lpApplicationName) );
394 if ( lpApplicationName == NULL || lpBinaryType == NULL )
397 /* Open the file indicated by lpApplicationName for reading.
399 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
400 NULL, OPEN_EXISTING, 0, 0 );
401 if ( hfile == INVALID_HANDLE_VALUE )
406 switch(MODULE_GetBinaryType( hfile, NULL, NULL ))
410 static const WCHAR comW[] = { '.','C','O','M',0 };
411 static const WCHAR pifW[] = { '.','P','I','F',0 };
414 /* try to determine from file name */
415 ptr = strrchrW( lpApplicationName, '.' );
417 if (!strcmpiW( ptr, comW ))
419 *lpBinaryType = SCS_DOS_BINARY;
422 else if (!strcmpiW( ptr, pifW ))
424 *lpBinaryType = SCS_PIF_BINARY;
431 *lpBinaryType = SCS_32BIT_BINARY;
435 *lpBinaryType = SCS_WOW_BINARY;
439 *lpBinaryType = SCS_OS216_BINARY;
443 *lpBinaryType = SCS_DOS_BINARY;
446 case BINARY_UNIX_EXE:
447 case BINARY_UNIX_LIB:
452 CloseHandle( hfile );
456 /***********************************************************************
457 * GetBinaryTypeA [KERNEL32.@]
458 * GetBinaryType [KERNEL32.@]
460 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
462 ANSI_STRING app_nameA;
465 TRACE("%s\n", debugstr_a(lpApplicationName));
469 if ( lpApplicationName == NULL || lpBinaryType == NULL )
472 RtlInitAnsiString(&app_nameA, lpApplicationName);
473 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
476 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
478 SetLastError(RtlNtStatusToDosError(status));
483 /***********************************************************************
484 * GetModuleHandleA (KERNEL32.@)
485 * GetModuleHandle32 (KERNEL.488)
487 * Get the handle of a dll loaded into the process address space.
490 * module [I] Name of the dll
493 * Success: A handle to the loaded dll.
494 * Failure: A NULL handle. Use GetLastError() to determine the cause.
496 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
500 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
501 if (!(moduleW = FILE_name_AtoW( module, FALSE ))) return 0;
502 return GetModuleHandleW( moduleW );
505 /***********************************************************************
506 * GetModuleHandleW (KERNEL32.@)
508 * Unicode version of GetModuleHandleA.
510 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
516 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
518 RtlInitUnicodeString( &wstr, module );
519 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
520 if (nts != STATUS_SUCCESS)
522 SetLastError( RtlNtStatusToDosError( nts ) );
529 /***********************************************************************
530 * GetModuleFileNameA (KERNEL32.@)
531 * GetModuleFileName32 (KERNEL.487)
533 * Get the file name of a loaded module from its handle.
536 * Success: The length of the file name, excluding the terminating NUL.
537 * Failure: 0. Use GetLastError() to determine the cause.
540 * This function always returns the long path of hModule (as opposed to
541 * GetModuleFileName16() which returns short paths when the modules version
543 * The function doesn't write a terminating '\0' if the buffer is too
546 DWORD WINAPI GetModuleFileNameA(
547 HMODULE hModule, /* [in] Module handle (32 bit) */
548 LPSTR lpFileName, /* [out] Destination for file name */
549 DWORD size ) /* [in] Size of lpFileName in characters */
551 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
556 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
559 if ((len = GetModuleFileNameW( hModule, filenameW, size )))
561 len = FILE_name_WtoA( filenameW, len, lpFileName, size );
562 if (len < size) lpFileName[len] = '\0';
564 HeapFree( GetProcessHeap(), 0, filenameW );
568 /***********************************************************************
569 * GetModuleFileNameW (KERNEL32.@)
571 * Unicode version of GetModuleFileNameA.
573 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
575 ULONG magic, len = 0;
578 WIN16_SUBSYSTEM_TIB *win16_tib;
580 if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
582 len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR));
583 memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
584 if (len < size) lpFileName[len] = '\0';
588 LdrLockLoaderLock( 0, NULL, &magic );
590 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
591 nts = LdrFindEntryForAddress( hModule, &pldr );
592 if (nts == STATUS_SUCCESS)
594 len = min(size, pldr->FullDllName.Length / sizeof(WCHAR));
595 memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR));
596 if (len < size) lpFileName[len] = '\0';
598 else SetLastError( RtlNtStatusToDosError( nts ) );
600 LdrUnlockLoaderLock( 0, magic );
602 TRACE( "%s\n", debugstr_wn(lpFileName, len) );
607 /***********************************************************************
608 * get_dll_system_path
610 static const WCHAR *get_dll_system_path(void)
612 static WCHAR *cached_path;
619 len += 2 * GetSystemDirectoryW( NULL, 0 );
620 len += GetWindowsDirectoryW( NULL, 0 );
621 p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
624 GetSystemDirectoryW( p, path + len - p);
626 /* if system directory ends in "32" add 16-bit version too */
627 if (p[-2] == '3' && p[-1] == '2')
630 GetSystemDirectoryW( p, path + len - p);
634 GetWindowsDirectoryW( p, path + len - p);
641 /******************************************************************
642 * MODULE_get_dll_load_path
644 * Compute the load path to use for a given dll.
645 * Returned pointer must be freed by caller.
647 WCHAR *MODULE_get_dll_load_path( LPCWSTR module )
649 static const WCHAR pathW[] = {'P','A','T','H',0};
651 const WCHAR *system_path = get_dll_system_path();
652 const WCHAR *mod_end = NULL;
653 UNICODE_STRING name, value;
655 int len = 0, path_len = 0;
657 /* adjust length for module name */
659 if (!module) module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
663 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
664 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
665 if (mod_end == module + 2 && module[1] == ':') mod_end++;
666 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
667 len += (mod_end - module) + 1;
669 len += strlenW( system_path ) + 2;
671 /* get the PATH variable */
673 RtlInitUnicodeString( &name, pathW );
675 value.MaximumLength = 0;
677 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
678 path_len = value.Length;
680 RtlEnterCriticalSection( &dlldir_section );
681 if (dll_directory) len += strlenW(dll_directory) + 1;
682 if ((p = ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) )))
686 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
687 p += (mod_end - module);
692 strcpyW( p, dll_directory );
697 RtlLeaveCriticalSection( &dlldir_section );
698 if (!ret) return NULL;
700 strcpyW( p, system_path );
704 value.MaximumLength = path_len;
706 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
710 /* grow the buffer and retry */
711 path_len = value.Length;
712 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
714 HeapFree( GetProcessHeap(), 0, ret );
717 value.Buffer = new_ptr + (value.Buffer - ret);
718 value.MaximumLength = path_len;
721 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
726 /******************************************************************
727 * load_library_as_datafile
729 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
731 static const WCHAR dotDLL[] = {'.','d','l','l',0};
733 WCHAR filenameW[MAX_PATH];
734 HANDLE hFile = INVALID_HANDLE_VALUE;
740 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
743 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
744 NULL, OPEN_EXISTING, 0, 0 );
746 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
748 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
749 CloseHandle( hFile );
750 if (!mapping) return FALSE;
752 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
753 CloseHandle( mapping );
754 if (!module) return FALSE;
756 /* make sure it's a valid PE file */
757 if (!RtlImageNtHeader(module))
759 UnmapViewOfFile( module );
762 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
767 /******************************************************************
770 * Helper for LoadLibraryExA/W.
772 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
778 if (flags & LOAD_LIBRARY_AS_DATAFILE)
780 /* The method in load_library_as_datafile allows searching for the
781 * 'native' libraries only
783 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
784 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
785 /* Fallback to normal behaviour */
788 load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
789 nts = LdrLoadDll( load_path, flags, libname, &hModule );
790 HeapFree( GetProcessHeap(), 0, load_path );
791 if (nts != STATUS_SUCCESS)
794 SetLastError( RtlNtStatusToDosError( nts ) );
800 /******************************************************************
801 * LoadLibraryExA (KERNEL32.@)
803 * Load a dll file into the process address space.
806 * libname [I] Name of the file to load
807 * hfile [I] Reserved, must be 0.
808 * flags [I] Flags for loading the dll
811 * Success: A handle to the loaded dll.
812 * Failure: A NULL handle. Use GetLastError() to determine the cause.
815 * The HFILE parameter is not used and marked reserved in the SDK. I can
816 * only guess that it should force a file to be mapped, but I rather
817 * ignore the parameter because it would be extremely difficult to
818 * integrate this with different types of module representations.
820 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
824 if (!(libnameW = FILE_name_AtoW( libname, FALSE ))) return 0;
825 return LoadLibraryExW( libnameW, hfile, flags );
828 /***********************************************************************
829 * LoadLibraryExW (KERNEL32.@)
831 * Unicode version of LoadLibraryExA.
833 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
840 SetLastError(ERROR_INVALID_PARAMETER);
843 RtlInitUnicodeString( &wstr, libnameW );
844 if (wstr.Buffer[wstr.Length/sizeof(WCHAR) - 1] != ' ')
845 return load_library( &wstr, flags );
847 /* Library name has trailing spaces */
848 RtlCreateUnicodeString( &wstr, libnameW );
849 while (wstr.Length > sizeof(WCHAR) &&
850 wstr.Buffer[wstr.Length/sizeof(WCHAR) - 1] == ' ')
852 wstr.Length -= sizeof(WCHAR);
854 wstr.Buffer[wstr.Length/sizeof(WCHAR)] = '\0';
855 res = load_library( &wstr, flags );
856 RtlFreeUnicodeString( &wstr );
860 /***********************************************************************
861 * LoadLibraryA (KERNEL32.@)
863 * Load a dll file into the process address space.
866 * libname [I] Name of the file to load
869 * Success: A handle to the loaded dll.
870 * Failure: A NULL handle. Use GetLastError() to determine the cause.
873 * See LoadLibraryExA().
875 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
877 return LoadLibraryExA(libname, 0, 0);
880 /***********************************************************************
881 * LoadLibraryW (KERNEL32.@)
883 * Unicode version of LoadLibraryA.
885 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
887 return LoadLibraryExW(libnameW, 0, 0);
890 /***********************************************************************
891 * FreeLibrary (KERNEL32.@)
892 * FreeLibrary32 (KERNEL.486)
894 * Free a dll loaded into the process address space.
897 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
900 * Success: TRUE. The dll is removed if it is not still in use.
901 * Failure: FALSE. Use GetLastError() to determine the cause.
903 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
910 SetLastError( ERROR_INVALID_HANDLE );
914 if ((ULONG_PTR)hLibModule & 1)
916 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
917 char *ptr = (char *)hLibModule - 1;
918 UnmapViewOfFile( ptr );
922 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
923 else SetLastError( RtlNtStatusToDosError( nts ) );
928 /***********************************************************************
929 * GetProcAddress (KERNEL32.@)
931 * Find the address of an exported symbol in a loaded dll.
934 * hModule [I] Handle to the dll returned by LoadLibraryA().
935 * function [I] Name of the symbol, or an integer ordinal number < 16384
938 * Success: A pointer to the symbol in the process address space.
939 * Failure: NULL. Use GetLastError() to determine the cause.
941 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
946 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
948 if (HIWORD(function))
952 RtlInitAnsiString( &str, function );
953 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
956 nts = LdrGetProcedureAddress( hModule, NULL, LOWORD(function), (void**)&fp );
957 if (nts != STATUS_SUCCESS)
959 SetLastError( RtlNtStatusToDosError( nts ) );
965 /***********************************************************************
966 * GetProcAddress32 (KERNEL.453)
968 * Find the address of an exported symbol in a loaded dll.
971 * hModule [I] Handle to the dll returned by LoadLibraryA().
972 * function [I] Name of the symbol, or an integer ordinal number < 16384
975 * Success: A pointer to the symbol in the process address space.
976 * Failure: NULL. Use GetLastError() to determine the cause.
978 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
980 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
981 return GetProcAddress( hModule, function );
985 /***********************************************************************
986 * DelayLoadFailureHook (KERNEL32.@)
988 FARPROC WINAPI DelayLoadFailureHook( LPCSTR name, LPCSTR function )
992 ERR( "failed to delay load %s.%s\n", name, function );
993 args[0] = (ULONG_PTR)name;
994 args[1] = (ULONG_PTR)function;
995 RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );