MSVC fixes.
[wine/wine64.git] / loader / module.c
blobb9d30826cfd5516e1a36dd4e1235916a7ea6ff6c
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 <assert.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include "wine/winbase16.h"
34 #include "winerror.h"
35 #include "winternl.h"
36 #include "heap.h"
37 #include "file.h"
38 #include "module.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
42 #include "wine/server.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(module);
45 WINE_DECLARE_DEBUG_CHANNEL(win32);
46 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
49 /***********************************************************************
50 * wait_input_idle
52 * Wrapper to call WaitForInputIdle USER function
54 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
56 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
58 HMODULE mod = GetModuleHandleA( "user32.dll" );
59 if (mod)
61 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
62 if (ptr) return ptr( process, timeout );
64 return 0;
68 /****************************************************************************
69 * DisableThreadLibraryCalls (KERNEL32.@)
71 * Don't call DllEntryPoint for DLL_THREAD_{ATTACH,DETACH} if set.
73 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
75 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
76 if (nts == STATUS_SUCCESS) return TRUE;
78 SetLastError( RtlNtStatusToDosError( nts ) );
79 return FALSE;
83 /* Check whether a file is an OS/2 or a very old Windows executable
84 * by testing on import of KERNEL.
86 * FIXME: is reading the module imports the only way of discerning
87 * old Windows binaries from OS/2 ones ? At least it seems so...
89 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
90 const IMAGE_OS2_HEADER *ne)
92 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
93 enum binary_type ret = BINARY_OS216;
94 LPWORD modtab = NULL;
95 LPSTR nametab = NULL;
96 DWORD len;
97 int i;
99 /* read modref table */
100 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
101 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
102 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
103 || (len != ne->ne_cmod*sizeof(WORD)) )
104 goto broken;
106 /* read imported names table */
107 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
108 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
109 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
110 || (len != ne->ne_enttab - ne->ne_imptab) )
111 goto broken;
113 for (i=0; i < ne->ne_cmod; i++)
115 LPSTR module = &nametab[modtab[i]];
116 TRACE("modref: %.*s\n", module[0], &module[1]);
117 if (!(strncmp(&module[1], "KERNEL", module[0])))
118 { /* very old Windows file */
119 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
120 ret = BINARY_WIN16;
121 goto good;
125 broken:
126 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
128 good:
129 HeapFree( GetProcessHeap(), 0, modtab);
130 HeapFree( GetProcessHeap(), 0, nametab);
131 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
132 return ret;
135 /***********************************************************************
136 * MODULE_GetBinaryType
138 enum binary_type MODULE_GetBinaryType( HANDLE hfile )
140 union
142 struct
144 unsigned char magic[4];
145 unsigned char ignored[12];
146 unsigned short type;
147 } elf;
148 IMAGE_DOS_HEADER mz;
149 } header;
151 char magic[4];
152 DWORD len;
154 /* Seek to the start of the file and read the header information. */
155 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
156 return BINARY_UNKNOWN;
157 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
158 return BINARY_UNKNOWN;
160 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
162 /* FIXME: we don't bother to check byte order, architecture, etc. */
163 switch(header.elf.type)
165 case 2: return BINARY_UNIX_EXE;
166 case 3: return BINARY_UNIX_LIB;
168 return BINARY_UNKNOWN;
171 /* Not ELF, try DOS */
173 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
175 /* We do have a DOS image so we will now try to seek into
176 * the file by the amount indicated by the field
177 * "Offset to extended header" and read in the
178 * "magic" field information at that location.
179 * This will tell us if there is more header information
180 * to read or not.
182 /* But before we do we will make sure that header
183 * structure encompasses the "Offset to extended header"
184 * field.
186 if ((header.mz.e_cparhdr << 4) < sizeof(IMAGE_DOS_HEADER))
187 return BINARY_DOS;
188 if (header.mz.e_crlc && (header.mz.e_lfarlc < sizeof(IMAGE_DOS_HEADER)))
189 return BINARY_DOS;
190 if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER))
191 return BINARY_DOS;
192 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
193 return BINARY_DOS;
194 if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
195 return BINARY_DOS;
197 /* Reading the magic field succeeded so
198 * we will try to determine what type it is.
200 if (!memcmp( magic, "PE\0\0", 4 ))
202 IMAGE_FILE_HEADER FileHeader;
204 if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
206 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
207 return BINARY_PE_EXE;
209 return BINARY_DOS;
212 if (!memcmp( magic, "NE", 2 ))
214 /* This is a Windows executable (NE) header. This can
215 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
216 * DOS program (running under a DOS extender). To decide
217 * which, we'll have to read the NE header.
219 IMAGE_OS2_HEADER ne;
220 if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
221 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
222 && len == sizeof(ne) )
224 switch ( ne.ne_exetyp )
226 case 2: return BINARY_WIN16;
227 case 5: return BINARY_DOS;
228 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
231 /* Couldn't read header, so abort. */
232 return BINARY_DOS;
235 /* Unknown extended header, but this file is nonetheless DOS-executable. */
236 return BINARY_DOS;
239 return BINARY_UNKNOWN;
242 /***********************************************************************
243 * GetBinaryTypeA [KERNEL32.@]
244 * GetBinaryType [KERNEL32.@]
246 * The GetBinaryType function determines whether a file is executable
247 * or not and if it is it returns what type of executable it is.
248 * The type of executable is a property that determines in which
249 * subsystem an executable file runs under.
251 * Binary types returned:
252 * SCS_32BIT_BINARY: A Win32 based application
253 * SCS_DOS_BINARY: An MS-Dos based application
254 * SCS_WOW_BINARY: A Win16 based application
255 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
256 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
257 * SCS_OS216_BINARY: A 16bit OS/2 based application
259 * Returns TRUE if the file is an executable in which case
260 * the value pointed by lpBinaryType is set.
261 * Returns FALSE if the file is not an executable or if the function fails.
263 * To do so it opens the file and reads in the header information
264 * if the extended header information is not present it will
265 * assume that the file is a DOS executable.
266 * If the extended header information is present it will
267 * determine if the file is a 16 or 32 bit Windows executable
268 * by check the flags in the header.
270 * Note that .COM and .PIF files are only recognized by their
271 * file name extension; but Windows does it the same way ...
273 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
275 BOOL ret = FALSE;
276 HANDLE hfile;
277 char *ptr;
279 TRACE_(win32)("%s\n", lpApplicationName );
281 /* Sanity check.
283 if ( lpApplicationName == NULL || lpBinaryType == NULL )
284 return FALSE;
286 /* Open the file indicated by lpApplicationName for reading.
288 hfile = CreateFileA( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
289 NULL, OPEN_EXISTING, 0, 0 );
290 if ( hfile == INVALID_HANDLE_VALUE )
291 return FALSE;
293 /* Check binary type
295 switch(MODULE_GetBinaryType( hfile ))
297 case BINARY_UNKNOWN:
298 /* try to determine from file name */
299 ptr = strrchr( lpApplicationName, '.' );
300 if (!ptr) break;
301 if (!FILE_strcasecmp( ptr, ".COM" ))
303 *lpBinaryType = SCS_DOS_BINARY;
304 ret = TRUE;
306 else if (!FILE_strcasecmp( ptr, ".PIF" ))
308 *lpBinaryType = SCS_PIF_BINARY;
309 ret = TRUE;
311 break;
312 case BINARY_PE_EXE:
313 case BINARY_PE_DLL:
314 *lpBinaryType = SCS_32BIT_BINARY;
315 ret = TRUE;
316 break;
317 case BINARY_WIN16:
318 *lpBinaryType = SCS_WOW_BINARY;
319 ret = TRUE;
320 break;
321 case BINARY_OS216:
322 *lpBinaryType = SCS_OS216_BINARY;
323 ret = TRUE;
324 break;
325 case BINARY_DOS:
326 *lpBinaryType = SCS_DOS_BINARY;
327 ret = TRUE;
328 break;
329 case BINARY_UNIX_EXE:
330 case BINARY_UNIX_LIB:
331 ret = FALSE;
332 break;
335 CloseHandle( hfile );
336 return ret;
339 /***********************************************************************
340 * GetBinaryTypeW [KERNEL32.@]
342 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
344 BOOL ret = FALSE;
345 LPSTR strNew = NULL;
347 TRACE_(win32)("%s\n", debugstr_w(lpApplicationName) );
349 /* Sanity check.
351 if ( lpApplicationName == NULL || lpBinaryType == NULL )
352 return FALSE;
354 /* Convert the wide string to a ascii string.
356 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
358 if ( strNew != NULL )
360 ret = GetBinaryTypeA( strNew, lpBinaryType );
362 /* Free the allocated string.
364 HeapFree( GetProcessHeap(), 0, strNew );
367 return ret;
371 /***********************************************************************
372 * WinExec (KERNEL32.@)
374 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
376 PROCESS_INFORMATION info;
377 STARTUPINFOA startup;
378 char *cmdline;
379 UINT ret;
381 memset( &startup, 0, sizeof(startup) );
382 startup.cb = sizeof(startup);
383 startup.dwFlags = STARTF_USESHOWWINDOW;
384 startup.wShowWindow = nCmdShow;
386 /* cmdline needs to be writeable for CreateProcess */
387 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
388 strcpy( cmdline, lpCmdLine );
390 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
391 0, NULL, NULL, &startup, &info ))
393 /* Give 30 seconds to the app to come up */
394 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF)
395 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
396 ret = 33;
397 /* Close off the handles */
398 CloseHandle( info.hThread );
399 CloseHandle( info.hProcess );
401 else if ((ret = GetLastError()) >= 32)
403 FIXME("Strange error set by CreateProcess: %d\n", ret );
404 ret = 11;
406 HeapFree( GetProcessHeap(), 0, cmdline );
407 return ret;
410 /**********************************************************************
411 * LoadModule (KERNEL32.@)
413 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
415 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
416 PROCESS_INFORMATION info;
417 STARTUPINFOA startup;
418 HINSTANCE hInstance;
419 LPSTR cmdline, p;
420 char filename[MAX_PATH];
421 BYTE len;
423 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
425 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
426 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
427 return (HINSTANCE)GetLastError();
429 len = (BYTE)params->lpCmdLine[0];
430 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
431 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
433 strcpy( cmdline, filename );
434 p = cmdline + strlen(cmdline);
435 *p++ = ' ';
436 memcpy( p, params->lpCmdLine + 1, len );
437 p[len] = 0;
439 memset( &startup, 0, sizeof(startup) );
440 startup.cb = sizeof(startup);
441 if (params->lpCmdShow)
443 startup.dwFlags = STARTF_USESHOWWINDOW;
444 startup.wShowWindow = params->lpCmdShow[1];
447 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
448 params->lpEnvAddress, NULL, &startup, &info ))
450 /* Give 30 seconds to the app to come up */
451 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF )
452 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
453 hInstance = (HINSTANCE)33;
454 /* Close off the handles */
455 CloseHandle( info.hThread );
456 CloseHandle( info.hProcess );
458 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
460 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
461 hInstance = (HINSTANCE)11;
464 HeapFree( GetProcessHeap(), 0, cmdline );
465 return hInstance;
469 /***********************************************************************
470 * GetModuleHandleA (KERNEL32.@)
471 * GetModuleHandle32 (KERNEL.488)
473 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
475 NTSTATUS nts;
476 HMODULE ret;
477 UNICODE_STRING wstr;
479 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
481 RtlCreateUnicodeStringFromAsciiz(&wstr, module);
482 nts = LdrGetDllHandle(0, 0, &wstr, &ret);
483 RtlFreeUnicodeString( &wstr );
484 if (nts != STATUS_SUCCESS)
486 ret = 0;
487 SetLastError( RtlNtStatusToDosError( nts ) );
489 return ret;
492 /***********************************************************************
493 * GetModuleHandleW (KERNEL32.@)
495 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
497 NTSTATUS nts;
498 HMODULE ret;
499 UNICODE_STRING wstr;
501 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
503 RtlInitUnicodeString( &wstr, module );
504 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
505 if (nts != STATUS_SUCCESS)
507 SetLastError( RtlNtStatusToDosError( nts ) );
508 ret = 0;
510 return ret;
514 /***********************************************************************
515 * GetModuleFileNameA (KERNEL32.@)
516 * GetModuleFileName32 (KERNEL.487)
518 * GetModuleFileNameA seems to *always* return the long path;
519 * it's only GetModuleFileName16 that decides between short/long path
520 * by checking if exe version >= 4.0.
521 * (SDK docu doesn't mention this)
523 DWORD WINAPI GetModuleFileNameA(
524 HMODULE hModule, /* [in] module handle (32bit) */
525 LPSTR lpFileName, /* [out] filenamebuffer */
526 DWORD size ) /* [in] size of filenamebuffer */
528 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
530 if (!filenameW)
532 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
533 return 0;
535 GetModuleFileNameW( hModule, filenameW, size );
536 WideCharToMultiByte( CP_ACP, 0, filenameW, -1, lpFileName, size, NULL, NULL );
537 HeapFree( GetProcessHeap(), 0, filenameW );
538 return strlen( lpFileName );
541 /***********************************************************************
542 * GetModuleFileNameW (KERNEL32.@)
544 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
546 ULONG magic;
548 lpFileName[0] = 0;
550 LdrLockLoaderLock( 0, NULL, &magic );
551 if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32))
553 /* 16-bit task - get current NE module name */
554 NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() );
555 if (pModule)
557 WCHAR path[MAX_PATH];
559 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
560 GetLongPathNameW(path, lpFileName, size);
563 else
565 LDR_MODULE* pldr;
566 NTSTATUS nts;
568 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
569 nts = LdrFindEntryForAddress( hModule, &pldr );
570 if (nts == STATUS_SUCCESS) lstrcpynW(lpFileName, pldr->FullDllName.Buffer, size);
571 else SetLastError( RtlNtStatusToDosError( nts ) );
574 LdrUnlockLoaderLock( 0, magic );
576 TRACE( "%s\n", debugstr_w(lpFileName) );
577 return strlenW(lpFileName);
580 /******************************************************************
581 * load_library_as_datafile
583 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
585 static const WCHAR dotDLL[] = {'.','d','l','l',0};
587 WCHAR filenameW[MAX_PATH];
588 HANDLE hFile = INVALID_HANDLE_VALUE;
589 HANDLE mapping;
591 *hmod = 0;
593 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
594 filenameW, NULL ))
596 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
597 NULL, OPEN_EXISTING, 0, 0 );
599 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
600 switch (MODULE_GetBinaryType( hFile ))
602 case BINARY_PE_EXE:
603 case BINARY_PE_DLL:
604 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
605 if (mapping)
607 *hmod = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
608 CloseHandle( mapping );
610 break;
611 default:
612 break;
614 CloseHandle( hFile );
616 return *hmod != 0;
619 /******************************************************************
620 * LoadLibraryExA (KERNEL32.@)
622 * The HFILE parameter is not used and marked reserved in the SDK. I can
623 * only guess that it should force a file to be mapped, but I rather
624 * ignore the parameter because it would be extremely difficult to
625 * integrate this with different types of module representations.
628 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
630 UNICODE_STRING wstr;
631 NTSTATUS nts;
632 HMODULE hModule;
634 if (!libname)
636 SetLastError(ERROR_INVALID_PARAMETER);
637 return 0;
639 RtlCreateUnicodeStringFromAsciiz( &wstr, libname );
641 if (flags & LOAD_LIBRARY_AS_DATAFILE)
643 /* The method in load_library_as_datafile allows searching for the
644 * 'native' libraries only
646 if (load_library_as_datafile( wstr.Buffer, &hModule))
648 RtlFreeUnicodeString( &wstr );
649 return (HMODULE)((ULONG_PTR)hModule + 1);
651 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
652 /* Fallback to normal behaviour */
655 nts = LdrLoadDll(NULL, flags, &wstr, &hModule);
656 if (nts != STATUS_SUCCESS)
658 hModule = 0;
659 SetLastError( RtlNtStatusToDosError( nts ) );
661 RtlFreeUnicodeString( &wstr );
663 return hModule;
666 /***********************************************************************
667 * LoadLibraryExW (KERNEL32.@)
669 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
671 UNICODE_STRING wstr;
672 NTSTATUS nts;
673 HMODULE hModule;
675 if (!libnameW)
677 SetLastError(ERROR_INVALID_PARAMETER);
678 return 0;
681 if (flags & LOAD_LIBRARY_AS_DATAFILE)
683 /* The method in load_library_as_datafile allows searching for the
684 * 'native' libraries only
686 if (load_library_as_datafile(libnameW, &hModule))
687 return (HMODULE)((ULONG_PTR)hModule + 1);
688 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
689 /* Fallback to normal behaviour */
692 RtlInitUnicodeString( &wstr, libnameW );
693 nts = LdrLoadDll(NULL, flags, &wstr, &hModule);
694 if (nts != STATUS_SUCCESS)
696 hModule = 0;
697 SetLastError( RtlNtStatusToDosError( nts ) );
699 return hModule;
702 /***********************************************************************
703 * LoadLibraryA (KERNEL32.@)
705 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
707 return LoadLibraryExA(libname, 0, 0);
710 /***********************************************************************
711 * LoadLibraryW (KERNEL32.@)
713 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
715 return LoadLibraryExW(libnameW, 0, 0);
718 /***********************************************************************
719 * LoadLibrary32 (KERNEL.452)
720 * LoadSystemLibrary32 (KERNEL.482)
722 HMODULE WINAPI LoadLibrary32_16( LPCSTR libname )
724 HMODULE hModule;
725 DWORD count;
727 ReleaseThunkLock( &count );
728 hModule = LoadLibraryA( libname );
729 RestoreThunkLock( count );
730 return hModule;
733 /***********************************************************************
734 * FreeLibrary (KERNEL32.@)
735 * FreeLibrary32 (KERNEL.486)
737 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
739 BOOL retv = FALSE;
740 NTSTATUS nts;
742 if (!hLibModule)
744 SetLastError( ERROR_INVALID_HANDLE );
745 return FALSE;
748 if ((ULONG_PTR)hLibModule & 1)
750 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
751 char *ptr = (char *)hLibModule - 1;
752 UnmapViewOfFile( ptr );
753 return TRUE;
756 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
757 else SetLastError( RtlNtStatusToDosError( nts ) );
759 return retv;
762 /***********************************************************************
763 * FreeLibraryAndExitThread (KERNEL32.@)
765 VOID WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
767 FreeLibrary(hLibModule);
768 ExitThread(dwExitCode);
771 /***********************************************************************
772 * PrivateLoadLibrary (KERNEL32.@)
774 * FIXME: rough guesswork, don't know what "Private" means
776 HINSTANCE16 WINAPI PrivateLoadLibrary(LPCSTR libname)
778 return LoadLibrary16(libname);
781 /***********************************************************************
782 * PrivateFreeLibrary (KERNEL32.@)
784 * FIXME: rough guesswork, don't know what "Private" means
786 void WINAPI PrivateFreeLibrary(HINSTANCE16 handle)
788 FreeLibrary16(handle);
792 /***********************************************************************
793 * GetProcAddress (KERNEL32.@)
795 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
797 NTSTATUS nts;
798 FARPROC fp;
800 if (HIWORD(function))
802 ANSI_STRING str;
804 RtlInitAnsiString( &str, function );
805 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
807 else
808 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
809 if (nts != STATUS_SUCCESS)
811 SetLastError( RtlNtStatusToDosError( nts ) );
812 fp = NULL;
814 return fp;
817 /***********************************************************************
818 * GetProcAddress32 (KERNEL.453)
820 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
822 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
823 return GetProcAddress( hModule, function );