mfmediaengine: Remove unnecessary import library.
[wine.git] / dlls / kernel32 / module.c
blob433366d364b1d4d2e80a77e41f72b9fe42f2029e
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "winerror.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winternl.h"
34 #include "kernel_private.h"
35 #include "psapi.h"
37 #include "wine/list.h"
38 #include "wine/asm.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(module);
44 /****************************************************************************
45 * GetDllDirectoryA (KERNEL32.@)
47 DWORD WINAPI GetDllDirectoryA( DWORD buf_len, LPSTR buffer )
49 UNICODE_STRING str;
50 NTSTATUS status;
51 WCHAR data[MAX_PATH];
52 DWORD len;
54 str.Buffer = data;
55 str.MaximumLength = sizeof(data);
57 for (;;)
59 status = LdrGetDllDirectory( &str );
60 if (status != STATUS_BUFFER_TOO_SMALL) break;
61 if (str.Buffer != data) HeapFree( GetProcessHeap(), 0, str.Buffer );
62 str.MaximumLength = str.Length;
63 if (!(str.Buffer = HeapAlloc( GetProcessHeap(), 0, str.MaximumLength )))
65 status = STATUS_NO_MEMORY;
66 break;
70 if (!set_ntstatus( status )) return 0;
72 len = FILE_name_WtoA( str.Buffer, str.Length / sizeof(WCHAR), NULL, 0 );
73 if (buffer && buf_len > len)
75 FILE_name_WtoA( str.Buffer, -1, buffer, buf_len );
77 else
79 len++; /* for terminating null */
80 if (buffer) *buffer = 0;
82 if (str.Buffer != data) HeapFree( GetProcessHeap(), 0, str.Buffer );
83 return len;
87 /****************************************************************************
88 * GetDllDirectoryW (KERNEL32.@)
90 DWORD WINAPI GetDllDirectoryW( DWORD buf_len, LPWSTR buffer )
92 UNICODE_STRING str;
93 NTSTATUS status;
95 str.Buffer = buffer;
96 str.MaximumLength = min( buf_len, UNICODE_STRING_MAX_CHARS ) * sizeof(WCHAR);
97 status = LdrGetDllDirectory( &str );
98 if (status == STATUS_BUFFER_TOO_SMALL) status = STATUS_SUCCESS;
99 if (!set_ntstatus( status )) return 0;
100 return str.Length / sizeof(WCHAR);
104 /****************************************************************************
105 * SetDllDirectoryA (KERNEL32.@)
107 BOOL WINAPI SetDllDirectoryA( LPCSTR dir )
109 WCHAR *dirW = NULL;
110 BOOL ret;
112 if (dir && !(dirW = FILE_name_AtoW( dir, TRUE ))) return FALSE;
113 ret = SetDllDirectoryW( dirW );
114 HeapFree( GetProcessHeap(), 0, dirW );
115 return ret;
119 /****************************************************************************
120 * SetDllDirectoryW (KERNEL32.@)
122 BOOL WINAPI SetDllDirectoryW( LPCWSTR dir )
124 UNICODE_STRING str;
126 RtlInitUnicodeString( &str, dir );
127 return set_ntstatus( LdrSetDllDirectory( &str ));
131 /***********************************************************************
132 * GetBinaryTypeW [KERNEL32.@]
134 * Determine whether a file is executable, and if so, what kind.
136 * PARAMS
137 * lpApplicationName [I] Path of the file to check
138 * lpBinaryType [O] Destination for the binary type
140 * RETURNS
141 * TRUE, if the file is an executable, in which case lpBinaryType is set.
142 * FALSE, if the file is not an executable or if the function fails.
144 * NOTES
145 * The type of executable is a property that determines which subsystem an
146 * executable file runs under. lpBinaryType can be set to one of the following
147 * values:
148 * SCS_32BIT_BINARY: A Win32 based application
149 * SCS_64BIT_BINARY: A Win64 based application
150 * SCS_DOS_BINARY: An MS-Dos based application
151 * SCS_WOW_BINARY: A Win16 based application
152 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
153 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
154 * SCS_OS216_BINARY: A 16bit OS/2 based application
156 * To find the binary type, this function reads in the files header information.
157 * If extended header information is not present it will assume that the file
158 * is a DOS executable. If extended header information is present it will
159 * determine if the file is a 16, 32 or 64 bit Windows executable by checking the
160 * flags in the header.
162 * ".com" and ".pif" files are only recognized by their file name extension,
163 * as per native Windows.
165 BOOL WINAPI GetBinaryTypeW( LPCWSTR name, LPDWORD type )
167 HANDLE hfile, mapping;
168 NTSTATUS status;
169 const WCHAR *ptr;
171 TRACE("%s\n", debugstr_w(name) );
173 if (type == NULL) return FALSE;
175 hfile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
176 if ( hfile == INVALID_HANDLE_VALUE )
177 return FALSE;
179 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY,
180 NULL, NULL, PAGE_READONLY, SEC_IMAGE, hfile );
181 CloseHandle( hfile );
183 switch (status)
185 case STATUS_SUCCESS:
187 SECTION_IMAGE_INFORMATION info;
189 status = NtQuerySection( mapping, SectionImageInformation, &info, sizeof(info), NULL );
190 CloseHandle( mapping );
191 if (status) return FALSE;
192 if (info.ImageCharacteristics & IMAGE_FILE_DLL) return FALSE;
193 switch (info.Machine)
195 case IMAGE_FILE_MACHINE_I386:
196 case IMAGE_FILE_MACHINE_ARMNT:
197 *type = SCS_32BIT_BINARY;
198 return TRUE;
199 case IMAGE_FILE_MACHINE_AMD64:
200 case IMAGE_FILE_MACHINE_ARM64:
201 *type = SCS_64BIT_BINARY;
202 return TRUE;
204 return FALSE;
206 case STATUS_INVALID_IMAGE_WIN_16:
207 *type = SCS_WOW_BINARY;
208 return TRUE;
209 case STATUS_INVALID_IMAGE_WIN_32:
210 *type = SCS_32BIT_BINARY;
211 return TRUE;
212 case STATUS_INVALID_IMAGE_WIN_64:
213 *type = SCS_64BIT_BINARY;
214 return TRUE;
215 case STATUS_INVALID_IMAGE_NE_FORMAT:
216 *type = SCS_OS216_BINARY;
217 return TRUE;
218 case STATUS_INVALID_IMAGE_PROTECT:
219 *type = SCS_DOS_BINARY;
220 return TRUE;
221 case STATUS_INVALID_IMAGE_NOT_MZ:
222 if ((ptr = wcsrchr( name, '.' )))
224 if (!wcsicmp( ptr, L".com" ))
226 *type = SCS_DOS_BINARY;
227 return TRUE;
229 if (!wcsicmp( ptr, L".pif" ))
231 *type = SCS_PIF_BINARY;
232 return TRUE;
235 return FALSE;
236 default:
237 return FALSE;
241 /***********************************************************************
242 * GetBinaryTypeA [KERNEL32.@]
243 * GetBinaryType [KERNEL32.@]
245 * See GetBinaryTypeW.
247 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
249 ANSI_STRING app_nameA;
251 TRACE("%s\n", debugstr_a(lpApplicationName));
253 /* Sanity check.
255 if ( lpApplicationName == NULL || lpBinaryType == NULL )
256 return FALSE;
258 RtlInitAnsiString(&app_nameA, lpApplicationName);
259 if (!set_ntstatus( RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
260 &app_nameA, FALSE )))
261 return FALSE;
262 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
265 /***********************************************************************
266 * GetProcAddress (KERNEL32.@)
268 * Find the address of an exported symbol in a loaded dll.
270 * PARAMS
271 * hModule [I] Handle to the dll returned by LoadLibraryA().
272 * function [I] Name of the symbol, or an integer ordinal number < 16384
274 * RETURNS
275 * Success: A pointer to the symbol in the process address space.
276 * Failure: NULL. Use GetLastError() to determine the cause.
278 FARPROC get_proc_address( HMODULE hModule, LPCSTR function )
280 FARPROC fp;
282 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
284 if ((ULONG_PTR)function >> 16)
286 ANSI_STRING str;
288 RtlInitAnsiString( &str, function );
289 if (!set_ntstatus( LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp ))) return NULL;
291 else
292 if (!set_ntstatus( LdrGetProcedureAddress( hModule, NULL, LOWORD(function), (void**)&fp )))
293 return NULL;
295 return fp;
298 #ifdef __x86_64__
300 * Work around a Delphi bug on x86_64. When delay loading a symbol,
301 * Delphi saves rcx, rdx, r8 and r9 to the stack. It then calls
302 * GetProcAddress(), pops the saved registers and calls the function.
303 * This works fine if all of the parameters are ints. However, since
304 * it does not save xmm0 - 3, it relies on GetProcAddress() preserving
305 * these registers if the function takes floating point parameters.
306 * This wrapper saves xmm0 - 3 to the stack.
308 extern FARPROC get_proc_address_wrapper( HMODULE module, LPCSTR function );
310 __ASM_GLOBAL_FUNC( get_proc_address_wrapper,
311 "pushq %rbp\n\t"
312 __ASM_SEH(".seh_pushreg %rbp\n\t")
313 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
314 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
315 "movq %rsp,%rbp\n\t"
316 __ASM_SEH(".seh_setframe %rbp,0\n\t")
317 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
318 __ASM_SEH(".seh_endprologue\n\t")
319 "subq $0x60,%rsp\n\t"
320 "andq $~15,%rsp\n\t"
321 "movaps %xmm0,0x20(%rsp)\n\t"
322 "movaps %xmm1,0x30(%rsp)\n\t"
323 "movaps %xmm2,0x40(%rsp)\n\t"
324 "movaps %xmm3,0x50(%rsp)\n\t"
325 "call " __ASM_NAME("get_proc_address") "\n\t"
326 "movaps 0x50(%rsp), %xmm3\n\t"
327 "movaps 0x40(%rsp), %xmm2\n\t"
328 "movaps 0x30(%rsp), %xmm1\n\t"
329 "movaps 0x20(%rsp), %xmm0\n\t"
330 "leaq 0(%rbp),%rsp\n\t"
331 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
332 "popq %rbp\n\t"
333 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
334 __ASM_CFI(".cfi_same_value %rbp\n\t")
335 "ret" )
336 #else /* __x86_64__ */
338 static inline FARPROC get_proc_address_wrapper( HMODULE module, LPCSTR function )
340 return get_proc_address( module, function );
343 #endif /* __x86_64__ */
345 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
347 return get_proc_address_wrapper( hModule, function );