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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
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 static WINE_EXCEPTION_FILTER(page_fault)
53 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ||
54 GetExceptionCode() == EXCEPTION_PRIV_INSTRUCTION)
55 return EXCEPTION_EXECUTE_HANDLER;
56 return EXCEPTION_CONTINUE_SEARCH;
59 /* retrieve the resource name to pass to the ntdll functions */
60 static NTSTATUS get_res_nameA( LPCSTR name, UNICODE_STRING *str )
64 str->Buffer = (LPWSTR)name;
65 return STATUS_SUCCESS;
70 if (RtlCharToInteger( name + 1, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
71 return STATUS_INVALID_PARAMETER;
72 str->Buffer = (LPWSTR)value;
73 return STATUS_SUCCESS;
75 RtlCreateUnicodeStringFromAsciiz( str, name );
76 RtlUpcaseUnicodeString( str, str, FALSE );
77 return STATUS_SUCCESS;
80 /* retrieve the resource name to pass to the ntdll functions */
81 static NTSTATUS get_res_nameW( LPCWSTR name, UNICODE_STRING *str )
85 str->Buffer = (LPWSTR)name;
86 return STATUS_SUCCESS;
91 RtlInitUnicodeString( str, name + 1 );
92 if (RtlUnicodeStringToInteger( str, 10, &value ) != STATUS_SUCCESS || HIWORD(value))
93 return STATUS_INVALID_PARAMETER;
94 str->Buffer = (LPWSTR)value;
95 return STATUS_SUCCESS;
97 RtlCreateUnicodeString( str, name );
98 RtlUpcaseUnicodeString( str, str, FALSE );
99 return STATUS_SUCCESS;
102 /* retrieve the resource names for the 16-bit FindResource function */
103 static BOOL get_res_name_type_WtoA( LPCWSTR name, LPCWSTR type, LPSTR *nameA, LPSTR *typeA )
105 *nameA = *typeA = NULL;
111 DWORD len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
112 *nameA = HeapAlloc( GetProcessHeap(), 0, len );
113 if (*nameA) WideCharToMultiByte( CP_ACP, 0, name, -1, *nameA, len, NULL, NULL );
115 else *nameA = (LPSTR)name;
119 DWORD len = WideCharToMultiByte( CP_ACP, 0, type, -1, NULL, 0, NULL, NULL );
120 *typeA = HeapAlloc( GetProcessHeap(), 0, len );
121 if (*typeA) WideCharToMultiByte( CP_ACP, 0, type, -1, *typeA, len, NULL, NULL );
123 else *typeA = (LPSTR)type;
127 if (HIWORD(*nameA)) HeapFree( GetProcessHeap(), 0, *nameA );
128 if (HIWORD(*typeA)) HeapFree( GetProcessHeap(), 0, *typeA );
129 SetLastError( ERROR_INVALID_PARAMETER );
136 /* implementation of FindResourceExA */
137 static HRSRC find_resourceA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
140 UNICODE_STRING nameW, typeW;
141 LDR_RESOURCE_INFO info;
142 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
146 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS) goto done;
147 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS) goto done;
148 info.Type = (ULONG)typeW.Buffer;
149 info.Name = (ULONG)nameW.Buffer;
150 info.Language = lang;
151 status = LdrFindResource_U( hModule, &info, 3, &entry );
153 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
157 SetLastError( ERROR_INVALID_PARAMETER );
161 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
162 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
167 /* implementation of FindResourceExW */
168 static HRSRC find_resourceW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
171 UNICODE_STRING nameW, typeW;
172 LDR_RESOURCE_INFO info;
173 const IMAGE_RESOURCE_DATA_ENTRY *entry = NULL;
175 nameW.Buffer = typeW.Buffer = NULL;
179 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS) goto done;
180 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS) goto done;
181 info.Type = (ULONG)typeW.Buffer;
182 info.Name = (ULONG)nameW.Buffer;
183 info.Language = lang;
184 status = LdrFindResource_U( hModule, &info, 3, &entry );
186 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
190 SetLastError( ERROR_INVALID_PARAMETER );
194 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
195 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
199 /**********************************************************************
200 * FindResourceExA (KERNEL32.@)
202 HRSRC WINAPI FindResourceExA( HMODULE hModule, LPCSTR type, LPCSTR name, WORD lang )
204 TRACE( "%p %s %s %04x\n", hModule, debugstr_a(type), debugstr_a(name), lang );
206 if (!hModule) hModule = GetModuleHandleW(0);
207 else if (!HIWORD(hModule))
209 return HRSRC_32( FindResource16( HMODULE_16(hModule), name, type ) );
211 return find_resourceA( hModule, type, name, lang );
215 /**********************************************************************
216 * FindResourceA (KERNEL32.@)
218 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
220 return FindResourceExA( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
224 /**********************************************************************
225 * FindResourceExW (KERNEL32.@)
227 HRSRC WINAPI FindResourceExW( HMODULE hModule, LPCWSTR type, LPCWSTR name, WORD lang )
229 TRACE( "%p %s %s %04x\n", hModule, debugstr_w(type), debugstr_w(name), lang );
231 if (!hModule) hModule = GetModuleHandleW(0);
232 else if (!HIWORD(hModule))
237 if (!get_res_name_type_WtoA( name, type, &nameA, &typeA )) return NULL;
239 ret = FindResource16( HMODULE_16(hModule), nameA, typeA );
240 if (HIWORD(nameA)) HeapFree( GetProcessHeap(), 0, nameA );
241 if (HIWORD(typeA)) HeapFree( GetProcessHeap(), 0, typeA );
242 return HRSRC_32(ret);
245 return find_resourceW( hModule, type, name, lang );
249 /**********************************************************************
250 * FindResourceW (KERNEL32.@)
252 HRSRC WINAPI FindResourceW( HINSTANCE hModule, LPCWSTR name, LPCWSTR type )
254 return FindResourceExW( hModule, type, name, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ) );
258 /**********************************************************************
259 * EnumResourceTypesA (KERNEL32.@)
261 BOOL WINAPI EnumResourceTypesA( HMODULE hmod, ENUMRESTYPEPROCA lpfun, LONG_PTR lparam )
266 DWORD len = 0, newlen;
268 const IMAGE_RESOURCE_DIRECTORY *resdir;
269 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
270 const IMAGE_RESOURCE_DIR_STRING_U *str;
272 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
274 if (!hmod) hmod = GetModuleHandleA( NULL );
276 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
278 SetLastError( RtlNtStatusToDosError(status) );
281 et = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(resdir + 1);
282 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
284 if (et[i].u1.s1.NameIsString)
286 str = (PIMAGE_RESOURCE_DIR_STRING_U) ((LPBYTE) resdir + et[i].u1.s1.NameOffset);
287 newlen = WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
288 if (newlen + 1 > len)
291 if (type) HeapFree( GetProcessHeap(), 0, type );
292 if (!(type = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
294 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, type, len, NULL, NULL);
296 ret = lpfun(hmod,type,lparam);
300 ret = lpfun( hmod, (LPSTR)(int)et[i].u1.s2.Id, lparam );
304 if (type) HeapFree( GetProcessHeap(), 0, type );
309 /**********************************************************************
310 * EnumResourceTypesW (KERNEL32.@)
312 BOOL WINAPI EnumResourceTypesW( HMODULE hmod, ENUMRESTYPEPROCW lpfun, LONG_PTR lparam )
318 const IMAGE_RESOURCE_DIRECTORY *resdir;
319 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
320 const IMAGE_RESOURCE_DIR_STRING_U *str;
322 TRACE( "%p %p %lx\n", hmod, lpfun, lparam );
324 if (!hmod) hmod = GetModuleHandleW( NULL );
326 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &resdir )) != STATUS_SUCCESS)
328 SetLastError( RtlNtStatusToDosError(status) );
331 et = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(resdir + 1);
332 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
334 if (et[i].u1.s1.NameIsString)
336 str = (PIMAGE_RESOURCE_DIR_STRING_U) ((LPBYTE) resdir + et[i].u1.s1.NameOffset);
337 if (str->Length + 1 > len)
339 len = str->Length + 1;
340 if (type) HeapFree( GetProcessHeap(), 0, type );
341 if (!(type = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
343 memcpy(type, str->NameString, str->Length * sizeof (WCHAR));
344 type[str->Length] = 0;
345 ret = lpfun(hmod,type,lparam);
349 ret = lpfun( hmod, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
353 if (type) HeapFree( GetProcessHeap(), 0, type );
358 /**********************************************************************
359 * EnumResourceNamesA (KERNEL32.@)
361 BOOL WINAPI EnumResourceNamesA( HMODULE hmod, LPCSTR type, ENUMRESNAMEPROCA lpfun, LONG_PTR lparam )
365 DWORD len = 0, newlen;
368 UNICODE_STRING typeW;
369 LDR_RESOURCE_INFO info;
370 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
371 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
372 const IMAGE_RESOURCE_DIR_STRING_U *str;
374 TRACE( "%p %s %p %lx\n", hmod, debugstr_a(type), lpfun, lparam );
376 if (!hmod) hmod = GetModuleHandleA( NULL );
378 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
380 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
382 info.Type = (ULONG)typeW.Buffer;
383 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
386 et = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(resdir + 1);
387 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
389 if (et[i].u1.s1.NameIsString)
391 str = (IMAGE_RESOURCE_DIR_STRING_U *) ((LPBYTE) basedir + et[i].u1.s1.NameOffset);
392 newlen = WideCharToMultiByte(CP_ACP, 0, str->NameString, str->Length, NULL, 0, NULL, NULL);
393 if (newlen + 1 > len)
396 if (name) HeapFree( GetProcessHeap(), 0, name );
397 if (!(name = HeapAlloc(GetProcessHeap(), 0, len + 1 )))
403 WideCharToMultiByte( CP_ACP, 0, str->NameString, str->Length, name, len, NULL, NULL );
405 ret = lpfun(hmod,type,name,lparam);
409 ret = lpfun( hmod, type, (LPSTR)(int)et[i].u1.s2.Id, lparam );
414 if (name) HeapFree( GetProcessHeap(), 0, name );
415 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
416 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
421 /**********************************************************************
422 * EnumResourceNamesW (KERNEL32.@)
424 BOOL WINAPI EnumResourceNamesW( HMODULE hmod, LPCWSTR type, ENUMRESNAMEPROCW lpfun, LONG_PTR lparam )
430 UNICODE_STRING typeW;
431 LDR_RESOURCE_INFO info;
432 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
433 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
434 const IMAGE_RESOURCE_DIR_STRING_U *str;
436 TRACE( "%p %s %p %lx\n", hmod, debugstr_w(type), lpfun, lparam );
438 if (!hmod) hmod = GetModuleHandleW( NULL );
440 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
442 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
444 info.Type = (ULONG)typeW.Buffer;
445 if ((status = LdrFindResourceDirectory_U( hmod, &info, 1, &resdir )) != STATUS_SUCCESS)
448 et = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(resdir + 1);
449 for (i = 0; i < resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries; i++)
451 if (et[i].u1.s1.NameIsString)
453 str = (IMAGE_RESOURCE_DIR_STRING_U *) ((LPBYTE) basedir + et[i].u1.s1.NameOffset);
454 if (str->Length + 1 > len)
456 len = str->Length + 1;
457 if (name) HeapFree( GetProcessHeap(), 0, name );
458 if (!(name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
464 memcpy(name, str->NameString, str->Length * sizeof (WCHAR));
465 name[str->Length] = 0;
466 ret = lpfun(hmod,type,name,lparam);
470 ret = lpfun( hmod, type, (LPWSTR)(int)et[i].u1.s2.Id, lparam );
475 if (name) HeapFree( GetProcessHeap(), 0, name );
476 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
477 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
482 /**********************************************************************
483 * EnumResourceLanguagesA (KERNEL32.@)
485 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmod, LPCSTR type, LPCSTR name,
486 ENUMRESLANGPROCA lpfun, LONG_PTR lparam )
491 UNICODE_STRING typeW, nameW;
492 LDR_RESOURCE_INFO info;
493 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
494 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
496 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_a(type), debugstr_a(name), lpfun, lparam );
498 if (!hmod) hmod = GetModuleHandleA( NULL );
499 typeW.Buffer = nameW.Buffer = NULL;
500 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
502 if ((status = get_res_nameA( type, &typeW )) != STATUS_SUCCESS)
504 if ((status = get_res_nameA( name, &nameW )) != STATUS_SUCCESS)
506 info.Type = (ULONG)typeW.Buffer;
507 info.Name = (ULONG)nameW.Buffer;
508 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
511 et = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(resdir + 1);
512 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
514 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
518 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
519 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
520 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
525 /**********************************************************************
526 * EnumResourceLanguagesW (KERNEL32.@)
528 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmod, LPCWSTR type, LPCWSTR name,
529 ENUMRESLANGPROCW lpfun, LONG_PTR lparam )
534 UNICODE_STRING typeW, nameW;
535 LDR_RESOURCE_INFO info;
536 const IMAGE_RESOURCE_DIRECTORY *basedir, *resdir;
537 const IMAGE_RESOURCE_DIRECTORY_ENTRY *et;
539 TRACE( "%p %s %s %p %lx\n", hmod, debugstr_w(type), debugstr_w(name), lpfun, lparam );
541 if (!hmod) hmod = GetModuleHandleW( NULL );
542 typeW.Buffer = nameW.Buffer = NULL;
543 if ((status = LdrFindResourceDirectory_U( hmod, NULL, 0, &basedir )) != STATUS_SUCCESS)
545 if ((status = get_res_nameW( type, &typeW )) != STATUS_SUCCESS)
547 if ((status = get_res_nameW( name, &nameW )) != STATUS_SUCCESS)
549 info.Type = (ULONG)typeW.Buffer;
550 info.Name = (ULONG)nameW.Buffer;
551 if ((status = LdrFindResourceDirectory_U( hmod, &info, 2, &resdir )) != STATUS_SUCCESS)
554 et = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)(resdir + 1);
555 for (i = 0; i < resdir->NumberOfNamedEntries + resdir->NumberOfIdEntries; i++)
557 ret = lpfun( hmod, type, name, et[i].u1.s2.Id, lparam );
561 if (HIWORD(typeW.Buffer)) HeapFree( GetProcessHeap(), 0, typeW.Buffer );
562 if (HIWORD(nameW.Buffer)) HeapFree( GetProcessHeap(), 0, nameW.Buffer );
563 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
568 /**********************************************************************
569 * LoadResource (KERNEL32.@)
571 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
576 TRACE( "%p %p\n", hModule, hRsrc );
578 if (hModule && !HIWORD(hModule))
579 /* FIXME: should convert return to 32-bit resource */
580 return HGLOBAL_32( LoadResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) ) );
582 if (!hRsrc) return 0;
583 if (!hModule) hModule = GetModuleHandleA( NULL );
584 status = LdrAccessResource( hModule, (IMAGE_RESOURCE_DATA_ENTRY *)hRsrc, &ret, NULL );
585 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
590 /**********************************************************************
591 * LockResource (KERNEL32.@)
593 LPVOID WINAPI LockResource( HGLOBAL handle )
595 TRACE("(%p)\n", handle );
597 if (HIWORD( handle )) /* 32-bit memory handle */
598 return (LPVOID)handle;
600 /* 16-bit memory handle */
601 return LockResource16( HGLOBAL_16(handle) );
605 /**********************************************************************
606 * FreeResource (KERNEL32.@)
608 BOOL WINAPI FreeResource( HGLOBAL handle )
610 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
611 return FreeResource16( HGLOBAL_16(handle) );
615 /**********************************************************************
616 * SizeofResource (KERNEL32.@)
618 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
620 if (hModule && !HIWORD(hModule))
621 return SizeofResource16( HMODULE_16(hModule), HRSRC_16(hRsrc) );
623 if (!hRsrc) return 0;
624 return ((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
631 struct list resources_list;
644 static BOOL CALLBACK enum_resources_languages_delete_all(HMODULE hModule, LPCWSTR lpType, LPCWSTR lpName, WORD wLang, LONG_PTR lParam)
646 return UpdateResourceW((HANDLE)lParam, lpType, lpName, wLang, NULL, 0);
649 static BOOL CALLBACK enum_resources_names_delete_all(HMODULE hModule, LPCWSTR lpType, LPWSTR lpName, LONG_PTR lParam)
651 return EnumResourceLanguagesW(hModule, lpType, lpName, enum_resources_languages_delete_all, lParam);
654 static BOOL CALLBACK enum_resources_types_delete_all(HMODULE hModule, LPWSTR lpType, LONG_PTR lParam)
656 return EnumResourceNamesW(hModule, lpType, enum_resources_names_delete_all, lParam);
659 static BOOL CALLBACK enum_resources_languages_add_all(HMODULE hModule, LPCWSTR lpType, LPCWSTR lpName, WORD wLang, LONG_PTR lParam)
662 HRSRC hResource = FindResourceExW(hModule, lpType, lpName, wLang);
666 if(hResource == NULL) return FALSE;
667 if(!(hGlobal = LoadResource(hModule, hResource))) return FALSE;
668 if(!(lpData = LockResource(hGlobal))) return FALSE;
669 if(!(size = SizeofResource(hModule, hResource))) return FALSE;
670 return UpdateResourceW((HANDLE)lParam, lpType, lpName, wLang, lpData, size);
673 static BOOL CALLBACK enum_resources_names_add_all(HMODULE hModule, LPCWSTR lpType, LPWSTR lpName, LONG_PTR lParam)
675 return EnumResourceLanguagesW(hModule, lpType, lpName, enum_resources_languages_add_all, lParam);
678 static BOOL CALLBACK enum_resources_types_add_all(HMODULE hModule, LPWSTR lpType, LONG_PTR lParam)
680 return EnumResourceNamesW(hModule, lpType, enum_resources_names_add_all, lParam);
683 /***********************************************************************
684 * BeginUpdateResourceW (KERNEL32.@)
686 HANDLE WINAPI BeginUpdateResourceW( LPCWSTR pFileName, BOOL bDeleteExistingResources )
690 HANDLE hModule = NULL;
691 HANDLE hUpdate = NULL;
692 QUEUEDUPDATES *current_updates = NULL;
695 TRACE("%s, %d\n",debugstr_w(pFileName),bDeleteExistingResources);
697 hFile = FindFirstFileW(pFileName, &fd);
698 if(hFile == INVALID_HANDLE_VALUE)
701 SetLastError(ERROR_FILE_NOT_FOUND);
704 if(fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
706 SetLastError(ERROR_FILE_READ_ONLY);
710 hModule = LoadLibraryW(pFileName);
713 SetLastError(ERROR_INVALID_PARAMETER);
717 if(!(hUpdate = GlobalAlloc(GHND, sizeof(QUEUEDUPDATES))))
719 SetLastError(ERROR_OUTOFMEMORY);
722 if(!(current_updates = GlobalLock(hUpdate)))
724 SetLastError(ERROR_INVALID_HANDLE);
727 if(!(current_updates->pFileName = HeapAlloc(GetProcessHeap(), 0, (strlenW(pFileName)+1)*sizeof(WCHAR))))
729 SetLastError(ERROR_OUTOFMEMORY);
732 strcpyW(current_updates->pFileName, pFileName);
733 list_init(¤t_updates->resources_list);
735 if(bDeleteExistingResources)
737 if(!EnumResourceTypesW(hModule, enum_resources_types_delete_all, (LONG_PTR)hUpdate))
742 if(!EnumResourceTypesW(hModule, enum_resources_types_add_all, (LONG_PTR)hUpdate))
748 if(!ret && current_updates)
750 if(current_updates->pFileName) HeapFree(GetProcessHeap(), 0, current_updates->pFileName);
751 GlobalUnlock(hUpdate);
755 if(hUpdate) GlobalUnlock(hUpdate);
756 if(hModule) FreeLibrary(hModule);
757 if(hFile) FindClose(hFile);
762 /***********************************************************************
763 * BeginUpdateResourceA (KERNEL32.@)
765 HANDLE WINAPI BeginUpdateResourceA( LPCSTR pFileName, BOOL bDeleteExistingResources )
767 UNICODE_STRING FileNameW;
769 RtlCreateUnicodeStringFromAsciiz(&FileNameW, pFileName);
770 ret = BeginUpdateResourceW(FileNameW.Buffer, bDeleteExistingResources);
771 RtlFreeUnicodeString(&FileNameW);
776 /***********************************************************************
777 * EndUpdateResourceW (KERNEL32.@)
779 BOOL WINAPI EndUpdateResourceW( HANDLE hUpdate, BOOL fDiscard )
781 QUEUEDUPDATES *current_updates = NULL;
784 struct list *ptr = NULL;
785 QUEUEDRESOURCE *current_resource = NULL;
787 FIXME("(%p,%d): stub\n",hUpdate,fDiscard);
789 if(!(current_updates = GlobalLock(hUpdate)))
791 SetLastError(ERROR_INVALID_HANDLE);
800 /* FIXME: This is the only missing part, an actual implementation */
801 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
808 while ((ptr = list_head(¤t_updates->resources_list)) != NULL)
810 current_resource = LIST_ENTRY(ptr, QUEUEDRESOURCE, entry);
811 list_remove(¤t_resource->entry);
812 if(HIWORD(current_resource->lpType)) HeapFree(GetProcessHeap(), 0, current_resource->lpType);
813 if(HIWORD(current_resource->lpName)) HeapFree(GetProcessHeap(), 0, current_resource->lpName);
814 if(current_resource->lpData) HeapFree(GetProcessHeap(), 0, current_resource->lpData);
815 HeapFree(GetProcessHeap(), 0, current_resource);
817 if(current_updates->pFileName) HeapFree(GetProcessHeap(), 0, current_updates->pFileName);
818 GlobalUnlock(hUpdate);
825 /***********************************************************************
826 * EndUpdateResourceA (KERNEL32.@)
828 BOOL WINAPI EndUpdateResourceA( HANDLE hUpdate, BOOL fDiscard )
830 return EndUpdateResourceW(hUpdate, fDiscard);
834 /***********************************************************************
835 * UpdateResourceW (KERNEL32.@)
837 BOOL WINAPI UpdateResourceW( HANDLE hUpdate, LPCWSTR lpType, LPCWSTR lpName,
838 WORD wLanguage, LPVOID lpData, DWORD cbData)
840 QUEUEDUPDATES *current_updates = NULL;
842 QUEUEDRESOURCE *current_resource = NULL;
845 TRACE("%p %s %s %08x %p %ld\n",hUpdate,debugstr_w(lpType),debugstr_w(lpName),wLanguage,lpData,cbData);
847 if(!(current_updates = GlobalLock(hUpdate)))
849 SetLastError(ERROR_INVALID_HANDLE);
854 if(!(current_resource = HeapAlloc(GetProcessHeap(), 0, sizeof(QUEUEDRESOURCE))))
856 SetLastError(ERROR_OUTOFMEMORY);
860 current_resource->lpType = (LPWSTR)lpType;
861 else if((current_resource->lpType = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpType)+1)*sizeof(WCHAR))))
862 strcpyW(current_resource->lpType, lpType);
865 SetLastError(ERROR_OUTOFMEMORY);
869 current_resource->lpName = (LPWSTR)lpName;
870 else if((current_resource->lpName = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpName)+1)*sizeof(WCHAR))))
871 strcpyW(current_resource->lpName, lpName);
874 SetLastError(ERROR_OUTOFMEMORY);
877 if(!(current_resource->lpData = HeapAlloc(GetProcessHeap(), 0, cbData)))
879 SetLastError(ERROR_OUTOFMEMORY);
882 current_resource->wLanguage = wLanguage;
883 memcpy(current_resource->lpData, lpData, cbData);
884 current_resource->cbData = cbData;
885 list_add_tail(¤t_updates->resources_list, ¤t_resource->entry);
889 if(!ret && current_resource)
891 if(HIWORD(current_resource->lpType)) HeapFree(GetProcessHeap(), 0, current_resource->lpType);
892 if(HIWORD(current_resource->lpName)) HeapFree(GetProcessHeap(), 0, current_resource->lpName);
893 if(current_resource->lpData) HeapFree(GetProcessHeap(), 0, current_resource->lpData);
894 HeapFree(GetProcessHeap(), 0, current_resource);
896 if(found) GlobalUnlock(hUpdate);
901 /***********************************************************************
902 * UpdateResourceA (KERNEL32.@)
904 BOOL WINAPI UpdateResourceA( HANDLE hUpdate, LPCSTR lpType, LPCSTR lpName,
905 WORD wLanguage, LPVOID lpData, DWORD cbData)
908 UNICODE_STRING TypeW;
909 UNICODE_STRING NameW;
911 TypeW.Buffer = (LPWSTR)lpType;
913 RtlCreateUnicodeStringFromAsciiz(&TypeW, lpType);
915 NameW.Buffer = (LPWSTR)lpName;
917 RtlCreateUnicodeStringFromAsciiz(&NameW, lpName);
918 ret = UpdateResourceW(hUpdate, TypeW.Buffer, NameW.Buffer, wLanguage, lpData, cbData);
919 if(HIWORD(lpType)) RtlFreeUnicodeString(&TypeW);
920 if(HIWORD(lpName)) RtlFreeUnicodeString(&NameW);