4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 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"
28 #include <sys/types.h>
36 #include "wine/winbase16.h"
37 #include "wine/exception.h"
39 #include "cursoricon.h"
42 #include "wine/debug.h"
45 #include "msvcrt/excpt.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(resource);
49 #define HRSRC_MAP_BLOCKSIZE 16
51 /* handle conversions */
52 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
54 typedef struct _HRSRC_ELEM
60 typedef struct _HRSRC_MAP
67 /**********************************************************************
70 static HRSRC MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
72 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
76 /* On first call, initialize HRSRC map */
79 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
80 sizeof(HRSRC_MAP) ) ) )
82 ERR("Cannot allocate HRSRC map\n" );
85 pModule->hRsrcMap = (LPVOID)map;
88 /* Check whether HRSRC32 already in map */
89 for ( i = 0; i < map->nUsed; i++ )
90 if ( map->elem[i].hRsrc == hRsrc32 )
91 return (HRSRC)(i + 1);
93 /* If no space left, grow table */
94 if ( map->nUsed == map->nAlloc )
96 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
98 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
99 * sizeof(HRSRC_ELEM) ) ))
101 ERR("Cannot grow HRSRC map\n" );
105 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
108 /* Add HRSRC32 to table */
109 map->elem[map->nUsed].hRsrc = hRsrc32;
110 map->elem[map->nUsed].type = type;
113 return (HRSRC)map->nUsed;
116 /**********************************************************************
119 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC hRsrc16 )
121 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
122 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
124 return map->elem[(int)hRsrc16-1].hRsrc;
127 /**********************************************************************
130 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC hRsrc16 )
132 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
133 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
135 return map->elem[(int)hRsrc16-1].type;
139 /* filter for page-fault exceptions */
140 static WINE_EXCEPTION_FILTER(page_fault)
142 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
143 return EXCEPTION_EXECUTE_HANDLER;
144 return EXCEPTION_CONTINUE_SEARCH;
147 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
148 LPCSTR name, WORD lang,
149 BOOL bUnicode, BOOL bRet16 )
153 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
155 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
156 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
159 bRet16? "NE" : "PE" );
161 if (!HIWORD(hModule))
163 HMODULE16 hMod16 = MapHModuleLS( hModule );
164 NE_MODULE *pModule = NE_GetPtr( hMod16 );
165 if (!pModule) return 0;
166 if (!pModule->module32)
168 /* 16-bit NE module */
169 LPSTR typeStr, nameStr;
171 if ( HIWORD( type ) && bUnicode )
172 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
174 typeStr = (LPSTR)type;
175 if ( HIWORD( name ) && bUnicode )
176 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
178 nameStr = (LPSTR)name;
180 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
182 if ( HIWORD( type ) && bUnicode )
183 HeapFree( GetProcessHeap(), 0, typeStr );
184 if ( HIWORD( name ) && bUnicode )
185 HeapFree( GetProcessHeap(), 0, nameStr );
187 /* If we need to return 32-bit HRSRC, no conversion is necessary,
188 we simply use the 16-bit HRSRC as 32-bit HRSRC */
192 /* 32-bit PE module */
193 hRsrc = RES_FindResource2( pModule->module32, type, name, lang, bUnicode, FALSE );
194 /* If we need to return 16-bit HRSRC, perform conversion */
196 hRsrc = MapHRsrc32To16( pModule, hRsrc,
197 HIWORD( type )? 0 : LOWORD( type ) );
202 /* 32-bit PE module */
203 LPWSTR typeStr, nameStr;
205 if ( HIWORD( type ) && !bUnicode )
206 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
208 typeStr = (LPWSTR)type;
209 if ( HIWORD( name ) && !bUnicode )
210 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
212 nameStr = (LPWSTR)name;
214 /* Here is the real difference between FindResouce and FindResourceEx */
215 if(lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ||
216 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) ||
217 lang == MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT) ||
218 lang == MAKELANGID(LANG_NEUTRAL, 3)) /* FIXME: real name? */
219 hRsrc = PE_FindResourceW( hModule, nameStr, typeStr );
221 hRsrc = PE_FindResourceExW( hModule, nameStr, typeStr, lang );
223 if ( HIWORD( type ) && !bUnicode )
224 HeapFree( GetProcessHeap(), 0, typeStr );
225 if ( HIWORD( name ) && !bUnicode )
226 HeapFree( GetProcessHeap(), 0, nameStr );
231 /**********************************************************************
235 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
236 LPCSTR name, WORD lang,
237 BOOL bUnicode, BOOL bRet16 )
242 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
246 WARN("page fault\n");
247 SetLastError(ERROR_INVALID_PARAMETER);
254 /**********************************************************************
257 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
259 if (!hRsrc) return 0;
261 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
263 if (!HIWORD(hModule))
265 HMODULE16 hMod16 = MapHModuleLS( hModule );
266 NE_MODULE *pModule = NE_GetPtr( hMod16 );
267 if (!pModule) return 0;
269 if (!pModule->module32) /* 16-bit NE module */
271 /* If we got a 32-bit hRsrc, we don't need to convert it */
272 return NE_SizeofResource( pModule, hRsrc );
275 /* If we got a 16-bit hRsrc, convert it */
276 if (!HIWORD(hRsrc)) hRsrc = MapHRsrc16To32( pModule, hRsrc );
279 /* 32-bit PE module */
280 return PE_SizeofResource( hRsrc );
283 /**********************************************************************
286 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
290 TRACE("(%08x, %08x, %s)\n", hModule, hRsrc, bRet16? "NE" : "PE" );
292 if (!hRsrc) return 0;
294 if (!HIWORD(hModule))
296 HMODULE16 hMod16 = MapHModuleLS( hModule );
297 NE_MODULE *pModule = NE_GetPtr( hMod16 );
298 if (!pModule) return 0;
299 if (!pModule->module32)
301 /* 16-bit NE module */
303 /* If we got a 32-bit hRsrc, we don't need to convert it */
304 hMem = NE_LoadResource( pModule, LOWORD(hRsrc) );
306 /* If we are to return a 32-bit resource, we should probably
307 convert it but we don't for now. FIXME !!! */
312 /* If we got a 16-bit hRsrc, convert it */
313 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
315 hMem = PE_LoadResource( pModule->module32, hRsrc32 );
317 /* If we need to return a 16-bit resource, convert it */
320 WORD type = MapHRsrc16ToType( pModule, hRsrc );
321 DWORD size = SizeofResource( hModule, hRsrc );
322 LPVOID bits = LockResource( hMem );
324 hMem = NE_LoadPEResource( pModule, type, bits, size );
330 /* 32-bit PE module */
331 hMem = PE_LoadResource( hModule, hRsrc );
337 /**********************************************************************
338 * FindResource (KERNEL.60)
339 * FindResource16 (KERNEL32.@)
341 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
343 return LOWORD( RES_FindResource( hModule, type, name,
344 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, TRUE ) );
347 /**********************************************************************
348 * FindResourceA (KERNEL32.@)
350 HRSRC WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
352 return RES_FindResource( hModule, type, name,
353 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), FALSE, FALSE );
356 /**********************************************************************
357 * FindResourceExA (KERNEL32.@)
359 HRSRC WINAPI FindResourceExA( HMODULE hModule,
360 LPCSTR type, LPCSTR name, WORD lang )
362 return RES_FindResource( hModule, type, name,
363 lang, FALSE, FALSE );
366 /**********************************************************************
367 * FindResourceExW (KERNEL32.@)
369 HRSRC WINAPI FindResourceExW( HMODULE hModule,
370 LPCWSTR type, LPCWSTR name, WORD lang )
372 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
376 /**********************************************************************
377 * FindResourceW (KERNEL32.@)
379 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
381 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
382 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), TRUE, FALSE );
385 /**********************************************************************
386 * LoadResource (KERNEL.61)
387 * LoadResource16 (KERNEL32.@)
389 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
391 return RES_LoadResource( hModule, HRSRC_32(hRsrc), TRUE );
394 /**********************************************************************
395 * LoadResource (KERNEL32.@)
397 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
399 return RES_LoadResource( hModule, hRsrc, FALSE );
402 /**********************************************************************
403 * LockResource (KERNEL.62)
405 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
407 TRACE("(%04x)\n", handle );
408 /* May need to reload the resource if discarded */
409 return K32WOWGlobalLock16( handle );
412 /**********************************************************************
413 * LockResource16 (KERNEL32.@)
415 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
417 return MapSL( WIN16_LockResource16(handle) );
420 /**********************************************************************
421 * LockResource (KERNEL32.@)
423 LPVOID WINAPI LockResource( HGLOBAL handle )
425 TRACE("(%08x)\n", handle );
427 if (HIWORD( handle )) /* 32-bit memory handle */
428 return (LPVOID)handle;
430 /* 16-bit memory handle */
431 return LockResource16( LOWORD(handle) );
434 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
437 /**********************************************************************
438 * FreeResource (KERNEL.63)
439 * FreeResource16 (KERNEL32.@)
441 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
443 HGLOBAL16 retv = handle;
444 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
446 TRACE("(%04x)\n", handle );
448 /* Try NE resource first */
449 retv = NE_FreeResource( pModule, handle );
451 /* If this failed, call USER.DestroyIcon32; this will check
452 whether it is a shared cursor/icon; if not it will call
456 pDestroyIcon32Proc proc;
457 HMODULE user = GetModuleHandleA( "user32.dll" );
459 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
460 retv = proc( handle, CID_RESOURCE );
462 retv = GlobalFree16( handle );
467 /**********************************************************************
468 * FreeResource (KERNEL32.@)
470 BOOL WINAPI FreeResource( HGLOBAL handle )
472 if (HIWORD(handle)) return 0; /* 32-bit memory handle: nothing to do */
474 return FreeResource16( LOWORD(handle) );
477 /**********************************************************************
478 * SizeofResource (KERNEL.65)
479 * SizeofResource16 (KERNEL32.@)
481 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
483 return RES_SizeofResource( hModule, HRSRC_32(hRsrc), TRUE );
486 /**********************************************************************
487 * SizeofResource (KERNEL32.@)
489 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
491 return RES_SizeofResource( hModule, hRsrc, FALSE );