mfreadwrite/reader: Add missing allocation check (Coverity).
[wine/zf.git] / dlls / kernelbase / loader.c
blobac463528e3c8c5d21a0403ce6f6389aea324c449
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 );
52 /***********************************************************************
53 * Modules
54 ***********************************************************************/
57 /******************************************************************
58 * get_proc_address
60 FARPROC WINAPI get_proc_address( HMODULE module, LPCSTR function )
62 FARPROC proc;
63 ANSI_STRING str;
65 if (!module) module = NtCurrentTeb()->Peb->ImageBaseAddress;
67 if ((ULONG_PTR)function >> 16)
69 RtlInitAnsiString( &str, function );
70 if (!set_ntstatus( LdrGetProcedureAddress( module, &str, 0, (void**)&proc ))) return NULL;
72 else if (!set_ntstatus( LdrGetProcedureAddress( module, NULL, LOWORD(function), (void**)&proc )))
73 return NULL;
75 return proc;
79 /******************************************************************
80 * load_library_as_datafile
82 static BOOL load_library_as_datafile( LPCWSTR load_path, DWORD flags, LPCWSTR name, HMODULE *mod_ret )
84 WCHAR filenameW[MAX_PATH];
85 HANDLE mapping, file = INVALID_HANDLE_VALUE;
86 HMODULE module = 0;
87 DWORD protect = PAGE_READONLY;
89 *mod_ret = 0;
91 if (flags & LOAD_LIBRARY_AS_IMAGE_RESOURCE) protect |= SEC_IMAGE;
93 if (SearchPathW( NULL, name, L".dll", ARRAY_SIZE( filenameW ), filenameW, NULL ))
95 file = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
96 NULL, OPEN_EXISTING, 0, 0 );
98 if (file == INVALID_HANDLE_VALUE) return FALSE;
100 mapping = CreateFileMappingW( file, NULL, protect, 0, 0, NULL );
101 if (!mapping) goto failed;
103 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
104 CloseHandle( mapping );
105 if (!module) goto failed;
107 if (!(flags & LOAD_LIBRARY_AS_IMAGE_RESOURCE))
109 /* make sure it's a valid PE file */
110 if (!RtlImageNtHeader( module ))
112 SetLastError( ERROR_BAD_EXE_FORMAT );
113 goto failed;
115 *mod_ret = (HMODULE)((char *)module + 1); /* set bit 0 for data file module */
117 if (flags & LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE)
119 struct exclusive_datafile *datafile = HeapAlloc( GetProcessHeap(), 0, sizeof(*datafile) );
120 if (!datafile) goto failed;
121 datafile->module = *mod_ret;
122 datafile->file = file;
123 list_add_head( &exclusive_datafile_list, &datafile->entry );
124 TRACE( "delaying close %p for module %p\n", datafile->file, datafile->module );
125 return TRUE;
128 else *mod_ret = (HMODULE)((char *)module + 2); /* set bit 1 for image resource module */
130 CloseHandle( file );
131 return TRUE;
133 failed:
134 if (module) UnmapViewOfFile( module );
135 CloseHandle( file );
136 return FALSE;
140 /******************************************************************
141 * load_library
143 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
145 const DWORD unsupported_flags = LOAD_IGNORE_CODE_AUTHZ_LEVEL | LOAD_LIBRARY_REQUIRE_SIGNED_TARGET;
146 NTSTATUS status;
147 HMODULE module;
148 WCHAR *load_path, *dummy;
150 if (flags & unsupported_flags) FIXME( "unsupported flag(s) used %#08x\n", flags );
152 if (!set_ntstatus( LdrGetDllPath( libname->Buffer, flags, &load_path, &dummy ))) return 0;
154 if (flags & (LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE |
155 LOAD_LIBRARY_AS_IMAGE_RESOURCE))
157 ULONG_PTR magic;
159 LdrLockLoaderLock( 0, NULL, &magic );
160 if (!LdrGetDllHandle( load_path, flags, libname, &module ))
161 LdrAddRefDll( 0, module );
162 else
163 load_library_as_datafile( load_path, flags, libname->Buffer, &module );
164 LdrUnlockLoaderLock( 0, magic );
166 else
168 status = LdrLoadDll( load_path, flags, libname, &module );
169 if (!set_ntstatus( status ))
171 module = 0;
172 if (status == STATUS_DLL_NOT_FOUND && (GetVersion() & 0x80000000))
173 SetLastError( ERROR_DLL_NOT_FOUND );
177 RtlReleasePath( load_path );
178 return module;
182 /****************************************************************************
183 * AddDllDirectory (kernelbase.@)
185 DLL_DIRECTORY_COOKIE WINAPI DECLSPEC_HOTPATCH AddDllDirectory( const WCHAR *dir )
187 UNICODE_STRING str;
188 void *cookie;
190 RtlInitUnicodeString( &str, dir );
191 if (!set_ntstatus( LdrAddDllDirectory( &str, &cookie ))) return NULL;
192 return cookie;
196 /***********************************************************************
197 * DelayLoadFailureHook (kernelbase.@)
199 FARPROC WINAPI DECLSPEC_HOTPATCH DelayLoadFailureHook( LPCSTR name, LPCSTR function )
201 ULONG_PTR args[2];
203 if ((ULONG_PTR)function >> 16)
204 ERR( "failed to delay load %s.%s\n", name, function );
205 else
206 ERR( "failed to delay load %s.%u\n", name, LOWORD(function) );
207 args[0] = (ULONG_PTR)name;
208 args[1] = (ULONG_PTR)function;
209 RaiseException( EXCEPTION_WINE_STUB, EH_NONCONTINUABLE, 2, args );
210 return NULL;
214 /****************************************************************************
215 * DisableThreadLibraryCalls (kernelbase.@)
217 BOOL WINAPI DECLSPEC_HOTPATCH DisableThreadLibraryCalls( HMODULE module )
219 return set_ntstatus( LdrDisableThreadCalloutsForDll( module ));
223 /***********************************************************************
224 * FreeLibrary (kernelbase.@)
226 BOOL WINAPI DECLSPEC_HOTPATCH FreeLibrary( HINSTANCE module )
228 if (!module)
230 SetLastError( ERROR_INVALID_HANDLE );
231 return FALSE;
234 if ((ULONG_PTR)module & 3) /* this is a datafile module */
236 void *ptr = (void *)((ULONG_PTR)module & ~3);
237 if (!RtlImageNtHeader( ptr ))
239 SetLastError( ERROR_BAD_EXE_FORMAT );
240 return FALSE;
242 if ((ULONG_PTR)module & 1)
244 struct exclusive_datafile *file;
245 ULONG_PTR magic;
247 LdrLockLoaderLock( 0, NULL, &magic );
248 LIST_FOR_EACH_ENTRY( file, &exclusive_datafile_list, struct exclusive_datafile, entry )
250 if (file->module != module) continue;
251 TRACE( "closing %p for module %p\n", file->file, file->module );
252 CloseHandle( file->file );
253 list_remove( &file->entry );
254 HeapFree( GetProcessHeap(), 0, file );
255 break;
257 LdrUnlockLoaderLock( 0, magic );
259 return UnmapViewOfFile( ptr );
262 return set_ntstatus( LdrUnloadDll( module ));
266 /***********************************************************************
267 * GetModuleFileNameA (kernelbase.@)
269 DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameA( HMODULE module, LPSTR filename, DWORD size )
271 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
272 DWORD len;
274 if (!filenameW)
276 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
277 return 0;
279 if ((len = GetModuleFileNameW( module, filenameW, size )))
281 len = file_name_WtoA( filenameW, len, filename, size );
282 if (len < size)
283 filename[len] = 0;
284 else
285 SetLastError( ERROR_INSUFFICIENT_BUFFER );
287 HeapFree( GetProcessHeap(), 0, filenameW );
288 return len;
292 /***********************************************************************
293 * GetModuleFileNameW (kernelbase.@)
295 DWORD WINAPI DECLSPEC_HOTPATCH GetModuleFileNameW( HMODULE module, LPWSTR filename, DWORD size )
297 ULONG len = 0;
298 ULONG_PTR magic;
299 LDR_DATA_TABLE_ENTRY *pldr;
300 WIN16_SUBSYSTEM_TIB *win16_tib;
302 if (!module && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
304 len = min( size, win16_tib->exe_name->Length / sizeof(WCHAR) );
305 memcpy( filename, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
306 if (len < size) filename[len] = 0;
307 goto done;
310 LdrLockLoaderLock( 0, NULL, &magic );
312 if (!module) module = NtCurrentTeb()->Peb->ImageBaseAddress;
313 if (set_ntstatus( LdrFindEntryForAddress( module, &pldr )))
315 len = min( size, pldr->FullDllName.Length / sizeof(WCHAR) );
316 memcpy( filename, pldr->FullDllName.Buffer, len * sizeof(WCHAR) );
317 if (len < size)
319 filename[len] = 0;
320 SetLastError( 0 );
322 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
325 LdrUnlockLoaderLock( 0, magic );
326 done:
327 TRACE( "%s\n", debugstr_wn(filename, len) );
328 return len;
332 /***********************************************************************
333 * GetModuleHandleA (kernelbase.@)
335 HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA( LPCSTR module )
337 HMODULE ret;
339 GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret );
340 return ret;
344 /***********************************************************************
345 * GetModuleHandleW (kernelbase.@)
347 HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleW( LPCWSTR module )
349 HMODULE ret;
351 GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, module, &ret );
352 return ret;
356 /***********************************************************************
357 * GetModuleHandleExA (kernelbase.@)
359 BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExA( DWORD flags, LPCSTR name, HMODULE *module )
361 WCHAR *nameW;
363 if (!name || (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS))
364 return GetModuleHandleExW( flags, (LPCWSTR)name, module );
366 if (!(nameW = file_name_AtoW( name, FALSE ))) return FALSE;
367 return GetModuleHandleExW( flags, nameW, module );
371 /***********************************************************************
372 * GetModuleHandleExW (kernelbase.@)
374 BOOL WINAPI DECLSPEC_HOTPATCH GetModuleHandleExW( DWORD flags, LPCWSTR name, HMODULE *module )
376 NTSTATUS status = STATUS_SUCCESS;
377 HMODULE ret = NULL;
378 ULONG_PTR magic;
379 BOOL lock;
381 if (!module)
383 SetLastError( ERROR_INVALID_PARAMETER );
384 return FALSE;
387 /* if we are messing with the refcount, grab the loader lock */
388 lock = (flags & GET_MODULE_HANDLE_EX_FLAG_PIN) || !(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT);
389 if (lock) LdrLockLoaderLock( 0, NULL, &magic );
391 if (!name)
393 ret = NtCurrentTeb()->Peb->ImageBaseAddress;
395 else if (flags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
397 void *dummy;
398 if (!(ret = RtlPcToFileHeader( (void *)name, &dummy ))) status = STATUS_DLL_NOT_FOUND;
400 else
402 UNICODE_STRING wstr;
403 RtlInitUnicodeString( &wstr, name );
404 status = LdrGetDllHandle( NULL, 0, &wstr, &ret );
407 if (status == STATUS_SUCCESS)
409 if (flags & GET_MODULE_HANDLE_EX_FLAG_PIN)
410 LdrAddRefDll( LDR_ADDREF_DLL_PIN, ret );
411 else if (!(flags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
412 LdrAddRefDll( 0, ret );
415 if (lock) LdrUnlockLoaderLock( 0, magic );
417 *module = ret;
418 return set_ntstatus( status );
422 /***********************************************************************
423 * GetProcAddress (kernelbase.@)
426 #ifdef __x86_64__
428 * Work around a Delphi bug on x86_64. When delay loading a symbol,
429 * Delphi saves rcx, rdx, r8 and r9 to the stack. It then calls
430 * GetProcAddress(), pops the saved registers and calls the function.
431 * This works fine if all of the parameters are ints. However, since
432 * it does not save xmm0 - 3, it relies on GetProcAddress() preserving
433 * these registers if the function takes floating point parameters.
434 * This wrapper saves xmm0 - 3 to the stack.
436 __ASM_GLOBAL_FUNC( GetProcAddress,
437 ".byte 0x48\n\t" /* hotpatch prolog */
438 "pushq %rbp\n\t"
439 __ASM_SEH(".seh_pushreg %rbp\n\t")
440 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
441 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
442 "movq %rsp,%rbp\n\t"
443 __ASM_SEH(".seh_setframe %rbp,0\n\t")
444 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
445 __ASM_SEH(".seh_endprologue\n\t")
446 "subq $0x60,%rsp\n\t"
447 "andq $~15,%rsp\n\t"
448 "movaps %xmm0,0x20(%rsp)\n\t"
449 "movaps %xmm1,0x30(%rsp)\n\t"
450 "movaps %xmm2,0x40(%rsp)\n\t"
451 "movaps %xmm3,0x50(%rsp)\n\t"
452 "call " __ASM_NAME("get_proc_address") "\n\t"
453 "movaps 0x50(%rsp), %xmm3\n\t"
454 "movaps 0x40(%rsp), %xmm2\n\t"
455 "movaps 0x30(%rsp), %xmm1\n\t"
456 "movaps 0x20(%rsp), %xmm0\n\t"
457 "leaq 0(%rbp),%rsp\n\t"
458 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
459 "popq %rbp\n\t"
460 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
461 __ASM_CFI(".cfi_same_value %rbp\n\t")
462 "ret" )
463 #else /* __x86_64__ */
465 FARPROC WINAPI DECLSPEC_HOTPATCH GetProcAddress( HMODULE module, LPCSTR function )
467 return get_proc_address( module, function );
470 #endif /* __x86_64__ */
473 /***********************************************************************
474 * LoadLibraryA (kernelbase.@)
476 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryA( LPCSTR name )
478 return LoadLibraryExA( name, 0, 0 );
482 /***********************************************************************
483 * LoadLibraryW (kernelbase.@)
485 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryW( LPCWSTR name )
487 return LoadLibraryExW( name, 0, 0 );
491 /******************************************************************
492 * LoadLibraryExA (kernelbase.@)
494 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryExA( LPCSTR name, HANDLE file, DWORD flags )
496 WCHAR *nameW;
498 if (!(nameW = file_name_AtoW( name, FALSE ))) return 0;
499 return LoadLibraryExW( nameW, file, flags );
503 /***********************************************************************
504 * LoadLibraryExW (kernelbase.@)
506 HMODULE WINAPI DECLSPEC_HOTPATCH LoadLibraryExW( LPCWSTR name, HANDLE file, DWORD flags )
508 UNICODE_STRING str;
509 HMODULE module;
511 if (!name)
513 SetLastError( ERROR_INVALID_PARAMETER );
514 return 0;
516 RtlInitUnicodeString( &str, name );
517 if (str.Buffer[str.Length/sizeof(WCHAR) - 1] != ' ') return load_library( &str, flags );
519 /* library name has trailing spaces */
520 RtlCreateUnicodeString( &str, name );
521 while (str.Length > sizeof(WCHAR) && str.Buffer[str.Length/sizeof(WCHAR) - 1] == ' ')
522 str.Length -= sizeof(WCHAR);
524 str.Buffer[str.Length/sizeof(WCHAR)] = 0;
525 module = load_library( &str, flags );
526 RtlFreeUnicodeString( &str );
527 return module;
531 /***********************************************************************
532 * LoadPackagedLibrary (kernelbase.@)
534 HMODULE WINAPI /* DECLSPEC_HOTPATCH */ LoadPackagedLibrary( LPCWSTR name, DWORD reserved )
536 FIXME( "semi-stub, name %s, reserved %#x.\n", debugstr_w(name), reserved );
537 SetLastError( APPMODEL_ERROR_NO_PACKAGE );
538 return NULL;
542 /***********************************************************************
543 * LoadAppInitDlls (kernelbase.@)
545 void WINAPI LoadAppInitDlls(void)
547 TRACE( "\n" );
551 /****************************************************************************
552 * RemoveDllDirectory (kernelbase.@)
554 BOOL WINAPI DECLSPEC_HOTPATCH RemoveDllDirectory( DLL_DIRECTORY_COOKIE cookie )
556 return set_ntstatus( LdrRemoveDllDirectory( cookie ));
560 /*************************************************************************
561 * SetDefaultDllDirectories (kernelbase.@)
563 BOOL WINAPI DECLSPEC_HOTPATCH SetDefaultDllDirectories( DWORD flags )
565 return set_ntstatus( LdrSetDefaultDllDirectories( flags ));
569 /***********************************************************************
570 * Resources
571 ***********************************************************************/
574 #define IS_INTRESOURCE(x) (((ULONG_PTR)(x) >> 16) == 0)
576 /* retrieve the resource name to pass to the ntdll functions */
577 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
579 if (IS_INTRESOURCE(name))
581 str->Buffer = ULongToPtr( LOWORD(name) );
582 return STATUS_SUCCESS;
584 if (name[0] == '#')
586 ULONG value;
587 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
588 return STATUS_INVALID_PARAMETER;
589 str->Buffer = ULongToPtr(value);
590 return STATUS_SUCCESS;
592 RtlCreateUnicodeStringFromAsciiz( str, name );
593 RtlUpcaseUnicodeString( str, str, FALSE );
594 return STATUS_SUCCESS;
597 /* retrieve the resource name to pass to the ntdll functions */
598 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
600 if (IS_INTRESOURCE(name))
602 str->Buffer = ULongToPtr( LOWORD(name) );
603 return STATUS_SUCCESS;
605 if (name[0] == '#')
607 ULONG value;
608 RtlInitUnicodeString( str, name + 1 );
609 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
610 return STATUS_INVALID_PARAMETER;
611 str->Buffer = ULongToPtr(value);
612 return STATUS_SUCCESS;
614 RtlCreateUnicodeString( str, name );
615 RtlUpcaseUnicodeString( str, str, FALSE );
616 return STATUS_SUCCESS;
620 /**********************************************************************
621 * EnumResourceLanguagesExA (kernelbase.@)
623 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExA( HMODULE module, LPCSTR type, LPCSTR name,
624 ENUMRESLANGPROCA func, LONG_PTR param,
625 DWORD flags, LANGID lang )
627 int i;
628 BOOL ret = FALSE;
629 NTSTATUS status;
630 UNICODE_STRING typeW, nameW;
631 LDR_RESOURCE_INFO info;
632 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
633 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
635 TRACE( "%p %s %s %p %lx %x %d\n", module, debugstr_a(type), debugstr_a(name),
636 func, param, flags, lang );
638 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
639 FIXME( "unimplemented flags: %x\n", flags );
641 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
642 if (!(flags & RESOURCE_ENUM_LN)) return ret;
644 if (!module) module = GetModuleHandleW( 0 );
645 typeW.Buffer = nameW.Buffer = NULL;
646 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
647 goto done;
648 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
649 goto done;
650 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
651 goto done;
652 info.Type = (ULONG_PTR)typeW.Buffer;
653 info.Name = (ULONG_PTR)nameW.Buffer;
654 if ((status = LdrFindResourceDirectory_U( module, &info, 2, &resdir )) != STATUS_SUCCESS)
655 goto done;
657 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
658 __TRY
660 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
662 ret = func( module, type, name, et[i].u.Id, param );
663 if (!ret) break;
666 __EXCEPT_PAGE_FAULT
668 ret = FALSE;
669 status = STATUS_ACCESS_VIOLATION;
671 __ENDTRY
672 done:
673 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
674 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
675 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
676 return ret;
680 /**********************************************************************
681 * EnumResourceLanguagesExW (kernelbase.@)
683 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceLanguagesExW( HMODULE module, LPCWSTR type, LPCWSTR name,
684 ENUMRESLANGPROCW func, LONG_PTR param,
685 DWORD flags, LANGID lang )
687 int i;
688 BOOL ret = FALSE;
689 NTSTATUS status;
690 UNICODE_STRING typeW, nameW;
691 LDR_RESOURCE_INFO info;
692 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
693 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
695 TRACE( "%p %s %s %p %lx %x %d\n", module, debugstr_w(type), debugstr_w(name),
696 func, param, flags, lang );
698 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
699 FIXME( "unimplemented flags: %x\n", flags );
701 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
702 if (!(flags & RESOURCE_ENUM_LN)) return ret;
704 if (!module) module = GetModuleHandleW( 0 );
705 typeW.Buffer = nameW.Buffer = NULL;
706 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
707 goto done;
708 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
709 goto done;
710 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
711 goto done;
712 info.Type = (ULONG_PTR)typeW.Buffer;
713 info.Name = (ULONG_PTR)nameW.Buffer;
714 if ((status = LdrFindResourceDirectory_U( module, &info, 2, &resdir )) != STATUS_SUCCESS)
715 goto done;
717 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
718 __TRY
720 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
722 ret = func( module, type, name, et[i].u.Id, param );
723 if (!ret) break;
726 __EXCEPT_PAGE_FAULT
728 ret = FALSE;
729 status = STATUS_ACCESS_VIOLATION;
731 __ENDTRY
732 done:
733 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
734 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
735 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
736 return ret;
740 /**********************************************************************
741 * EnumResourceNamesExA (kernelbase.@)
743 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesExA( HMODULE module, LPCSTR type, ENUMRESNAMEPROCA func,
744 LONG_PTR param, DWORD flags, LANGID lang )
746 int i;
747 BOOL ret = FALSE;
748 DWORD len = 0, newlen;
749 LPSTR name = NULL;
750 NTSTATUS status;
751 UNICODE_STRING typeW;
752 LDR_RESOURCE_INFO info;
753 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
754 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
755 const IMAGE_RESOURCE_DIR_STRING_U *str;
757 TRACE( "%p %s %p %lx\n", module, debugstr_a(type), func, param );
759 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
760 FIXME( "unimplemented flags: %x\n", flags );
762 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
763 if (!(flags & RESOURCE_ENUM_LN)) return ret;
765 if (!module) module = GetModuleHandleW( 0 );
766 typeW.Buffer = NULL;
767 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
768 goto done;
769 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
770 goto done;
771 info.Type = (ULONG_PTR)typeW.Buffer;
772 if ((status = LdrFindResourceDirectory_U( module, &info, 1, &resdir )) != STATUS_SUCCESS)
773 goto done;
775 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
776 __TRY
778 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
780 if (et[i].u.s.NameIsString)
782 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u.s.NameOffset);
783 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
784 if (newlen + 1 > len)
786 len = newlen + 1;
787 HeapFree( GetProcessHeap(), 0, name );
788 if (!(name = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
790 ret = FALSE;
791 break;
794 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
795 name[newlen] = 0;
796 ret = func( module, type, name, param );
798 else
800 ret = func( module, type, UIntToPtr(et[i].u.Id), param );
802 if (!ret) break;
805 __EXCEPT_PAGE_FAULT
807 ret = FALSE;
808 status = STATUS_ACCESS_VIOLATION;
810 __ENDTRY
812 done:
813 HeapFree( GetProcessHeap(), 0, name );
814 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
815 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
816 return ret;
820 /**********************************************************************
821 * EnumResourceNamesExW (kernelbase.@)
823 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesExW( HMODULE module, LPCWSTR type, ENUMRESNAMEPROCW func,
824 LONG_PTR param, DWORD flags, LANGID lang )
826 int i, len = 0;
827 BOOL ret = FALSE;
828 LPWSTR name = NULL;
829 NTSTATUS status;
830 UNICODE_STRING typeW;
831 LDR_RESOURCE_INFO info;
832 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
833 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
834 const IMAGE_RESOURCE_DIR_STRING_U *str;
836 TRACE( "%p %s %p %lx\n", module, debugstr_w(type), func, param );
838 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
839 FIXME( "unimplemented flags: %x\n", flags );
841 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
842 if (!(flags & RESOURCE_ENUM_LN)) return ret;
844 if (!module) module = GetModuleHandleW( 0 );
845 typeW.Buffer = NULL;
846 if ((status = LdrFindResourceDirectory_U( module, NULL, 0, &basedir )) != STATUS_SUCCESS)
847 goto done;
848 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
849 goto done;
850 info.Type = (ULONG_PTR)typeW.Buffer;
851 if ((status = LdrFindResourceDirectory_U( module, &info, 1, &resdir )) != STATUS_SUCCESS)
852 goto done;
854 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
855 __TRY
857 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
859 if (et[i].u.s.NameIsString)
861 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)basedir + et[i].u.s.NameOffset);
862 if (str->Length + 1 > len)
864 len = str->Length + 1;
865 HeapFree( GetProcessHeap(), 0, name );
866 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
868 ret = FALSE;
869 break;
872 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
873 name[str->Length] = 0;
874 ret = func( module, type, name, param );
876 else
878 ret = func( module, type, UIntToPtr(et[i].u.Id), param );
880 if (!ret) break;
883 __EXCEPT_PAGE_FAULT
885 ret = FALSE;
886 status = STATUS_ACCESS_VIOLATION;
888 __ENDTRY
889 done:
890 HeapFree( GetProcessHeap(), 0, name );
891 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
892 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
893 return ret;
897 /**********************************************************************
898 * EnumResourceNamesW (kernelbase.@)
900 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceNamesW( HMODULE module, LPCWSTR type,
901 ENUMRESNAMEPROCW func, LONG_PTR param )
903 return EnumResourceNamesExW( module, type, func, param, 0, 0 );
907 /**********************************************************************
908 * EnumResourceTypesExA (kernelbase.@)
910 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceTypesExA( HMODULE module, ENUMRESTYPEPROCA func, LONG_PTR param,
911 DWORD flags, LANGID lang )
913 int i;
914 BOOL ret = FALSE;
915 LPSTR type = NULL;
916 DWORD len = 0, newlen;
917 const IMAGE_RESOURCE_DIRECTORY *resdir;
918 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
919 const IMAGE_RESOURCE_DIR_STRING_U *str;
921 TRACE( "%p %p %lx\n", module, func, param );
923 if (flags & (RESOURCE_ENUM_MUI | RESOURCE_ENUM_MUI_SYSTEM | RESOURCE_ENUM_VALIDATE))
924 FIXME( "unimplemented flags: %x\n", flags );
926 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
927 if (!(flags & RESOURCE_ENUM_LN)) return ret;
929 if (!module) module = GetModuleHandleW( 0 );
931 if (!set_ntstatus( LdrFindResourceDirectory_U( module, NULL, 0, &resdir ))) return FALSE;
933 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
934 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
936 if (et[i].u.s.NameIsString)
938 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u.s.NameOffset);
939 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
940 if (newlen + 1 > len)
942 len = newlen + 1;
943 HeapFree( GetProcessHeap(), 0, type );
944 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
946 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
947 type[newlen] = 0;
948 ret = func( module, type, param );
950 else
952 ret = func( module, UIntToPtr(et[i].u.Id), param );
954 if (!ret) break;
956 HeapFree( GetProcessHeap(), 0, type );
957 return ret;
961 /**********************************************************************
962 * EnumResourceTypesExW (kernelbase.@)
964 BOOL WINAPI DECLSPEC_HOTPATCH EnumResourceTypesExW( HMODULE module, ENUMRESTYPEPROCW func, LONG_PTR param,
965 DWORD flags, LANGID lang )
967 int i, len = 0;
968 BOOL ret = FALSE;
969 LPWSTR type = NULL;
970 const IMAGE_RESOURCE_DIRECTORY *resdir;
971 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
972 const IMAGE_RESOURCE_DIR_STRING_U *str;
974 TRACE( "%p %p %lx\n", module, func, param );
976 if (!flags) flags = RESOURCE_ENUM_LN | RESOURCE_ENUM_MUI;
977 if (!(flags & RESOURCE_ENUM_LN)) return ret;
979 if (!module) module = GetModuleHandleW( 0 );
981 if (!set_ntstatus( LdrFindResourceDirectory_U( module, NULL, 0, &resdir ))) return FALSE;
983 et = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
984 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
986 if (et[i].u.s.NameIsString)
988 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const BYTE *)resdir + et[i].u.s.NameOffset);
989 if (str->Length + 1 > len)
991 len = str->Length + 1;
992 HeapFree( GetProcessHeap(), 0, type );
993 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
995 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
996 type[str->Length] = 0;
997 ret = func( module, type, param );
999 else
1001 ret = func( module, UIntToPtr(et[i].u.Id), param );
1003 if (!ret) break;
1005 HeapFree( GetProcessHeap(), 0, type );
1006 return ret;
1010 /**********************************************************************
1011 * FindResourceExW (kernelbase.@)
1013 HRSRC WINAPI DECLSPEC_HOTPATCH FindResourceExW( HMODULE module, LPCWSTR type, LPCWSTR name, WORD lang )
1015 NTSTATUS status;
1016 UNICODE_STRING nameW, typeW;
1017 LDR_RESOURCE_INFO info;
1018 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
1020 TRACE( "%p %s %s %04x\n", module, debugstr_w(type), debugstr_w(name), lang );
1022 if (!module) module = GetModuleHandleW( 0 );
1023 nameW.Buffer = typeW.Buffer = NULL;
1025 __TRY
1027 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
1028 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
1029 info.Type = (ULONG_PTR)typeW.Buffer;
1030 info.Name = (ULONG_PTR)nameW.Buffer;
1031 info.Language = lang;
1032 status = LdrFindResource_U( module, &info, 3, &entry );
1033 done:
1034 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1036 __EXCEPT_PAGE_FAULT
1038 SetLastError( ERROR_INVALID_PARAMETER );
1040 __ENDTRY
1042 if (!IS_INTRESOURCE(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
1043 if (!IS_INTRESOURCE(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
1044 return (HRSRC)entry;
1048 /**********************************************************************
1049 * FindResourceW (kernelbase.@)
1051 HRSRC WINAPI DECLSPEC_HOTPATCH FindResourceW( HINSTANCE module, LPCWSTR name, LPCWSTR type )
1053 return FindResourceExW( module, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
1057 /**********************************************************************
1058 * FreeResource (kernelbase.@)
1060 BOOL WINAPI DECLSPEC_HOTPATCH FreeResource( HGLOBAL handle )
1062 return FALSE;
1066 /**********************************************************************
1067 * LoadResource (kernelbase.@)
1069 HGLOBAL WINAPI DECLSPEC_HOTPATCH LoadResource( HINSTANCE module, HRSRC rsrc )
1071 void *ret;
1073 TRACE( "%p %p\n", module, rsrc );
1075 if (!rsrc) return 0;
1076 if (!module) module = GetModuleHandleW( 0 );
1077 if (!set_ntstatus( LdrAccessResource( module, (IMAGE_RESOURCE_DATA_ENTRY *)rsrc, &ret, NULL )))
1078 return 0;
1079 return ret;
1083 /**********************************************************************
1084 * LockResource (kernelbase.@)
1086 LPVOID WINAPI DECLSPEC_HOTPATCH LockResource( HGLOBAL handle )
1088 return handle;
1092 /**********************************************************************
1093 * SizeofResource (kernelbase.@)
1095 DWORD WINAPI DECLSPEC_HOTPATCH SizeofResource( HINSTANCE module, HRSRC rsrc )
1097 if (!rsrc) return 0;
1098 return ((IMAGE_RESOURCE_DATA_ENTRY *)rsrc)->Size;
1102 /***********************************************************************
1103 * Activation contexts
1104 ***********************************************************************/
1107 /***********************************************************************
1108 * ActivateActCtx (kernelbase.@)
1110 BOOL WINAPI DECLSPEC_HOTPATCH ActivateActCtx( HANDLE context, ULONG_PTR *cookie )
1112 return set_ntstatus( RtlActivateActivationContext( 0, context, cookie ));
1116 /***********************************************************************
1117 * AddRefActCtx (kernelbase.@)
1119 void WINAPI DECLSPEC_HOTPATCH AddRefActCtx( HANDLE context )
1121 RtlAddRefActivationContext( context );
1125 /***********************************************************************
1126 * CreateActCtxW (kernelbase.@)
1128 HANDLE WINAPI DECLSPEC_HOTPATCH CreateActCtxW( PCACTCTXW ctx )
1130 HANDLE context;
1132 TRACE( "%p %08x\n", ctx, ctx ? ctx->dwFlags : 0 );
1134 if (!set_ntstatus( RtlCreateActivationContext( &context, ctx ))) return INVALID_HANDLE_VALUE;
1135 return context;
1139 /***********************************************************************
1140 * DeactivateActCtx (kernelbase.@)
1142 BOOL WINAPI DECLSPEC_HOTPATCH DeactivateActCtx( DWORD flags, ULONG_PTR cookie )
1144 RtlDeactivateActivationContext( flags, cookie );
1145 return TRUE;
1149 /***********************************************************************
1150 * FindActCtxSectionGuid (kernelbase.@)
1152 BOOL WINAPI DECLSPEC_HOTPATCH FindActCtxSectionGuid( DWORD flags, const GUID *ext_guid, ULONG id,
1153 const GUID *guid, PACTCTX_SECTION_KEYED_DATA info )
1155 return set_ntstatus( RtlFindActivationContextSectionGuid( flags, ext_guid, id, guid, info ));
1159 /***********************************************************************
1160 * FindActCtxSectionStringW (kernelbase.@)
1162 BOOL WINAPI DECLSPEC_HOTPATCH FindActCtxSectionStringW( DWORD flags, const GUID *ext_guid, ULONG id,
1163 LPCWSTR str, PACTCTX_SECTION_KEYED_DATA info )
1165 UNICODE_STRING us;
1167 if (!info)
1169 SetLastError( ERROR_INVALID_PARAMETER );
1170 return FALSE;
1172 RtlInitUnicodeString( &us, str );
1173 return set_ntstatus( RtlFindActivationContextSectionString( flags, ext_guid, id, &us, info ));
1177 /***********************************************************************
1178 * GetCurrentActCtx (kernelbase.@)
1180 BOOL WINAPI DECLSPEC_HOTPATCH GetCurrentActCtx( HANDLE *pcontext )
1182 return set_ntstatus( RtlGetActiveActivationContext( pcontext ));
1186 /***********************************************************************
1187 * QueryActCtxSettingsW (kernelbase.@)
1189 BOOL WINAPI DECLSPEC_HOTPATCH QueryActCtxSettingsW( DWORD flags, HANDLE ctx, const WCHAR *ns,
1190 const WCHAR *settings, WCHAR *buffer, SIZE_T size,
1191 SIZE_T *written )
1193 return set_ntstatus( RtlQueryActivationContextApplicationSettings( flags, ctx, ns, settings,
1194 buffer, size, written ));
1198 /***********************************************************************
1199 * QueryActCtxW (kernelbase.@)
1201 BOOL WINAPI DECLSPEC_HOTPATCH QueryActCtxW( DWORD flags, HANDLE context, PVOID inst, ULONG class,
1202 PVOID buffer, SIZE_T size, SIZE_T *written )
1204 return set_ntstatus( RtlQueryInformationActivationContext( flags, context, inst, class,
1205 buffer, size, written ));
1209 /***********************************************************************
1210 * ReleaseActCtx (kernelbase.@)
1212 void WINAPI DECLSPEC_HOTPATCH ReleaseActCtx( HANDLE context )
1214 RtlReleaseActivationContext( context );
1218 /***********************************************************************
1219 * ZombifyActCtx (kernelbase.@)
1221 BOOL WINAPI DECLSPEC_HOTPATCH ZombifyActCtx( HANDLE context )
1223 return set_ntstatus( RtlZombifyActivationContext( context ));