windows.media: Partially implement IClosedCaptionPropertiesStatics_get_FontEffect.
[wine.git] / dlls / kernelbase / loader.c
blob0fd2d7b7c999bebcee58ee21129f54b316fa9364
1 /*
2 * Module loader
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 2006 Mike McCormack
6 * Copyright 1995, 2003, 2019 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winternl.h"
33 #include "kernelbase.h"
34 #include "wine/list.h"
35 #include "wine/asm.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(module);
42 /* to keep track of LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE file handles */
43 struct exclusive_datafile
45 struct list entry;
46 HMODULE module;
47 HANDLE file;
49 static struct list exclusive_datafile_list = LIST_INIT( exclusive_datafile_list );
51 static CRITICAL_SECTION exclusive_datafile_list_section;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
54 0, 0, &exclusive_datafile_list_section,
55 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56 0, 0, { (DWORD_PTR)(__FILE__ ": exclusive_datafile_list_section") }
58 static CRITICAL_SECTION exclusive_datafile_list_section = { &critsect_debug, -1, 0, 0, 0, 0 };
60 /***********************************************************************
61 * Modules
62 ***********************************************************************/
65 /******************************************************************
66 * get_proc_address
68 FARPROC WINAPI get_proc_address( HMODULE module, LPCSTR function )
70 FARPROC proc;
71 ANSI_STRING str;
73 if (!module) module = NtCurrentTeb()->Peb->ImageBaseAddress;
75 if ((ULONG_PTR)function >> 16)
77 RtlInitAnsiString( &str, function );
78 if (!set_ntstatus( LdrGetProcedureAddress( module, &str, 0, (void**)&proc ))) return NULL;
80 else if (!set_ntstatus( LdrGetProcedureAddress( module, NULL, LOWORD(function), (void**)&proc )))
81 return NULL;
83 return proc;
87 /******************************************************************
88 * load_library_as_datafile
90 static BOOL load_library_as_datafile( LPCWSTR load_path, DWORD flags, LPCWSTR name, HMODULE *mod_ret )
92 WCHAR filenameW[MAX_PATH];
93 HANDLE mapping, file = INVALID_HANDLE_VALUE;
94 HMODULE module = 0;
95 DWORD protect = PAGE_READONLY;
97 *mod_ret = 0;
99 if (flags & LOAD_LIBRARY_AS_IMAGE_RESOURCE) protect |= SEC_IMAGE;
101 if (SearchPathW( NULL, name, L".dll", ARRAY_SIZE( filenameW ), filenameW, NULL ))
103 file = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
104 NULL, OPEN_EXISTING, 0, 0 );
106 if (file == INVALID_HANDLE_VALUE) return FALSE;
108 mapping = CreateFileMappingW( file, NULL, protect, 0, 0, NULL );
109 if (!mapping) goto failed;
111 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
112 CloseHandle( mapping );
113 if (!module) goto failed;
115 if (!(flags & LOAD_LIBRARY_AS_IMAGE_RESOURCE))
117 /* make sure it's a valid PE file */
118 if (!RtlImageNtHeader( module ))
120 SetLastError( ERROR_BAD_EXE_FORMAT );
121 goto failed;
123 *mod_ret = (HMODULE)((char *)module + 1); /* set bit 0 for data file module */
125 if (flags & LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE)
127 struct exclusive_datafile *datafile = HeapAlloc( GetProcessHeap(), 0, sizeof(*datafile) );
128 if (!datafile) goto failed;
129 datafile->module = *mod_ret;
130 datafile->file = file;
131 RtlEnterCriticalSection( &exclusive_datafile_list_section );
132 list_add_head( &exclusive_datafile_list, &datafile->entry );
133 RtlLeaveCriticalSection( &exclusive_datafile_list_section );
134 TRACE( "delaying close %p for module %p\n", datafile->file, datafile->module );
135 return TRUE;
138 else *mod_ret = (HMODULE)((char *)module + 2); /* set bit 1 for image resource module */
140 CloseHandle( file );
141 return TRUE;
143 failed:
144 if (module) UnmapViewOfFile( module );
145 CloseHandle( file );
146 return FALSE;
150 /******************************************************************
151 * load_library
153 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
155 const DWORD unsupported_flags = LOAD_IGNORE_CODE_AUTHZ_LEVEL | LOAD_LIBRARY_REQUIRE_SIGNED_TARGET;
156 NTSTATUS status;
157 HMODULE module;
158 WCHAR *load_path, *dummy;
160 if (flags & unsupported_flags) FIXME( "unsupported flag(s) used %#08lx\n", flags );
162 if (!set_ntstatus( LdrGetDllPath( libname->Buffer, flags, &load_path, &dummy ))) return 0;
164 if (flags & (LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE |
165 LOAD_LIBRARY_AS_IMAGE_RESOURCE))
167 if (LdrGetDllHandleEx( 0, load_path, NULL, libname, &module ))
168 load_library_as_datafile( load_path, flags, libname->Buffer, &module );
170 else
172 status = LdrLoadDll( load_path, flags, libname, &module );
173 if (!set_ntstatus( status ))
175 module = 0;
176 if (status == STATUS_DLL_NOT_FOUND && (GetVersion() & 0x80000000))
177 SetLastError( ERROR_DLL_NOT_FOUND );
181 RtlReleasePath( load_path );
182 return module;
186 /****************************************************************************
187 * AddDllDirectory (kernelbase.@)
189 DLL_DIRECTORY_COOKIE WINAPI DECLSPEC_HOTPATCH AddDllDirectory( const WCHAR *dir )
191 UNICODE_STRING str;
192 void *cookie;
194 RtlInitUnicodeString( &str, dir );
195 if (!set_ntstatus( LdrAddDllDirectory( &str, &cookie ))) return NULL;
196 return cookie;
200 /***********************************************************************
201 * DelayLoadFailureHook (kernelbase.@)
203 FARPROC WINAPI DECLSPEC_HOTPATCH DelayLoadFailureHook( LPCSTR name, LPCSTR function )
205 ULONG_PTR args[2];
207 if ((ULONG_PTR)function >> 16)
208 ERR( "failed to delay load %s.%s\n", name, function );
209 else
210 ERR( "failed to delay load %s.%u\n", name, LOWORD(function) );
211 args[0] = (ULONG_PTR)name;
212 args[1] = (ULONG_PTR)function;
213 RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );
214 return NULL;
218 /****************************************************************************
219 * DisableThreadLibraryCalls (kernelbase.@)
221 BOOL WINAPI DECLSPEC_HOTPATCH DisableThreadLibraryCalls( HMODULE module )
223 return set_ntstatus( LdrDisableThreadCalloutsForDll( module ));
227 /***********************************************************************
228 * FreeLibrary (kernelbase.@)
230 BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary( HINSTANCE module )
232 if (!module)
234 SetLastError( ERROR_INVALID_HANDLE );
235 return FALSE;
238 if ((ULONG_PTR)module & 3) /* this is a datafile module */
240 void *ptr = (void *)((ULONG_PTR)module & ~3);
241 if (!RtlImageNtHeader( ptr ))
243 SetLastError( ERROR_BAD_EXE_FORMAT );
244 return FALSE;
246 if ((ULONG_PTR)module & 1)
248 struct exclusive_datafile *file;
250 RtlEnterCriticalSection( &exclusive_datafile_list_section );
251 LIST_FOR_EACH_ENTRY( file, &exclusive_datafile_list, struct exclusive_datafile, entry )
253 if (file->module != module) continue;
254 TRACE( "closing %p for module %p\n", file->file, file->module );
255 CloseHandle( file->file );
256 list_remove( &file->entry );
257 HeapFree( GetProcessHeap(), 0, file );
258 break;
260 RtlLeaveCriticalSection( &exclusive_datafile_list_section );
262 return UnmapViewOfFile( ptr );
265 return set_ntstatus( LdrUnloadDll( module ));
269 /***********************************************************************
270 * GetModuleFileNameA (kernelbase.@)
272 DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameA( HMODULE module, LPSTR filename, DWORD size )
274 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
275 DWORD len;
277 if (!filenameW)
279 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
280 return 0;
282 if ((len = GetModuleFileNameW( module, filenameW, size )))
284 len = file_name_WtoA( filenameW, len, filename, size );
285 if (len < size)
286 filename[len] = 0;
287 else
288 SetLastError( ERROR_INSUFFICIENT_BUFFER );
290 HeapFree( GetProcessHeap(), 0, filenameW );
291 return len;
295 /***********************************************************************
296 * GetModuleFileNameW (kernelbase.@)
298 DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameW( HMODULE module, LPWSTR filename, DWORD size )
300 ULONG len = 0;
301 WIN16_SUBSYSTEM_TIB *win16_tib;
302 UNICODE_STRING name;
303 NTSTATUS status;
305 if (!module && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
307 len = min( size, win16_tib->exe_name->Length / sizeof(WCHAR) );
308 memcpy( filename, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
309 if (len < size) filename[len] = 0;
310 goto done;
313 name.Buffer = filename;
314 name.MaximumLength = min( size, UNICODE_STRING_MAX_CHARS ) * sizeof(WCHAR);
315 status = LdrGetDllFullName( module, &name );
316 if (!status || status == STATUS_BUFFER_TOO_SMALL) len = name.Length / sizeof(WCHAR);
317 SetLastError( RtlNtStatusToDosError( status ));
318 done:
319 TRACE( "%s\n", debugstr_wn(filename, len) );
320 return len;
324 /***********************************************************************
325 * GetModuleHandleA (kernelbase.@)
327 HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA( LPCSTR module )
329 HMODULE ret;
331 GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret );
332 return ret;
336 /***********************************************************************
337 * GetModuleHandleW (kernelbase.@)
339 HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleW( LPCWSTR module )
341 HMODULE ret;
343 GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret );
344 return ret;
348 /***********************************************************************
349 * GetModuleHandleExA (kernelbase.@)
351 BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExA( DWORD flags, LPCSTR name, HMODULE *module )
353 WCHAR *nameW;
355 if (!name || (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS))
356 return GetModuleHandleExW( flags, (LPCWSTR)name, module );
358 if (!(nameW = file_name_AtoW( name, FALSE ))) return FALSE;
359 return GetModuleHandleExW( flags, nameW, module );
363 /***********************************************************************
364 * GetModuleHandleExW (kernelbase.@)
366 BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExW( DWORD flags, LPCWSTR name, HMODULE *module )
368 HMODULE ret = NULL;
369 NTSTATUS status;
370 void *dummy;
372 if (!module)
374 SetLastError( ERROR_INVALID_PARAMETER );
375 return FALSE;
378 if ((flags & ~(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
379 | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS))
380 || (flags & (GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
381 == (GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
383 *module = NULL;
384 SetLastError( ERROR_INVALID_PARAMETER );
385 return FALSE;
388 if (name && !(flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS))
390 UNICODE_STRING wstr;
391 ULONG ldr_flags = 0;
393 if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN)
394 ldr_flags |= LDR_GET_DLL_HANDLE_EX_FLAG_PIN;
395 if (flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)
396 ldr_flags |= LDR_GET_DLL_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
398 RtlInitUnicodeString( &wstr, name );
399 status = LdrGetDllHandleEx( ldr_flags, NULL, NULL, &wstr, &ret );
401 else
403 ret = name ? RtlPcToFileHeader( (void *)name, &dummy ) : NtCurrentTeb()->Peb->ImageBaseAddress;
405 if (ret)
407 if (!(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
408 status = LdrAddRefDll( flags & GET_MODULE_HANDLE_EX_FLAG_PIN ? LDR_ADDREF_DLL_PIN : 0, ret );
409 else
410 status = STATUS_SUCCESS;
411 } else status = STATUS_DLL_NOT_FOUND;
414 *module = ret;
415 return set_ntstatus( status );
419 /***********************************************************************
420 * GetProcAddress (kernelbase.@)
423 #ifdef __x86_64__
425 * Work around a Delphi bug on x86_64. When delay loading a symbol,
426 * Delphi saves rcx, rdx, r8 and r9 to the stack. It then calls
427 * GetProcAddress(), pops the saved registers and calls the function.
428 * This works fine if all of the parameters are ints. However, since
429 * it does not save xmm0 - 3, it relies on GetProcAddress() preserving
430 * these registers if the function takes floating point parameters.
431 * This wrapper saves xmm0 - 3 to the stack.
433 __ASM_GLOBAL_FUNC( GetProcAddress,
434 ".byte 0x48\n\t" /* hotpatch prolog */
435 "pushq %rbp\n\t"
436 __ASM_SEH(".seh_pushreg %rbp\n\t")
437 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
438 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
439 "movq %rsp,%rbp\n\t"
440 __ASM_SEH(".seh_setframe %rbp,0\n\t")
441 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
442 __ASM_SEH(".seh_endprologue\n\t")
443 "subq $0x60,%rsp\n\t"
444 "andq $~15,%rsp\n\t"
445 "movaps %xmm0,0x20(%rsp)\n\t"
446 "movaps %xmm1,0x30(%rsp)\n\t"
447 "movaps %xmm2,0x40(%rsp)\n\t"
448 "movaps %xmm3,0x50(%rsp)\n\t"
449 "call " __ASM_NAME("get_proc_address") "\n\t"
450 "movaps 0x50(%rsp), %xmm3\n\t"
451 "movaps 0x40(%rsp), %xmm2\n\t"
452 "movaps 0x30(%rsp), %xmm1\n\t"
453 "movaps 0x20(%rsp), %xmm0\n\t"
454 "leaq 0(%rbp),%rsp\n\t"
455 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
456 "popq %rbp\n\t"
457 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
458 __ASM_CFI(".cfi_same_value %rbp\n\t")
459 "ret" )
460 #else /* __x86_64__ */
462 FARPROC WINAPI DECLSPEC_HOTPATCH GetProcAddress( HMODULE module, LPCSTR function )
464 return get_proc_address( module, function );
467 #endif /* __x86_64__ */
470 /***********************************************************************
471 * IsApiSetImplemented (kernelbase.@)
473 BOOL WINAPI IsApiSetImplemented( LPCSTR name )
475 UNICODE_STRING str;
476 NTSTATUS status;
477 BOOLEAN in_schema, present;
479 if (!RtlCreateUnicodeStringFromAsciiz( &str, name )) return FALSE;
480 status = ApiSetQueryApiSetPresenceEx( &str, &in_schema, &present );
481 RtlFreeUnicodeString( &str );
482 return !status && present;
486 /***********************************************************************
487 * LoadLibraryA (kernelbase.@)
489 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryA( LPCSTR name )
491 return LoadLibraryExA( name, 0, 0 );
495 /***********************************************************************
496 * LoadLibraryW (kernelbase.@)
498 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryW( LPCWSTR name )
500 return LoadLibraryExW( name, 0, 0 );
504 /******************************************************************
505 * LoadLibraryExA (kernelbase.@)
507 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryExA( LPCSTR name, HANDLE file, DWORD flags )
509 WCHAR *nameW;
511 if (!(nameW = file_name_AtoW( name, FALSE ))) return 0;
512 return LoadLibraryExW( nameW, file, flags );
516 /***********************************************************************
517 * LoadLibraryExW (kernelbase.@)
519 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryExW( LPCWSTR name, HANDLE file, DWORD flags )
521 UNICODE_STRING str;
522 HMODULE module;
524 if (!name)
526 SetLastError( ERROR_INVALID_PARAMETER );
527 return 0;
529 RtlInitUnicodeString( &str, name );
530 if (str.Buffer[str.Length/sizeof(WCHAR) - 1] != ' ') return load_library( &str, flags );
532 /* library name has trailing spaces */
533 RtlCreateUnicodeString( &str, name );
534 while (str.Length > sizeof(WCHAR) && str.Buffer[str.Length/sizeof(WCHAR) - 1] == ' ')
535 str.Length -= sizeof(WCHAR);
537 str.Buffer[str.Length/sizeof(WCHAR)] = 0;
538 module = load_library( &str, flags );
539 RtlFreeUnicodeString( &str );
540 return module;
544 /***********************************************************************
545 * LoadPackagedLibrary (kernelbase.@)
547 HMODULE WINAPI /* DECLSPEC_HOTPATCH */ LoadPackagedLibrary( LPCWSTR name, DWORD reserved )
549 FIXME( "semi-stub, name %s, reserved %#lx.\n", debugstr_w(name), reserved );
550 SetLastError( APPMODEL_ERROR_NO_PACKAGE );
551 return NULL;
555 /***********************************************************************
556 * LoadAppInitDlls (kernelbase.@)
558 void WINAPI LoadAppInitDlls(void)
560 TRACE( "\n" );
564 /****************************************************************************
565 * RemoveDllDirectory (kernelbase.@)
567 BOOL WINAPI DECLSPEC_HOTPATCH RemoveDllDirectory( DLL_DIRECTORY_COOKIE cookie )
569 return set_ntstatus( LdrRemoveDllDirectory( cookie ));
573 /*************************************************************************
574 * SetDefaultDllDirectories (kernelbase.@)
576 BOOL WINAPI DECLSPEC_HOTPATCH SetDefaultDllDirectories( DWORD flags )
578 return set_ntstatus( LdrSetDefaultDllDirectories( flags ));
582 /***********************************************************************
583 * Resources
584 ***********************************************************************/
587 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
589 /* retrieve the resource name to pass to the ntdll functions */
590 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
592 if (IS_INTRESOURCE(name))
594 str->Buffer = ULongToPtr( LOWORD(name) );
595 return STATUS_SUCCESS;
597 if (name[0] == '#')
599 ULONG value;
600 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
601 return STATUS_INVALID_PARAMETER;
602 str->Buffer = ULongToPtr(value);
603 return STATUS_SUCCESS;
605 RtlCreateUnicodeStringFromAsciiz( str, name );
606 RtlUpcaseUnicodeString( str, str, FALSE );
607 return STATUS_SUCCESS;
610 /* retrieve the resource name to pass to the ntdll functions */
611 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
613 if (IS_INTRESOURCE(name))
615 str->Buffer = ULongToPtr( LOWORD(name) );
616 return STATUS_SUCCESS;
618 if (name[0] == '#')
620 ULONG value;
621 RtlInitUnicodeString( str, name + 1 );
622 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
623 return STATUS_INVALID_PARAMETER;
624 str->Buffer = ULongToPtr(value);
625 return STATUS_SUCCESS;
627 RtlCreateUnicodeString( str, name );
628 RtlUpcaseUnicodeString( str, str, FALSE );
629 return STATUS_SUCCESS;
633 /**********************************************************************
634 * EnumResourceLanguagesExA (kernelbase.@)
636 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExA( HMODULE module, LPCSTR type, LPCSTR name,
637 ENUMRESLANGPROCA func, LONG_PTR param,
638 DWORD flags, LANGID lang )
640 int i;
641 BOOL ret = FALSE;
642 NTSTATUS status;
643 UNICODE_STRING typeW, nameW;
644 LDR_RESOURCE_INFO info;
645 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
646 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
648 TRACE( "%p %s %s %p %Ix %lx %d\n", module, debugstr_a(type), debugstr_a(name),
649 func, param, flags, lang );
651 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
652 FIXME( "unimplemented flags: %lx\n", flags );
654 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
655 if (!(flags & RESOURCE_ENUM_LN)) return ret;
657 if (!module) module = GetModuleHandleW( 0 );
658 typeW.Buffer = nameW.Buffer = NULL;
659 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
660 goto done;
661 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
662 goto done;
663 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
664 goto done;
665 info.Type = (ULONG_PTR)typeW.Buffer;
666 info.Name = (ULONG_PTR)nameW.Buffer;
667 if ((status = LdrFindResourceDirectory_U( module, &info, 2, &resdir )) != STATUS_SUCCESS)
668 goto done;
670 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
671 __TRY
673 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
675 ret = func( module, type, name, et[i].u.Id, param );
676 if (!ret) break;
679 __EXCEPT_PAGE_FAULT
681 ret = FALSE;
682 status = STATUS_ACCESS_VIOLATION;
684 __ENDTRY
685 done:
686 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
687 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
688 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
689 return ret;
693 /**********************************************************************
694 * EnumResourceLanguagesExW (kernelbase.@)
696 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExW( HMODULE module, LPCWSTR type, LPCWSTR name,
697 ENUMRESLANGPROCW func, LONG_PTR param,
698 DWORD flags, LANGID lang )
700 int i;
701 BOOL ret = FALSE;
702 NTSTATUS status;
703 UNICODE_STRING typeW, nameW;
704 LDR_RESOURCE_INFO info;
705 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
706 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
708 TRACE( "%p %s %s %p %Ix %lx %d\n", module, debugstr_w(type), debugstr_w(name),
709 func, param, flags, lang );
711 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
712 FIXME( "unimplemented flags: %lx\n", flags );
714 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
715 if (!(flags & RESOURCE_ENUM_LN)) return ret;
717 if (!module) module = GetModuleHandleW( 0 );
718 typeW.Buffer = nameW.Buffer = NULL;
719 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
720 goto done;
721 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
722 goto done;
723 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
724 goto done;
725 info.Type = (ULONG_PTR)typeW.Buffer;
726 info.Name = (ULONG_PTR)nameW.Buffer;
727 if ((status = LdrFindResourceDirectory_U( module, &info, 2, &resdir )) != STATUS_SUCCESS)
728 goto done;
730 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
731 __TRY
733 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
735 ret = func( module, type, name, et[i].u.Id, param );
736 if (!ret) break;
739 __EXCEPT_PAGE_FAULT
741 ret = FALSE;
742 status = STATUS_ACCESS_VIOLATION;
744 __ENDTRY
745 done:
746 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
747 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
748 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
749 return ret;
753 /**********************************************************************
754 * EnumResourceNamesExA (kernelbase.@)
756 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesExA( HMODULE module, LPCSTR type, ENUMRESNAMEPROCA func,
757 LONG_PTR param, DWORD flags, LANGID lang )
759 int i;
760 BOOL ret = FALSE;
761 DWORD len = 0, newlen;
762 LPSTR name = NULL;
763 NTSTATUS status;
764 UNICODE_STRING typeW;
765 LDR_RESOURCE_INFO info;
766 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
767 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
768 const IMAGE_RESOURCE_DIR_STRING_U *str;
770 TRACE( "%p %s %p %Ix\n", module, debugstr_a(type), func, param );
772 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
773 FIXME( "unimplemented flags: %lx\n", flags );
775 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
776 if (!(flags & RESOURCE_ENUM_LN)) return ret;
778 if (!module) module = GetModuleHandleW( 0 );
779 typeW.Buffer = NULL;
780 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
781 goto done;
782 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
783 goto done;
784 info.Type = (ULONG_PTR)typeW.Buffer;
785 if ((status = LdrFindResourceDirectory_U( module, &info, 1, &resdir )) != STATUS_SUCCESS)
786 goto done;
788 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
789 __TRY
791 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
793 if (et[i].u.s.NameIsString)
795 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u.s.NameOffset);
796 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
797 if (newlen + 1 > len)
799 len = newlen + 1;
800 HeapFree( GetProcessHeap(), 0, name );
801 if (!(name = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
803 ret = FALSE;
804 break;
807 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
808 name[newlen] = 0;
809 ret = func( module, type, name, param );
811 else
813 ret = func( module, type, UIntToPtr(et[i].u.Id), param );
815 if (!ret) break;
818 __EXCEPT_PAGE_FAULT
820 ret = FALSE;
821 status = STATUS_ACCESS_VIOLATION;
823 __ENDTRY
825 done:
826 HeapFree( GetProcessHeap(), 0, name );
827 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
828 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
829 return ret;
833 /**********************************************************************
834 * EnumResourceNamesExW (kernelbase.@)
836 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesExW( HMODULE module, LPCWSTR type, ENUMRESNAMEPROCW func,
837 LONG_PTR param, DWORD flags, LANGID lang )
839 int i, len = 0;
840 BOOL ret = FALSE;
841 LPWSTR name = NULL;
842 NTSTATUS status;
843 UNICODE_STRING typeW;
844 LDR_RESOURCE_INFO info;
845 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
846 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
847 const IMAGE_RESOURCE_DIR_STRING_U *str;
849 TRACE( "%p %s %p %Ix\n", module, debugstr_w(type), func, param );
851 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
852 FIXME( "unimplemented flags: %lx\n", flags );
854 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
855 if (!(flags & RESOURCE_ENUM_LN)) return ret;
857 if (!module) module = GetModuleHandleW( 0 );
858 typeW.Buffer = NULL;
859 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
860 goto done;
861 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
862 goto done;
863 info.Type = (ULONG_PTR)typeW.Buffer;
864 if ((status = LdrFindResourceDirectory_U( module, &info, 1, &resdir )) != STATUS_SUCCESS)
865 goto done;
867 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
868 __TRY
870 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
872 if (et[i].u.s.NameIsString)
874 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u.s.NameOffset);
875 if (str->Length + 1 > len)
877 len = str->Length + 1;
878 HeapFree( GetProcessHeap(), 0, name );
879 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
881 ret = FALSE;
882 break;
885 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
886 name[str->Length] = 0;
887 ret = func( module, type, name, param );
889 else
891 ret = func( module, type, UIntToPtr(et[i].u.Id), param );
893 if (!ret) break;
896 __EXCEPT_PAGE_FAULT
898 ret = FALSE;
899 status = STATUS_ACCESS_VIOLATION;
901 __ENDTRY
902 done:
903 HeapFree( GetProcessHeap(), 0, name );
904 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
905 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
906 return ret;
910 /**********************************************************************
911 * EnumResourceNamesW (kernelbase.@)
913 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesW( HMODULE module, LPCWSTR type,
914 ENUMRESNAMEPROCW func, LONG_PTR param )
916 return EnumResourceNamesExW( module, type, func, param, 0, 0 );
920 /**********************************************************************
921 * EnumResourceTypesExA (kernelbase.@)
923 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceTypesExA( HMODULE module, ENUMRESTYPEPROCA func, LONG_PTR param,
924 DWORD flags, LANGID lang )
926 int i;
927 BOOL ret = FALSE;
928 LPSTR type = NULL;
929 DWORD len = 0, newlen;
930 const IMAGE_RESOURCE_DIRECTORY *resdir;
931 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
932 const IMAGE_RESOURCE_DIR_STRING_U *str;
934 TRACE( "%p %p %Ix\n", module, func, param );
936 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
937 FIXME( "unimplemented flags: %lx\n", flags );
939 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
940 if (!(flags & RESOURCE_ENUM_LN)) return ret;
942 if (!module) module = GetModuleHandleW( 0 );
944 if (!set_ntstatus( LdrFindResourceDirectory_U( module, NULL, 0, &resdir ))) return FALSE;
946 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
947 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
949 if (et[i].u.s.NameIsString)
951 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u.s.NameOffset);
952 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
953 if (newlen + 1 > len)
955 len = newlen + 1;
956 HeapFree( GetProcessHeap(), 0, type );
957 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
959 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
960 type[newlen] = 0;
961 ret = func( module, type, param );
963 else
965 ret = func( module, UIntToPtr(et[i].u.Id), param );
967 if (!ret) break;
969 HeapFree( GetProcessHeap(), 0, type );
970 return ret;
974 /**********************************************************************
975 * EnumResourceTypesExW (kernelbase.@)
977 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceTypesExW( HMODULE module, ENUMRESTYPEPROCW func, LONG_PTR param,
978 DWORD flags, LANGID lang )
980 int i, len = 0;
981 BOOL ret = FALSE;
982 LPWSTR type = NULL;
983 const IMAGE_RESOURCE_DIRECTORY *resdir;
984 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
985 const IMAGE_RESOURCE_DIR_STRING_U *str;
987 TRACE( "%p %p %Ix\n", module, func, param );
989 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
990 if (!(flags & RESOURCE_ENUM_LN)) return ret;
992 if (!module) module = GetModuleHandleW( 0 );
994 if (!set_ntstatus( LdrFindResourceDirectory_U( module, NULL, 0, &resdir ))) return FALSE;
996 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
997 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
999 if (et[i].u.s.NameIsString)
1001 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u.s.NameOffset);
1002 if (str->Length + 1 > len)
1004 len = str->Length + 1;
1005 HeapFree( GetProcessHeap(), 0, type );
1006 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
1008 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
1009 type[str->Length] = 0;
1010 ret = func( module, type, param );
1012 else
1014 ret = func( module, UIntToPtr(et[i].u.Id), param );
1016 if (!ret) break;
1018 HeapFree( GetProcessHeap(), 0, type );
1019 return ret;
1023 /**********************************************************************
1024 * FindResourceExW (kernelbase.@)
1026 HRSRC WINAPI DECLSPEC_HOTPATCH FindResourceExW( HMODULE module, LPCWSTR type, LPCWSTR name, WORD lang )
1028 NTSTATUS status;
1029 UNICODE_STRING nameW, typeW;
1030 LDR_RESOURCE_INFO info;
1031 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
1033 TRACE( "%p %s %s %04x\n", module, debugstr_w(type), debugstr_w(name), lang );
1035 if (!module) module = GetModuleHandleW( 0 );
1036 nameW.Buffer = typeW.Buffer = NULL;
1038 __TRY
1040 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
1041 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
1042 info.Type = (ULONG_PTR)typeW.Buffer;
1043 info.Name = (ULONG_PTR)nameW.Buffer;
1044 info.Language = lang;
1045 status = LdrFindResource_U( module, &info, 3, &entry );
1046 done:
1047 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1049 __EXCEPT_PAGE_FAULT
1051 SetLastError( ERROR_INVALID_PARAMETER );
1053 __ENDTRY
1055 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
1056 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
1057 return (HRSRC)entry;
1061 /**********************************************************************
1062 * FindResourceW (kernelbase.@)
1064 HRSRC WINAPI DECLSPEC_HOTPATCH FindResourceW( HINSTANCE module, LPCWSTR name, LPCWSTR type )
1066 return FindResourceExW( module, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
1070 /**********************************************************************
1071 * FreeResource (kernelbase.@)
1073 BOOL WINAPI DECLSPEC_HOTPATCH FreeResource( HGLOBAL handle )
1075 return FALSE;
1079 /**********************************************************************
1080 * LoadResource (kernelbase.@)
1082 HGLOBAL WINAPI DECLSPEC_HOTPATCH LoadResource( HINSTANCE module, HRSRC rsrc )
1084 void *ret;
1086 TRACE( "%p %p\n", module, rsrc );
1088 if (!rsrc) return 0;
1089 if (!module) module = GetModuleHandleW( 0 );
1090 if (!set_ntstatus( LdrAccessResource( module, (IMAGE_RESOURCE_DATA_ENTRY *)rsrc, &ret, NULL )))
1091 return 0;
1092 return ret;
1096 /**********************************************************************
1097 * LockResource (kernelbase.@)
1099 LPVOID WINAPI DECLSPEC_HOTPATCH LockResource( HGLOBAL handle )
1101 return handle;
1105 /**********************************************************************
1106 * SizeofResource (kernelbase.@)
1108 DWORD WINAPI DECLSPEC_HOTPATCH SizeofResource( HINSTANCE module, HRSRC rsrc )
1110 if (!rsrc) return 0;
1111 return ((IMAGE_RESOURCE_DATA_ENTRY *)rsrc)->Size;
1115 /***********************************************************************
1116 * Activation contexts
1117 ***********************************************************************/
1120 /***********************************************************************
1121 * ActivateActCtx (kernelbase.@)
1123 BOOL WINAPI DECLSPEC_HOTPATCH ActivateActCtx( HANDLE context, ULONG_PTR *cookie )
1125 return set_ntstatus( RtlActivateActivationContext( 0, context, cookie ));
1129 /***********************************************************************
1130 * AddRefActCtx (kernelbase.@)
1132 void WINAPI DECLSPEC_HOTPATCH AddRefActCtx( HANDLE context )
1134 RtlAddRefActivationContext( context );
1138 /***********************************************************************
1139 * CreateActCtxW (kernelbase.@)
1141 HANDLE WINAPI DECLSPEC_HOTPATCH CreateActCtxW( PCACTCTXW ctx )
1143 HANDLE context;
1145 TRACE( "%p %08lx\n", ctx, ctx ? ctx->dwFlags : 0 );
1147 if (!set_ntstatus( RtlCreateActivationContext( &context, ctx ))) return INVALID_HANDLE_VALUE;
1148 return context;
1152 /***********************************************************************
1153 * DeactivateActCtx (kernelbase.@)
1155 BOOL WINAPI DECLSPEC_HOTPATCH DeactivateActCtx( DWORD flags, ULONG_PTR cookie )
1157 RtlDeactivateActivationContext( flags, cookie );
1158 return TRUE;
1162 /***********************************************************************
1163 * FindActCtxSectionGuid (kernelbase.@)
1165 BOOL WINAPI DECLSPEC_HOTPATCH FindActCtxSectionGuid( DWORD flags, const GUID *ext_guid, ULONG id,
1166 const GUID *guid, PACTCTX_SECTION_KEYED_DATA info )
1168 return set_ntstatus( RtlFindActivationContextSectionGuid( flags, ext_guid, id, guid, info ));
1172 /***********************************************************************
1173 * FindActCtxSectionStringW (kernelbase.@)
1175 BOOL WINAPI DECLSPEC_HOTPATCH FindActCtxSectionStringW( DWORD flags, const GUID *ext_guid, ULONG id,
1176 LPCWSTR str, PACTCTX_SECTION_KEYED_DATA info )
1178 UNICODE_STRING us;
1180 if (!info)
1182 SetLastError( ERROR_INVALID_PARAMETER );
1183 return FALSE;
1185 RtlInitUnicodeString( &us, str );
1186 return set_ntstatus( RtlFindActivationContextSectionString( flags, ext_guid, id, &us, info ));
1190 /***********************************************************************
1191 * GetCurrentActCtx (kernelbase.@)
1193 BOOL WINAPI DECLSPEC_HOTPATCH GetCurrentActCtx( HANDLE *pcontext )
1195 return set_ntstatus( RtlGetActiveActivationContext( pcontext ));
1199 /***********************************************************************
1200 * QueryActCtxSettingsW (kernelbase.@)
1202 BOOL WINAPI DECLSPEC_HOTPATCH QueryActCtxSettingsW( DWORD flags, HANDLE ctx, const WCHAR *ns,
1203 const WCHAR *settings, WCHAR *buffer, SIZE_T size,
1204 SIZE_T *written )
1206 return set_ntstatus( RtlQueryActivationContextApplicationSettings( flags, ctx, ns, settings,
1207 buffer, size, written ));
1211 /***********************************************************************
1212 * QueryActCtxW (kernelbase.@)
1214 BOOL WINAPI DECLSPEC_HOTPATCH QueryActCtxW( DWORD flags, HANDLE context, PVOID inst, ULONG class,
1215 PVOID buffer, SIZE_T size, SIZE_T *written )
1217 return set_ntstatus( RtlQueryInformationActivationContext( flags, context, inst, class,
1218 buffer, size, written ));
1222 /***********************************************************************
1223 * ReleaseActCtx (kernelbase.@)
1225 void WINAPI DECLSPEC_HOTPATCH ReleaseActCtx( HANDLE context )
1227 RtlReleaseActivationContext( context );
1231 /***********************************************************************
1232 * ZombifyActCtx (kernelbase.@)
1234 BOOL WINAPI DECLSPEC_HOTPATCH ZombifyActCtx( HANDLE context )
1236 return set_ntstatus( RtlZombifyActivationContext( context ));