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
27 #include <sys/types.h>
35 #include "wine/winbase16.h"
36 #include "wine/exception.h"
38 #include "cursoricon.h"
41 #include "wine/debug.h"
44 #include "msvcrt/excpt.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
48 #define HRSRC_MAP_BLOCKSIZE 16
50 typedef struct _HRSRC_ELEM
56 typedef struct _HRSRC_MAP
63 /**********************************************************************
66 static HRSRC
MapHRsrc32To16( NE_MODULE
*pModule
, HANDLE hRsrc32
, WORD type
)
68 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
72 /* On first call, initialize HRSRC map */
75 if ( !(map
= (HRSRC_MAP
*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
76 sizeof(HRSRC_MAP
) ) ) )
78 ERR("Cannot allocate HRSRC map\n" );
81 pModule
->hRsrcMap
= (LPVOID
)map
;
84 /* Check whether HRSRC32 already in map */
85 for ( i
= 0; i
< map
->nUsed
; i
++ )
86 if ( map
->elem
[i
].hRsrc
== hRsrc32
)
87 return (HRSRC
)(i
+ 1);
89 /* If no space left, grow table */
90 if ( map
->nUsed
== map
->nAlloc
)
92 if ( !(newElem
= (HRSRC_ELEM
*)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
94 (map
->nAlloc
+ HRSRC_MAP_BLOCKSIZE
)
95 * sizeof(HRSRC_ELEM
) ) ))
97 ERR("Cannot grow HRSRC map\n" );
101 map
->nAlloc
+= HRSRC_MAP_BLOCKSIZE
;
104 /* Add HRSRC32 to table */
105 map
->elem
[map
->nUsed
].hRsrc
= hRsrc32
;
106 map
->elem
[map
->nUsed
].type
= type
;
109 return (HRSRC
)map
->nUsed
;
112 /**********************************************************************
115 static HRSRC
MapHRsrc16To32( NE_MODULE
*pModule
, HRSRC hRsrc16
)
117 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
118 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
120 return map
->elem
[(int)hRsrc16
-1].hRsrc
;
123 /**********************************************************************
126 static WORD
MapHRsrc16ToType( NE_MODULE
*pModule
, HRSRC hRsrc16
)
128 HRSRC_MAP
*map
= (HRSRC_MAP
*)pModule
->hRsrcMap
;
129 if ( !map
|| !hRsrc16
|| (int)hRsrc16
> map
->nUsed
) return 0;
131 return map
->elem
[(int)hRsrc16
-1].type
;
135 /* filter for page-fault exceptions */
136 static WINE_EXCEPTION_FILTER(page_fault
)
138 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
139 return EXCEPTION_EXECUTE_HANDLER
;
140 return EXCEPTION_CONTINUE_SEARCH
;
143 static HRSRC
RES_FindResource2( HMODULE hModule
, LPCSTR type
,
144 LPCSTR name
, WORD lang
,
145 BOOL bUnicode
, BOOL bRet16
)
149 TRACE("(%08x, %08x%s, %08x%s, %04x, %s, %s)\n",
151 (UINT
)type
, HIWORD(type
)? (bUnicode
? debugstr_w((LPWSTR
)type
) : debugstr_a(type
)) : "",
152 (UINT
)name
, HIWORD(name
)? (bUnicode
? debugstr_w((LPWSTR
)name
) : debugstr_a(name
)) : "",
155 bRet16
? "NE" : "PE" );
157 if (!HIWORD(hModule
))
159 HMODULE16 hMod16
= MapHModuleLS( hModule
);
160 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
161 if (!pModule
) return 0;
162 if (!pModule
->module32
)
164 /* 16-bit NE module */
165 LPSTR typeStr
, nameStr
;
167 if ( HIWORD( type
) && bUnicode
)
168 typeStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)type
);
170 typeStr
= (LPSTR
)type
;
171 if ( HIWORD( name
) && bUnicode
)
172 nameStr
= HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR
)name
);
174 nameStr
= (LPSTR
)name
;
176 hRsrc
= NE_FindResource( pModule
, nameStr
, typeStr
);
178 if ( HIWORD( type
) && bUnicode
)
179 HeapFree( GetProcessHeap(), 0, typeStr
);
180 if ( HIWORD( name
) && bUnicode
)
181 HeapFree( GetProcessHeap(), 0, nameStr
);
183 /* If we need to return 32-bit HRSRC, no conversion is necessary,
184 we simply use the 16-bit HRSRC as 32-bit HRSRC */
188 /* 32-bit PE module */
189 hRsrc
= RES_FindResource2( pModule
->module32
, type
, name
, lang
, bUnicode
, FALSE
);
190 /* If we need to return 16-bit HRSRC, perform conversion */
192 hRsrc
= MapHRsrc32To16( pModule
, hRsrc
,
193 HIWORD( type
)? 0 : LOWORD( type
) );
198 /* 32-bit PE module */
199 LPWSTR typeStr
, nameStr
;
201 if ( HIWORD( type
) && !bUnicode
)
202 typeStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, type
);
204 typeStr
= (LPWSTR
)type
;
205 if ( HIWORD( name
) && !bUnicode
)
206 nameStr
= HEAP_strdupAtoW( GetProcessHeap(), 0, name
);
208 nameStr
= (LPWSTR
)name
;
210 /* Here is the real difference between FindResouce and FindResourceEx */
211 if(lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
) ||
212 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
) ||
213 lang
== MAKELANGID(LANG_NEUTRAL
, SUBLANG_SYS_DEFAULT
) ||
214 lang
== MAKELANGID(LANG_NEUTRAL
, 3)) /* FIXME: real name? */
215 hRsrc
= PE_FindResourceW( hModule
, nameStr
, typeStr
);
217 hRsrc
= PE_FindResourceExW( hModule
, nameStr
, typeStr
, lang
);
219 if ( HIWORD( type
) && !bUnicode
)
220 HeapFree( GetProcessHeap(), 0, typeStr
);
221 if ( HIWORD( name
) && !bUnicode
)
222 HeapFree( GetProcessHeap(), 0, nameStr
);
227 /**********************************************************************
231 static HRSRC
RES_FindResource( HMODULE hModule
, LPCSTR type
,
232 LPCSTR name
, WORD lang
,
233 BOOL bUnicode
, BOOL bRet16
)
238 hRsrc
= RES_FindResource2(hModule
, type
, name
, lang
, bUnicode
, bRet16
);
242 WARN("page fault\n");
243 SetLastError(ERROR_INVALID_PARAMETER
);
250 /**********************************************************************
253 static DWORD
RES_SizeofResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
255 if (!hRsrc
) return 0;
257 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
259 if (!HIWORD(hModule
))
261 HMODULE16 hMod16
= MapHModuleLS( hModule
);
262 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
263 if (!pModule
) return 0;
265 if (!pModule
->module32
) /* 16-bit NE module */
267 /* If we got a 32-bit hRsrc, we don't need to convert it */
268 return NE_SizeofResource( pModule
, hRsrc
);
271 /* If we got a 16-bit hRsrc, convert it */
272 if (!HIWORD(hRsrc
)) hRsrc
= MapHRsrc16To32( pModule
, hRsrc
);
275 /* 32-bit PE module */
276 return PE_SizeofResource( hRsrc
);
279 /**********************************************************************
282 static HGLOBAL
RES_LoadResource( HMODULE hModule
, HRSRC hRsrc
, BOOL bRet16
)
286 TRACE("(%08x, %08x, %s)\n", hModule
, hRsrc
, bRet16
? "NE" : "PE" );
288 if (!hRsrc
) return 0;
290 if (!HIWORD(hModule
))
292 HMODULE16 hMod16
= MapHModuleLS( hModule
);
293 NE_MODULE
*pModule
= NE_GetPtr( hMod16
);
294 if (!pModule
) return 0;
295 if (!pModule
->module32
)
297 /* 16-bit NE module */
299 /* If we got a 32-bit hRsrc, we don't need to convert it */
300 hMem
= NE_LoadResource( pModule
, LOWORD(hRsrc
) );
302 /* If we are to return a 32-bit resource, we should probably
303 convert it but we don't for now. FIXME !!! */
308 /* If we got a 16-bit hRsrc, convert it */
309 HRSRC hRsrc32
= HIWORD(hRsrc
)? hRsrc
: MapHRsrc16To32( pModule
, hRsrc
);
311 hMem
= PE_LoadResource( pModule
->module32
, hRsrc32
);
313 /* If we need to return a 16-bit resource, convert it */
316 WORD type
= MapHRsrc16ToType( pModule
, hRsrc
);
317 DWORD size
= SizeofResource( hModule
, hRsrc
);
318 LPVOID bits
= LockResource( hMem
);
320 hMem
= NE_LoadPEResource( pModule
, type
, bits
, size
);
326 /* 32-bit PE module */
327 hMem
= PE_LoadResource( hModule
, hRsrc
);
333 /**********************************************************************
334 * FindResource (KERNEL.60)
335 * FindResource16 (KERNEL32.@)
337 HRSRC16 WINAPI
FindResource16( HMODULE16 hModule
, LPCSTR name
, LPCSTR type
)
339 return LOWORD( RES_FindResource( hModule
, type
, name
,
340 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, TRUE
) );
343 /**********************************************************************
344 * FindResourceA (KERNEL32.@)
346 HRSRC WINAPI
FindResourceA( HMODULE hModule
, LPCSTR name
, LPCSTR type
)
348 return RES_FindResource( hModule
, type
, name
,
349 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), FALSE
, FALSE
);
352 /**********************************************************************
353 * FindResourceExA (KERNEL32.@)
355 HRSRC WINAPI
FindResourceExA( HMODULE hModule
,
356 LPCSTR type
, LPCSTR name
, WORD lang
)
358 return RES_FindResource( hModule
, type
, name
,
359 lang
, FALSE
, FALSE
);
362 /**********************************************************************
363 * FindResourceExW (KERNEL32.@)
365 HRSRC WINAPI
FindResourceExW( HMODULE hModule
,
366 LPCWSTR type
, LPCWSTR name
, WORD lang
)
368 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
372 /**********************************************************************
373 * FindResourceW (KERNEL32.@)
375 HRSRC WINAPI
FindResourceW(HINSTANCE hModule
, LPCWSTR name
, LPCWSTR type
)
377 return RES_FindResource( hModule
, (LPCSTR
)type
, (LPCSTR
)name
,
378 MAKELANGID(LANG_NEUTRAL
, SUBLANG_NEUTRAL
), TRUE
, FALSE
);
381 /**********************************************************************
382 * LoadResource (KERNEL.61)
383 * LoadResource16 (KERNEL32.@)
385 HGLOBAL16 WINAPI
LoadResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
387 return RES_LoadResource( hModule
, hRsrc
, TRUE
);
390 /**********************************************************************
391 * LoadResource (KERNEL32.@)
393 HGLOBAL WINAPI
LoadResource( HINSTANCE hModule
, HRSRC hRsrc
)
395 return RES_LoadResource( hModule
, hRsrc
, FALSE
);
398 /**********************************************************************
399 * LockResource (KERNEL.62)
401 SEGPTR WINAPI
WIN16_LockResource16( HGLOBAL16 handle
)
403 TRACE("(%04x)\n", handle
);
404 /* May need to reload the resource if discarded */
405 return K32WOWGlobalLock16( handle
);
408 /**********************************************************************
409 * LockResource16 (KERNEL32.@)
411 LPVOID WINAPI
LockResource16( HGLOBAL16 handle
)
413 return MapSL( WIN16_LockResource16(handle
) );
416 /**********************************************************************
417 * LockResource (KERNEL32.@)
419 LPVOID WINAPI
LockResource( HGLOBAL handle
)
421 TRACE("(%08x)\n", handle
);
423 if (HIWORD( handle
)) /* 32-bit memory handle */
424 return (LPVOID
)handle
;
426 /* 16-bit memory handle */
427 return LockResource16( LOWORD(handle
) );
430 typedef WORD (WINAPI
*pDestroyIcon32Proc
)( HGLOBAL16 handle
, UINT16 flags
);
433 /**********************************************************************
434 * FreeResource (KERNEL.63)
435 * FreeResource16 (KERNEL32.@)
437 BOOL16 WINAPI
FreeResource16( HGLOBAL16 handle
)
439 HGLOBAL16 retv
= handle
;
440 NE_MODULE
*pModule
= NE_GetPtr( FarGetOwner16( handle
) );
442 TRACE("(%04x)\n", handle
);
444 /* Try NE resource first */
445 retv
= NE_FreeResource( pModule
, handle
);
447 /* If this failed, call USER.DestroyIcon32; this will check
448 whether it is a shared cursor/icon; if not it will call
452 pDestroyIcon32Proc proc
;
453 HMODULE user
= GetModuleHandleA( "user32.dll" );
455 if (user
&& (proc
= (pDestroyIcon32Proc
)GetProcAddress( user
, "DestroyIcon32" )))
456 retv
= proc( handle
, CID_RESOURCE
);
458 retv
= GlobalFree16( handle
);
463 /**********************************************************************
464 * FreeResource (KERNEL32.@)
466 BOOL WINAPI
FreeResource( HGLOBAL handle
)
468 if (HIWORD(handle
)) return 0; /* 32-bit memory handle: nothing to do */
470 return FreeResource16( LOWORD(handle
) );
473 /**********************************************************************
474 * SizeofResource (KERNEL.65)
475 * SizeofResource16 (KERNEL32.@)
477 DWORD WINAPI
SizeofResource16( HMODULE16 hModule
, HRSRC16 hRsrc
)
479 return RES_SizeofResource( hModule
, hRsrc
, TRUE
);
482 /**********************************************************************
483 * SizeofResource (KERNEL32.@)
485 DWORD WINAPI
SizeofResource( HINSTANCE hModule
, HRSRC hRsrc
)
487 return RES_SizeofResource( hModule
, hRsrc
, FALSE
);