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"
20 #include "cursoricon.h"
23 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(resource
);
29 #define HRSRC_MAP_BLOCKSIZE 16
31 typedef struct _HRSRC_ELEM
37 typedef struct _HRSRC_MAP
44 /**********************************************************************
47 static HRSRC16
MapHRsrc32To16( NE_MODULE
*pModule
, HANDLE hRsrc32
, WORD type
)
49 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
53 /* On first call, initialize HRSRC map */
56 if ( !(map
= (HRSRC_MAP
*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
57 sizeof(HRSRC_MAP
) ) ) )
59 ERR("Cannot allocate HRSRC map\n" );
62 pModule
->hRsrcMap
= (LPVOID
)map
;
65 /* Check whether HRSRC32 already in map */
66 for ( i
= 0; i
< map
->nUsed
; i
++ )
67 if ( map
->elem
[i
].hRsrc
== hRsrc32
)
68 return (HRSRC16
)(i
+ 1);
70 /* If no space left, grow table */
71 if ( map
->nUsed
== map
->nAlloc
)
73 if ( !(newElem
= (HRSRC_ELEM
*)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
75 (map
->nAlloc
+ HRSRC_MAP_BLOCKSIZE
)
76 * sizeof(HRSRC_ELEM
) ) ))
78 ERR("Cannot grow HRSRC map\n" );
82 map
->nAlloc
+= HRSRC_MAP_BLOCKSIZE
;
85 /* Add HRSRC32 to table */
86 map
->elem
[map
->nUsed
].hRsrc
= hRsrc32
;
87 map
->elem
[map
->nUsed
].type
= type
;
90 return (HRSRC16
)map
->nUsed
;
93 /**********************************************************************
96 static HANDLE
MapHRsrc16To32( NE_MODULE
*pModule
, HRSRC16 hRsrc16
)
98 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
99 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
101 return map
->elem
[(int)hRsrc16
-1].hRsrc
;
104 /**********************************************************************
107 static WORD
MapHRsrc16ToType( NE_MODULE
*pModule
, HRSRC16 hRsrc16
)
109 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
110 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
112 return map
->elem
[(int)hRsrc16
-1].type
;
116 /* filter for page-fault exceptions */
117 static WINE_EXCEPTION_FILTER(page_fault
)
119 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
120 return EXCEPTION_EXECUTE_HANDLER
;
121 return EXCEPTION_CONTINUE_SEARCH
;
124 static HRSRC
RES_FindResource2( HMODULE hModule
, LPCSTR type
,
125 LPCSTR name
, WORD lang
,
126 BOOL bUnicode
, BOOL bRet16
)
130 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
132 (UINT
)type
, HIWORD(type
)? (bUnicode
? debugstr_w((LPWSTR
)type
) : debugstr_a(type
)) : "",
133 (UINT
)name
, HIWORD(name
)? (bUnicode
? debugstr_w((LPWSTR
)name
) : debugstr_a(name
)) : "",
136 bRet16
? "NE" : "PE" );
138 if (!HIWORD(hModule
))
140 HMODULE16 hMod16
= MapHModuleLS( hModule
);
141 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
142 if (!pModule
) return 0;
143 if (!pModule
->module32
)
145 /* 16-bit NE module */
146 LPSTR typeStr
, nameStr
;
148 if ( HIWORD( type
) && bUnicode
)
149 typeStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)type
);
151 typeStr
= (LPSTR
)type
;
152 if ( HIWORD( name
) && bUnicode
)
153 nameStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)name
);
155 nameStr
= (LPSTR
)name
;
157 hRsrc
= NE_FindResource( pModule
, nameStr
, typeStr
);
159 if ( HIWORD( type
) && bUnicode
)
160 HeapFree( GetProcessHeap(), 0, typeStr
);
161 if ( HIWORD( name
) && bUnicode
)
162 HeapFree( GetProcessHeap(), 0, nameStr
);
164 /* If we need to return 32-bit HRSRC, no conversion is necessary,
165 we simply use the 16-bit HRSRC as 32-bit HRSRC */
169 /* 32-bit PE module */
170 hRsrc
= RES_FindResource2( pModule
->module32
, type
, name
, lang
, bUnicode
, FALSE
);
171 /* If we need to return 16-bit HRSRC, perform conversion */
173 hRsrc
= MapHRsrc32To16( pModule
, hRsrc
,
174 HIWORD( type
)? 0 : LOWORD( type
) );
179 /* 32-bit PE module */
180 LPWSTR typeStr
, nameStr
;
182 if ( HIWORD( type
) && !bUnicode
)
183 typeStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, type
);
185 typeStr
= (LPWSTR
)type
;
186 if ( HIWORD( name
) && !bUnicode
)
187 nameStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, name
);
189 nameStr
= (LPWSTR
)name
;
191 /* Here is the real difference between FindResouce and FindResourceEx */
192 if(lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
) ||
193 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
) ||
194 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_SYS_DEFAULT
) ||
195 lang
== MAKELANGID(LANG_NEUTRAL
, 3)) /* FIXME: real name? */
196 hRsrc
= PE_FindResourceW( hModule
, nameStr
, typeStr
);
198 hRsrc
= PE_FindResourceExW( hModule
, nameStr
, typeStr
, lang
);
200 if ( HIWORD( type
) && !bUnicode
)
201 HeapFree( GetProcessHeap(), 0, typeStr
);
202 if ( HIWORD( name
) && !bUnicode
)
203 HeapFree( GetProcessHeap(), 0, nameStr
);
208 /**********************************************************************
212 static HRSRC
RES_FindResource( HMODULE hModule
, LPCSTR type
,
213 LPCSTR name
, WORD lang
,
214 BOOL bUnicode
, BOOL bRet16
)
219 hRsrc
= RES_FindResource2(hModule
, type
, name
, lang
, bUnicode
, bRet16
);
223 WARN("page fault\n");
224 SetLastError(ERROR_INVALID_PARAMETER
);
231 /**********************************************************************
234 static DWORD
RES_SizeofResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
236 if (!hRsrc
) return 0;
238 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
240 if (!HIWORD(hModule
))
242 HMODULE16 hMod16
= MapHModuleLS( hModule
);
243 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
244 if (!pModule
) return 0;
246 if (!pModule
->module32
) /* 16-bit NE module */
248 /* If we got a 32-bit hRsrc, we don't need to convert it */
249 return NE_SizeofResource( pModule
, hRsrc
);
252 /* If we got a 16-bit hRsrc, convert it */
253 if (!HIWORD(hRsrc
)) hRsrc
= MapHRsrc16To32( pModule
, hRsrc
);
256 /* 32-bit PE module */
257 return PE_SizeofResource( hRsrc
);
260 /**********************************************************************
263 static HGLOBAL
RES_LoadResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
267 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
269 if (!hRsrc
) return 0;
271 if (!HIWORD(hModule
))
273 HMODULE16 hMod16
= MapHModuleLS( hModule
);
274 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
275 if (!pModule
) return 0;
276 if (!pModule
->module32
)
278 /* 16-bit NE module */
280 /* If we got a 32-bit hRsrc, we don't need to convert it */
281 hMem
= NE_LoadResource( pModule
, hRsrc
);
283 /* If we are to return a 32-bit resource, we should probably
284 convert it but we don't for now. FIXME !!! */
289 /* If we got a 16-bit hRsrc, convert it */
290 HRSRC hRsrc32
= HIWORD(hRsrc
)? hRsrc
: MapHRsrc16To32( pModule
, hRsrc
);
292 hMem
= PE_LoadResource( pModule
->module32
, hRsrc32
);
294 /* If we need to return a 16-bit resource, convert it */
297 WORD type
= MapHRsrc16ToType( pModule
, hRsrc
);
298 DWORD size
= SizeofResource( hModule
, hRsrc
);
299 LPVOID bits
= LockResource( hMem
);
301 hMem
= NE_LoadPEResource( pModule
, type
, bits
, size
);
307 /* 32-bit PE module */
308 hMem
= PE_LoadResource( hModule
, hRsrc
);
314 /**********************************************************************
315 * FindResource (KERNEL.60)
316 * FindResource16 (KERNEL32.@)
318 HRSRC16 WINAPI
FindResource16( HMODULE16 hModule
, LPCSTR name
, LPCSTR type
)
320 return RES_FindResource( hModule
, type
, name
,
321 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, TRUE
);
324 /**********************************************************************
325 * FindResourceA (KERNEL32.@)
327 HRSRC WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
329 return RES_FindResource( hModule
, type
, name
,
330 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, FALSE
);
333 /**********************************************************************
334 * FindResourceExA (KERNEL32.@)
336 HRSRC WINAPI
FindResourceExA( HMODULE hModule
,
337 LPCSTR type
, LPCSTR name
, WORD lang
)
339 return RES_FindResource( hModule
, type
, name
,
340 lang
, FALSE
, FALSE
);
343 /**********************************************************************
344 * FindResourceExW (KERNEL32.@)
346 HRSRC WINAPI
FindResourceExW( HMODULE hModule
,
347 LPCWSTR type
, LPCWSTR name
, WORD lang
)
349 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
353 /**********************************************************************
354 * FindResourceW (KERNEL32.@)
356 HRSRC WINAPI
FindResourceW(HINSTANCE hModule
, LPCWSTR name
, LPCWSTR type
)
358 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
359 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), TRUE
, FALSE
);
362 /**********************************************************************
363 * LoadResource (KERNEL.61)
364 * LoadResource16 (KERNEL32.@)
366 HGLOBAL16 WINAPI
LoadResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
368 return RES_LoadResource( hModule
, hRsrc
, TRUE
);
371 /**********************************************************************
372 * LoadResource (KERNEL32.@)
374 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
376 return RES_LoadResource( hModule
, hRsrc
, FALSE
);
379 /**********************************************************************
380 * LockResource (KERNEL.62)
382 SEGPTR WINAPI
WIN16_LockResource16( HGLOBAL16 handle
)
384 TRACE("(%04x)\n", handle
);
385 /* May need to reload the resource if discarded */
386 return K32WOWGlobalLock16( handle
);
389 /**********************************************************************
390 * LockResource16 (KERNEL32.@)
392 LPVOID WINAPI
LockResource16( HGLOBAL16 handle
)
394 return MapSL( WIN16_LockResource16(handle
) );
397 /**********************************************************************
398 * LockResource (KERNEL32.@)
400 LPVOID WINAPI
LockResource( HGLOBAL handle
)
402 TRACE("(%08x)\n", handle
);
404 if (HIWORD( handle
)) /* 32-bit memory handle */
405 return (LPVOID
)handle
;
407 /* 16-bit memory handle */
408 return LockResource16( handle
);
411 typedef WORD
WINAPI (*pDestroyIcon32Proc
)( HGLOBAL16 handle
, UINT16 flags
);
414 /**********************************************************************
415 * FreeResource (KERNEL.63)
416 * FreeResource16 (KERNEL32.@)
418 BOOL16 WINAPI
FreeResource16( HGLOBAL16 handle
)
420 HGLOBAL retv
= handle
;
421 NE_MODULE
*pModule
= NE_GetPtr( FarGetOwner16( handle
) );
423 TRACE("(%04x)\n", handle
);
425 /* Try NE resource first */
426 retv
= NE_FreeResource( pModule
, handle
);
428 /* If this failed, call USER.DestroyIcon32; this will check
429 whether it is a shared cursor/icon; if not it will call
433 pDestroyIcon32Proc proc
;
434 HMODULE user
= GetModuleHandleA( "user32.dll" );
436 if (user
&& (proc
= (pDestroyIcon32Proc
)GetProcAddress( user
, "DestroyIcon32" )))
437 retv
= proc( handle
, CID_RESOURCE
);
439 retv
= GlobalFree16( handle
);
444 /**********************************************************************
445 * FreeResource (KERNEL32.@)
447 BOOL WINAPI
FreeResource( HGLOBAL handle
)
449 if (HIWORD(handle
)) return 0; /* 32-bit memory handle: nothing to do */
451 return FreeResource16( handle
);
454 /**********************************************************************
455 * SizeofResource (KERNEL.65)
456 * SizeofResource16 (KERNEL32.@)
458 DWORD WINAPI
SizeofResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
460 return RES_SizeofResource( hModule
, hRsrc
, TRUE
);
463 /**********************************************************************
464 * SizeofResource (KERNEL32.@)
466 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
468 return RES_SizeofResource( hModule
, hRsrc
, FALSE
);