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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #define WIN32_NO_STATUS
33 #include "kernelbase.h"
34 #include "wine/list.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
49 static struct list exclusive_datafile_list
= LIST_INIT( exclusive_datafile_list
);
52 /***********************************************************************
54 ***********************************************************************/
57 /******************************************************************
60 FARPROC WINAPI
get_proc_address( HMODULE module
, LPCSTR function
)
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
)))
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
;
87 DWORD protect
= PAGE_READONLY
;
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
);
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
);
128 else *mod_ret
= (HMODULE
)((char *)module
+ 2); /* set bit 1 for image resource module */
134 if (module
) UnmapViewOfFile( module
);
140 /******************************************************************
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
;
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
))
159 LdrLockLoaderLock( 0, NULL
, &magic
);
160 if (!LdrGetDllHandle( load_path
, flags
, libname
, &module
))
161 LdrAddRefDll( 0, module
);
163 load_library_as_datafile( load_path
, flags
, libname
->Buffer
, &module
);
164 LdrUnlockLoaderLock( 0, magic
);
168 status
= LdrLoadDll( load_path
, flags
, libname
, &module
);
169 if (!set_ntstatus( status
))
172 if (status
== STATUS_DLL_NOT_FOUND
&& (GetVersion() & 0x80000000))
173 SetLastError( ERROR_DLL_NOT_FOUND
);
177 RtlReleasePath( load_path
);
182 /****************************************************************************
183 * AddDllDirectory (kernelbase.@)
185 DLL_DIRECTORY_COOKIE WINAPI DECLSPEC_HOTPATCH
AddDllDirectory( const WCHAR
*dir
)
190 RtlInitUnicodeString( &str
, dir
);
191 if (!set_ntstatus( LdrAddDllDirectory( &str
, &cookie
))) return NULL
;
196 /***********************************************************************
197 * DelayLoadFailureHook (kernelbase.@)
199 FARPROC WINAPI DECLSPEC_HOTPATCH
DelayLoadFailureHook( LPCSTR name
, LPCSTR function
)
203 if ((ULONG_PTR
)function
>> 16)
204 ERR( "failed to delay load %s.%s\n", name
, function
);
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
);
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
)
230 SetLastError( ERROR_INVALID_HANDLE
);
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
);
242 if ((ULONG_PTR
)module
& 1)
244 struct exclusive_datafile
*file
;
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
);
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
) );
276 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
279 if ((len
= GetModuleFileNameW( module
, filenameW
, size
)))
281 len
= file_name_WtoA( filenameW
, len
, filename
, size
);
285 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
287 HeapFree( GetProcessHeap(), 0, filenameW
);
292 /***********************************************************************
293 * GetModuleFileNameW (kernelbase.@)
295 DWORD WINAPI DECLSPEC_HOTPATCH
GetModuleFileNameW( HMODULE module
, LPWSTR filename
, DWORD size
)
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;
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
) );
322 else SetLastError( ERROR_INSUFFICIENT_BUFFER
);
325 LdrUnlockLoaderLock( 0, magic
);
327 TRACE( "%s\n", debugstr_wn(filename
, len
) );
332 /***********************************************************************
333 * GetModuleHandleA (kernelbase.@)
335 HMODULE WINAPI DECLSPEC_HOTPATCH
GetModuleHandleA( LPCSTR module
)
339 GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
, module
, &ret
);
344 /***********************************************************************
345 * GetModuleHandleW (kernelbase.@)
347 HMODULE WINAPI DECLSPEC_HOTPATCH
GetModuleHandleW( LPCWSTR module
)
351 GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
, module
, &ret
);
356 /***********************************************************************
357 * GetModuleHandleExA (kernelbase.@)
359 BOOL WINAPI DECLSPEC_HOTPATCH
GetModuleHandleExA( DWORD flags
, LPCSTR name
, HMODULE
*module
)
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
;
383 SetLastError( ERROR_INVALID_PARAMETER
);
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
);
393 ret
= NtCurrentTeb()->Peb
->ImageBaseAddress
;
395 else if (flags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
)
398 if (!(ret
= RtlPcToFileHeader( (void *)name
, &dummy
))) status
= STATUS_DLL_NOT_FOUND
;
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
);
418 return set_ntstatus( status
);
422 /***********************************************************************
423 * GetProcAddress (kernelbase.@)
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 */
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")
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"
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")
460 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
461 __ASM_CFI(".cfi_same_value %rbp\n\t")
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
)
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
)
513 SetLastError( ERROR_INVALID_PARAMETER
);
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
);
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
);
542 /***********************************************************************
543 * LoadAppInitDlls (kernelbase.@)
545 void WINAPI
LoadAppInitDlls(void)
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 /***********************************************************************
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
;
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
;
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
)
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
)
648 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
650 if ((status
= get_res_nameA( name
, &nameW
)) != STATUS_SUCCESS
)
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
)
657 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
660 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
662 ret
= func( module
, type
, name
, et
[i
].u
.Id
, param
);
669 status
= STATUS_ACCESS_VIOLATION
;
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
) );
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
)
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
)
708 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
710 if ((status
= get_res_nameW( name
, &nameW
)) != STATUS_SUCCESS
)
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
)
717 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
720 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
722 ret
= func( module
, type
, name
, et
[i
].u
.Id
, param
);
729 status
= STATUS_ACCESS_VIOLATION
;
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
) );
740 /**********************************************************************
741 * EnumResourceNamesExA (kernelbase.@)
743 BOOL WINAPI DECLSPEC_HOTPATCH
EnumResourceNamesExA( HMODULE module
, LPCSTR type
, ENUMRESNAMEPROCA func
,
744 LONG_PTR param
, DWORD flags
, LANGID lang
)
748 DWORD len
= 0, newlen
;
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 );
767 if ((status
= LdrFindResourceDirectory_U( module
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
769 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
771 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
772 if ((status
= LdrFindResourceDirectory_U( module
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
775 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
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
)
787 HeapFree( GetProcessHeap(), 0, name
);
788 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
794 WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, name
, len
, NULL
, NULL
);
796 ret
= func( module
, type
, name
, param
);
800 ret
= func( module
, type
, UIntToPtr(et
[i
].u
.Id
), param
);
808 status
= STATUS_ACCESS_VIOLATION
;
813 HeapFree( GetProcessHeap(), 0, name
);
814 if (!IS_INTRESOURCE(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
815 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
820 /**********************************************************************
821 * EnumResourceNamesExW (kernelbase.@)
823 BOOL WINAPI DECLSPEC_HOTPATCH
EnumResourceNamesExW( HMODULE module
, LPCWSTR type
, ENUMRESNAMEPROCW func
,
824 LONG_PTR param
, DWORD flags
, LANGID lang
)
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 );
846 if ((status
= LdrFindResourceDirectory_U( module
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
848 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
850 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
851 if ((status
= LdrFindResourceDirectory_U( module
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
854 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
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
) )))
872 memcpy(name
, str
->NameString
, str
->Length
* sizeof (WCHAR
));
873 name
[str
->Length
] = 0;
874 ret
= func( module
, type
, name
, param
);
878 ret
= func( module
, type
, UIntToPtr(et
[i
].u
.Id
), param
);
886 status
= STATUS_ACCESS_VIOLATION
;
890 HeapFree( GetProcessHeap(), 0, name
);
891 if (!IS_INTRESOURCE(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
892 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
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
)
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
)
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
);
948 ret
= func( module
, type
, param
);
952 ret
= func( module
, UIntToPtr(et
[i
].u
.Id
), param
);
956 HeapFree( GetProcessHeap(), 0, type
);
961 /**********************************************************************
962 * EnumResourceTypesExW (kernelbase.@)
964 BOOL WINAPI DECLSPEC_HOTPATCH
EnumResourceTypesExW( HMODULE module
, ENUMRESTYPEPROCW func
, LONG_PTR param
,
965 DWORD flags
, LANGID lang
)
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
);
1001 ret
= func( module
, UIntToPtr(et
[i
].u
.Id
), param
);
1005 HeapFree( GetProcessHeap(), 0, type
);
1010 /**********************************************************************
1011 * FindResourceExW (kernelbase.@)
1013 HRSRC WINAPI DECLSPEC_HOTPATCH
FindResourceExW( HMODULE module
, LPCWSTR type
, LPCWSTR name
, WORD lang
)
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
;
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
);
1034 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
1038 SetLastError( ERROR_INVALID_PARAMETER
);
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
)
1066 /**********************************************************************
1067 * LoadResource (kernelbase.@)
1069 HGLOBAL WINAPI DECLSPEC_HOTPATCH
LoadResource( HINSTANCE module
, HRSRC rsrc
)
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
)))
1083 /**********************************************************************
1084 * LockResource (kernelbase.@)
1086 LPVOID WINAPI DECLSPEC_HOTPATCH
LockResource( HGLOBAL 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
)
1132 TRACE( "%p %08x\n", ctx
, ctx
? ctx
->dwFlags
: 0 );
1134 if (!set_ntstatus( RtlCreateActivationContext( &context
, ctx
))) return INVALID_HANDLE_VALUE
;
1139 /***********************************************************************
1140 * DeactivateActCtx (kernelbase.@)
1142 BOOL WINAPI DECLSPEC_HOTPATCH
DeactivateActCtx( DWORD flags
, ULONG_PTR cookie
)
1144 RtlDeactivateActivationContext( flags
, cookie
);
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
)
1169 SetLastError( ERROR_INVALID_PARAMETER
);
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
,
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
));