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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
29 #include <sys/types.h>
34 #define WIN32_NO_STATUS
39 #include "kernel_private.h"
42 #include "wine/exception.h"
43 #include "wine/list.h"
44 #include "wine/debug.h"
45 #include "wine/unicode.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(module
);
49 #define NE_FFLAGS_LIBMODULE 0x8000
57 static struct list dll_dir_list
= LIST_INIT( dll_dir_list
); /* extra dirs from AddDllDirectory */
58 static WCHAR
*dll_directory
; /* extra path for SetDllDirectoryW */
59 static DWORD default_search_flags
; /* default flags set by SetDefaultDllDirectories */
61 /* to keep track of LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE file handles */
62 struct exclusive_datafile
68 static struct list exclusive_datafile_list
= LIST_INIT( exclusive_datafile_list
);
70 static CRITICAL_SECTION dlldir_section
;
71 static CRITICAL_SECTION_DEBUG critsect_debug
=
73 0, 0, &dlldir_section
,
74 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
75 0, 0, { (DWORD_PTR
)(__FILE__
": dlldir_section") }
77 static CRITICAL_SECTION dlldir_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
79 /****************************************************************************
80 * GetDllDirectoryA (KERNEL32.@)
82 DWORD WINAPI
GetDllDirectoryA( DWORD buf_len
, LPSTR buffer
)
86 RtlEnterCriticalSection( &dlldir_section
);
87 len
= dll_directory
? FILE_name_WtoA( dll_directory
, strlenW(dll_directory
), NULL
, 0 ) : 0;
88 if (buffer
&& buf_len
> len
)
90 if (dll_directory
) FILE_name_WtoA( dll_directory
, -1, buffer
, buf_len
);
95 len
++; /* for terminating null */
96 if (buffer
) *buffer
= 0;
98 RtlLeaveCriticalSection( &dlldir_section
);
103 /****************************************************************************
104 * GetDllDirectoryW (KERNEL32.@)
106 DWORD WINAPI
GetDllDirectoryW( DWORD buf_len
, LPWSTR buffer
)
110 RtlEnterCriticalSection( &dlldir_section
);
111 len
= dll_directory
? strlenW( dll_directory
) : 0;
112 if (buffer
&& buf_len
> len
)
114 if (dll_directory
) memcpy( buffer
, dll_directory
, (len
+ 1) * sizeof(WCHAR
) );
119 len
++; /* for terminating null */
120 if (buffer
) *buffer
= 0;
122 RtlLeaveCriticalSection( &dlldir_section
);
127 /****************************************************************************
128 * SetDllDirectoryA (KERNEL32.@)
130 BOOL WINAPI
SetDllDirectoryA( LPCSTR dir
)
135 if (!(dirW
= FILE_name_AtoW( dir
, TRUE
))) return FALSE
;
136 ret
= SetDllDirectoryW( dirW
);
137 HeapFree( GetProcessHeap(), 0, dirW
);
142 /****************************************************************************
143 * SetDllDirectoryW (KERNEL32.@)
145 BOOL WINAPI
SetDllDirectoryW( LPCWSTR dir
)
147 WCHAR
*newdir
= NULL
;
151 DWORD len
= (strlenW(dir
) + 1) * sizeof(WCHAR
);
152 if (!(newdir
= HeapAlloc( GetProcessHeap(), 0, len
)))
154 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
157 memcpy( newdir
, dir
, len
);
160 RtlEnterCriticalSection( &dlldir_section
);
161 HeapFree( GetProcessHeap(), 0, dll_directory
);
162 dll_directory
= newdir
;
163 RtlLeaveCriticalSection( &dlldir_section
);
168 /****************************************************************************
169 * AddDllDirectory (KERNEL32.@)
171 DLL_DIRECTORY_COOKIE WINAPI
AddDllDirectory( const WCHAR
*dir
)
173 WCHAR path
[MAX_PATH
];
175 struct dll_dir_entry
*ptr
;
176 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U( dir
);
178 if (type
!= ABSOLUTE_PATH
&& type
!= ABSOLUTE_DRIVE_PATH
)
180 SetLastError( ERROR_INVALID_PARAMETER
);
183 if (!(len
= GetFullPathNameW( dir
, MAX_PATH
, path
, NULL
))) return NULL
;
184 if (GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
) return NULL
;
186 if (!(ptr
= HeapAlloc( GetProcessHeap(), 0, offsetof(struct dll_dir_entry
, dir
[++len
] )))) return NULL
;
187 memcpy( ptr
->dir
, path
, len
* sizeof(WCHAR
) );
188 TRACE( "%s\n", debugstr_w( ptr
->dir
));
190 RtlEnterCriticalSection( &dlldir_section
);
191 list_add_head( &dll_dir_list
, &ptr
->entry
);
192 RtlLeaveCriticalSection( &dlldir_section
);
197 /****************************************************************************
198 * RemoveDllDirectory (KERNEL32.@)
200 BOOL WINAPI
RemoveDllDirectory( DLL_DIRECTORY_COOKIE cookie
)
202 struct dll_dir_entry
*ptr
= cookie
;
204 TRACE( "%s\n", debugstr_w( ptr
->dir
));
206 RtlEnterCriticalSection( &dlldir_section
);
207 list_remove( &ptr
->entry
);
208 HeapFree( GetProcessHeap(), 0, ptr
);
209 RtlLeaveCriticalSection( &dlldir_section
);
214 /*************************************************************************
215 * SetDefaultDllDirectories (KERNEL32.@)
217 BOOL WINAPI
SetDefaultDllDirectories( DWORD flags
)
219 /* LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR doesn't make sense in default dirs */
220 const DWORD load_library_search_flags
= (LOAD_LIBRARY_SEARCH_APPLICATION_DIR
|
221 LOAD_LIBRARY_SEARCH_USER_DIRS
|
222 LOAD_LIBRARY_SEARCH_SYSTEM32
|
223 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
);
225 if (!flags
|| (flags
& ~load_library_search_flags
))
227 SetLastError( ERROR_INVALID_PARAMETER
);
230 default_search_flags
= flags
;
235 /****************************************************************************
236 * DisableThreadLibraryCalls (KERNEL32.@)
238 * Inform the module loader that thread notifications are not required for a dll.
241 * hModule [I] Module handle to skip calls for
244 * Success: TRUE. Thread attach and detach notifications will not be sent
246 * Failure: FALSE. Use GetLastError() to determine the cause.
249 * This is typically called from the dll entry point of a dll during process
250 * attachment, for dlls that do not need to process thread notifications.
252 BOOL WINAPI
DisableThreadLibraryCalls( HMODULE hModule
)
254 NTSTATUS nts
= LdrDisableThreadCalloutsForDll( hModule
);
255 if (nts
== STATUS_SUCCESS
) return TRUE
;
257 SetLastError( RtlNtStatusToDosError( nts
) );
262 /***********************************************************************
263 * MODULE_GetBinaryType
265 void MODULE_get_binary_info( HANDLE hfile
, struct binary_info
*info
)
271 unsigned char magic
[4];
274 unsigned char ignored1
[10];
276 unsigned short machine
;
277 unsigned char ignored2
[8];
279 unsigned char ignored3
[12];
280 unsigned short phnum
;
284 unsigned char magic
[4];
287 unsigned char ignored1
[10];
289 unsigned short machine
;
290 unsigned char ignored2
[12];
291 unsigned __int64 phoff
;
292 unsigned char ignored3
[16];
293 unsigned short phnum
;
298 unsigned int cputype
;
299 unsigned int cpusubtype
;
300 unsigned int filetype
;
307 memset( info
, 0, sizeof(*info
) );
309 /* Seek to the start of the file and read the header information. */
310 if (SetFilePointer( hfile
, 0, NULL
, SEEK_SET
) == -1) return;
311 if (!ReadFile( hfile
, &header
, sizeof(header
), &len
, NULL
) || len
!= sizeof(header
)) return;
313 if (!memcmp( header
.elf
.magic
, "\177ELF", 4 ))
315 #ifdef WORDS_BIGENDIAN
316 BOOL byteswap
= (header
.elf
.data
== 1);
318 BOOL byteswap
= (header
.elf
.data
== 2);
320 if (header
.elf
.class == 2) info
->flags
|= BINARY_FLAG_64BIT
;
323 header
.elf
.type
= RtlUshortByteSwap( header
.elf
.type
);
324 header
.elf
.machine
= RtlUshortByteSwap( header
.elf
.machine
);
326 switch(header
.elf
.type
)
329 info
->type
= BINARY_UNIX_EXE
;
334 unsigned short phnum
;
336 if (header
.elf
.class == 2)
338 phoff
.QuadPart
= byteswap
? RtlUlonglongByteSwap( header
.elf64
.phoff
) : header
.elf64
.phoff
;
339 phnum
= byteswap
? RtlUshortByteSwap( header
.elf64
.phnum
) : header
.elf64
.phnum
;
343 phoff
.QuadPart
= byteswap
? RtlUlongByteSwap( header
.elf
.phoff
) : header
.elf
.phoff
;
344 phnum
= byteswap
? RtlUshortByteSwap( header
.elf
.phnum
) : header
.elf
.phnum
;
348 if (SetFilePointerEx( hfile
, phoff
, NULL
, FILE_BEGIN
) == -1) return;
349 if (!ReadFile( hfile
, &type
, sizeof(type
), &len
, NULL
) || len
< sizeof(type
)) return;
350 if (byteswap
) type
= RtlUlongByteSwap( type
);
353 info
->type
= BINARY_UNIX_EXE
;
356 phoff
.QuadPart
+= (header
.elf
.class == 2) ? 56 : 32;
358 if (!info
->type
) info
->type
= BINARY_UNIX_LIB
;
364 switch(header
.elf
.machine
)
366 case 3: info
->arch
= IMAGE_FILE_MACHINE_I386
; break;
367 case 20: info
->arch
= IMAGE_FILE_MACHINE_POWERPC
; break;
368 case 40: info
->arch
= IMAGE_FILE_MACHINE_ARMNT
; break;
369 case 50: info
->arch
= IMAGE_FILE_MACHINE_IA64
; break;
370 case 62: info
->arch
= IMAGE_FILE_MACHINE_AMD64
; break;
371 case 183: info
->arch
= IMAGE_FILE_MACHINE_ARM64
; break;
374 /* Mach-o File with Endian set to Big Endian or Little Endian */
375 else if (header
.macho
.magic
== 0xfeedface || header
.macho
.magic
== 0xcefaedfe ||
376 header
.macho
.magic
== 0xfeedfacf || header
.macho
.magic
== 0xcffaedfe)
378 if ((header
.macho
.cputype
>> 24) == 1) info
->flags
|= BINARY_FLAG_64BIT
;
379 if (header
.macho
.magic
== 0xcefaedfe || header
.macho
.magic
== 0xcffaedfe)
381 header
.macho
.filetype
= RtlUlongByteSwap( header
.macho
.filetype
);
382 header
.macho
.cputype
= RtlUlongByteSwap( header
.macho
.cputype
);
384 switch(header
.macho
.filetype
)
386 case 2: info
->type
= BINARY_UNIX_EXE
; break;
387 case 8: info
->type
= BINARY_UNIX_LIB
; break;
389 switch(header
.macho
.cputype
)
391 case 0x00000007: info
->arch
= IMAGE_FILE_MACHINE_I386
; break;
392 case 0x01000007: info
->arch
= IMAGE_FILE_MACHINE_AMD64
; break;
393 case 0x0000000c: info
->arch
= IMAGE_FILE_MACHINE_ARMNT
; break;
394 case 0x0100000c: info
->arch
= IMAGE_FILE_MACHINE_ARM64
; break;
395 case 0x00000012: info
->arch
= IMAGE_FILE_MACHINE_POWERPC
; break;
398 /* Not ELF, try DOS */
399 else if (header
.mz
.e_magic
== IMAGE_DOS_SIGNATURE
)
403 IMAGE_OS2_HEADER os2
;
404 IMAGE_NT_HEADERS32 nt
;
405 IMAGE_NT_HEADERS64 nt64
;
408 /* We do have a DOS image so we will now try to seek into
409 * the file by the amount indicated by the field
410 * "Offset to extended header" and read in the
411 * "magic" field information at that location.
412 * This will tell us if there is more header information
415 info
->type
= BINARY_WIN16
;
416 info
->arch
= IMAGE_FILE_MACHINE_I386
;
417 if (SetFilePointer( hfile
, header
.mz
.e_lfanew
, NULL
, SEEK_SET
) == -1) return;
418 if (!ReadFile( hfile
, &ext_header
, sizeof(ext_header
), &len
, NULL
) || len
< 4) return;
420 /* Reading the magic field succeeded so
421 * we will try to determine what type it is.
423 if (!memcmp( &ext_header
.nt
.Signature
, "PE\0\0", 4 ))
425 if (len
>= sizeof(ext_header
.nt
.FileHeader
))
427 static const char fakedll_signature
[] = "Wine placeholder DLL";
428 char buffer
[sizeof(fakedll_signature
)];
430 info
->type
= BINARY_PE
;
431 info
->arch
= ext_header
.nt
.FileHeader
.Machine
;
432 if (ext_header
.nt
.FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
433 info
->flags
|= BINARY_FLAG_DLL
;
434 if (len
< sizeof(ext_header
)) /* clear remaining part of header if missing */
435 memset( (char *)&ext_header
+ len
, 0, sizeof(ext_header
) - len
);
436 switch (ext_header
.nt
.OptionalHeader
.Magic
)
438 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
439 info
->res_start
= ext_header
.nt
.OptionalHeader
.ImageBase
;
440 info
->res_end
= info
->res_start
+ ext_header
.nt
.OptionalHeader
.SizeOfImage
;
442 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
443 info
->res_start
= ext_header
.nt64
.OptionalHeader
.ImageBase
;
444 info
->res_end
= info
->res_start
+ ext_header
.nt64
.OptionalHeader
.SizeOfImage
;
445 info
->flags
|= BINARY_FLAG_64BIT
;
449 if (header
.mz
.e_lfanew
>= sizeof(header
.mz
) + sizeof(fakedll_signature
) &&
450 SetFilePointer( hfile
, sizeof(header
.mz
), NULL
, SEEK_SET
) == sizeof(header
.mz
) &&
451 ReadFile( hfile
, buffer
, sizeof(fakedll_signature
), &len
, NULL
) &&
452 len
== sizeof(fakedll_signature
) &&
453 !memcmp( buffer
, fakedll_signature
, sizeof(fakedll_signature
) ))
455 info
->flags
|= BINARY_FLAG_FAKEDLL
;
462 /***********************************************************************
463 * GetBinaryTypeW [KERNEL32.@]
465 * Determine whether a file is executable, and if so, what kind.
468 * lpApplicationName [I] Path of the file to check
469 * lpBinaryType [O] Destination for the binary type
472 * TRUE, if the file is an executable, in which case lpBinaryType is set.
473 * FALSE, if the file is not an executable or if the function fails.
476 * The type of executable is a property that determines which subsystem an
477 * executable file runs under. lpBinaryType can be set to one of the following
479 * SCS_32BIT_BINARY: A Win32 based application
480 * SCS_64BIT_BINARY: A Win64 based application
481 * SCS_DOS_BINARY: An MS-Dos based application
482 * SCS_WOW_BINARY: A Win16 based application
483 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
484 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
485 * SCS_OS216_BINARY: A 16bit OS/2 based application
487 * To find the binary type, this function reads in the files header information.
488 * If extended header information is not present it will assume that the file
489 * is a DOS executable. If extended header information is present it will
490 * determine if the file is a 16, 32 or 64 bit Windows executable by checking the
491 * flags in the header.
493 * ".com" and ".pif" files are only recognized by their file name extension,
494 * as per native Windows.
496 BOOL WINAPI
GetBinaryTypeW( LPCWSTR name
, LPDWORD type
)
498 static const WCHAR comW
[] = { '.','c','o','m',0 };
499 static const WCHAR pifW
[] = { '.','p','i','f',0 };
500 HANDLE hfile
, mapping
;
504 TRACE("%s\n", debugstr_w(name
) );
506 if (type
== NULL
) return FALSE
;
508 hfile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
509 if ( hfile
== INVALID_HANDLE_VALUE
)
512 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
,
513 NULL
, NULL
, PAGE_READONLY
, SEC_IMAGE
, hfile
);
514 CloseHandle( hfile
);
520 SECTION_IMAGE_INFORMATION info
;
522 status
= NtQuerySection( mapping
, SectionImageInformation
, &info
, sizeof(info
), NULL
);
523 CloseHandle( mapping
);
524 if (status
) return FALSE
;
525 switch (info
.Machine
)
527 case IMAGE_FILE_MACHINE_I386
:
528 case IMAGE_FILE_MACHINE_ARM
:
529 case IMAGE_FILE_MACHINE_THUMB
:
530 case IMAGE_FILE_MACHINE_ARMNT
:
531 case IMAGE_FILE_MACHINE_POWERPC
:
532 *type
= SCS_32BIT_BINARY
;
534 case IMAGE_FILE_MACHINE_AMD64
:
535 case IMAGE_FILE_MACHINE_ARM64
:
536 *type
= SCS_64BIT_BINARY
;
541 case STATUS_INVALID_IMAGE_WIN_16
:
542 *type
= SCS_WOW_BINARY
;
544 case STATUS_INVALID_IMAGE_WIN_32
:
545 *type
= SCS_32BIT_BINARY
;
547 case STATUS_INVALID_IMAGE_WIN_64
:
548 *type
= SCS_64BIT_BINARY
;
550 case STATUS_INVALID_IMAGE_NE_FORMAT
:
551 *type
= SCS_OS216_BINARY
;
553 case STATUS_INVALID_IMAGE_PROTECT
:
554 *type
= SCS_DOS_BINARY
;
556 case STATUS_INVALID_IMAGE_NOT_MZ
:
557 if ((ptr
= strrchrW( name
, '.' )))
559 if (!strcmpiW( ptr
, comW
))
561 *type
= SCS_DOS_BINARY
;
564 if (!strcmpiW( ptr
, pifW
))
566 *type
= SCS_PIF_BINARY
;
576 /***********************************************************************
577 * GetBinaryTypeA [KERNEL32.@]
578 * GetBinaryType [KERNEL32.@]
580 * See GetBinaryTypeW.
582 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
584 ANSI_STRING app_nameA
;
587 TRACE("%s\n", debugstr_a(lpApplicationName
));
591 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
594 RtlInitAnsiString(&app_nameA
, lpApplicationName
);
595 status
= RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString
,
598 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString
.Buffer
, lpBinaryType
);
600 SetLastError(RtlNtStatusToDosError(status
));
604 /***********************************************************************
605 * GetModuleHandleExA (KERNEL32.@)
607 BOOL WINAPI
GetModuleHandleExA( DWORD flags
, LPCSTR name
, HMODULE
*module
)
611 if (!name
|| (flags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
))
612 return GetModuleHandleExW( flags
, (LPCWSTR
)name
, module
);
614 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return FALSE
;
615 return GetModuleHandleExW( flags
, nameW
, module
);
618 /***********************************************************************
619 * GetModuleHandleExW (KERNEL32.@)
621 BOOL WINAPI
GetModuleHandleExW( DWORD flags
, LPCWSTR name
, HMODULE
*module
)
623 NTSTATUS status
= STATUS_SUCCESS
;
630 SetLastError( ERROR_INVALID_PARAMETER
);
634 /* if we are messing with the refcount, grab the loader lock */
635 lock
= (flags
& GET_MODULE_HANDLE_EX_FLAG_PIN
) || !(flags
& GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
);
637 LdrLockLoaderLock( 0, NULL
, &magic
);
641 ret
= NtCurrentTeb()->Peb
->ImageBaseAddress
;
643 else if (flags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
)
646 if (!(ret
= RtlPcToFileHeader( (void *)name
, &dummy
))) status
= STATUS_DLL_NOT_FOUND
;
651 RtlInitUnicodeString( &wstr
, name
);
652 status
= LdrGetDllHandle( NULL
, 0, &wstr
, &ret
);
655 if (status
== STATUS_SUCCESS
)
657 if (flags
& GET_MODULE_HANDLE_EX_FLAG_PIN
)
658 LdrAddRefDll( LDR_ADDREF_DLL_PIN
, ret
);
659 else if (!(flags
& GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
))
660 LdrAddRefDll( 0, ret
);
662 else SetLastError( RtlNtStatusToDosError( status
) );
665 LdrUnlockLoaderLock( 0, magic
);
667 if (status
== STATUS_SUCCESS
) *module
= ret
;
670 return (status
== STATUS_SUCCESS
);
673 /***********************************************************************
674 * GetModuleHandleA (KERNEL32.@)
676 * Get the handle of a dll loaded into the process address space.
679 * module [I] Name of the dll
682 * Success: A handle to the loaded dll.
683 * Failure: A NULL handle. Use GetLastError() to determine the cause.
685 HMODULE WINAPI DECLSPEC_HOTPATCH
GetModuleHandleA(LPCSTR module
)
689 GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
, module
, &ret
);
693 /***********************************************************************
694 * GetModuleHandleW (KERNEL32.@)
696 * Unicode version of GetModuleHandleA.
698 HMODULE WINAPI
GetModuleHandleW(LPCWSTR module
)
702 GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
, module
, &ret
);
707 /***********************************************************************
708 * GetModuleFileNameA (KERNEL32.@)
710 * Get the file name of a loaded module from its handle.
713 * Success: The length of the file name, excluding the terminating NUL.
714 * Failure: 0. Use GetLastError() to determine the cause.
717 * This function always returns the long path of hModule
718 * The function doesn't write a terminating '\0' if the buffer is too
721 DWORD WINAPI
GetModuleFileNameA(
722 HMODULE hModule
, /* [in] Module handle (32 bit) */
723 LPSTR lpFileName
, /* [out] Destination for file name */
724 DWORD size
) /* [in] Size of lpFileName in characters */
726 LPWSTR filenameW
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) );
731 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
734 if ((len
= GetModuleFileNameW( hModule
, filenameW
, size
)))
736 len
= FILE_name_WtoA( filenameW
, len
, lpFileName
, size
);
738 lpFileName
[len
] = '\0';
740 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
742 HeapFree( GetProcessHeap(), 0, filenameW
);
746 /***********************************************************************
747 * GetModuleFileNameW (KERNEL32.@)
749 * Unicode version of GetModuleFileNameA.
751 DWORD WINAPI
GetModuleFileNameW( HMODULE hModule
, LPWSTR lpFileName
, DWORD size
)
757 WIN16_SUBSYSTEM_TIB
*win16_tib
;
759 if (!hModule
&& ((win16_tib
= NtCurrentTeb()->Tib
.SubSystemTib
)) && win16_tib
->exe_name
)
761 len
= min(size
, win16_tib
->exe_name
->Length
/ sizeof(WCHAR
));
762 memcpy( lpFileName
, win16_tib
->exe_name
->Buffer
, len
* sizeof(WCHAR
) );
763 if (len
< size
) lpFileName
[len
] = '\0';
767 LdrLockLoaderLock( 0, NULL
, &magic
);
769 if (!hModule
) hModule
= NtCurrentTeb()->Peb
->ImageBaseAddress
;
770 nts
= LdrFindEntryForAddress( hModule
, &pldr
);
771 if (nts
== STATUS_SUCCESS
)
773 len
= min(size
, pldr
->FullDllName
.Length
/ sizeof(WCHAR
));
774 memcpy(lpFileName
, pldr
->FullDllName
.Buffer
, len
* sizeof(WCHAR
));
777 lpFileName
[len
] = '\0';
781 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
783 else SetLastError( RtlNtStatusToDosError( nts
) );
785 LdrUnlockLoaderLock( 0, magic
);
787 TRACE( "%s\n", debugstr_wn(lpFileName
, len
) );
792 /***********************************************************************
793 * get_dll_system_path
795 static const WCHAR
*get_dll_system_path(void)
797 static WCHAR
*cached_path
;
804 len
+= 2 * GetSystemDirectoryW( NULL
, 0 );
805 len
+= GetWindowsDirectoryW( NULL
, 0 );
806 p
= path
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
807 GetSystemDirectoryW( p
, path
+ len
- p
);
809 /* if system directory ends in "32" add 16-bit version too */
810 if (p
[-2] == '3' && p
[-1] == '2')
813 GetSystemDirectoryW( p
, path
+ len
- p
);
817 GetWindowsDirectoryW( p
, path
+ len
- p
);
823 /***********************************************************************
826 static BOOL
get_dll_safe_mode(void)
828 static const WCHAR keyW
[] = {'\\','R','e','g','i','s','t','r','y','\\',
829 'M','a','c','h','i','n','e','\\',
830 'S','y','s','t','e','m','\\',
831 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
832 'C','o','n','t','r','o','l','\\',
833 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
834 static const WCHAR valueW
[] = {'S','a','f','e','D','l','l','S','e','a','r','c','h','M','o','d','e',0};
836 static int safe_mode
= -1;
840 char buffer
[offsetof(KEY_VALUE_PARTIAL_INFORMATION
, Data
[sizeof(DWORD
)])];
841 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
842 OBJECT_ATTRIBUTES attr
;
843 UNICODE_STRING nameW
;
845 DWORD size
= sizeof(buffer
);
847 attr
.Length
= sizeof(attr
);
848 attr
.RootDirectory
= 0;
849 attr
.ObjectName
= &nameW
;
851 attr
.SecurityDescriptor
= NULL
;
852 attr
.SecurityQualityOfService
= NULL
;
855 RtlInitUnicodeString( &nameW
, keyW
);
856 if (!NtOpenKey( &hkey
, KEY_READ
, &attr
))
858 RtlInitUnicodeString( &nameW
, valueW
);
859 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, buffer
, size
, &size
) &&
860 info
->Type
== REG_DWORD
&& info
->DataLength
== sizeof(DWORD
))
861 safe_mode
= !!*(DWORD
*)info
->Data
;
864 if (!safe_mode
) TRACE( "SafeDllSearchMode disabled through the registry\n" );
869 /******************************************************************
870 * get_module_path_end
872 * Returns the end of the directory component of the module path.
874 static inline const WCHAR
*get_module_path_end(const WCHAR
*module
)
877 const WCHAR
*mod_end
= module
;
878 if (!module
) return mod_end
;
880 if ((p
= strrchrW( mod_end
, '\\' ))) mod_end
= p
;
881 if ((p
= strrchrW( mod_end
, '/' ))) mod_end
= p
;
882 if (mod_end
== module
+ 2 && module
[1] == ':') mod_end
++;
883 if (mod_end
== module
&& module
[0] && module
[1] == ':') mod_end
+= 2;
889 /******************************************************************
892 * Append a counted string to the load path. Helper for MODULE_get_dll_load_path.
894 static inline WCHAR
*append_path_len( WCHAR
*p
, const WCHAR
*str
, DWORD len
)
897 memcpy( p
, str
, len
* sizeof(WCHAR
) );
903 /******************************************************************
906 * Append a string to the load path. Helper for MODULE_get_dll_load_path.
908 static inline WCHAR
*append_path( WCHAR
*p
, const WCHAR
*str
)
910 return append_path_len( p
, str
, strlenW(str
) );
914 /******************************************************************
915 * MODULE_get_dll_load_path
917 * Compute the load path to use for a given dll.
918 * Returned pointer must be freed by caller.
920 WCHAR
*MODULE_get_dll_load_path( LPCWSTR module
, int safe_mode
)
922 static const WCHAR pathW
[] = {'P','A','T','H',0};
923 static const WCHAR dotW
[] = {'.',0};
925 const WCHAR
*system_path
= get_dll_system_path();
926 const WCHAR
*mod_end
= NULL
;
927 UNICODE_STRING name
, value
;
929 int len
= 0, path_len
= 0;
931 /* adjust length for module name */
934 mod_end
= get_module_path_end( module
);
935 /* if module is NULL or doesn't contain a path, fall back to directory
936 * process was loaded from */
937 if (module
== mod_end
)
939 module
= NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
.Buffer
;
940 mod_end
= get_module_path_end( module
);
942 len
+= (mod_end
- module
) + 1;
944 len
+= strlenW( system_path
) + 2;
946 /* get the PATH variable */
948 RtlInitUnicodeString( &name
, pathW
);
950 value
.MaximumLength
= 0;
952 if (RtlQueryEnvironmentVariable_U( NULL
, &name
, &value
) == STATUS_BUFFER_TOO_SMALL
)
953 path_len
= value
.Length
;
955 RtlEnterCriticalSection( &dlldir_section
);
956 if (safe_mode
== -1) safe_mode
= get_dll_safe_mode();
957 if (dll_directory
) len
+= strlenW(dll_directory
) + 1;
958 else len
+= 2; /* current directory */
959 if ((p
= ret
= HeapAlloc( GetProcessHeap(), 0, path_len
+ len
* sizeof(WCHAR
) )))
961 if (module
) p
= append_path_len( p
, module
, mod_end
- module
);
963 if (dll_directory
) p
= append_path( p
, dll_directory
);
964 else if (!safe_mode
) p
= append_path( p
, dotW
);
966 p
= append_path( p
, system_path
);
968 if (!dll_directory
&& safe_mode
) p
= append_path( p
, dotW
);
970 RtlLeaveCriticalSection( &dlldir_section
);
971 if (!ret
) return NULL
;
974 value
.MaximumLength
= path_len
;
976 while (RtlQueryEnvironmentVariable_U( NULL
, &name
, &value
) == STATUS_BUFFER_TOO_SMALL
)
980 /* grow the buffer and retry */
981 path_len
= value
.Length
;
982 if (!(new_ptr
= HeapReAlloc( GetProcessHeap(), 0, ret
, path_len
+ len
* sizeof(WCHAR
) )))
984 HeapFree( GetProcessHeap(), 0, ret
);
987 value
.Buffer
= new_ptr
+ (value
.Buffer
- ret
);
988 value
.MaximumLength
= path_len
;
991 value
.Buffer
[value
.Length
/ sizeof(WCHAR
)] = 0;
996 /******************************************************************
997 * get_dll_load_path_search_flags
999 static WCHAR
*get_dll_load_path_search_flags( LPCWSTR module
, DWORD flags
)
1001 const WCHAR
*image
= NULL
, *mod_end
, *image_end
;
1002 struct dll_dir_entry
*dir
;
1006 if (flags
& LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
)
1007 flags
|= (LOAD_LIBRARY_SEARCH_APPLICATION_DIR
|
1008 LOAD_LIBRARY_SEARCH_USER_DIRS
|
1009 LOAD_LIBRARY_SEARCH_SYSTEM32
);
1011 if (flags
& LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
)
1013 DWORD type
= RtlDetermineDosPathNameType_U( module
);
1014 if (type
!= ABSOLUTE_DRIVE_PATH
&& type
!= ABSOLUTE_PATH
)
1016 SetLastError( ERROR_INVALID_PARAMETER
);
1019 mod_end
= get_module_path_end( module
);
1020 len
+= (mod_end
- module
) + 1;
1024 RtlEnterCriticalSection( &dlldir_section
);
1026 if (flags
& LOAD_LIBRARY_SEARCH_APPLICATION_DIR
)
1028 image
= NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
.Buffer
;
1029 image_end
= get_module_path_end( image
);
1030 len
+= (image_end
- image
) + 1;
1033 if (flags
& LOAD_LIBRARY_SEARCH_USER_DIRS
)
1035 LIST_FOR_EACH_ENTRY( dir
, &dll_dir_list
, struct dll_dir_entry
, entry
)
1036 len
+= strlenW( dir
->dir
) + 1;
1037 if (dll_directory
) len
+= strlenW(dll_directory
) + 1;
1040 if (flags
& LOAD_LIBRARY_SEARCH_SYSTEM32
) len
+= GetSystemDirectoryW( NULL
, 0 );
1042 if ((p
= ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1044 if (module
) p
= append_path_len( p
, module
, mod_end
- module
);
1045 if (image
) p
= append_path_len( p
, image
, image_end
- image
);
1046 if (flags
& LOAD_LIBRARY_SEARCH_USER_DIRS
)
1048 LIST_FOR_EACH_ENTRY( dir
, &dll_dir_list
, struct dll_dir_entry
, entry
)
1049 p
= append_path( p
, dir
->dir
);
1050 if (dll_directory
) p
= append_path( p
, dll_directory
);
1052 if (flags
& LOAD_LIBRARY_SEARCH_SYSTEM32
) GetSystemDirectoryW( p
, ret
+ len
- p
);
1060 RtlLeaveCriticalSection( &dlldir_section
);
1065 /******************************************************************
1066 * load_library_as_datafile
1068 static BOOL
load_library_as_datafile( LPCWSTR name
, HMODULE
*hmod
, DWORD flags
)
1070 static const WCHAR dotDLL
[] = {'.','d','l','l',0};
1072 WCHAR filenameW
[MAX_PATH
];
1073 HANDLE hFile
= INVALID_HANDLE_VALUE
;
1076 DWORD protect
= PAGE_READONLY
;
1077 DWORD sharing
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
1081 if (flags
& LOAD_LIBRARY_AS_IMAGE_RESOURCE
) protect
|= SEC_IMAGE
;
1083 if (SearchPathW( NULL
, name
, dotDLL
, ARRAY_SIZE( filenameW
), filenameW
, NULL
))
1085 hFile
= CreateFileW( filenameW
, GENERIC_READ
, sharing
, NULL
, OPEN_EXISTING
, 0, 0 );
1087 if (hFile
== INVALID_HANDLE_VALUE
) return FALSE
;
1089 mapping
= CreateFileMappingW( hFile
, NULL
, protect
, 0, 0, NULL
);
1090 if (!mapping
) goto failed
;
1092 module
= MapViewOfFile( mapping
, FILE_MAP_READ
, 0, 0, 0 );
1093 CloseHandle( mapping
);
1094 if (!module
) goto failed
;
1096 if (!(flags
& LOAD_LIBRARY_AS_IMAGE_RESOURCE
))
1098 /* make sure it's a valid PE file */
1099 if (!RtlImageNtHeader( module
)) goto failed
;
1100 *hmod
= (HMODULE
)((char *)module
+ 1); /* set bit 0 for data file module */
1102 if (flags
& LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE
)
1104 struct exclusive_datafile
*file
= HeapAlloc( GetProcessHeap(), 0, sizeof(*file
) );
1105 if (!file
) goto failed
;
1106 file
->module
= *hmod
;
1108 list_add_head( &exclusive_datafile_list
, &file
->entry
);
1109 TRACE( "delaying close %p for module %p\n", file
->file
, file
->module
);
1113 else *hmod
= (HMODULE
)((char *)module
+ 2); /* set bit 1 for image resource module */
1115 CloseHandle( hFile
);
1119 if (module
) UnmapViewOfFile( module
);
1120 CloseHandle( hFile
);
1125 /******************************************************************
1128 * Helper for LoadLibraryExA/W.
1130 static HMODULE
load_library( const UNICODE_STRING
*libname
, DWORD flags
)
1135 const DWORD load_library_search_flags
= (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
|
1136 LOAD_LIBRARY_SEARCH_APPLICATION_DIR
|
1137 LOAD_LIBRARY_SEARCH_USER_DIRS
|
1138 LOAD_LIBRARY_SEARCH_SYSTEM32
|
1139 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
);
1140 const DWORD unsupported_flags
= (LOAD_IGNORE_CODE_AUTHZ_LEVEL
|
1141 LOAD_LIBRARY_REQUIRE_SIGNED_TARGET
);
1143 if (!(flags
& load_library_search_flags
)) flags
|= default_search_flags
;
1145 if( flags
& unsupported_flags
)
1146 FIXME("unsupported flag(s) used (flags: 0x%08x)\n", flags
);
1148 if (flags
& load_library_search_flags
)
1149 load_path
= get_dll_load_path_search_flags( libname
->Buffer
, flags
);
1151 load_path
= MODULE_get_dll_load_path( flags
& LOAD_WITH_ALTERED_SEARCH_PATH
? libname
->Buffer
: NULL
, -1 );
1152 if (!load_path
) return 0;
1154 if (flags
& (LOAD_LIBRARY_AS_DATAFILE
| LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE
| LOAD_LIBRARY_AS_IMAGE_RESOURCE
))
1158 LdrLockLoaderLock( 0, NULL
, &magic
);
1159 if (!LdrGetDllHandle( load_path
, flags
, libname
, &hModule
))
1161 LdrAddRefDll( 0, hModule
);
1162 LdrUnlockLoaderLock( 0, magic
);
1165 if (load_library_as_datafile( libname
->Buffer
, &hModule
, flags
))
1167 LdrUnlockLoaderLock( 0, magic
);
1170 LdrUnlockLoaderLock( 0, magic
);
1171 flags
|= DONT_RESOLVE_DLL_REFERENCES
; /* Just in case */
1172 /* Fallback to normal behaviour */
1175 nts
= LdrLoadDll( load_path
, flags
, libname
, &hModule
);
1176 if (nts
!= STATUS_SUCCESS
)
1179 if (nts
== STATUS_DLL_NOT_FOUND
&& (GetVersion() & 0x80000000))
1180 SetLastError( ERROR_DLL_NOT_FOUND
);
1182 SetLastError( RtlNtStatusToDosError( nts
) );
1185 HeapFree( GetProcessHeap(), 0, load_path
);
1190 /******************************************************************
1191 * LoadLibraryExA (KERNEL32.@)
1193 * Load a dll file into the process address space.
1196 * libname [I] Name of the file to load
1197 * hfile [I] Reserved, must be 0.
1198 * flags [I] Flags for loading the dll
1201 * Success: A handle to the loaded dll.
1202 * Failure: A NULL handle. Use GetLastError() to determine the cause.
1205 * The HFILE parameter is not used and marked reserved in the SDK. I can
1206 * only guess that it should force a file to be mapped, but I rather
1207 * ignore the parameter because it would be extremely difficult to
1208 * integrate this with different types of module representations.
1210 HMODULE WINAPI DECLSPEC_HOTPATCH
LoadLibraryExA(LPCSTR libname
, HANDLE hfile
, DWORD flags
)
1214 if (!(libnameW
= FILE_name_AtoW( libname
, FALSE
))) return 0;
1215 return LoadLibraryExW( libnameW
, hfile
, flags
);
1218 /***********************************************************************
1219 * LoadLibraryExW (KERNEL32.@)
1221 * Unicode version of LoadLibraryExA.
1223 HMODULE WINAPI DECLSPEC_HOTPATCH
LoadLibraryExW(LPCWSTR libnameW
, HANDLE hfile
, DWORD flags
)
1225 UNICODE_STRING wstr
;
1230 SetLastError(ERROR_INVALID_PARAMETER
);
1233 RtlInitUnicodeString( &wstr
, libnameW
);
1234 if (wstr
.Buffer
[wstr
.Length
/sizeof(WCHAR
) - 1] != ' ')
1235 return load_library( &wstr
, flags
);
1237 /* Library name has trailing spaces */
1238 RtlCreateUnicodeString( &wstr
, libnameW
);
1239 while (wstr
.Length
> sizeof(WCHAR
) &&
1240 wstr
.Buffer
[wstr
.Length
/sizeof(WCHAR
) - 1] == ' ')
1242 wstr
.Length
-= sizeof(WCHAR
);
1244 wstr
.Buffer
[wstr
.Length
/sizeof(WCHAR
)] = '\0';
1245 res
= load_library( &wstr
, flags
);
1246 RtlFreeUnicodeString( &wstr
);
1250 /***********************************************************************
1251 * LoadLibraryA (KERNEL32.@)
1253 * Load a dll file into the process address space.
1256 * libname [I] Name of the file to load
1259 * Success: A handle to the loaded dll.
1260 * Failure: A NULL handle. Use GetLastError() to determine the cause.
1263 * See LoadLibraryExA().
1265 HMODULE WINAPI DECLSPEC_HOTPATCH
LoadLibraryA(LPCSTR libname
)
1267 return LoadLibraryExA(libname
, 0, 0);
1270 /***********************************************************************
1271 * LoadLibraryW (KERNEL32.@)
1273 * Unicode version of LoadLibraryA.
1275 HMODULE WINAPI DECLSPEC_HOTPATCH
LoadLibraryW(LPCWSTR libnameW
)
1277 return LoadLibraryExW(libnameW
, 0, 0);
1280 /***********************************************************************
1281 * FreeLibrary (KERNEL32.@)
1283 * Free a dll loaded into the process address space.
1286 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
1289 * Success: TRUE. The dll is removed if it is not still in use.
1290 * Failure: FALSE. Use GetLastError() to determine the cause.
1292 BOOL WINAPI DECLSPEC_HOTPATCH
FreeLibrary(HINSTANCE hLibModule
)
1299 SetLastError( ERROR_INVALID_HANDLE
);
1303 if ((ULONG_PTR
)hLibModule
& 3) /* this is a datafile module */
1305 if ((ULONG_PTR
)hLibModule
& 1)
1307 struct exclusive_datafile
*file
;
1310 LdrLockLoaderLock( 0, NULL
, &magic
);
1311 LIST_FOR_EACH_ENTRY( file
, &exclusive_datafile_list
, struct exclusive_datafile
, entry
)
1313 if (file
->module
!= hLibModule
) continue;
1314 TRACE( "closing %p for module %p\n", file
->file
, file
->module
);
1315 CloseHandle( file
->file
);
1316 list_remove( &file
->entry
);
1317 HeapFree( GetProcessHeap(), 0, file
);
1320 LdrUnlockLoaderLock( 0, magic
);
1322 return UnmapViewOfFile( (void *)((ULONG_PTR
)hLibModule
& ~3) );
1325 if ((nts
= LdrUnloadDll( hLibModule
)) == STATUS_SUCCESS
) retv
= TRUE
;
1326 else SetLastError( RtlNtStatusToDosError( nts
) );
1331 /***********************************************************************
1332 * GetProcAddress (KERNEL32.@)
1334 * Find the address of an exported symbol in a loaded dll.
1337 * hModule [I] Handle to the dll returned by LoadLibraryA().
1338 * function [I] Name of the symbol, or an integer ordinal number < 16384
1341 * Success: A pointer to the symbol in the process address space.
1342 * Failure: NULL. Use GetLastError() to determine the cause.
1344 FARPROC
get_proc_address( HMODULE hModule
, LPCSTR function
)
1349 if (!hModule
) hModule
= NtCurrentTeb()->Peb
->ImageBaseAddress
;
1351 if ((ULONG_PTR
)function
>> 16)
1355 RtlInitAnsiString( &str
, function
);
1356 nts
= LdrGetProcedureAddress( hModule
, &str
, 0, (void**)&fp
);
1359 nts
= LdrGetProcedureAddress( hModule
, NULL
, LOWORD(function
), (void**)&fp
);
1360 if (nts
!= STATUS_SUCCESS
)
1362 SetLastError( RtlNtStatusToDosError( nts
) );
1370 * Work around a Delphi bug on x86_64. When delay loading a symbol,
1371 * Delphi saves rcx, rdx, r8 and r9 to the stack. It then calls
1372 * GetProcAddress(), pops the saved registers and calls the function.
1373 * This works fine if all of the parameters are ints. However, since
1374 * it does not save xmm0 - 3, it relies on GetProcAddress() preserving
1375 * these registers if the function takes floating point parameters.
1376 * This wrapper saves xmm0 - 3 to the stack.
1378 extern FARPROC
get_proc_address_wrapper( HMODULE module
, LPCSTR function
);
1380 __ASM_GLOBAL_FUNC( get_proc_address_wrapper
,
1382 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
1383 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
1384 "movq %rsp,%rbp\n\t"
1385 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
1386 "subq $0x40,%rsp\n\t"
1387 "movaps %xmm0,-0x10(%rbp)\n\t"
1388 "movaps %xmm1,-0x20(%rbp)\n\t"
1389 "movaps %xmm2,-0x30(%rbp)\n\t"
1390 "movaps %xmm3,-0x40(%rbp)\n\t"
1391 "call " __ASM_NAME("get_proc_address") "\n\t"
1392 "movaps -0x40(%rbp), %xmm3\n\t"
1393 "movaps -0x30(%rbp), %xmm2\n\t"
1394 "movaps -0x20(%rbp), %xmm1\n\t"
1395 "movaps -0x10(%rbp), %xmm0\n\t"
1396 "movq %rbp,%rsp\n\t"
1397 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
1399 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
1400 __ASM_CFI(".cfi_same_value %rbp\n\t")
1402 #else /* __x86_64__ */
1404 static inline FARPROC
get_proc_address_wrapper( HMODULE module
, LPCSTR function
)
1406 return get_proc_address( module
, function
);
1409 #endif /* __x86_64__ */
1411 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
1413 return get_proc_address_wrapper( hModule
, function
);
1416 /***********************************************************************
1417 * DelayLoadFailureHook (KERNEL32.@)
1419 FARPROC WINAPI
DelayLoadFailureHook( LPCSTR name
, LPCSTR function
)
1423 if ((ULONG_PTR
)function
>> 16)
1424 ERR( "failed to delay load %s.%s\n", name
, function
);
1426 ERR( "failed to delay load %s.%u\n", name
, LOWORD(function
) );
1427 args
[0] = (ULONG_PTR
)name
;
1428 args
[1] = (ULONG_PTR
)function
;
1429 RaiseException( EXCEPTION_WINE_STUB
, EH_NONCONTINUABLE
, 2, args
);
1433 typedef struct _PEB32
1435 BOOLEAN InheritedAddressSpace
;
1436 BOOLEAN ReadImageFileExecOptions
;
1437 BOOLEAN BeingDebugged
;
1440 DWORD ImageBaseAddress
;
1444 typedef struct _LIST_ENTRY32
1450 typedef struct _PEB_LDR_DATA32
1453 BOOLEAN Initialized
;
1455 LIST_ENTRY32 InLoadOrderModuleList
;
1458 typedef struct _UNICODE_STRING32
1461 USHORT MaximumLength
;
1465 typedef struct _LDR_MODULE32
1467 LIST_ENTRY32 InLoadOrderModuleList
;
1468 LIST_ENTRY32 InMemoryOrderModuleList
;
1469 LIST_ENTRY32 InInitializationOrderModuleList
;
1473 UNICODE_STRING32 FullDllName
;
1474 UNICODE_STRING32 BaseDllName
;
1479 PLIST_ENTRY head
, current
;
1480 LDR_MODULE ldr_module
;
1482 LDR_MODULE32 ldr_module32
;
1485 static BOOL
init_module_iterator(MODULE_ITERATOR
*iter
, HANDLE process
)
1487 PROCESS_BASIC_INFORMATION pbi
;
1488 PPEB_LDR_DATA ldr_data
;
1491 if (!IsWow64Process(process
, &iter
->wow64
))
1494 /* Get address of PEB */
1495 status
= NtQueryInformationProcess(process
, ProcessBasicInformation
,
1496 &pbi
, sizeof(pbi
), NULL
);
1497 if (status
!= STATUS_SUCCESS
)
1499 SetLastError(RtlNtStatusToDosError(status
));
1503 if (sizeof(void *) == 8 && iter
->wow64
)
1505 PEB_LDR_DATA32
*ldr_data32_ptr
;
1506 DWORD ldr_data32
, first_module
;
1509 peb32
= (PEB32
*)(DWORD_PTR
)pbi
.PebBaseAddress
;
1511 if (!ReadProcessMemory(process
, &peb32
->LdrData
, &ldr_data32
,
1512 sizeof(ldr_data32
), NULL
))
1514 ldr_data32_ptr
= (PEB_LDR_DATA32
*)(DWORD_PTR
) ldr_data32
;
1516 if (!ReadProcessMemory(process
,
1517 &ldr_data32_ptr
->InLoadOrderModuleList
.Flink
,
1518 &first_module
, sizeof(first_module
), NULL
))
1520 iter
->head
= (LIST_ENTRY
*)&ldr_data32_ptr
->InLoadOrderModuleList
;
1521 iter
->current
= (LIST_ENTRY
*)(DWORD_PTR
) first_module
;
1522 iter
->process
= process
;
1527 /* Read address of LdrData from PEB */
1528 if (!ReadProcessMemory(process
, &pbi
.PebBaseAddress
->LdrData
,
1529 &ldr_data
, sizeof(ldr_data
), NULL
))
1532 /* Read address of first module from LdrData */
1533 if (!ReadProcessMemory(process
,
1534 &ldr_data
->InLoadOrderModuleList
.Flink
,
1535 &iter
->current
, sizeof(iter
->current
), NULL
))
1538 iter
->head
= &ldr_data
->InLoadOrderModuleList
;
1539 iter
->process
= process
;
1544 static int module_iterator_next(MODULE_ITERATOR
*iter
)
1546 if (iter
->current
== iter
->head
)
1549 if (sizeof(void *) == 8 && iter
->wow64
)
1551 LIST_ENTRY32
*entry32
= (LIST_ENTRY32
*)iter
->current
;
1553 if (!ReadProcessMemory(iter
->process
,
1554 CONTAINING_RECORD(entry32
, LDR_MODULE32
, InLoadOrderModuleList
),
1555 &iter
->ldr_module32
, sizeof(iter
->ldr_module32
), NULL
))
1558 iter
->current
= (LIST_ENTRY
*)(DWORD_PTR
) iter
->ldr_module32
.InLoadOrderModuleList
.Flink
;
1562 if (!ReadProcessMemory(iter
->process
,
1563 CONTAINING_RECORD(iter
->current
, LDR_MODULE
, InLoadOrderModuleList
),
1564 &iter
->ldr_module
, sizeof(iter
->ldr_module
), NULL
))
1567 iter
->current
= iter
->ldr_module
.InLoadOrderModuleList
.Flink
;
1571 static BOOL
get_ldr_module(HANDLE process
, HMODULE module
, LDR_MODULE
*ldr_module
)
1573 MODULE_ITERATOR iter
;
1576 if (!init_module_iterator(&iter
, process
))
1579 while ((ret
= module_iterator_next(&iter
)) > 0)
1580 /* When hModule is NULL we return the process image - which will be
1581 * the first module since our iterator uses InLoadOrderModuleList */
1582 if (!module
|| module
== iter
.ldr_module
.BaseAddress
)
1584 *ldr_module
= iter
.ldr_module
;
1589 SetLastError(ERROR_INVALID_HANDLE
);
1594 static BOOL
get_ldr_module32(HANDLE process
, HMODULE module
, LDR_MODULE32
*ldr_module
)
1596 MODULE_ITERATOR iter
;
1599 if (!init_module_iterator(&iter
, process
))
1602 while ((ret
= module_iterator_next(&iter
)) > 0)
1603 /* When hModule is NULL we return the process image - which will be
1604 * the first module since our iterator uses InLoadOrderModuleList */
1605 if (!module
|| (DWORD
)(DWORD_PTR
) module
== iter
.ldr_module32
.BaseAddress
)
1607 *ldr_module
= iter
.ldr_module32
;
1612 SetLastError(ERROR_INVALID_HANDLE
);
1617 /***********************************************************************
1618 * K32EnumProcessModules (KERNEL32.@)
1621 * Returned list is in load order.
1623 BOOL WINAPI
K32EnumProcessModules(HANDLE process
, HMODULE
*lphModule
,
1624 DWORD cb
, DWORD
*needed
)
1626 MODULE_ITERATOR iter
;
1630 if (!init_module_iterator(&iter
, process
))
1633 if (cb
&& !lphModule
)
1635 SetLastError(ERROR_NOACCESS
);
1639 while ((ret
= module_iterator_next(&iter
)) > 0)
1641 if (cb
>= sizeof(HMODULE
))
1643 if (sizeof(void *) == 8 && iter
.wow64
)
1644 *lphModule
++ = (HMODULE
) (DWORD_PTR
)iter
.ldr_module32
.BaseAddress
;
1646 *lphModule
++ = iter
.ldr_module
.BaseAddress
;
1647 cb
-= sizeof(HMODULE
);
1649 size
+= sizeof(HMODULE
);
1654 SetLastError(ERROR_NOACCESS
);
1662 /***********************************************************************
1663 * K32EnumProcessModulesEx (KERNEL32.@)
1666 * Returned list is in load order.
1668 BOOL WINAPI
K32EnumProcessModulesEx(HANDLE process
, HMODULE
*lphModule
,
1669 DWORD cb
, DWORD
*needed
, DWORD filter
)
1671 FIXME("(%p, %p, %d, %p, %d) semi-stub\n",
1672 process
, lphModule
, cb
, needed
, filter
);
1673 return K32EnumProcessModules(process
, lphModule
, cb
, needed
);
1676 /***********************************************************************
1677 * K32GetModuleBaseNameW (KERNEL32.@)
1679 DWORD WINAPI
K32GetModuleBaseNameW(HANDLE process
, HMODULE module
,
1680 LPWSTR base_name
, DWORD size
)
1682 LDR_MODULE ldr_module
;
1685 if (!IsWow64Process(process
, &wow64
))
1688 if (sizeof(void *) == 8 && wow64
)
1690 LDR_MODULE32 ldr_module32
;
1692 if (!get_ldr_module32(process
, module
, &ldr_module32
))
1695 size
= min(ldr_module32
.BaseDllName
.Length
/ sizeof(WCHAR
), size
);
1696 if (!ReadProcessMemory(process
, (void *)(DWORD_PTR
)ldr_module32
.BaseDllName
.Buffer
,
1697 base_name
, size
* sizeof(WCHAR
), NULL
))
1702 if (!get_ldr_module(process
, module
, &ldr_module
))
1705 size
= min(ldr_module
.BaseDllName
.Length
/ sizeof(WCHAR
), size
);
1706 if (!ReadProcessMemory(process
, ldr_module
.BaseDllName
.Buffer
,
1707 base_name
, size
* sizeof(WCHAR
), NULL
))
1711 base_name
[size
] = 0;
1715 /***********************************************************************
1716 * K32GetModuleBaseNameA (KERNEL32.@)
1718 DWORD WINAPI
K32GetModuleBaseNameA(HANDLE process
, HMODULE module
,
1719 LPSTR base_name
, DWORD size
)
1724 if(!base_name
|| !size
) {
1725 SetLastError(ERROR_INVALID_PARAMETER
);
1729 base_name_w
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * size
);
1733 len
= K32GetModuleBaseNameW(process
, module
, base_name_w
, size
);
1734 TRACE("%d, %s\n", len
, debugstr_w(base_name_w
));
1737 ret
= WideCharToMultiByte(CP_ACP
, 0, base_name_w
, len
,
1738 base_name
, size
, NULL
, NULL
);
1739 if (ret
< size
) base_name
[ret
] = 0;
1741 HeapFree(GetProcessHeap(), 0, base_name_w
);
1745 /***********************************************************************
1746 * K32GetModuleFileNameExW (KERNEL32.@)
1748 DWORD WINAPI
K32GetModuleFileNameExW(HANDLE process
, HMODULE module
,
1749 LPWSTR file_name
, DWORD size
)
1751 LDR_MODULE ldr_module
;
1755 if (!size
) return 0;
1757 if (!IsWow64Process(process
, &wow64
))
1760 if (sizeof(void *) == 8 && wow64
)
1762 LDR_MODULE32 ldr_module32
;
1764 if (!get_ldr_module32(process
, module
, &ldr_module32
))
1767 len
= ldr_module32
.FullDllName
.Length
/ sizeof(WCHAR
);
1768 if (!ReadProcessMemory(process
, (void *)(DWORD_PTR
)ldr_module32
.FullDllName
.Buffer
,
1769 file_name
, min( len
, size
) * sizeof(WCHAR
), NULL
))
1774 if (!get_ldr_module(process
, module
, &ldr_module
))
1777 len
= ldr_module
.FullDllName
.Length
/ sizeof(WCHAR
);
1778 if (!ReadProcessMemory(process
, ldr_module
.FullDllName
.Buffer
,
1779 file_name
, min( len
, size
) * sizeof(WCHAR
), NULL
))
1790 file_name
[size
- 1] = 0;
1795 /***********************************************************************
1796 * K32GetModuleFileNameExA (KERNEL32.@)
1798 DWORD WINAPI
K32GetModuleFileNameExA(HANDLE process
, HMODULE module
,
1799 LPSTR file_name
, DWORD size
)
1804 TRACE("(hProcess=%p, hModule=%p, %p, %d)\n", process
, module
, file_name
, size
);
1806 if (!file_name
|| !size
)
1808 SetLastError( ERROR_INVALID_PARAMETER
);
1812 if ( process
== GetCurrentProcess() )
1814 len
= GetModuleFileNameA( module
, file_name
, size
);
1815 if (size
) file_name
[size
- 1] = '\0';
1819 if (!(ptr
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)))) return 0;
1821 len
= K32GetModuleFileNameExW(process
, module
, ptr
, size
);
1824 file_name
[0] = '\0';
1828 if (!WideCharToMultiByte( CP_ACP
, 0, ptr
, -1, file_name
, size
, NULL
, NULL
))
1830 file_name
[size
- 1] = 0;
1833 else if (len
< size
) len
= strlen( file_name
);
1836 HeapFree(GetProcessHeap(), 0, ptr
);
1840 /***********************************************************************
1841 * K32GetModuleInformation (KERNEL32.@)
1843 BOOL WINAPI
K32GetModuleInformation(HANDLE process
, HMODULE module
,
1844 MODULEINFO
*modinfo
, DWORD cb
)
1846 LDR_MODULE ldr_module
;
1849 if (cb
< sizeof(MODULEINFO
))
1851 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1855 if (!IsWow64Process(process
, &wow64
))
1858 if (sizeof(void *) == 8 && wow64
)
1860 LDR_MODULE32 ldr_module32
;
1862 if (!get_ldr_module32(process
, module
, &ldr_module32
))
1865 modinfo
->lpBaseOfDll
= (void *)(DWORD_PTR
)ldr_module32
.BaseAddress
;
1866 modinfo
->SizeOfImage
= ldr_module32
.SizeOfImage
;
1867 modinfo
->EntryPoint
= (void *)(DWORD_PTR
)ldr_module32
.EntryPoint
;
1871 if (!get_ldr_module(process
, module
, &ldr_module
))
1874 modinfo
->lpBaseOfDll
= ldr_module
.BaseAddress
;
1875 modinfo
->SizeOfImage
= ldr_module
.SizeOfImage
;
1876 modinfo
->EntryPoint
= ldr_module
.EntryPoint
;
1883 /***********************************************************************
1884 * __wine_dll_register_16 (KERNEL32.@)
1888 void __wine_dll_register_16( const IMAGE_DOS_HEADER
*header
, const char *file_name
)
1890 ERR( "loading old style 16-bit dll %s no longer supported\n", file_name
);
1894 /***********************************************************************
1895 * __wine_dll_unregister_16 (KERNEL32.@)
1899 void __wine_dll_unregister_16( const IMAGE_DOS_HEADER
*header
)