4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
17 #include "wine/winbase16.h"
18 #include "wine/exception.h"
23 #include "cursoricon.h"
29 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(resource
);
35 #define HRSRC_MAP_BLOCKSIZE 16
37 typedef struct _HRSRC_ELEM
43 typedef struct _HRSRC_MAP
50 /**********************************************************************
53 static HRSRC16
MapHRsrc32To16( NE_MODULE
*pModule
, HANDLE hRsrc32
, WORD type
)
55 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
59 /* On first call, initialize HRSRC map */
62 if ( !(map
= (HRSRC_MAP
*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
63 sizeof(HRSRC_MAP
) ) ) )
65 ERR("Cannot allocate HRSRC map\n" );
68 pModule
->hRsrcMap
= (LPVOID
)map
;
71 /* Check whether HRSRC32 already in map */
72 for ( i
= 0; i
< map
->nUsed
; i
++ )
73 if ( map
->elem
[i
].hRsrc
== hRsrc32
)
74 return (HRSRC16
)(i
+ 1);
76 /* If no space left, grow table */
77 if ( map
->nUsed
== map
->nAlloc
)
79 if ( !(newElem
= (HRSRC_ELEM
*)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
81 (map
->nAlloc
+ HRSRC_MAP_BLOCKSIZE
)
82 * sizeof(HRSRC_ELEM
) ) ))
84 ERR("Cannot grow HRSRC map\n" );
88 map
->nAlloc
+= HRSRC_MAP_BLOCKSIZE
;
91 /* Add HRSRC32 to table */
92 map
->elem
[map
->nUsed
].hRsrc
= hRsrc32
;
93 map
->elem
[map
->nUsed
].type
= type
;
96 return (HRSRC16
)map
->nUsed
;
99 /**********************************************************************
102 static HANDLE
MapHRsrc16To32( NE_MODULE
*pModule
, HRSRC16 hRsrc16
)
104 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
105 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
107 return map
->elem
[(int)hRsrc16
-1].hRsrc
;
110 /**********************************************************************
113 static WORD
MapHRsrc16ToType( NE_MODULE
*pModule
, HRSRC16 hRsrc16
)
115 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
116 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
118 return map
->elem
[(int)hRsrc16
-1].type
;
122 /* filter for page-fault exceptions */
123 static WINE_EXCEPTION_FILTER(page_fault
)
125 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
126 return EXCEPTION_EXECUTE_HANDLER
;
127 return EXCEPTION_CONTINUE_SEARCH
;
130 static HRSRC
RES_FindResource2( HMODULE hModule
, LPCSTR type
,
131 LPCSTR name
, WORD lang
,
132 BOOL bUnicode
, BOOL bRet16
)
136 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
138 (UINT
)type
, HIWORD(type
)? (bUnicode
? debugstr_w((LPWSTR
)type
) : debugstr_a(type
)) : "",
139 (UINT
)name
, HIWORD(name
)? (bUnicode
? debugstr_w((LPWSTR
)name
) : debugstr_a(name
)) : "",
142 bRet16
? "NE" : "PE" );
144 if (!HIWORD(hModule
))
146 HMODULE16 hMod16
= MapHModuleLS( hModule
);
147 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
148 if (!pModule
) return 0;
149 if (!pModule
->module32
)
151 /* 16-bit NE module */
152 LPSTR typeStr
, nameStr
;
154 if ( HIWORD( type
) && bUnicode
)
155 typeStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)type
);
157 typeStr
= (LPSTR
)type
;
158 if ( HIWORD( name
) && bUnicode
)
159 nameStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)name
);
161 nameStr
= (LPSTR
)name
;
163 hRsrc
= NE_FindResource( pModule
, nameStr
, typeStr
);
165 if ( HIWORD( type
) && bUnicode
)
166 HeapFree( GetProcessHeap(), 0, typeStr
);
167 if ( HIWORD( name
) && bUnicode
)
168 HeapFree( GetProcessHeap(), 0, nameStr
);
170 /* If we need to return 32-bit HRSRC, no conversion is necessary,
171 we simply use the 16-bit HRSRC as 32-bit HRSRC */
175 /* 32-bit PE module */
176 hRsrc
= RES_FindResource2( pModule
->module32
, type
, name
, lang
, bUnicode
, FALSE
);
177 /* If we need to return 16-bit HRSRC, perform conversion */
179 hRsrc
= MapHRsrc32To16( pModule
, hRsrc
,
180 HIWORD( type
)? 0 : LOWORD( type
) );
185 /* 32-bit PE module */
186 LPWSTR typeStr
, nameStr
;
188 if ( HIWORD( type
) && !bUnicode
)
189 typeStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, type
);
191 typeStr
= (LPWSTR
)type
;
192 if ( HIWORD( name
) && !bUnicode
)
193 nameStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, name
);
195 nameStr
= (LPWSTR
)name
;
197 /* Here is the real difference between FindResouce and FindResourceEx */
198 if(lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
) ||
199 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
) ||
200 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_SYS_DEFAULT
) ||
201 lang
== MAKELANGID(LANG_NEUTRAL
, 3)) /* FIXME: real name? */
202 hRsrc
= PE_FindResourceW( hModule
, nameStr
, typeStr
);
204 hRsrc
= PE_FindResourceExW( hModule
, nameStr
, typeStr
, lang
);
206 if ( HIWORD( type
) && !bUnicode
)
207 HeapFree( GetProcessHeap(), 0, typeStr
);
208 if ( HIWORD( name
) && !bUnicode
)
209 HeapFree( GetProcessHeap(), 0, nameStr
);
214 /**********************************************************************
218 static HRSRC
RES_FindResource( HMODULE hModule
, LPCSTR type
,
219 LPCSTR name
, WORD lang
,
220 BOOL bUnicode
, BOOL bRet16
)
225 hRsrc
= RES_FindResource2(hModule
, type
, name
, lang
, bUnicode
, bRet16
);
229 WARN("page fault\n");
230 SetLastError(ERROR_INVALID_PARAMETER
);
237 /**********************************************************************
240 static DWORD
RES_SizeofResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
242 if (!hRsrc
) return 0;
244 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
246 if (!HIWORD(hModule
))
248 HMODULE16 hMod16
= MapHModuleLS( hModule
);
249 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
250 if (!pModule
) return 0;
252 if (!pModule
->module32
) /* 16-bit NE module */
254 /* If we got a 32-bit hRsrc, we don't need to convert it */
255 return NE_SizeofResource( pModule
, hRsrc
);
258 /* If we got a 16-bit hRsrc, convert it */
259 if (!HIWORD(hRsrc
)) hRsrc
= MapHRsrc16To32( pModule
, hRsrc
);
262 /* 32-bit PE module */
263 return PE_SizeofResource( hRsrc
);
266 /**********************************************************************
269 static HGLOBAL
RES_LoadResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
273 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
275 if (!hRsrc
) return 0;
277 if (!HIWORD(hModule
))
279 HMODULE16 hMod16
= MapHModuleLS( hModule
);
280 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
281 if (!pModule
) return 0;
282 if (!pModule
->module32
)
284 /* 16-bit NE module */
286 /* If we got a 32-bit hRsrc, we don't need to convert it */
287 hMem
= NE_LoadResource( pModule
, hRsrc
);
289 /* If we are to return a 32-bit resource, we should probably
290 convert it but we don't for now. FIXME !!! */
295 /* If we got a 16-bit hRsrc, convert it */
296 HRSRC hRsrc32
= HIWORD(hRsrc
)? hRsrc
: MapHRsrc16To32( pModule
, hRsrc
);
298 hMem
= PE_LoadResource( pModule
->module32
, hRsrc32
);
300 /* If we need to return a 16-bit resource, convert it */
303 WORD type
= MapHRsrc16ToType( pModule
, hRsrc
);
304 DWORD size
= SizeofResource( hModule
, hRsrc
);
305 LPVOID bits
= LockResource( hMem
);
307 hMem
= NE_LoadPEResource( pModule
, type
, bits
, size
);
313 /* 32-bit PE module */
314 hMem
= PE_LoadResource( hModule
, hRsrc
);
320 /**********************************************************************
321 * FindResource16 (KERNEL.60)
323 HRSRC16 WINAPI
FindResource16( HMODULE16 hModule
, SEGPTR name
, SEGPTR type
)
325 LPCSTR nameStr
= HIWORD(name
)? PTR_SEG_TO_LIN(name
) : (LPCSTR
)name
;
326 LPCSTR typeStr
= HIWORD(type
)? PTR_SEG_TO_LIN(type
) : (LPCSTR
)type
;
328 return RES_FindResource( hModule
, typeStr
, nameStr
,
329 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, TRUE
);
332 /**********************************************************************
333 * FindResourceA (KERNEL32.128)
335 HANDLE WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
337 return RES_FindResource( hModule
, type
, name
,
338 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, FALSE
);
341 /**********************************************************************
342 * FindResourceExA (KERNEL32.129)
344 HANDLE WINAPI
FindResourceExA( HMODULE hModule
,
345 LPCSTR type
, LPCSTR name
, WORD lang
)
347 return RES_FindResource( hModule
, type
, name
,
348 lang
, FALSE
, FALSE
);
351 /**********************************************************************
352 * FindResourceExW (KERNEL32.130)
354 HRSRC WINAPI
FindResourceExW( HMODULE hModule
,
355 LPCWSTR type
, LPCWSTR name
, WORD lang
)
357 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
361 /**********************************************************************
362 * FindResourceW (KERNEL32.131)
364 HRSRC WINAPI
FindResourceW(HINSTANCE hModule
, LPCWSTR name
, LPCWSTR type
)
366 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
367 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), TRUE
, FALSE
);
370 /**********************************************************************
371 * LoadResource16 (KERNEL.61)
373 HGLOBAL16 WINAPI
LoadResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
375 return RES_LoadResource( hModule
, hRsrc
, TRUE
);
378 /**********************************************************************
379 * LoadResource (KERNEL32.370)
381 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
383 return RES_LoadResource( hModule
, hRsrc
, FALSE
);
386 /**********************************************************************
387 * LockResource16 (KERNEL.62)
389 SEGPTR WINAPI
WIN16_LockResource16( HGLOBAL16 handle
)
391 TRACE("(%04x)\n", handle
);
392 /* May need to reload the resource if discarded */
393 return WIN16_GlobalLock16( handle
);
395 LPVOID WINAPI
LockResource16( HGLOBAL16 handle
)
397 return PTR_SEG_TO_LIN( WIN16_LockResource16(handle
) );
400 /**********************************************************************
401 * LockResource (KERNEL32.384)
403 LPVOID WINAPI
LockResource( HGLOBAL handle
)
405 TRACE("(%08x)\n", handle
);
407 if (HIWORD( handle
)) /* 32-bit memory handle */
408 return (LPVOID
)handle
;
410 /* 16-bit memory handle */
411 return LockResource16( handle
);
414 /**********************************************************************
415 * FreeResource16 (KERNEL.63)
417 BOOL16 WINAPI
FreeResource16( HGLOBAL16 handle
)
419 HGLOBAL retv
= handle
;
420 NE_MODULE
*pModule
= NE_GetPtr( FarGetOwner16( handle
) );
422 TRACE("(%04x)\n", handle
);
424 /* Try NE resource first */
425 retv
= NE_FreeResource( pModule
, handle
);
427 /* If this failed, call USER.DestroyIcon32; this will check
428 whether it is a shared cursor/icon; if not it will call
432 if ( Callout
.DestroyIcon32
)
433 retv
= Callout
.DestroyIcon32( handle
, CID_RESOURCE
);
435 retv
= GlobalFree16( handle
);
440 /**********************************************************************
441 * FreeResource (KERNEL32.145)
443 BOOL WINAPI
FreeResource( HGLOBAL handle
)
445 if (HIWORD(handle
)) return 0; /* 32-bit memory handle: nothing to do */
447 return FreeResource16( handle
);
450 /**********************************************************************
451 * SizeofResource16 (KERNEL.65)
453 DWORD WINAPI
SizeofResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
455 return RES_SizeofResource( hModule
, hRsrc
, TRUE
);
458 /**********************************************************************
459 * SizeofResource (KERNEL32.522)
461 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
463 return RES_SizeofResource( hModule
, hRsrc
, FALSE
);