Added version info.
[wine/hacks.git] / dlls / kernel / module.c
blob4fd9eeaefe60d67fa4bb6f71c50f9373fc8fde4b
1 /*
2 * Modules
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include "wine/winbase16.h"
33 #include "winerror.h"
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "winternl.h"
39 #include "thread.h"
40 #include "module.h"
41 #include "kernel_private.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 /****************************************************************************
52 * DisableThreadLibraryCalls (KERNEL32.@)
54 * Inform the module loader that thread notifications are not required for a dll.
56 * PARAMS
57 * hModule [I] Module handle to skip calls for
59 * RETURNS
60 * Success: TRUE. Thread attach and detach notifications will not be sent
61 * to hModule.
62 * Failure: FALSE. Use GetLastError() to determine the cause.
64 * NOTES
65 * This is typically called from the dll entry point of a dll during process
66 * attachment, for dlls that do not need to process thread notifications.
68 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
70 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
71 if (nts == STATUS_SUCCESS) return TRUE;
73 SetLastError( RtlNtStatusToDosError( nts ) );
74 return FALSE;
78 /* Check whether a file is an OS/2 or a very old Windows executable
79 * by testing on import of KERNEL.
81 * FIXME: is reading the module imports the only way of discerning
82 * old Windows binaries from OS/2 ones ? At least it seems so...
84 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
85 const IMAGE_OS2_HEADER *ne)
87 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
88 enum binary_type ret = BINARY_OS216;
89 LPWORD modtab = NULL;
90 LPSTR nametab = NULL;
91 DWORD len;
92 int i;
94 /* read modref table */
95 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
96 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
97 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
98 || (len != ne->ne_cmod*sizeof(WORD)) )
99 goto broken;
101 /* read imported names table */
102 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
103 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
104 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
105 || (len != ne->ne_enttab - ne->ne_imptab) )
106 goto broken;
108 for (i=0; i < ne->ne_cmod; i++)
110 LPSTR module = &nametab[modtab[i]];
111 TRACE("modref: %.*s\n", module[0], &module[1]);
112 if (!(strncmp(&module[1], "KERNEL", module[0])))
113 { /* very old Windows file */
114 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
115 ret = BINARY_WIN16;
116 goto good;
120 broken:
121 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
123 good:
124 HeapFree( GetProcessHeap(), 0, modtab);
125 HeapFree( GetProcessHeap(), 0, nametab);
126 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
127 return ret;
130 /***********************************************************************
131 * MODULE_GetBinaryType
133 enum binary_type MODULE_GetBinaryType( HANDLE hfile )
135 union
137 struct
139 unsigned char magic[4];
140 unsigned char ignored[12];
141 unsigned short type;
142 } elf;
143 struct
145 unsigned long magic;
146 unsigned long cputype;
147 unsigned long cpusubtype;
148 unsigned long filetype;
149 } macho;
150 IMAGE_DOS_HEADER mz;
151 } header;
153 char magic[4];
154 DWORD len;
156 /* Seek to the start of the file and read the header information. */
157 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
158 return BINARY_UNKNOWN;
159 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
160 return BINARY_UNKNOWN;
162 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
164 /* FIXME: we don't bother to check byte order, architecture, etc. */
165 switch(header.elf.type)
167 case 2: return BINARY_UNIX_EXE;
168 case 3: return BINARY_UNIX_LIB;
170 return BINARY_UNKNOWN;
173 /* Mach-o File with Endian set to Big Endian or Little Endian*/
174 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
176 switch(header.macho.filetype)
178 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
180 return BINARY_UNKNOWN;
183 /* Not ELF, try DOS */
185 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
187 /* We do have a DOS image so we will now try to seek into
188 * the file by the amount indicated by the field
189 * "Offset to extended header" and read in the
190 * "magic" field information at that location.
191 * This will tell us if there is more header information
192 * to read or not.
194 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
195 return BINARY_DOS;
196 if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
197 return BINARY_DOS;
199 /* Reading the magic field succeeded so
200 * we will try to determine what type it is.
202 if (!memcmp( magic, "PE\0\0", 4 ))
204 IMAGE_FILE_HEADER FileHeader;
206 if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
208 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
209 return BINARY_PE_EXE;
211 return BINARY_DOS;
214 if (!memcmp( magic, "NE", 2 ))
216 /* This is a Windows executable (NE) header. This can
217 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
218 * DOS program (running under a DOS extender). To decide
219 * which, we'll have to read the NE header.
221 IMAGE_OS2_HEADER ne;
222 if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
223 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
224 && len == sizeof(ne) )
226 switch ( ne.ne_exetyp )
228 case 2: return BINARY_WIN16;
229 case 5: return BINARY_DOS;
230 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
233 /* Couldn't read header, so abort. */
234 return BINARY_DOS;
237 /* Unknown extended header, but this file is nonetheless DOS-executable. */
238 return BINARY_DOS;
241 return BINARY_UNKNOWN;
244 /***********************************************************************
245 * GetBinaryTypeW [KERNEL32.@]
247 * Determine whether a file is executable, and if so, what kind.
249 * PARAMS
250 * lpApplicationName [I] Path of the file to check
251 * lpBinaryType [O] Destination for the binary type
253 * RETURNS
254 * TRUE, if the file is an executable, in which case lpBinaryType is set.
255 * FALSE, if the file is not an executable or if the function fails.
257 * NOTES
258 * The type of executable is a property that determines which subsytem an
259 * executable file runs under. lpBinaryType can be set to one of the following
260 * values:
261 * SCS_32BIT_BINARY: A Win32 based application
262 * SCS_DOS_BINARY: An MS-Dos based application
263 * SCS_WOW_BINARY: A Win16 based application
264 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
265 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
266 * SCS_OS216_BINARY: A 16bit OS/2 based application
268 * To find the binary type, this function reads in the files header information.
269 * If extended header information is not present it will assume that the file
270 * is a DOS executable. If extended header information is present it will
271 * determine if the file is a 16 or 32 bit Windows executable by checking the
272 * flags in the header.
274 * ".com" and ".pif" files are only recognized by their file name extension,
275 * as per native Windows.
277 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
279 BOOL ret = FALSE;
280 HANDLE hfile;
282 TRACE("%s\n", debugstr_w(lpApplicationName) );
284 /* Sanity check.
286 if ( lpApplicationName == NULL || lpBinaryType == NULL )
287 return FALSE;
289 /* Open the file indicated by lpApplicationName for reading.
291 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
292 NULL, OPEN_EXISTING, 0, 0 );
293 if ( hfile == INVALID_HANDLE_VALUE )
294 return FALSE;
296 /* Check binary type
298 switch(MODULE_GetBinaryType( hfile ))
300 case BINARY_UNKNOWN:
302 static const WCHAR comW[] = { '.','C','O','M',0 };
303 static const WCHAR pifW[] = { '.','P','I','F',0 };
304 const WCHAR *ptr;
306 /* try to determine from file name */
307 ptr = strrchrW( lpApplicationName, '.' );
308 if (!ptr) break;
309 if (!strcmpiW( ptr, comW ))
311 *lpBinaryType = SCS_DOS_BINARY;
312 ret = TRUE;
314 else if (!strcmpiW( ptr, pifW ))
316 *lpBinaryType = SCS_PIF_BINARY;
317 ret = TRUE;
319 break;
321 case BINARY_PE_EXE:
322 case BINARY_PE_DLL:
323 *lpBinaryType = SCS_32BIT_BINARY;
324 ret = TRUE;
325 break;
326 case BINARY_WIN16:
327 *lpBinaryType = SCS_WOW_BINARY;
328 ret = TRUE;
329 break;
330 case BINARY_OS216:
331 *lpBinaryType = SCS_OS216_BINARY;
332 ret = TRUE;
333 break;
334 case BINARY_DOS:
335 *lpBinaryType = SCS_DOS_BINARY;
336 ret = TRUE;
337 break;
338 case BINARY_UNIX_EXE:
339 case BINARY_UNIX_LIB:
340 ret = FALSE;
341 break;
344 CloseHandle( hfile );
345 return ret;
348 /***********************************************************************
349 * GetBinaryTypeA [KERNEL32.@]
350 * GetBinaryType [KERNEL32.@]
352 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
354 ANSI_STRING app_nameA;
355 NTSTATUS status;
357 TRACE("%s\n", debugstr_a(lpApplicationName));
359 /* Sanity check.
361 if ( lpApplicationName == NULL || lpBinaryType == NULL )
362 return FALSE;
364 RtlInitAnsiString(&app_nameA, lpApplicationName);
365 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
366 &app_nameA, FALSE);
367 if (!status)
368 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
370 SetLastError(RtlNtStatusToDosError(status));
371 return FALSE;
375 /***********************************************************************
376 * GetModuleHandleA (KERNEL32.@)
377 * GetModuleHandle32 (KERNEL.488)
379 * Get the handle of a dll loaded into the process address space.
381 * PARAMS
382 * module [I] Name of the dll
384 * RETURNS
385 * Success: A handle to the loaded dll.
386 * Failure: A NULL handle. Use GetLastError() to determine the cause.
388 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
390 NTSTATUS nts;
391 HMODULE ret;
392 UNICODE_STRING wstr;
394 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
396 RtlCreateUnicodeStringFromAsciiz(&wstr, module);
397 nts = LdrGetDllHandle(0, 0, &wstr, &ret);
398 RtlFreeUnicodeString( &wstr );
399 if (nts != STATUS_SUCCESS)
401 ret = 0;
402 SetLastError( RtlNtStatusToDosError( nts ) );
404 return ret;
407 /***********************************************************************
408 * GetModuleHandleW (KERNEL32.@)
410 * Unicode version of GetModuleHandleA.
412 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
414 NTSTATUS nts;
415 HMODULE ret;
416 UNICODE_STRING wstr;
418 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
420 RtlInitUnicodeString( &wstr, module );
421 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
422 if (nts != STATUS_SUCCESS)
424 SetLastError( RtlNtStatusToDosError( nts ) );
425 ret = 0;
427 return ret;
431 /***********************************************************************
432 * GetModuleFileNameA (KERNEL32.@)
433 * GetModuleFileName32 (KERNEL.487)
435 * Get the file name of a loaded module from its handle.
437 * RETURNS
438 * Success: The length of the file name, excluding the terminating NUL.
439 * Failure: 0. Use GetLastError() to determine the cause.
441 * NOTES
442 * This function always returns the long path of hModule (as opposed to
443 * GetModuleFileName16() which returns short paths when the modules version
444 * field is < 4.0).
446 DWORD WINAPI GetModuleFileNameA(
447 HMODULE hModule, /* [in] Module handle (32 bit) */
448 LPSTR lpFileName, /* [out] Destination for file name */
449 DWORD size ) /* [in] Size of lpFileName in characters */
451 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
453 if (!filenameW)
455 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
456 return 0;
458 GetModuleFileNameW( hModule, filenameW, size );
459 WideCharToMultiByte( CP_ACP, 0, filenameW, -1, lpFileName, size, NULL, NULL );
460 HeapFree( GetProcessHeap(), 0, filenameW );
461 return strlen( lpFileName );
464 /***********************************************************************
465 * GetModuleFileNameW (KERNEL32.@)
467 * Unicode version of GetModuleFileNameA.
469 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
471 ULONG magic;
472 LDR_MODULE *pldr;
473 NTSTATUS nts;
474 WIN16_SUBSYSTEM_TIB *win16_tib;
476 lpFileName[0] = 0;
478 if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
480 lstrcpynW( lpFileName, win16_tib->exe_name->Buffer, size );
481 goto done;
484 LdrLockLoaderLock( 0, NULL, &magic );
486 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
487 nts = LdrFindEntryForAddress( hModule, &pldr );
488 if (nts == STATUS_SUCCESS) lstrcpynW(lpFileName, pldr->FullDllName.Buffer, size);
489 else SetLastError( RtlNtStatusToDosError( nts ) );
491 LdrUnlockLoaderLock( 0, magic );
492 done:
493 TRACE( "%s\n", debugstr_w(lpFileName) );
494 return strlenW(lpFileName);
498 /***********************************************************************
499 * get_dll_system_path
501 static const WCHAR *get_dll_system_path(void)
503 static WCHAR *cached_path;
505 if (!cached_path)
507 WCHAR *p, *path;
508 int len = 3;
510 len += GetSystemDirectoryW( NULL, 0 );
511 len += GetWindowsDirectoryW( NULL, 0 );
512 p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
513 *p++ = '.';
514 *p++ = ';';
515 GetSystemDirectoryW( p, path + len - p);
516 p += strlenW(p);
517 *p++ = ';';
518 GetWindowsDirectoryW( p, path + len - p);
519 cached_path = path;
521 return cached_path;
525 /******************************************************************
526 * MODULE_get_dll_load_path
528 * Compute the load path to use for a given dll.
529 * Returned pointer must be freed by caller.
531 WCHAR *MODULE_get_dll_load_path( LPCWSTR module )
533 static const WCHAR pathW[] = {'P','A','T','H',0};
535 const WCHAR *system_path = get_dll_system_path();
536 const WCHAR *mod_end = NULL;
537 UNICODE_STRING name, value;
538 WCHAR *p, *ret;
539 int len = 0, path_len = 0;
541 /* adjust length for module name */
543 if (!module) module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
544 if (module)
546 mod_end = module;
547 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
548 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
549 if (mod_end == module + 2 && module[1] == ':') mod_end++;
550 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
551 len += (mod_end - module) + 1;
553 len += strlenW( system_path ) + 2;
555 /* get the PATH variable */
557 RtlInitUnicodeString( &name, pathW );
558 value.Length = 0;
559 value.MaximumLength = 0;
560 value.Buffer = NULL;
561 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
562 path_len = value.Length;
564 if (!(ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) return NULL;
565 p = ret;
566 if (module)
568 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
569 p += (mod_end - module);
570 *p++ = ';';
572 strcpyW( p, system_path );
573 p += strlenW(p);
574 *p++ = ';';
575 value.Buffer = p;
576 value.MaximumLength = path_len;
578 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
580 WCHAR *new_ptr;
582 /* grow the buffer and retry */
583 path_len = value.Length;
584 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
586 HeapFree( GetProcessHeap(), 0, ret );
587 return NULL;
589 value.Buffer = new_ptr + (value.Buffer - ret);
590 value.MaximumLength = path_len;
591 ret = new_ptr;
593 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
594 return ret;
598 /******************************************************************
599 * load_library_as_datafile
601 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
603 static const WCHAR dotDLL[] = {'.','d','l','l',0};
605 WCHAR filenameW[MAX_PATH];
606 HANDLE hFile = INVALID_HANDLE_VALUE;
607 HANDLE mapping;
608 HMODULE module;
610 *hmod = 0;
612 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
613 filenameW, NULL ))
615 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
616 NULL, OPEN_EXISTING, 0, 0 );
618 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
620 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
621 CloseHandle( hFile );
622 if (!mapping) return FALSE;
624 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
625 CloseHandle( mapping );
626 if (!module) return FALSE;
628 /* make sure it's a valid PE file */
629 if (!RtlImageNtHeader(module))
631 UnmapViewOfFile( module );
632 return FALSE;
634 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
635 return TRUE;
639 /******************************************************************
640 * load_library
642 * Helper for LoadLibraryExA/W.
644 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
646 NTSTATUS nts;
647 HMODULE hModule;
648 WCHAR *load_path;
650 if (flags & LOAD_LIBRARY_AS_DATAFILE)
652 /* The method in load_library_as_datafile allows searching for the
653 * 'native' libraries only
655 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
656 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
657 /* Fallback to normal behaviour */
660 load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
661 nts = LdrLoadDll( load_path, flags, libname, &hModule );
662 HeapFree( GetProcessHeap(), 0, load_path );
663 if (nts != STATUS_SUCCESS)
665 hModule = 0;
666 SetLastError( RtlNtStatusToDosError( nts ) );
668 return hModule;
672 /******************************************************************
673 * LoadLibraryExA (KERNEL32.@)
675 * Load a dll file into the process address space.
677 * PARAMS
678 * libname [I] Name of the file to load
679 * hfile [I] Reserved, must be 0.
680 * flags [I] Flags for loading the dll
682 * RETURNS
683 * Success: A handle to the loaded dll.
684 * Failure: A NULL handle. Use GetLastError() to determine the cause.
686 * NOTES
687 * The HFILE parameter is not used and marked reserved in the SDK. I can
688 * only guess that it should force a file to be mapped, but I rather
689 * ignore the parameter because it would be extremely difficult to
690 * integrate this with different types of module representations.
692 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
694 UNICODE_STRING wstr;
695 HMODULE hModule;
697 if (!libname)
699 SetLastError(ERROR_INVALID_PARAMETER);
700 return 0;
702 RtlCreateUnicodeStringFromAsciiz( &wstr, libname );
703 hModule = load_library( &wstr, flags );
704 RtlFreeUnicodeString( &wstr );
705 return hModule;
708 /***********************************************************************
709 * LoadLibraryExW (KERNEL32.@)
711 * Unicode version of LoadLibraryExA.
713 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
715 UNICODE_STRING wstr;
717 if (!libnameW)
719 SetLastError(ERROR_INVALID_PARAMETER);
720 return 0;
722 RtlInitUnicodeString( &wstr, libnameW );
723 return load_library( &wstr, flags );
726 /***********************************************************************
727 * LoadLibraryA (KERNEL32.@)
729 * Load a dll file into the process address space.
731 * PARAMS
732 * libname [I] Name of the file to load
734 * RETURNS
735 * Success: A handle to the loaded dll.
736 * Failure: A NULL handle. Use GetLastError() to determine the cause.
738 * NOTES
739 * See LoadLibraryExA().
741 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
743 return LoadLibraryExA(libname, 0, 0);
746 /***********************************************************************
747 * LoadLibraryW (KERNEL32.@)
749 * Unicode version of LoadLibraryA.
751 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
753 return LoadLibraryExW(libnameW, 0, 0);
756 /***********************************************************************
757 * FreeLibrary (KERNEL32.@)
758 * FreeLibrary32 (KERNEL.486)
760 * Free a dll loaded into the process address space.
762 * PARAMS
763 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
765 * RETURNS
766 * Success: TRUE. The dll is removed if it is not still in use.
767 * Failure: FALSE. Use GetLastError() to determine the cause.
769 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
771 BOOL retv = FALSE;
772 NTSTATUS nts;
774 if (!hLibModule)
776 SetLastError( ERROR_INVALID_HANDLE );
777 return FALSE;
780 if ((ULONG_PTR)hLibModule & 1)
782 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
783 char *ptr = (char *)hLibModule - 1;
784 UnmapViewOfFile( ptr );
785 return TRUE;
788 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
789 else SetLastError( RtlNtStatusToDosError( nts ) );
791 return retv;
794 /***********************************************************************
795 * GetProcAddress (KERNEL32.@)
797 * Find the address of an exported symbol in a loaded dll.
799 * PARAMS
800 * hModule [I] Handle to the dll returned by LoadLibraryA().
801 * function [I] Name of the symbol, or an integer ordinal number < 16384
803 * RETURNS
804 * Success: A pointer to the symbol in the process address space.
805 * Failure: NULL. Use GetLastError() to determine the cause.
807 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
809 NTSTATUS nts;
810 FARPROC fp;
812 if (HIWORD(function))
814 ANSI_STRING str;
816 RtlInitAnsiString( &str, function );
817 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
819 else
820 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
821 if (nts != STATUS_SUCCESS)
823 SetLastError( RtlNtStatusToDosError( nts ) );
824 fp = NULL;
826 return fp;
829 /***********************************************************************
830 * GetProcAddress32 (KERNEL.453)
832 * Find the address of an exported symbol in a loaded dll.
834 * PARAMS
835 * hModule [I] Handle to the dll returned by LoadLibraryA().
836 * function [I] Name of the symbol, or an integer ordinal number < 16384
838 * RETURNS
839 * Success: A pointer to the symbol in the process address space.
840 * Failure: NULL. Use GetLastError() to determine the cause.
842 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
844 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
845 return GetProcAddress( hModule, function );