Changed the DLGTEMPLATE and DLGITEMTEMPLATE types to adhere to the AW
[wine/multimedia.git] / loader / resource.c
blob09a5d198d73e5b749147b3858bf970647c0ed675
1 /*
2 * Resources
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "winbase.h"
16 #include "windef.h"
17 #include "winuser.h"
18 #include "wine/winbase16.h"
19 #include "wine/winuser16.h"
20 #include "gdi.h"
21 #include "global.h"
22 #include "heap.h"
23 #include "callback.h"
24 #include "cursoricon.h"
25 #include "neexe.h"
26 #include "task.h"
27 #include "process.h"
28 #include "module.h"
29 #include "file.h"
30 #include "debug.h"
31 #include "libres.h"
32 #include "winerror.h"
33 #include "debugstr.h"
35 extern WORD WINE_LanguageId;
37 #define HRSRC_MAP_BLOCKSIZE 16
39 typedef struct _HRSRC_ELEM
41 HANDLE hRsrc;
42 WORD type;
43 } HRSRC_ELEM;
45 typedef struct _HRSRC_MAP
47 int nAlloc;
48 int nUsed;
49 HRSRC_ELEM *elem;
50 } HRSRC_MAP;
52 /**********************************************************************
53 * MapHRsrc32To16
55 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
57 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
58 HRSRC_ELEM *newElem;
59 int i;
61 /* On first call, initialize HRSRC map */
62 if ( !map )
64 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
65 sizeof(HRSRC_MAP) ) ) )
67 ERR( resource, "Cannot allocate HRSRC map\n" );
68 return 0;
70 pModule->hRsrcMap = (LPVOID)map;
73 /* Check whether HRSRC32 already in map */
74 for ( i = 0; i < map->nUsed; i++ )
75 if ( map->elem[i].hRsrc == hRsrc32 )
76 return (HRSRC16)(i + 1);
78 /* If no space left, grow table */
79 if ( map->nUsed == map->nAlloc )
81 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
82 map->elem,
83 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
84 * sizeof(HRSRC_ELEM) ) ))
86 ERR( resource, "Cannot grow HRSRC map\n" );
87 return 0;
89 map->elem = newElem;
90 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
93 /* Add HRSRC32 to table */
94 map->elem[map->nUsed].hRsrc = hRsrc32;
95 map->elem[map->nUsed].type = type;
96 map->nUsed++;
98 return (HRSRC16)map->nUsed;
101 /**********************************************************************
102 * MapHRsrc16To32
104 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
106 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
107 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
109 return map->elem[(int)hRsrc16-1].hRsrc;
112 /**********************************************************************
113 * MapHRsrc16ToType
115 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 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].type;
124 /**********************************************************************
125 * RES_FindResource
127 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
128 LPCSTR name, WORD lang,
129 BOOL bUnicode, BOOL bRet16 )
131 HRSRC hRsrc = 0;
133 HMODULE16 hMod16 = MapHModuleLS( hModule );
134 NE_MODULE *pModule = NE_GetPtr( hMod16 );
135 WINE_MODREF *wm = pModule && pModule->module32?
136 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
138 TRACE( resource, "(%08x %s, %08x%s, %08x%s, %04x, %s, %s)\n",
139 hModule, NE_MODULE_NAME(pModule),
140 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
141 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
142 lang,
143 bUnicode? "W" : "A",
144 bRet16? "NE" : "PE" );
146 if ( !pModule ) return 0;
148 if ( wm )
150 /* 32-bit PE/ELF module */
151 LPWSTR typeStr, nameStr;
153 if ( HIWORD( type ) && !bUnicode )
154 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
155 else
156 typeStr = (LPWSTR)type;
157 if ( HIWORD( name ) && !bUnicode )
158 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
159 else
160 nameStr = (LPWSTR)name;
162 switch ( wm->type )
164 case MODULE32_PE:
165 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
166 break;
168 case MODULE32_ELF:
169 hRsrc = LIBRES_FindResource( hModule, nameStr, typeStr );
170 break;
172 default:
173 ERR( resource, "unknown module type %d\n", wm->type );
174 break;
177 if ( HIWORD( type ) && !bUnicode )
178 HeapFree( GetProcessHeap(), 0, typeStr );
179 if ( HIWORD( name ) && !bUnicode )
180 HeapFree( GetProcessHeap(), 0, nameStr );
183 /* If we need to return 16-bit HRSRC, perform conversion */
184 if ( bRet16 )
185 hRsrc = MapHRsrc32To16( pModule, hRsrc,
186 HIWORD( type )? 0 : LOWORD( type ) );
188 else
190 /* 16-bit NE module */
191 LPSTR typeStr, nameStr;
193 if ( HIWORD( type ) && bUnicode )
194 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
195 else
196 typeStr = (LPSTR)type;
197 if ( HIWORD( name ) && bUnicode )
198 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
199 else
200 nameStr = (LPSTR)name;
202 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
204 if ( HIWORD( type ) && bUnicode )
205 HeapFree( GetProcessHeap(), 0, typeStr );
206 if ( HIWORD( name ) && bUnicode )
207 HeapFree( GetProcessHeap(), 0, nameStr );
210 /* If we need to return 32-bit HRSRC, no conversion is necessary,
211 we simply use the 16-bit HRSRC as 32-bit HRSRC */
214 return hRsrc;
217 /**********************************************************************
218 * RES_SizeofResource
220 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
222 DWORD size = 0;
224 HMODULE16 hMod16 = MapHModuleLS( hModule );
225 NE_MODULE *pModule = NE_GetPtr( hMod16 );
226 WINE_MODREF *wm = pModule && pModule->module32?
227 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
229 TRACE( resource, "(%08x %s, %08x, %s)\n",
230 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
232 if ( !pModule || !hRsrc ) return 0;
234 if ( wm )
236 /* 32-bit PE/ELF module */
238 /* If we got a 16-bit hRsrc, convert it */
239 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
241 switch ( wm->type )
243 case MODULE32_PE:
244 size = PE_SizeofResource( hModule, hRsrc32 );
245 break;
247 case MODULE32_ELF:
248 size = LIBRES_SizeofResource( hModule, hRsrc32 );
249 break;
251 default:
252 ERR( resource, "unknown module type %d\n", wm->type );
253 break;
256 else
258 /* 16-bit NE module */
260 /* If we got a 32-bit hRsrc, we don't need to convert it */
262 size = NE_AccessResource( pModule, hRsrc );
265 return size;
268 /**********************************************************************
269 * RES_AccessResource
271 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
273 HFILE hFile = HFILE_ERROR;
275 HMODULE16 hMod16 = MapHModuleLS( hModule );
276 NE_MODULE *pModule = NE_GetPtr( hMod16 );
277 WINE_MODREF *wm = pModule && pModule->module32?
278 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
280 TRACE( resource, "(%08x %s, %08x, %s)\n",
281 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
283 if ( !pModule || !hRsrc ) return HFILE_ERROR;
285 if ( wm )
287 /* 32-bit PE/ELF module */
288 #if 0
289 /* If we got a 16-bit hRsrc, convert it */
290 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
291 #endif
293 FIXME( resource, "32-bit modules not yet supported.\n" );
294 hFile = HFILE_ERROR;
296 /* If we need to return a 16-bit file handle, convert it */
297 if ( bRet16 )
298 hFile = FILE_AllocDosHandle( hFile );
300 else
302 /* 16-bit NE module */
304 /* If we got a 32-bit hRsrc, we don't need to convert it */
306 hFile = NE_AccessResource( pModule, hRsrc );
308 /* If we are to return a 32-bit file handle, convert it */
309 if ( !bRet16 )
310 hFile = FILE_GetHandle( hFile );
313 return hFile;
316 /**********************************************************************
317 * RES_LoadResource
319 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
321 HGLOBAL hMem = 0;
323 HMODULE16 hMod16 = MapHModuleLS( hModule );
324 NE_MODULE *pModule = NE_GetPtr( hMod16 );
325 WINE_MODREF *wm = pModule && pModule->module32?
326 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
328 TRACE( resource, "(%08x %s, %08x, %s)\n",
329 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
331 if ( !pModule || !hRsrc ) return 0;
333 if ( wm )
335 /* 32-bit PE/ELF module */
337 /* If we got a 16-bit hRsrc, convert it */
338 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
340 switch ( wm->type )
342 case MODULE32_PE:
343 hMem = PE_LoadResource( wm, hRsrc32 );
344 break;
346 case MODULE32_ELF:
347 hMem = LIBRES_LoadResource( hModule, hRsrc32 );
348 break;
350 default:
351 ERR( resource, "unknown module type %d\n", wm->type );
352 break;
355 /* If we need to return a 16-bit resource, convert it */
356 if ( bRet16 )
358 WORD type = MapHRsrc16ToType( pModule, hRsrc );
359 DWORD size = SizeofResource( hModule, hRsrc );
360 LPVOID bits = LockResource( hMem );
362 hMem = NE_LoadPEResource( pModule, type, bits, size );
365 else
367 /* 16-bit NE module */
369 /* If we got a 32-bit hRsrc, we don't need to convert it */
371 hMem = NE_LoadResource( pModule, hRsrc );
373 /* If we are to return a 32-bit resource, we should probably
374 convert it but we don't for now. FIXME !!! */
377 return hMem;
380 /**********************************************************************
381 * RES_LockResource
383 static LPVOID RES_LockResource( HGLOBAL handle, BOOL bRet16 )
385 LPVOID bits = NULL;
387 TRACE( resource, "(%08x, %s)\n", handle, bRet16? "NE" : "PE" );
389 if ( HIWORD( handle ) )
391 /* 32-bit memory handle */
393 if ( bRet16 )
394 FIXME( resource, "can't return SEGPTR to 32-bit resource %08x.\n", handle );
395 else
396 bits = (LPVOID)handle;
398 else
400 /* 16-bit memory handle */
402 /* May need to reload the resource if discarded */
403 SEGPTR segPtr = WIN16_GlobalLock16( handle );
405 if ( bRet16 )
406 bits = (LPVOID)segPtr;
407 else
408 bits = PTR_SEG_TO_LIN( segPtr );
411 return bits;
414 /**********************************************************************
415 * RES_FreeResource
417 static BOOL RES_FreeResource( HGLOBAL handle )
419 HGLOBAL retv = handle;
421 TRACE( resource, "(%08x)\n", handle );
423 if ( HIWORD( handle ) )
425 /* 32-bit memory handle: nothing to do */
427 else
429 /* 16-bit memory handle */
430 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
432 /* Try NE resource first */
433 retv = NE_FreeResource( pModule, handle );
435 /* If this failed, call USER.DestroyIcon32; this will check
436 whether it is a shared cursor/icon; if not it will call
437 GlobalFree16() */
438 if ( retv )
439 if ( Callout.DestroyIcon32 )
440 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
441 else
442 retv = GlobalFree16( handle );
445 return (BOOL)retv;
449 /**********************************************************************
450 * FindResource16 (KERNEL.60)
452 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
454 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
455 LPCSTR typeStr = HIWORD(type)? PTR_SEG_TO_LIN(type) : (LPCSTR)type;
457 return RES_FindResource( hModule, typeStr, nameStr,
458 WINE_LanguageId, FALSE, TRUE );
461 /**********************************************************************
462 * FindResourceA (KERNEL32.128)
464 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
466 return RES_FindResource( hModule, type, name,
467 WINE_LanguageId, FALSE, FALSE );
470 /**********************************************************************
471 * FindResourceExA (KERNEL32.129)
473 HANDLE WINAPI FindResourceExA( HMODULE hModule,
474 LPCSTR type, LPCSTR name, WORD lang )
476 return RES_FindResource( hModule, type, name,
477 lang, FALSE, FALSE );
480 /**********************************************************************
481 * FindResourceExW (KERNEL32.130)
483 HRSRC WINAPI FindResourceExW( HMODULE hModule,
484 LPCWSTR type, LPCWSTR name, WORD lang )
486 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
487 lang, TRUE, FALSE );
490 /**********************************************************************
491 * FindResourceW (KERNEL32.131)
493 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
495 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
496 WINE_LanguageId, TRUE, FALSE );
499 /**********************************************************************
500 * LoadResource16 (KERNEL.61)
502 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
504 return RES_LoadResource( hModule, hRsrc, TRUE );
507 /**********************************************************************
508 * LoadResource (KERNEL32.370)
510 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
512 return RES_LoadResource( hModule, hRsrc, FALSE );
515 /**********************************************************************
516 * LockResource16 (KERNEL.62)
518 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
520 return (SEGPTR)RES_LockResource( handle, TRUE );
522 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
524 return RES_LockResource( handle, FALSE );
527 /**********************************************************************
528 * LockResource (KERNEL32.384)
530 LPVOID WINAPI LockResource( HGLOBAL handle )
532 return RES_LockResource( handle, FALSE );
535 /**********************************************************************
536 * FreeResource16 (KERNEL.63)
538 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
540 return RES_FreeResource( handle );
543 /**********************************************************************
544 * FreeResource (KERNEL32.145)
546 BOOL WINAPI FreeResource( HGLOBAL handle )
548 return RES_FreeResource( handle );
551 /**********************************************************************
552 * AccessResource16 (KERNEL.64)
554 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
556 return RES_AccessResource( hModule, hRsrc, TRUE );
559 /**********************************************************************
560 * AccessResource (KERNEL32.64)
562 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
564 return RES_AccessResource( hModule, hRsrc, FALSE );
567 /**********************************************************************
568 * SizeofResource16 (KERNEL.65)
570 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
572 return RES_SizeofResource( hModule, hRsrc, TRUE );
575 /**********************************************************************
576 * SizeofResource (KERNEL32.522)
578 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
580 return RES_SizeofResource( hModule, hRsrc, FALSE );
585 /**********************************************************************
586 * LoadAccelerators16 [USER.177]
588 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
590 HRSRC16 hRsrc;
592 if (HIWORD(lpTableName))
593 TRACE(accel, "%04x '%s'\n",
594 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
595 else
596 TRACE(accel, "%04x %04x\n",
597 instance, LOWORD(lpTableName) );
599 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
600 WARN(accel, "couldn't find accelerator table resource\n");
601 return 0;
604 TRACE(accel, "returning HACCEL 0x%x\n", hRsrc);
605 return LoadResource16(instance,hRsrc);
608 /**********************************************************************
609 * LoadAccelerators32W [USER.177]
610 * The image layout seems to look like this (not 100% sure):
611 * 00: BYTE type type of accelerator
612 * 01: BYTE pad (to WORD boundary)
613 * 02: WORD event
614 * 04: WORD IDval
615 * 06: WORD pad (to DWORD boundary)
617 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
619 HRSRC hRsrc;
620 HACCEL hMem,hRetval=0;
621 DWORD size;
623 if (HIWORD(lpTableName))
624 TRACE(accel, "%p '%s'\n",
625 (LPVOID)instance, (char *)( lpTableName ) );
626 else
627 TRACE(accel, "%p 0x%04x\n",
628 (LPVOID)instance, LOWORD(lpTableName) );
630 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
632 WARN(accel, "couldn't find accelerator table resource\n");
633 } else {
634 hMem = LoadResource( instance, hRsrc );
635 size = SizeofResource( instance, hRsrc );
636 if(size>=sizeof(PE_ACCEL))
638 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
639 LPACCEL16 accel16;
640 int i,nrofaccells = size/sizeof(PE_ACCEL);
642 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
643 accel16 = (LPACCEL16)GlobalLock16(hRetval);
644 for (i=0;i<nrofaccells;i++) {
645 accel16[i].fVirt = accel_table[i].fVirt;
646 accel16[i].key = accel_table[i].key;
647 accel16[i].cmd = accel_table[i].cmd;
649 accel16[i-1].fVirt |= 0x80;
652 TRACE(accel, "returning HACCEL 0x%x\n", hRsrc);
653 return hRetval;
656 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
658 LPWSTR uni;
659 HACCEL result;
660 if (HIWORD(lpTableName))
661 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
662 else
663 uni = (LPWSTR)lpTableName;
664 result = LoadAcceleratorsW(instance,uni);
665 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
666 return result;
669 /**********************************************************************
670 * CopyAcceleratorTable32A (USER32.58)
672 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
674 return CopyAcceleratorTableW(src, dst, entries);
677 /**********************************************************************
678 * CopyAcceleratorTable32W (USER32.59)
680 * By mortene@pvv.org 980321
682 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
683 INT entries)
685 int i,xsize;
686 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
687 BOOL done = FALSE;
689 /* Do parameter checking to avoid the explosions and the screaming
690 as far as possible. */
691 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
692 WARN(accel, "Application sent invalid parameters (%p %p %d).\n",
693 (LPVOID)src, (LPVOID)dst, entries);
694 return 0;
696 xsize = GlobalSize16(src)/sizeof(ACCEL16);
697 if (xsize>entries) entries=xsize;
699 i=0;
700 while(!done) {
701 /* Spit out some debugging information. */
702 TRACE(accel, "accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
703 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
705 /* Copy data to the destination structure array (if dst == NULL,
706 we're just supposed to count the number of entries). */
707 if(dst) {
708 dst[i].fVirt = accel[i].fVirt;
709 dst[i].key = accel[i].key;
710 dst[i].cmd = accel[i].cmd;
712 /* Check if we've reached the end of the application supplied
713 accelerator table. */
714 if(i+1 == entries) {
715 /* Turn off the high order bit, just in case. */
716 dst[i].fVirt &= 0x7f;
717 done = TRUE;
721 /* The highest order bit seems to mark the end of the accelerator
722 resource table, but not always. Use GlobalSize() check too. */
723 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
725 i++;
728 return i;
731 /*********************************************************************
732 * CreateAcceleratorTable (USER32.64)
734 * By mortene@pvv.org 980321
736 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
738 HACCEL hAccel;
739 LPACCEL16 accel;
740 int i;
742 /* Do parameter checking just in case someone's trying to be
743 funny. */
744 if(cEntries < 1) {
745 WARN(accel, "Application sent invalid parameters (%p %d).\n",
746 lpaccel, cEntries);
747 SetLastError(ERROR_INVALID_PARAMETER);
748 return (HACCEL)NULL;
750 FIXME(accel, "should check that the accelerator descriptions are valid,"
751 " return NULL and SetLastError() if not.\n");
754 /* Allocate memory and copy the table. */
755 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
757 TRACE(accel, "handle %x\n", hAccel);
758 if(!hAccel) {
759 ERR(accel, "Out of memory.\n");
760 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
761 return (HACCEL)NULL;
763 accel = GlobalLock16(hAccel);
764 for (i=0;i<cEntries;i++) {
765 accel[i].fVirt = lpaccel[i].fVirt;
766 accel[i].key = lpaccel[i].key;
767 accel[i].cmd = lpaccel[i].cmd;
769 /* Set the end-of-table terminator. */
770 accel[cEntries-1].fVirt |= 0x80;
772 TRACE(accel, "Allocated accelerator handle %x\n", hAccel);
773 return hAccel;
777 /******************************************************************************
778 * DestroyAcceleratorTable [USER32.130]
779 * Destroys an accelerator table
781 * NOTES
782 * By mortene@pvv.org 980321
784 * PARAMS
785 * handle [I] Handle to accelerator table
787 * RETURNS STD
789 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
791 FIXME(accel, "(0x%x): stub\n", handle);
792 /* FIXME: GlobalFree16(handle); */
793 return TRUE;
796 /**********************************************************************
797 * LoadString16
799 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
800 LPSTR buffer, INT16 buflen )
802 HGLOBAL16 hmem;
803 HRSRC16 hrsrc;
804 unsigned char *p;
805 int string_num;
806 int i;
808 TRACE(resource,"inst=%04x id=%04x buff=%08x len=%d\n",
809 instance, resource_id, (int) buffer, buflen);
811 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
812 if (!hrsrc) return 0;
813 hmem = LoadResource16( instance, hrsrc );
814 if (!hmem) return 0;
816 p = LockResource16(hmem);
817 string_num = resource_id & 0x000f;
818 for (i = 0; i < string_num; i++)
819 p += *p + 1;
821 TRACE(resource, "strlen = %d\n", (int)*p );
823 i = MIN(buflen - 1, *p);
824 if (buffer == NULL)
825 return i;
826 if (i > 0) {
827 memcpy(buffer, p + 1, i);
828 buffer[i] = '\0';
829 } else {
830 if (buflen > 1) {
831 buffer[0] = '\0';
832 return 0;
834 WARN(resource,"Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
836 FreeResource16( hmem );
838 TRACE(resource,"'%s' loaded !\n", buffer);
839 return i;
842 /**********************************************************************
843 * LoadString32W (USER32.376)
845 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
846 LPWSTR buffer, INT buflen )
848 HGLOBAL hmem;
849 HRSRC hrsrc;
850 WCHAR *p;
851 int string_num;
852 int i;
854 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
855 resource_id = (UINT)(-((INT)resource_id));
856 TRACE(resource, "instance = %04x, id = %04x, buffer = %08x, "
857 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
859 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
860 * 20 - 31. */
861 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
862 RT_STRINGW );
863 if (!hrsrc) return 0;
864 hmem = LoadResource( instance, hrsrc );
865 if (!hmem) return 0;
867 p = LockResource(hmem);
868 string_num = resource_id & 0x000f;
869 for (i = 0; i < string_num; i++)
870 p += *p + 1;
872 TRACE(resource, "strlen = %d\n", (int)*p );
874 i = MIN(buflen - 1, *p);
875 if (buffer == NULL)
876 return i;
877 if (i > 0) {
878 memcpy(buffer, p + 1, i * sizeof (WCHAR));
879 buffer[i] = (WCHAR) 0;
880 } else {
881 if (buflen > 1) {
882 buffer[0] = (WCHAR) 0;
883 return 0;
885 #if 0
886 WARN(resource,"Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
887 #endif
890 TRACE(resource,"%s loaded !\n", debugstr_w(buffer));
891 return i;
894 /**********************************************************************
895 * LoadString32A (USER32.375)
897 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
898 LPSTR buffer, INT buflen )
900 INT retval;
901 LPWSTR buffer2 = NULL;
902 if (buffer && buflen)
903 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
904 retval = LoadStringW(instance,resource_id,buffer2,buflen);
906 if (buffer2)
908 if (retval) {
909 lstrcpynWtoA( buffer, buffer2, buflen );
910 retval = lstrlenA( buffer );
912 else
913 *buffer = 0;
914 HeapFree( GetProcessHeap(), 0, buffer2 );
916 return retval;
919 /* Messages...used by FormatMessage32* (KERNEL32.something)
921 * They can be specified either directly or using a message ID and
922 * loading them from the resource.
924 * The resourcedata has following format:
925 * start:
926 * 0: DWORD nrofentries
927 * nrofentries * subentry:
928 * 0: DWORD firstentry
929 * 4: DWORD lastentry
930 * 8: DWORD offset from start to the stringentries
932 * (lastentry-firstentry) * stringentry:
933 * 0: WORD len (0 marks end)
934 * 2: WORD unknown (flags?)
935 * 4: CHAR[len-4]
936 * (stringentry i of a subentry refers to the ID 'firstentry+i')
938 * Yes, ANSI strings in win32 resources. Go figure.
941 /**********************************************************************
942 * LoadMessage32A (internal)
944 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
945 LPSTR buffer, INT buflen )
947 HGLOBAL hmem;
948 HRSRC hrsrc;
949 BYTE *p;
950 int nrofentries,i,slen;
951 struct _subentry {
952 DWORD firstentry;
953 DWORD lastentry;
954 DWORD offset;
955 } *se;
956 struct _stringentry {
957 WORD len;
958 WORD unknown;
959 CHAR str[1];
960 } *stre;
962 TRACE(resource, "instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
964 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
965 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
966 if (!hrsrc) return 0;
967 hmem = LoadResource( instance, hrsrc );
968 if (!hmem) return 0;
970 p = LockResource(hmem);
971 nrofentries = *(DWORD*)p;
972 stre = NULL;
973 se = (struct _subentry*)(p+4);
974 for (i=nrofentries;i--;) {
975 if ((id>=se->firstentry) && (id<=se->lastentry)) {
976 stre = (struct _stringentry*)(p+se->offset);
977 id -= se->firstentry;
978 break;
980 se++;
982 if (!stre)
983 return 0;
984 for (i=id;i--;) {
985 if (!(slen=stre->len))
986 return 0;
987 stre = (struct _stringentry*)(((char*)stre)+slen);
989 slen=stre->len;
990 TRACE(resource," - strlen=%d\n",slen);
991 i = MIN(buflen - 1, slen);
992 if (buffer == NULL)
993 return slen; /* different to LoadString */
994 if (i>0) {
995 lstrcpynA(buffer,stre->str,i);
996 buffer[i]=0;
997 } else {
998 if (buflen>1) {
999 buffer[0]=0;
1000 return 0;
1003 if (buffer)
1004 TRACE(resource,"'%s' copied !\n", buffer);
1005 return i;
1008 /**********************************************************************
1009 * LoadMessage32W (internal)
1011 INT WINAPI LoadMessageW( HMODULE instance, UINT id, WORD lang,
1012 LPWSTR buffer, INT buflen )
1014 INT retval;
1015 LPSTR buffer2 = NULL;
1016 if (buffer && buflen)
1017 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
1018 retval = LoadMessageA(instance,id,lang,buffer2,buflen);
1019 if (buffer)
1021 if (retval) {
1022 lstrcpynAtoW( buffer, buffer2, buflen );
1023 retval = lstrlenW( buffer );
1025 HeapFree( GetProcessHeap(), 0, buffer2 );
1027 return retval;
1031 /**********************************************************************
1032 * EnumResourceTypesA (KERNEL32.90)
1034 BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
1035 LONG lParam)
1037 /* FIXME: move WINE_MODREF stuff here */
1038 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
1041 /**********************************************************************
1042 * EnumResourceTypesW (KERNEL32.91)
1044 BOOL WINAPI EnumResourceTypesW( HMODULE hmodule,ENUMRESTYPEPROCW lpfun,
1045 LONG lParam)
1047 /* FIXME: move WINE_MODREF stuff here */
1048 return PE_EnumResourceTypesW(hmodule,lpfun,lParam);
1051 /**********************************************************************
1052 * EnumResourceNamesA (KERNEL32.88)
1054 BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
1055 ENUMRESNAMEPROCA lpfun, LONG lParam )
1057 /* FIXME: move WINE_MODREF stuff here */
1058 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
1060 /**********************************************************************
1061 * EnumResourceNamesW (KERNEL32.89)
1063 BOOL WINAPI EnumResourceNamesW( HMODULE hmodule, LPCWSTR type,
1064 ENUMRESNAMEPROCW lpfun, LONG lParam )
1066 /* FIXME: move WINE_MODREF stuff here */
1067 return PE_EnumResourceNamesW(hmodule,type,lpfun,lParam);
1070 /**********************************************************************
1071 * EnumResourceLanguagesA (KERNEL32.86)
1073 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
1074 LPCSTR name, ENUMRESLANGPROCA lpfun,
1075 LONG lParam)
1077 /* FIXME: move WINE_MODREF stuff here */
1078 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
1080 /**********************************************************************
1081 * EnumResourceLanguagesW (KERNEL32.87)
1083 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmodule, LPCWSTR type,
1084 LPCWSTR name, ENUMRESLANGPROCW lpfun,
1085 LONG lParam)
1087 /* FIXME: move WINE_MODREF stuff here */
1088 return PE_EnumResourceLanguagesW(hmodule,type,name,lpfun,lParam);