4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995, 2003 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
35 #include "wine/winbase16.h"
36 #include "wine/debug.h"
38 #include "wine/exception.h"
39 #include "wine/unicode.h"
40 #include "wine/list.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
44 /* handle conversions */
45 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
46 #define HRSRC_16(h32) (LOWORD(h32))
47 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
48 #define HGLOBAL_16(h32) (LOWORD(h32))
49 #define HMODULE_16(h32) (LOWORD(h32))
51 /* retrieve the resource name to pass to the ntdll functions */
52 static NTSTATUS
get_res_nameA( LPCSTR name
, UNICODE_STRING
*str
)
56 str
->Buffer
= (LPWSTR
)name
;
57 return STATUS_SUCCESS
;
62 if (RtlCharToInteger( name
+ 1, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
63 return STATUS_INVALID_PARAMETER
;
64 str
->Buffer
= (LPWSTR
)value
;
65 return STATUS_SUCCESS
;
67 RtlCreateUnicodeStringFromAsciiz( str
, name
);
68 RtlUpcaseUnicodeString( str
, str
, FALSE
);
69 return STATUS_SUCCESS
;
72 /* retrieve the resource name to pass to the ntdll functions */
73 static NTSTATUS
get_res_nameW( LPCWSTR name
, UNICODE_STRING
*str
)
77 str
->Buffer
= (LPWSTR
)name
;
78 return STATUS_SUCCESS
;
83 RtlInitUnicodeString( str
, name
+ 1 );
84 if (RtlUnicodeStringToInteger( str
, 10, &value
) != STATUS_SUCCESS
|| HIWORD(value
))
85 return STATUS_INVALID_PARAMETER
;
86 str
->Buffer
= (LPWSTR
)value
;
87 return STATUS_SUCCESS
;
89 RtlCreateUnicodeString( str
, name
);
90 RtlUpcaseUnicodeString( str
, str
, FALSE
);
91 return STATUS_SUCCESS
;
94 /* retrieve the resource names for the 16-bit FindResource function */
95 static BOOL
get_res_name_type_WtoA( LPCWSTR name
, LPCWSTR type
, LPSTR
*nameA
, LPSTR
*typeA
)
97 *nameA
= *typeA
= NULL
;
103 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
104 *nameA
= HeapAlloc( GetProcessHeap(), 0, len
);
105 if (*nameA
) WideCharToMultiByte( CP_ACP
, 0, name
, -1, *nameA
, len
, NULL
, NULL
);
107 else *nameA
= (LPSTR
)name
;
111 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, type
, -1, NULL
, 0, NULL
, NULL
);
112 *typeA
= HeapAlloc( GetProcessHeap(), 0, len
);
113 if (*typeA
) WideCharToMultiByte( CP_ACP
, 0, type
, -1, *typeA
, len
, NULL
, NULL
);
115 else *typeA
= (LPSTR
)type
;
119 if (HIWORD(*nameA
)) HeapFree( GetProcessHeap(), 0, *nameA
);
120 if (HIWORD(*typeA
)) HeapFree( GetProcessHeap(), 0, *typeA
);
121 SetLastError( ERROR_INVALID_PARAMETER
);
128 /* implementation of FindResourceExA */
129 static HRSRC
find_resourceA( HMODULE hModule
, LPCSTR type
, LPCSTR name
, WORD lang
)
132 UNICODE_STRING nameW
, typeW
;
133 LDR_RESOURCE_INFO info
;
134 const IMAGE_RESOURCE_DATA_ENTRY
*entry
= NULL
;
138 if ((status
= get_res_nameA( name
, &nameW
)) != STATUS_SUCCESS
) goto done
;
139 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
) goto done
;
140 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
141 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
142 info
.Language
= lang
;
143 status
= LdrFindResource_U( hModule
, &info
, 3, &entry
);
145 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
149 SetLastError( ERROR_INVALID_PARAMETER
);
153 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
154 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
159 /* implementation of FindResourceExW */
160 static HRSRC
find_resourceW( HMODULE hModule
, LPCWSTR type
, LPCWSTR name
, WORD lang
)
163 UNICODE_STRING nameW
, typeW
;
164 LDR_RESOURCE_INFO info
;
165 const IMAGE_RESOURCE_DATA_ENTRY
*entry
= NULL
;
167 nameW
.Buffer
= typeW
.Buffer
= NULL
;
171 if ((status
= get_res_nameW( name
, &nameW
)) != STATUS_SUCCESS
) goto done
;
172 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
) goto done
;
173 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
174 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
175 info
.Language
= lang
;
176 status
= LdrFindResource_U( hModule
, &info
, 3, &entry
);
178 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
182 SetLastError( ERROR_INVALID_PARAMETER
);
186 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
187 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
191 /**********************************************************************
192 * FindResourceExA (KERNEL32.@)
194 HRSRC WINAPI
FindResourceExA( HMODULE hModule
, LPCSTR type
, LPCSTR name
, WORD lang
)
196 TRACE( "%p %s %s %04x\n", hModule
, debugstr_a(type
), debugstr_a(name
), lang
);
198 if (!hModule
) hModule
= GetModuleHandleW(0);
199 else if (!HIWORD(hModule
))
201 return HRSRC_32( FindResource16( HMODULE_16(hModule
), name
, type
) );
203 return find_resourceA( hModule
, type
, name
, lang
);
207 /**********************************************************************
208 * FindResourceA (KERNEL32.@)
210 HRSRC WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
212 return FindResourceExA( hModule
, type
, name
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
216 /**********************************************************************
217 * FindResourceExW (KERNEL32.@)
219 HRSRC WINAPI
FindResourceExW( HMODULE hModule
, LPCWSTR type
, LPCWSTR name
, WORD lang
)
221 TRACE( "%p %s %s %04x\n", hModule
, debugstr_w(type
), debugstr_w(name
), lang
);
223 if (!hModule
) hModule
= GetModuleHandleW(0);
224 else if (!HIWORD(hModule
))
229 if (!get_res_name_type_WtoA( name
, type
, &nameA
, &typeA
)) return NULL
;
231 ret
= FindResource16( HMODULE_16(hModule
), nameA
, typeA
);
232 if (HIWORD(nameA
)) HeapFree( GetProcessHeap(), 0, nameA
);
233 if (HIWORD(typeA
)) HeapFree( GetProcessHeap(), 0, typeA
);
234 return HRSRC_32(ret
);
237 return find_resourceW( hModule
, type
, name
, lang
);
241 /**********************************************************************
242 * FindResourceW (KERNEL32.@)
244 HRSRC WINAPI
FindResourceW( HINSTANCE hModule
, LPCWSTR name
, LPCWSTR type
)
246 return FindResourceExW( hModule
, type
, name
, MAKELANGID( LANG_NEUTRAL
, SUBLANG_NEUTRAL
) );
250 /**********************************************************************
251 * EnumResourceTypesA (KERNEL32.@)
253 BOOL WINAPI
EnumResourceTypesA( HMODULE hmod
, ENUMRESTYPEPROCA lpfun
, LONG_PTR lparam
)
258 DWORD len
= 0, newlen
;
260 const IMAGE_RESOURCE_DIRECTORY
*resdir
;
261 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
262 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
264 TRACE( "%p %p %lx\n", hmod
, lpfun
, lparam
);
266 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
268 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &resdir
)) != STATUS_SUCCESS
)
270 SetLastError( RtlNtStatusToDosError(status
) );
273 et
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(resdir
+ 1);
274 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
276 if (et
[i
].u1
.s1
.NameIsString
)
278 str
= (PIMAGE_RESOURCE_DIR_STRING_U
) ((LPBYTE
) resdir
+ et
[i
].u1
.s1
.NameOffset
);
279 newlen
= WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, NULL
, 0, NULL
, NULL
);
280 if (newlen
+ 1 > len
)
283 HeapFree( GetProcessHeap(), 0, type
);
284 if (!(type
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
286 WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, type
, len
, NULL
, NULL
);
288 ret
= lpfun(hmod
,type
,lparam
);
292 ret
= lpfun( hmod
, (LPSTR
)(int)et
[i
].u1
.s2
.Id
, lparam
);
296 HeapFree( GetProcessHeap(), 0, type
);
301 /**********************************************************************
302 * EnumResourceTypesW (KERNEL32.@)
304 BOOL WINAPI
EnumResourceTypesW( HMODULE hmod
, ENUMRESTYPEPROCW lpfun
, LONG_PTR lparam
)
310 const IMAGE_RESOURCE_DIRECTORY
*resdir
;
311 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
312 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
314 TRACE( "%p %p %lx\n", hmod
, lpfun
, lparam
);
316 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
318 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &resdir
)) != STATUS_SUCCESS
)
320 SetLastError( RtlNtStatusToDosError(status
) );
323 et
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(resdir
+ 1);
324 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
326 if (et
[i
].u1
.s1
.NameIsString
)
328 str
= (PIMAGE_RESOURCE_DIR_STRING_U
) ((LPBYTE
) resdir
+ et
[i
].u1
.s1
.NameOffset
);
329 if (str
->Length
+ 1 > len
)
331 len
= str
->Length
+ 1;
332 HeapFree( GetProcessHeap(), 0, type
);
333 if (!(type
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return FALSE
;
335 memcpy(type
, str
->NameString
, str
->Length
* sizeof (WCHAR
));
336 type
[str
->Length
] = 0;
337 ret
= lpfun(hmod
,type
,lparam
);
341 ret
= lpfun( hmod
, (LPWSTR
)(int)et
[i
].u1
.s2
.Id
, lparam
);
345 HeapFree( GetProcessHeap(), 0, type
);
350 /**********************************************************************
351 * EnumResourceNamesA (KERNEL32.@)
353 BOOL WINAPI
EnumResourceNamesA( HMODULE hmod
, LPCSTR type
, ENUMRESNAMEPROCA lpfun
, LONG_PTR lparam
)
357 DWORD len
= 0, newlen
;
360 UNICODE_STRING typeW
;
361 LDR_RESOURCE_INFO info
;
362 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
363 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
364 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
366 TRACE( "%p %s %p %lx\n", hmod
, debugstr_a(type
), lpfun
, lparam
);
368 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
370 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
372 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
374 info
.Type
= (ULONG
)typeW
.Buffer
;
375 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
378 et
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
379 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
381 if (et
[i
].u1
.s1
.NameIsString
)
383 str
= (IMAGE_RESOURCE_DIR_STRING_U
*) ((LPBYTE
) basedir
+ et
[i
].u1
.s1
.NameOffset
);
384 newlen
= WideCharToMultiByte(CP_ACP
, 0, str
->NameString
, str
->Length
, NULL
, 0, NULL
, NULL
);
385 if (newlen
+ 1 > len
)
388 HeapFree( GetProcessHeap(), 0, name
);
389 if (!(name
= HeapAlloc(GetProcessHeap(), 0, len
+ 1 )))
395 WideCharToMultiByte( CP_ACP
, 0, str
->NameString
, str
->Length
, name
, len
, NULL
, NULL
);
397 ret
= lpfun(hmod
,type
,name
,lparam
);
401 ret
= lpfun( hmod
, type
, (LPSTR
)(int)et
[i
].u1
.s2
.Id
, lparam
);
406 HeapFree( GetProcessHeap(), 0, name
);
407 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
408 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
413 /**********************************************************************
414 * EnumResourceNamesW (KERNEL32.@)
416 BOOL WINAPI
EnumResourceNamesW( HMODULE hmod
, LPCWSTR type
, ENUMRESNAMEPROCW lpfun
, LONG_PTR lparam
)
422 UNICODE_STRING typeW
;
423 LDR_RESOURCE_INFO info
;
424 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
425 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
426 const IMAGE_RESOURCE_DIR_STRING_U
*str
;
428 TRACE( "%p %s %p %lx\n", hmod
, debugstr_w(type
), lpfun
, lparam
);
430 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
432 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
434 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
436 info
.Type
= (ULONG
)typeW
.Buffer
;
437 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 1, &resdir
)) != STATUS_SUCCESS
)
440 et
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(resdir
+ 1);
441 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+resdir
->NumberOfIdEntries
; i
++)
443 if (et
[i
].u1
.s1
.NameIsString
)
445 str
= (IMAGE_RESOURCE_DIR_STRING_U
*) ((LPBYTE
) basedir
+ et
[i
].u1
.s1
.NameOffset
);
446 if (str
->Length
+ 1 > len
)
448 len
= str
->Length
+ 1;
449 HeapFree( GetProcessHeap(), 0, name
);
450 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
456 memcpy(name
, str
->NameString
, str
->Length
* sizeof (WCHAR
));
457 name
[str
->Length
] = 0;
458 ret
= lpfun(hmod
,type
,name
,lparam
);
462 ret
= lpfun( hmod
, type
, (LPWSTR
)(int)et
[i
].u1
.s2
.Id
, lparam
);
467 HeapFree( GetProcessHeap(), 0, name
);
468 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
469 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
474 /**********************************************************************
475 * EnumResourceLanguagesA (KERNEL32.@)
477 BOOL WINAPI
EnumResourceLanguagesA( HMODULE hmod
, LPCSTR type
, LPCSTR name
,
478 ENUMRESLANGPROCA lpfun
, LONG_PTR lparam
)
483 UNICODE_STRING typeW
, nameW
;
484 LDR_RESOURCE_INFO info
;
485 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
486 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
488 TRACE( "%p %s %s %p %lx\n", hmod
, debugstr_a(type
), debugstr_a(name
), lpfun
, lparam
);
490 if (!hmod
) hmod
= GetModuleHandleA( NULL
);
491 typeW
.Buffer
= nameW
.Buffer
= NULL
;
492 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
494 if ((status
= get_res_nameA( type
, &typeW
)) != STATUS_SUCCESS
)
496 if ((status
= get_res_nameA( name
, &nameW
)) != STATUS_SUCCESS
)
498 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
499 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
500 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 2, &resdir
)) != STATUS_SUCCESS
)
503 et
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(resdir
+ 1);
504 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
506 ret
= lpfun( hmod
, type
, name
, et
[i
].u1
.s2
.Id
, lparam
);
510 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
511 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
512 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
517 /**********************************************************************
518 * EnumResourceLanguagesW (KERNEL32.@)
520 BOOL WINAPI
EnumResourceLanguagesW( HMODULE hmod
, LPCWSTR type
, LPCWSTR name
,
521 ENUMRESLANGPROCW lpfun
, LONG_PTR lparam
)
526 UNICODE_STRING typeW
, nameW
;
527 LDR_RESOURCE_INFO info
;
528 const IMAGE_RESOURCE_DIRECTORY
*basedir
, *resdir
;
529 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
531 TRACE( "%p %s %s %p %lx\n", hmod
, debugstr_w(type
), debugstr_w(name
), lpfun
, lparam
);
533 if (!hmod
) hmod
= GetModuleHandleW( NULL
);
534 typeW
.Buffer
= nameW
.Buffer
= NULL
;
535 if ((status
= LdrFindResourceDirectory_U( hmod
, NULL
, 0, &basedir
)) != STATUS_SUCCESS
)
537 if ((status
= get_res_nameW( type
, &typeW
)) != STATUS_SUCCESS
)
539 if ((status
= get_res_nameW( name
, &nameW
)) != STATUS_SUCCESS
)
541 info
.Type
= (ULONG_PTR
)typeW
.Buffer
;
542 info
.Name
= (ULONG_PTR
)nameW
.Buffer
;
543 if ((status
= LdrFindResourceDirectory_U( hmod
, &info
, 2, &resdir
)) != STATUS_SUCCESS
)
546 et
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(resdir
+ 1);
547 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
549 ret
= lpfun( hmod
, type
, name
, et
[i
].u1
.s2
.Id
, lparam
);
553 if (HIWORD(typeW
.Buffer
)) HeapFree( GetProcessHeap(), 0, typeW
.Buffer
);
554 if (HIWORD(nameW
.Buffer
)) HeapFree( GetProcessHeap(), 0, nameW
.Buffer
);
555 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
560 /**********************************************************************
561 * LoadResource (KERNEL32.@)
563 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
568 TRACE( "%p %p\n", hModule
, hRsrc
);
570 if (hModule
&& !HIWORD(hModule
))
571 /* FIXME: should convert return to 32-bit resource */
572 return HGLOBAL_32( LoadResource16( HMODULE_16(hModule
), HRSRC_16(hRsrc
) ) );
574 if (!hRsrc
) return 0;
575 if (!hModule
) hModule
= GetModuleHandleA( NULL
);
576 status
= LdrAccessResource( hModule
, (IMAGE_RESOURCE_DATA_ENTRY
*)hRsrc
, &ret
, NULL
);
577 if (status
!= STATUS_SUCCESS
) SetLastError( RtlNtStatusToDosError(status
) );
582 /**********************************************************************
583 * LockResource (KERNEL32.@)
585 LPVOID WINAPI
LockResource( HGLOBAL handle
)
587 TRACE("(%p)\n", handle
);
589 if (HIWORD( handle
)) /* 32-bit memory handle */
590 return (LPVOID
)handle
;
592 /* 16-bit memory handle */
593 return LockResource16( HGLOBAL_16(handle
) );
597 /**********************************************************************
598 * FreeResource (KERNEL32.@)
600 BOOL WINAPI
FreeResource( HGLOBAL handle
)
602 if (HIWORD(handle
)) return 0; /* 32-bit memory handle: nothing to do */
603 return FreeResource16( HGLOBAL_16(handle
) );
607 /**********************************************************************
608 * SizeofResource (KERNEL32.@)
610 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
612 if (hModule
&& !HIWORD(hModule
))
613 return SizeofResource16( HMODULE_16(hModule
), HRSRC_16(hRsrc
) );
615 if (!hRsrc
) return 0;
616 return ((PIMAGE_RESOURCE_DATA_ENTRY
)hRsrc
)->Size
;
623 struct list resources_list
;
636 static BOOL CALLBACK
enum_resources_languages_delete_all(HMODULE hModule
, LPCWSTR lpType
, LPCWSTR lpName
, WORD wLang
, LONG_PTR lParam
)
638 return UpdateResourceW((HANDLE
)lParam
, lpType
, lpName
, wLang
, NULL
, 0);
641 static BOOL CALLBACK
enum_resources_names_delete_all(HMODULE hModule
, LPCWSTR lpType
, LPWSTR lpName
, LONG_PTR lParam
)
643 return EnumResourceLanguagesW(hModule
, lpType
, lpName
, enum_resources_languages_delete_all
, lParam
);
646 static BOOL CALLBACK
enum_resources_types_delete_all(HMODULE hModule
, LPWSTR lpType
, LONG_PTR lParam
)
648 return EnumResourceNamesW(hModule
, lpType
, enum_resources_names_delete_all
, lParam
);
651 static BOOL CALLBACK
enum_resources_languages_add_all(HMODULE hModule
, LPCWSTR lpType
, LPCWSTR lpName
, WORD wLang
, LONG_PTR lParam
)
654 HRSRC hResource
= FindResourceExW(hModule
, lpType
, lpName
, wLang
);
658 if(hResource
== NULL
) return FALSE
;
659 if(!(hGlobal
= LoadResource(hModule
, hResource
))) return FALSE
;
660 if(!(lpData
= LockResource(hGlobal
))) return FALSE
;
661 if(!(size
= SizeofResource(hModule
, hResource
))) return FALSE
;
662 return UpdateResourceW((HANDLE
)lParam
, lpType
, lpName
, wLang
, lpData
, size
);
665 static BOOL CALLBACK
enum_resources_names_add_all(HMODULE hModule
, LPCWSTR lpType
, LPWSTR lpName
, LONG_PTR lParam
)
667 return EnumResourceLanguagesW(hModule
, lpType
, lpName
, enum_resources_languages_add_all
, lParam
);
670 static BOOL CALLBACK
enum_resources_types_add_all(HMODULE hModule
, LPWSTR lpType
, LONG_PTR lParam
)
672 return EnumResourceNamesW(hModule
, lpType
, enum_resources_names_add_all
, lParam
);
675 /***********************************************************************
676 * BeginUpdateResourceW (KERNEL32.@)
678 HANDLE WINAPI
BeginUpdateResourceW( LPCWSTR pFileName
, BOOL bDeleteExistingResources
)
682 HANDLE hModule
= NULL
;
683 HANDLE hUpdate
= NULL
;
684 QUEUEDUPDATES
*current_updates
= NULL
;
687 TRACE("%s, %d\n",debugstr_w(pFileName
),bDeleteExistingResources
);
689 hFile
= FindFirstFileW(pFileName
, &fd
);
690 if(hFile
== INVALID_HANDLE_VALUE
)
693 SetLastError(ERROR_FILE_NOT_FOUND
);
696 if(fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
)
698 SetLastError(ERROR_FILE_READ_ONLY
);
702 hModule
= LoadLibraryW(pFileName
);
705 SetLastError(ERROR_INVALID_PARAMETER
);
709 if(!(hUpdate
= GlobalAlloc(GHND
, sizeof(QUEUEDUPDATES
))))
711 SetLastError(ERROR_OUTOFMEMORY
);
714 if(!(current_updates
= GlobalLock(hUpdate
)))
716 SetLastError(ERROR_INVALID_HANDLE
);
719 if(!(current_updates
->pFileName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(pFileName
)+1)*sizeof(WCHAR
))))
721 SetLastError(ERROR_OUTOFMEMORY
);
724 strcpyW(current_updates
->pFileName
, pFileName
);
725 list_init(¤t_updates
->resources_list
);
727 if(bDeleteExistingResources
)
729 if(!EnumResourceTypesW(hModule
, enum_resources_types_delete_all
, (LONG_PTR
)hUpdate
))
734 if(!EnumResourceTypesW(hModule
, enum_resources_types_add_all
, (LONG_PTR
)hUpdate
))
740 if(!ret
&& current_updates
)
742 HeapFree(GetProcessHeap(), 0, current_updates
->pFileName
);
743 GlobalUnlock(hUpdate
);
747 if(hUpdate
) GlobalUnlock(hUpdate
);
748 if(hModule
) FreeLibrary(hModule
);
749 if(hFile
) FindClose(hFile
);
754 /***********************************************************************
755 * BeginUpdateResourceA (KERNEL32.@)
757 HANDLE WINAPI
BeginUpdateResourceA( LPCSTR pFileName
, BOOL bDeleteExistingResources
)
759 UNICODE_STRING FileNameW
;
761 RtlCreateUnicodeStringFromAsciiz(&FileNameW
, pFileName
);
762 ret
= BeginUpdateResourceW(FileNameW
.Buffer
, bDeleteExistingResources
);
763 RtlFreeUnicodeString(&FileNameW
);
768 /***********************************************************************
769 * EndUpdateResourceW (KERNEL32.@)
771 BOOL WINAPI
EndUpdateResourceW( HANDLE hUpdate
, BOOL fDiscard
)
773 QUEUEDUPDATES
*current_updates
= NULL
;
776 struct list
*ptr
= NULL
;
777 QUEUEDRESOURCE
*current_resource
= NULL
;
779 FIXME("(%p,%d): stub\n",hUpdate
,fDiscard
);
781 if(!(current_updates
= GlobalLock(hUpdate
)))
783 SetLastError(ERROR_INVALID_HANDLE
);
792 /* FIXME: This is the only missing part, an actual implementation */
793 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
800 while ((ptr
= list_head(¤t_updates
->resources_list
)) != NULL
)
802 current_resource
= LIST_ENTRY(ptr
, QUEUEDRESOURCE
, entry
);
803 list_remove(¤t_resource
->entry
);
804 if(HIWORD(current_resource
->lpType
)) HeapFree(GetProcessHeap(), 0, current_resource
->lpType
);
805 if(HIWORD(current_resource
->lpName
)) HeapFree(GetProcessHeap(), 0, current_resource
->lpName
);
806 HeapFree(GetProcessHeap(), 0, current_resource
->lpData
);
807 HeapFree(GetProcessHeap(), 0, current_resource
);
809 HeapFree(GetProcessHeap(), 0, current_updates
->pFileName
);
810 GlobalUnlock(hUpdate
);
817 /***********************************************************************
818 * EndUpdateResourceA (KERNEL32.@)
820 BOOL WINAPI
EndUpdateResourceA( HANDLE hUpdate
, BOOL fDiscard
)
822 return EndUpdateResourceW(hUpdate
, fDiscard
);
826 /***********************************************************************
827 * UpdateResourceW (KERNEL32.@)
829 BOOL WINAPI
UpdateResourceW( HANDLE hUpdate
, LPCWSTR lpType
, LPCWSTR lpName
,
830 WORD wLanguage
, LPVOID lpData
, DWORD cbData
)
832 QUEUEDUPDATES
*current_updates
= NULL
;
834 QUEUEDRESOURCE
*current_resource
= NULL
;
837 TRACE("%p %s %s %08x %p %ld\n",hUpdate
,debugstr_w(lpType
),debugstr_w(lpName
),wLanguage
,lpData
,cbData
);
839 if(!(current_updates
= GlobalLock(hUpdate
)))
841 SetLastError(ERROR_INVALID_HANDLE
);
846 if(!(current_resource
= HeapAlloc(GetProcessHeap(), 0, sizeof(QUEUEDRESOURCE
))))
848 SetLastError(ERROR_OUTOFMEMORY
);
852 current_resource
->lpType
= (LPWSTR
)lpType
;
853 else if((current_resource
->lpType
= HeapAlloc(GetProcessHeap(), 0, (strlenW(lpType
)+1)*sizeof(WCHAR
))))
854 strcpyW(current_resource
->lpType
, lpType
);
857 SetLastError(ERROR_OUTOFMEMORY
);
861 current_resource
->lpName
= (LPWSTR
)lpName
;
862 else if((current_resource
->lpName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(lpName
)+1)*sizeof(WCHAR
))))
863 strcpyW(current_resource
->lpName
, lpName
);
866 SetLastError(ERROR_OUTOFMEMORY
);
869 if(!(current_resource
->lpData
= HeapAlloc(GetProcessHeap(), 0, cbData
)))
871 SetLastError(ERROR_OUTOFMEMORY
);
874 current_resource
->wLanguage
= wLanguage
;
875 memcpy(current_resource
->lpData
, lpData
, cbData
);
876 current_resource
->cbData
= cbData
;
877 list_add_tail(¤t_updates
->resources_list
, ¤t_resource
->entry
);
881 if(!ret
&& current_resource
)
883 if(HIWORD(current_resource
->lpType
)) HeapFree(GetProcessHeap(), 0, current_resource
->lpType
);
884 if(HIWORD(current_resource
->lpName
)) HeapFree(GetProcessHeap(), 0, current_resource
->lpName
);
885 HeapFree(GetProcessHeap(), 0, current_resource
->lpData
);
886 HeapFree(GetProcessHeap(), 0, current_resource
);
888 if(found
) GlobalUnlock(hUpdate
);
893 /***********************************************************************
894 * UpdateResourceA (KERNEL32.@)
896 BOOL WINAPI
UpdateResourceA( HANDLE hUpdate
, LPCSTR lpType
, LPCSTR lpName
,
897 WORD wLanguage
, LPVOID lpData
, DWORD cbData
)
900 UNICODE_STRING TypeW
;
901 UNICODE_STRING NameW
;
903 TypeW
.Buffer
= (LPWSTR
)lpType
;
905 RtlCreateUnicodeStringFromAsciiz(&TypeW
, lpType
);
907 NameW
.Buffer
= (LPWSTR
)lpName
;
909 RtlCreateUnicodeStringFromAsciiz(&NameW
, lpName
);
910 ret
= UpdateResourceW(hUpdate
, TypeW
.Buffer
, NameW
.Buffer
, wLanguage
, lpData
, cbData
);
911 if(HIWORD(lpType
)) RtlFreeUnicodeString(&TypeW
);
912 if(HIWORD(lpName
)) RtlFreeUnicodeString(&NameW
);