Move the FPS computation from the D3D code to the common code.
[wine/multimedia.git] / dlls / kernel / module.c
blobd06b0c495c32cc63d8f7ddad4e09301ba073d034
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, void **res_start, void **res_end )
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 DWORD len;
155 /* Seek to the start of the file and read the header information. */
156 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
157 return BINARY_UNKNOWN;
158 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
159 return BINARY_UNKNOWN;
161 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
163 /* FIXME: we don't bother to check byte order, architecture, etc. */
164 switch(header.elf.type)
166 case 2: return BINARY_UNIX_EXE;
167 case 3: return BINARY_UNIX_LIB;
169 return BINARY_UNKNOWN;
172 /* Mach-o File with Endian set to Big Endian or Little Endian*/
173 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
175 switch(header.macho.filetype)
177 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
179 return BINARY_UNKNOWN;
182 /* Not ELF, try DOS */
184 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
186 union
188 IMAGE_OS2_HEADER os2;
189 IMAGE_NT_HEADERS nt;
190 } ext_header;
192 /* We do have a DOS image so we will now try to seek into
193 * the file by the amount indicated by the field
194 * "Offset to extended header" and read in the
195 * "magic" field information at that location.
196 * This will tell us if there is more header information
197 * to read or not.
199 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
200 return BINARY_DOS;
201 if (!ReadFile( hfile, &ext_header, sizeof(ext_header), &len, NULL ) || len < 4)
202 return BINARY_DOS;
204 /* Reading the magic field succeeded so
205 * we will try to determine what type it is.
207 if (!memcmp( &ext_header.nt.Signature, "PE\0\0", 4 ))
209 if (len >= sizeof(ext_header.nt.FileHeader))
211 if (len < sizeof(ext_header.nt)) /* clear remaining part of header if missing */
212 memset( (char *)&ext_header.nt + len, 0, sizeof(ext_header.nt) - len );
213 if (res_start) *res_start = (void *)ext_header.nt.OptionalHeader.ImageBase;
214 if (res_end) *res_end = (void *)(ext_header.nt.OptionalHeader.ImageBase +
215 ext_header.nt.OptionalHeader.SizeOfImage);
216 if (ext_header.nt.FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
217 return BINARY_PE_EXE;
219 return BINARY_DOS;
222 if (!memcmp( &ext_header.os2.ne_magic, "NE", 2 ))
224 /* This is a Windows executable (NE) header. This can
225 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
226 * DOS program (running under a DOS extender). To decide
227 * which, we'll have to read the NE header.
229 if (len >= sizeof(ext_header.os2))
231 switch ( ext_header.os2.ne_exetyp )
233 case 2: return BINARY_WIN16;
234 case 5: return BINARY_DOS;
235 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ext_header.os2);
238 /* Couldn't read header, so abort. */
239 return BINARY_DOS;
242 /* Unknown extended header, but this file is nonetheless DOS-executable. */
243 return BINARY_DOS;
246 return BINARY_UNKNOWN;
249 /***********************************************************************
250 * GetBinaryTypeW [KERNEL32.@]
252 * Determine whether a file is executable, and if so, what kind.
254 * PARAMS
255 * lpApplicationName [I] Path of the file to check
256 * lpBinaryType [O] Destination for the binary type
258 * RETURNS
259 * TRUE, if the file is an executable, in which case lpBinaryType is set.
260 * FALSE, if the file is not an executable or if the function fails.
262 * NOTES
263 * The type of executable is a property that determines which subsytem an
264 * executable file runs under. lpBinaryType can be set to one of the following
265 * values:
266 * SCS_32BIT_BINARY: A Win32 based application
267 * SCS_DOS_BINARY: An MS-Dos based application
268 * SCS_WOW_BINARY: A Win16 based application
269 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
270 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
271 * SCS_OS216_BINARY: A 16bit OS/2 based application
273 * To find the binary type, this function reads in the files header information.
274 * If extended header information is not present it will assume that the file
275 * is a DOS executable. If extended header information is present it will
276 * determine if the file is a 16 or 32 bit Windows executable by checking the
277 * flags in the header.
279 * ".com" and ".pif" files are only recognized by their file name extension,
280 * as per native Windows.
282 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
284 BOOL ret = FALSE;
285 HANDLE hfile;
287 TRACE("%s\n", debugstr_w(lpApplicationName) );
289 /* Sanity check.
291 if ( lpApplicationName == NULL || lpBinaryType == NULL )
292 return FALSE;
294 /* Open the file indicated by lpApplicationName for reading.
296 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
297 NULL, OPEN_EXISTING, 0, 0 );
298 if ( hfile == INVALID_HANDLE_VALUE )
299 return FALSE;
301 /* Check binary type
303 switch(MODULE_GetBinaryType( hfile, NULL, NULL ))
305 case BINARY_UNKNOWN:
307 static const WCHAR comW[] = { '.','C','O','M',0 };
308 static const WCHAR pifW[] = { '.','P','I','F',0 };
309 const WCHAR *ptr;
311 /* try to determine from file name */
312 ptr = strrchrW( lpApplicationName, '.' );
313 if (!ptr) break;
314 if (!strcmpiW( ptr, comW ))
316 *lpBinaryType = SCS_DOS_BINARY;
317 ret = TRUE;
319 else if (!strcmpiW( ptr, pifW ))
321 *lpBinaryType = SCS_PIF_BINARY;
322 ret = TRUE;
324 break;
326 case BINARY_PE_EXE:
327 case BINARY_PE_DLL:
328 *lpBinaryType = SCS_32BIT_BINARY;
329 ret = TRUE;
330 break;
331 case BINARY_WIN16:
332 *lpBinaryType = SCS_WOW_BINARY;
333 ret = TRUE;
334 break;
335 case BINARY_OS216:
336 *lpBinaryType = SCS_OS216_BINARY;
337 ret = TRUE;
338 break;
339 case BINARY_DOS:
340 *lpBinaryType = SCS_DOS_BINARY;
341 ret = TRUE;
342 break;
343 case BINARY_UNIX_EXE:
344 case BINARY_UNIX_LIB:
345 ret = FALSE;
346 break;
349 CloseHandle( hfile );
350 return ret;
353 /***********************************************************************
354 * GetBinaryTypeA [KERNEL32.@]
355 * GetBinaryType [KERNEL32.@]
357 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
359 ANSI_STRING app_nameA;
360 NTSTATUS status;
362 TRACE("%s\n", debugstr_a(lpApplicationName));
364 /* Sanity check.
366 if ( lpApplicationName == NULL || lpBinaryType == NULL )
367 return FALSE;
369 RtlInitAnsiString(&app_nameA, lpApplicationName);
370 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
371 &app_nameA, FALSE);
372 if (!status)
373 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
375 SetLastError(RtlNtStatusToDosError(status));
376 return FALSE;
380 /***********************************************************************
381 * GetModuleHandleA (KERNEL32.@)
382 * GetModuleHandle32 (KERNEL.488)
384 * Get the handle of a dll loaded into the process address space.
386 * PARAMS
387 * module [I] Name of the dll
389 * RETURNS
390 * Success: A handle to the loaded dll.
391 * Failure: A NULL handle. Use GetLastError() to determine the cause.
393 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
395 WCHAR *moduleW;
397 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
398 if (!(moduleW = FILE_name_AtoW( module, FALSE ))) return 0;
399 return GetModuleHandleW( moduleW );
402 /***********************************************************************
403 * GetModuleHandleW (KERNEL32.@)
405 * Unicode version of GetModuleHandleA.
407 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
409 NTSTATUS nts;
410 HMODULE ret;
411 UNICODE_STRING wstr;
413 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
415 RtlInitUnicodeString( &wstr, module );
416 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
417 if (nts != STATUS_SUCCESS)
419 SetLastError( RtlNtStatusToDosError( nts ) );
420 ret = 0;
422 return ret;
426 /***********************************************************************
427 * GetModuleFileNameA (KERNEL32.@)
428 * GetModuleFileName32 (KERNEL.487)
430 * Get the file name of a loaded module from its handle.
432 * RETURNS
433 * Success: The length of the file name, excluding the terminating NUL.
434 * Failure: 0. Use GetLastError() to determine the cause.
436 * NOTES
437 * This function always returns the long path of hModule (as opposed to
438 * GetModuleFileName16() which returns short paths when the modules version
439 * field is < 4.0).
440 * The function doesn't write a terminating '\0' is the buffer is too
441 * small.
443 DWORD WINAPI GetModuleFileNameA(
444 HMODULE hModule, /* [in] Module handle (32 bit) */
445 LPSTR lpFileName, /* [out] Destination for file name */
446 DWORD size ) /* [in] Size of lpFileName in characters */
448 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
449 DWORD len;
451 if (!filenameW)
453 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
454 return 0;
456 if ((len = GetModuleFileNameW( hModule, filenameW, size )))
458 len = FILE_name_WtoA( filenameW, len, lpFileName, size );
459 if (len < size) lpFileName[len] = '\0';
461 HeapFree( GetProcessHeap(), 0, filenameW );
462 return len;
465 /***********************************************************************
466 * GetModuleFileNameW (KERNEL32.@)
468 * Unicode version of GetModuleFileNameA.
470 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
472 ULONG magic, len = 0;
473 LDR_MODULE *pldr;
474 NTSTATUS nts;
475 WIN16_SUBSYSTEM_TIB *win16_tib;
477 if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
479 len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR));
480 memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
481 if (len < size) lpFileName[len] = '\0';
482 goto done;
485 LdrLockLoaderLock( 0, NULL, &magic );
487 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
488 nts = LdrFindEntryForAddress( hModule, &pldr );
489 if (nts == STATUS_SUCCESS)
491 len = min(size, pldr->FullDllName.Length / sizeof(WCHAR));
492 memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR));
493 if (len < size) lpFileName[len] = '\0';
495 else SetLastError( RtlNtStatusToDosError( nts ) );
497 LdrUnlockLoaderLock( 0, magic );
498 done:
499 TRACE( "%s\n", debugstr_wn(lpFileName, len) );
500 return len;
504 /***********************************************************************
505 * get_dll_system_path
507 static const WCHAR *get_dll_system_path(void)
509 static WCHAR *cached_path;
511 if (!cached_path)
513 WCHAR *p, *path;
514 int len = 3;
516 len += GetSystemDirectoryW( NULL, 0 );
517 len += GetWindowsDirectoryW( NULL, 0 );
518 p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
519 *p++ = '.';
520 *p++ = ';';
521 GetSystemDirectoryW( p, path + len - p);
522 p += strlenW(p);
523 *p++ = ';';
524 GetWindowsDirectoryW( p, path + len - p);
525 cached_path = path;
527 return cached_path;
531 /******************************************************************
532 * MODULE_get_dll_load_path
534 * Compute the load path to use for a given dll.
535 * Returned pointer must be freed by caller.
537 WCHAR *MODULE_get_dll_load_path( LPCWSTR module )
539 static const WCHAR pathW[] = {'P','A','T','H',0};
541 const WCHAR *system_path = get_dll_system_path();
542 const WCHAR *mod_end = NULL;
543 UNICODE_STRING name, value;
544 WCHAR *p, *ret;
545 int len = 0, path_len = 0;
547 /* adjust length for module name */
549 if (!module) module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
550 if (module)
552 mod_end = module;
553 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
554 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
555 if (mod_end == module + 2 && module[1] == ':') mod_end++;
556 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
557 len += (mod_end - module) + 1;
559 len += strlenW( system_path ) + 2;
561 /* get the PATH variable */
563 RtlInitUnicodeString( &name, pathW );
564 value.Length = 0;
565 value.MaximumLength = 0;
566 value.Buffer = NULL;
567 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
568 path_len = value.Length;
570 if (!(ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) return NULL;
571 p = ret;
572 if (module)
574 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
575 p += (mod_end - module);
576 *p++ = ';';
578 strcpyW( p, system_path );
579 p += strlenW(p);
580 *p++ = ';';
581 value.Buffer = p;
582 value.MaximumLength = path_len;
584 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
586 WCHAR *new_ptr;
588 /* grow the buffer and retry */
589 path_len = value.Length;
590 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
592 HeapFree( GetProcessHeap(), 0, ret );
593 return NULL;
595 value.Buffer = new_ptr + (value.Buffer - ret);
596 value.MaximumLength = path_len;
597 ret = new_ptr;
599 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
600 return ret;
604 /******************************************************************
605 * load_library_as_datafile
607 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
609 static const WCHAR dotDLL[] = {'.','d','l','l',0};
611 WCHAR filenameW[MAX_PATH];
612 HANDLE hFile = INVALID_HANDLE_VALUE;
613 HANDLE mapping;
614 HMODULE module;
616 *hmod = 0;
618 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
619 filenameW, NULL ))
621 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
622 NULL, OPEN_EXISTING, 0, 0 );
624 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
626 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
627 CloseHandle( hFile );
628 if (!mapping) return FALSE;
630 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
631 CloseHandle( mapping );
632 if (!module) return FALSE;
634 /* make sure it's a valid PE file */
635 if (!RtlImageNtHeader(module))
637 UnmapViewOfFile( module );
638 return FALSE;
640 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
641 return TRUE;
645 /******************************************************************
646 * load_library
648 * Helper for LoadLibraryExA/W.
650 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
652 NTSTATUS nts;
653 HMODULE hModule;
654 WCHAR *load_path;
656 if (flags & LOAD_LIBRARY_AS_DATAFILE)
658 /* The method in load_library_as_datafile allows searching for the
659 * 'native' libraries only
661 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
662 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
663 /* Fallback to normal behaviour */
666 load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
667 nts = LdrLoadDll( load_path, flags, libname, &hModule );
668 HeapFree( GetProcessHeap(), 0, load_path );
669 if (nts != STATUS_SUCCESS)
671 hModule = 0;
672 SetLastError( RtlNtStatusToDosError( nts ) );
674 return hModule;
678 /******************************************************************
679 * LoadLibraryExA (KERNEL32.@)
681 * Load a dll file into the process address space.
683 * PARAMS
684 * libname [I] Name of the file to load
685 * hfile [I] Reserved, must be 0.
686 * flags [I] Flags for loading the dll
688 * RETURNS
689 * Success: A handle to the loaded dll.
690 * Failure: A NULL handle. Use GetLastError() to determine the cause.
692 * NOTES
693 * The HFILE parameter is not used and marked reserved in the SDK. I can
694 * only guess that it should force a file to be mapped, but I rather
695 * ignore the parameter because it would be extremely difficult to
696 * integrate this with different types of module representations.
698 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
700 WCHAR *libnameW;
702 if (!(libnameW = FILE_name_AtoW( libname, FALSE ))) return 0;
703 return LoadLibraryExW( libnameW, hfile, flags );
706 /***********************************************************************
707 * LoadLibraryExW (KERNEL32.@)
709 * Unicode version of LoadLibraryExA.
711 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
713 UNICODE_STRING wstr;
715 if (!libnameW)
717 SetLastError(ERROR_INVALID_PARAMETER);
718 return 0;
720 RtlInitUnicodeString( &wstr, libnameW );
721 return load_library( &wstr, flags );
724 /***********************************************************************
725 * LoadLibraryA (KERNEL32.@)
727 * Load a dll file into the process address space.
729 * PARAMS
730 * libname [I] Name of the file to load
732 * RETURNS
733 * Success: A handle to the loaded dll.
734 * Failure: A NULL handle. Use GetLastError() to determine the cause.
736 * NOTES
737 * See LoadLibraryExA().
739 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
741 return LoadLibraryExA(libname, 0, 0);
744 /***********************************************************************
745 * LoadLibraryW (KERNEL32.@)
747 * Unicode version of LoadLibraryA.
749 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
751 return LoadLibraryExW(libnameW, 0, 0);
754 /***********************************************************************
755 * FreeLibrary (KERNEL32.@)
756 * FreeLibrary32 (KERNEL.486)
758 * Free a dll loaded into the process address space.
760 * PARAMS
761 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
763 * RETURNS
764 * Success: TRUE. The dll is removed if it is not still in use.
765 * Failure: FALSE. Use GetLastError() to determine the cause.
767 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
769 BOOL retv = FALSE;
770 NTSTATUS nts;
772 if (!hLibModule)
774 SetLastError( ERROR_INVALID_HANDLE );
775 return FALSE;
778 if ((ULONG_PTR)hLibModule & 1)
780 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
781 char *ptr = (char *)hLibModule - 1;
782 UnmapViewOfFile( ptr );
783 return TRUE;
786 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
787 else SetLastError( RtlNtStatusToDosError( nts ) );
789 return retv;
792 /***********************************************************************
793 * GetProcAddress (KERNEL32.@)
795 * Find the address of an exported symbol in a loaded dll.
797 * PARAMS
798 * hModule [I] Handle to the dll returned by LoadLibraryA().
799 * function [I] Name of the symbol, or an integer ordinal number < 16384
801 * RETURNS
802 * Success: A pointer to the symbol in the process address space.
803 * Failure: NULL. Use GetLastError() to determine the cause.
805 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
807 NTSTATUS nts;
808 FARPROC fp;
810 if (HIWORD(function))
812 ANSI_STRING str;
814 RtlInitAnsiString( &str, function );
815 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
817 else
818 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
819 if (nts != STATUS_SUCCESS)
821 SetLastError( RtlNtStatusToDosError( nts ) );
822 fp = NULL;
824 return fp;
827 /***********************************************************************
828 * GetProcAddress32 (KERNEL.453)
830 * Find the address of an exported symbol in a loaded dll.
832 * PARAMS
833 * hModule [I] Handle to the dll returned by LoadLibraryA().
834 * function [I] Name of the symbol, or an integer ordinal number < 16384
836 * RETURNS
837 * Success: A pointer to the symbol in the process address space.
838 * Failure: NULL. Use GetLastError() to determine the cause.
840 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
842 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
843 return GetProcAddress( hModule, function );