Fix spec file entries for EnumFontFamiliesEx*.
[wine/multimedia.git] / loader / resource.c
blobbfe3ca1f4de34eeb2dbc698c4050c6894389afb6
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 "wingdi.h"
18 #include "winuser.h"
19 #include "wine/winbase16.h"
20 #include "wine/winuser16.h"
21 #include "ldt.h"
22 #include "global.h"
23 #include "heap.h"
24 #include "callback.h"
25 #include "cursoricon.h"
26 #include "neexe.h"
27 #include "task.h"
28 #include "process.h"
29 #include "module.h"
30 #include "file.h"
31 #include "debugtools.h"
32 #include "winerror.h"
33 #include "winnls.h"
35 DEFAULT_DEBUG_CHANNEL(resource);
36 DECLARE_DEBUG_CHANNEL(accel);
38 extern WORD WINE_LanguageId;
40 #define HRSRC_MAP_BLOCKSIZE 16
42 typedef struct _HRSRC_ELEM
44 HANDLE hRsrc;
45 WORD type;
46 } HRSRC_ELEM;
48 typedef struct _HRSRC_MAP
50 int nAlloc;
51 int nUsed;
52 HRSRC_ELEM *elem;
53 } HRSRC_MAP;
55 /**********************************************************************
56 * MapHRsrc32To16
58 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
60 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
61 HRSRC_ELEM *newElem;
62 int i;
64 /* On first call, initialize HRSRC map */
65 if ( !map )
67 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
68 sizeof(HRSRC_MAP) ) ) )
70 ERR("Cannot allocate HRSRC map\n" );
71 return 0;
73 pModule->hRsrcMap = (LPVOID)map;
76 /* Check whether HRSRC32 already in map */
77 for ( i = 0; i < map->nUsed; i++ )
78 if ( map->elem[i].hRsrc == hRsrc32 )
79 return (HRSRC16)(i + 1);
81 /* If no space left, grow table */
82 if ( map->nUsed == map->nAlloc )
84 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
85 map->elem,
86 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
87 * sizeof(HRSRC_ELEM) ) ))
89 ERR("Cannot grow HRSRC map\n" );
90 return 0;
92 map->elem = newElem;
93 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
96 /* Add HRSRC32 to table */
97 map->elem[map->nUsed].hRsrc = hRsrc32;
98 map->elem[map->nUsed].type = type;
99 map->nUsed++;
101 return (HRSRC16)map->nUsed;
104 /**********************************************************************
105 * MapHRsrc16To32
107 static HANDLE MapHRsrc16To32( 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].hRsrc;
115 /**********************************************************************
116 * MapHRsrc16ToType
118 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
120 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
121 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
123 return map->elem[(int)hRsrc16-1].type;
127 /**********************************************************************
128 * RES_FindResource
130 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
131 LPCSTR name, WORD lang,
132 BOOL bUnicode, BOOL bRet16 )
134 HRSRC hRsrc = 0;
136 HMODULE16 hMod16 = MapHModuleLS( hModule );
137 NE_MODULE *pModule = NE_GetPtr( hMod16 );
138 WINE_MODREF *wm = pModule && pModule->module32?
139 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
141 TRACE("(%08x %s, %08x%s, %08x%s, %04x, %s, %s)\n",
142 hModule,
143 pModule ? (char *)NE_MODULE_NAME(pModule) : "NULL dereference",
144 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
145 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
146 lang,
147 bUnicode? "W" : "A",
148 bRet16? "NE" : "PE" );
150 if ( !pModule ) return 0;
152 if ( wm )
154 /* 32-bit PE module */
155 LPWSTR typeStr, nameStr;
157 if ( HIWORD( type ) && !bUnicode )
158 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
159 else
160 typeStr = (LPWSTR)type;
161 if ( HIWORD( name ) && !bUnicode )
162 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
163 else
164 nameStr = (LPWSTR)name;
166 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
168 if ( HIWORD( type ) && !bUnicode )
169 HeapFree( GetProcessHeap(), 0, typeStr );
170 if ( HIWORD( name ) && !bUnicode )
171 HeapFree( GetProcessHeap(), 0, nameStr );
174 /* If we need to return 16-bit HRSRC, perform conversion */
175 if ( bRet16 )
176 hRsrc = MapHRsrc32To16( pModule, hRsrc,
177 HIWORD( type )? 0 : LOWORD( type ) );
179 else
181 /* 16-bit NE module */
182 LPSTR typeStr, nameStr;
184 if ( HIWORD( type ) && bUnicode )
185 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
186 else
187 typeStr = (LPSTR)type;
188 if ( HIWORD( name ) && bUnicode )
189 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
190 else
191 nameStr = (LPSTR)name;
193 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
195 if ( HIWORD( type ) && bUnicode )
196 HeapFree( GetProcessHeap(), 0, typeStr );
197 if ( HIWORD( name ) && bUnicode )
198 HeapFree( GetProcessHeap(), 0, nameStr );
201 /* If we need to return 32-bit HRSRC, no conversion is necessary,
202 we simply use the 16-bit HRSRC as 32-bit HRSRC */
205 return hRsrc;
208 /**********************************************************************
209 * RES_SizeofResource
211 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
213 DWORD size = 0;
215 HMODULE16 hMod16 = MapHModuleLS( hModule );
216 NE_MODULE *pModule = NE_GetPtr( hMod16 );
217 WINE_MODREF *wm = pModule && pModule->module32?
218 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
220 TRACE("(%08x %s, %08x, %s)\n",
221 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
223 if ( !pModule || !hRsrc ) return 0;
225 if ( wm )
227 /* 32-bit PE module */
229 /* If we got a 16-bit hRsrc, convert it */
230 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
232 size = PE_SizeofResource( hModule, hRsrc32 );
234 else
236 /* 16-bit NE module */
238 /* If we got a 32-bit hRsrc, we don't need to convert it */
240 size = NE_SizeofResource( pModule, hRsrc );
243 return size;
246 /**********************************************************************
247 * RES_AccessResource
249 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
251 HFILE hFile = HFILE_ERROR;
253 HMODULE16 hMod16 = MapHModuleLS( hModule );
254 NE_MODULE *pModule = NE_GetPtr( hMod16 );
255 WINE_MODREF *wm = pModule && pModule->module32?
256 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
258 TRACE("(%08x %s, %08x, %s)\n",
259 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
261 if ( !pModule || !hRsrc ) return HFILE_ERROR;
263 if ( wm )
265 /* 32-bit PE module */
266 #if 0
267 /* If we got a 16-bit hRsrc, convert it */
268 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
269 #endif
271 FIXME("32-bit modules not yet supported.\n" );
272 hFile = HFILE_ERROR;
274 /* If we need to return a 16-bit file handle, convert it */
275 if ( bRet16 )
276 hFile = FILE_AllocDosHandle( hFile );
278 else
280 /* 16-bit NE module */
282 /* If we got a 32-bit hRsrc, we don't need to convert it */
284 hFile = NE_AccessResource( pModule, hRsrc );
286 /* If we are to return a 32-bit file handle, convert it */
287 if ( !bRet16 )
288 hFile = FILE_GetHandle( hFile );
291 return hFile;
294 /**********************************************************************
295 * RES_LoadResource
297 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
299 HGLOBAL hMem = 0;
301 HMODULE16 hMod16 = MapHModuleLS( hModule );
302 NE_MODULE *pModule = NE_GetPtr( hMod16 );
303 WINE_MODREF *wm = pModule && pModule->module32?
304 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
306 TRACE("(%08x %s, %08x, %s)\n",
307 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
309 if ( !pModule || !hRsrc ) return 0;
311 if ( wm )
313 /* 32-bit PE module */
315 /* If we got a 16-bit hRsrc, convert it */
316 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
318 hMem = PE_LoadResource( wm, hRsrc32 );
320 /* If we need to return a 16-bit resource, convert it */
321 if ( bRet16 )
323 WORD type = MapHRsrc16ToType( pModule, hRsrc );
324 DWORD size = SizeofResource( hModule, hRsrc );
325 LPVOID bits = LockResource( hMem );
327 hMem = NE_LoadPEResource( pModule, type, bits, size );
330 else
332 /* 16-bit NE module */
334 /* If we got a 32-bit hRsrc, we don't need to convert it */
336 hMem = NE_LoadResource( pModule, hRsrc );
338 /* If we are to return a 32-bit resource, we should probably
339 convert it but we don't for now. FIXME !!! */
342 return hMem;
345 /**********************************************************************
346 * RES_LockResource
348 static LPVOID RES_LockResource( HGLOBAL handle, BOOL bRet16 )
350 LPVOID bits = NULL;
352 TRACE("(%08x, %s)\n", handle, bRet16? "NE" : "PE" );
354 if ( HIWORD( handle ) )
356 /* 32-bit memory handle */
358 if ( bRet16 )
359 FIXME("can't return SEGPTR to 32-bit resource %08x.\n", handle );
360 else
361 bits = (LPVOID)handle;
363 else
365 /* 16-bit memory handle */
367 /* May need to reload the resource if discarded */
368 SEGPTR segPtr = WIN16_GlobalLock16( handle );
370 if ( bRet16 )
371 bits = (LPVOID)segPtr;
372 else
373 bits = PTR_SEG_TO_LIN( segPtr );
376 return bits;
379 /**********************************************************************
380 * RES_FreeResource
382 static BOOL RES_FreeResource( HGLOBAL handle )
384 HGLOBAL retv = handle;
386 TRACE("(%08x)\n", handle );
388 if ( HIWORD( handle ) )
390 /* 32-bit memory handle: nothing to do */
392 else
394 /* 16-bit memory handle */
395 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
397 /* Try NE resource first */
398 retv = NE_FreeResource( pModule, handle );
400 /* If this failed, call USER.DestroyIcon32; this will check
401 whether it is a shared cursor/icon; if not it will call
402 GlobalFree16() */
403 if ( retv ) {
404 if ( Callout.DestroyIcon32 )
405 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
406 else
407 retv = GlobalFree16( handle );
411 return (BOOL)retv;
415 /**********************************************************************
416 * FindResource16 (KERNEL.60)
418 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
420 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
421 LPCSTR typeStr = HIWORD(type)? PTR_SEG_TO_LIN(type) : (LPCSTR)type;
423 return RES_FindResource( hModule, typeStr, nameStr,
424 WINE_LanguageId, FALSE, TRUE );
427 /**********************************************************************
428 * FindResourceA (KERNEL32.128)
430 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
432 return RES_FindResource( hModule, type, name,
433 WINE_LanguageId, FALSE, FALSE );
436 /**********************************************************************
437 * FindResourceExA (KERNEL32.129)
439 HANDLE WINAPI FindResourceExA( HMODULE hModule,
440 LPCSTR type, LPCSTR name, WORD lang )
442 return RES_FindResource( hModule, type, name,
443 lang, FALSE, FALSE );
446 /**********************************************************************
447 * FindResourceExW (KERNEL32.130)
449 HRSRC WINAPI FindResourceExW( HMODULE hModule,
450 LPCWSTR type, LPCWSTR name, WORD lang )
452 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
453 lang, TRUE, FALSE );
456 /**********************************************************************
457 * FindResourceW (KERNEL32.131)
459 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
461 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
462 WINE_LanguageId, TRUE, FALSE );
465 /**********************************************************************
466 * LoadResource16 (KERNEL.61)
468 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
470 return RES_LoadResource( hModule, hRsrc, TRUE );
473 /**********************************************************************
474 * LoadResource (KERNEL32.370)
476 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
478 return RES_LoadResource( hModule, hRsrc, FALSE );
481 /**********************************************************************
482 * LockResource16 (KERNEL.62)
484 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
486 return (SEGPTR)RES_LockResource( handle, TRUE );
488 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
490 return RES_LockResource( handle, FALSE );
493 /**********************************************************************
494 * LockResource (KERNEL32.384)
496 LPVOID WINAPI LockResource( HGLOBAL handle )
498 return RES_LockResource( handle, FALSE );
501 /**********************************************************************
502 * FreeResource16 (KERNEL.63)
504 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
506 return RES_FreeResource( handle );
509 /**********************************************************************
510 * FreeResource (KERNEL32.145)
512 BOOL WINAPI FreeResource( HGLOBAL handle )
514 return RES_FreeResource( handle );
517 /**********************************************************************
518 * AccessResource16 (KERNEL.64)
520 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
522 return RES_AccessResource( hModule, hRsrc, TRUE );
525 /**********************************************************************
526 * AccessResource (KERNEL32.64)
528 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
530 return RES_AccessResource( hModule, hRsrc, FALSE );
533 /**********************************************************************
534 * SizeofResource16 (KERNEL.65)
536 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
538 return RES_SizeofResource( hModule, hRsrc, TRUE );
541 /**********************************************************************
542 * SizeofResource (KERNEL32.522)
544 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
546 return RES_SizeofResource( hModule, hRsrc, FALSE );
551 /**********************************************************************
552 * LoadAccelerators16 [USER.177]
554 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
556 HRSRC16 hRsrc;
558 if (HIWORD(lpTableName))
559 TRACE_(accel)("%04x '%s'\n",
560 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
561 else
562 TRACE_(accel)("%04x %04x\n",
563 instance, LOWORD(lpTableName) );
565 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
566 WARN_(accel)("couldn't find accelerator table resource\n");
567 return 0;
570 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
571 return LoadResource16(instance,hRsrc);
574 /**********************************************************************
575 * LoadAccelerators32W [USER.177]
576 * The image layout seems to look like this (not 100% sure):
577 * 00: BYTE type type of accelerator
578 * 01: BYTE pad (to WORD boundary)
579 * 02: WORD event
580 * 04: WORD IDval
581 * 06: WORD pad (to DWORD boundary)
583 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
585 HRSRC hRsrc;
586 HACCEL hMem,hRetval=0;
587 DWORD size;
589 if (HIWORD(lpTableName))
590 TRACE_(accel)("%p '%s'\n",
591 (LPVOID)instance, (char *)( lpTableName ) );
592 else
593 TRACE_(accel)("%p 0x%04x\n",
594 (LPVOID)instance, LOWORD(lpTableName) );
596 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
598 WARN_(accel)("couldn't find accelerator table resource\n");
599 } else {
600 hMem = LoadResource( instance, hRsrc );
601 size = SizeofResource( instance, hRsrc );
602 if(size>=sizeof(PE_ACCEL))
604 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
605 LPACCEL16 accel16;
606 int i,nrofaccells = size/sizeof(PE_ACCEL);
608 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
609 accel16 = (LPACCEL16)GlobalLock16(hRetval);
610 for (i=0;i<nrofaccells;i++) {
611 accel16[i].fVirt = accel_table[i].fVirt;
612 accel16[i].key = accel_table[i].key;
613 accel16[i].cmd = accel_table[i].cmd;
615 accel16[i-1].fVirt |= 0x80;
618 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
619 return hRetval;
622 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
624 LPWSTR uni;
625 HACCEL result;
626 if (HIWORD(lpTableName))
627 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
628 else
629 uni = (LPWSTR)lpTableName;
630 result = LoadAcceleratorsW(instance,uni);
631 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
632 return result;
635 /**********************************************************************
636 * CopyAcceleratorTable32A (USER32.58)
638 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
640 return CopyAcceleratorTableW(src, dst, entries);
643 /**********************************************************************
644 * CopyAcceleratorTable32W (USER32.59)
646 * By mortene@pvv.org 980321
648 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
649 INT entries)
651 int i,xsize;
652 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
653 BOOL done = FALSE;
655 /* Do parameter checking to avoid the explosions and the screaming
656 as far as possible. */
657 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
658 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
659 (LPVOID)src, (LPVOID)dst, entries);
660 return 0;
662 xsize = GlobalSize16(src)/sizeof(ACCEL16);
663 if (xsize>entries) entries=xsize;
665 i=0;
666 while(!done) {
667 /* Spit out some debugging information. */
668 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
669 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
671 /* Copy data to the destination structure array (if dst == NULL,
672 we're just supposed to count the number of entries). */
673 if(dst) {
674 dst[i].fVirt = accel[i].fVirt;
675 dst[i].key = accel[i].key;
676 dst[i].cmd = accel[i].cmd;
678 /* Check if we've reached the end of the application supplied
679 accelerator table. */
680 if(i+1 == entries) {
681 /* Turn off the high order bit, just in case. */
682 dst[i].fVirt &= 0x7f;
683 done = TRUE;
687 /* The highest order bit seems to mark the end of the accelerator
688 resource table, but not always. Use GlobalSize() check too. */
689 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
691 i++;
694 return i;
697 /*********************************************************************
698 * CreateAcceleratorTable (USER32.64)
700 * By mortene@pvv.org 980321
702 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
704 HACCEL hAccel;
705 LPACCEL16 accel;
706 int i;
708 /* Do parameter checking just in case someone's trying to be
709 funny. */
710 if(cEntries < 1) {
711 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
712 lpaccel, cEntries);
713 SetLastError(ERROR_INVALID_PARAMETER);
714 return (HACCEL)NULL;
716 FIXME_(accel)("should check that the accelerator descriptions are valid,"
717 " return NULL and SetLastError() if not.\n");
720 /* Allocate memory and copy the table. */
721 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
723 TRACE_(accel)("handle %x\n", hAccel);
724 if(!hAccel) {
725 ERR_(accel)("Out of memory.\n");
726 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
727 return (HACCEL)NULL;
729 accel = GlobalLock16(hAccel);
730 for (i=0;i<cEntries;i++) {
731 accel[i].fVirt = lpaccel[i].fVirt;
732 accel[i].key = lpaccel[i].key;
733 accel[i].cmd = lpaccel[i].cmd;
735 /* Set the end-of-table terminator. */
736 accel[cEntries-1].fVirt |= 0x80;
738 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
739 return hAccel;
742 /*********************************************************************
743 * CreateAcceleratorTableW (USER32.64)
747 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
749 HACCEL hAccel;
750 LPACCEL16 accel;
751 int i;
752 char ckey;
754 /* Do parameter checking just in case someone's trying to be
755 funny. */
756 if(cEntries < 1) {
757 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
758 lpaccel, cEntries);
759 SetLastError(ERROR_INVALID_PARAMETER);
760 return (HACCEL)NULL;
762 FIXME_(accel)("should check that the accelerator descriptions are valid,"
763 " return NULL and SetLastError() if not.\n");
766 /* Allocate memory and copy the table. */
767 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
769 TRACE_(accel)("handle %x\n", hAccel);
770 if(!hAccel) {
771 ERR_(accel)("Out of memory.\n");
772 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
773 return (HACCEL)NULL;
775 accel = GlobalLock16(hAccel);
778 for (i=0;i<cEntries;i++) {
779 accel[i].fVirt = lpaccel[i].fVirt;
780 if( !(accel[i].fVirt & FVIRTKEY) ) {
781 ckey = (char) lpaccel[i].key;
782 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
783 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
785 else
786 accel[i].key = lpaccel[i].key;
787 accel[i].cmd = lpaccel[i].cmd;
790 /* Set the end-of-table terminator. */
791 accel[cEntries-1].fVirt |= 0x80;
793 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
794 return hAccel;
797 /******************************************************************************
798 * DestroyAcceleratorTable [USER32.130]
799 * Destroys an accelerator table
801 * NOTES
802 * By mortene@pvv.org 980321
804 * PARAMS
805 * handle [I] Handle to accelerator table
807 * RETURNS STD
809 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
811 return GlobalFree16(handle);
814 /**********************************************************************
815 * LoadString16
817 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
818 LPSTR buffer, INT16 buflen )
820 HGLOBAL16 hmem;
821 HRSRC16 hrsrc;
822 unsigned char *p;
823 int string_num;
824 int i;
826 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
827 instance, resource_id, (int) buffer, buflen);
829 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
830 if (!hrsrc) return 0;
831 hmem = LoadResource16( instance, hrsrc );
832 if (!hmem) return 0;
834 p = LockResource16(hmem);
835 string_num = resource_id & 0x000f;
836 for (i = 0; i < string_num; i++)
837 p += *p + 1;
839 TRACE("strlen = %d\n", (int)*p );
841 if (buffer == NULL) return *p;
842 i = MIN(buflen - 1, *p);
843 if (i > 0) {
844 memcpy(buffer, p + 1, i);
845 buffer[i] = '\0';
846 } else {
847 if (buflen > 1) {
848 buffer[0] = '\0';
849 return 0;
851 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
853 FreeResource16( hmem );
855 TRACE("'%s' loaded !\n", buffer);
856 return i;
859 /**********************************************************************
860 * LoadString32W (USER32.376)
862 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
863 LPWSTR buffer, INT buflen )
865 HGLOBAL hmem;
866 HRSRC hrsrc;
867 WCHAR *p;
868 int string_num;
869 int i;
871 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
872 resource_id = (UINT)(-((INT)resource_id));
873 TRACE("instance = %04x, id = %04x, buffer = %08x, "
874 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
876 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
877 * 20 - 31. */
878 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
879 RT_STRINGW );
880 if (!hrsrc) return 0;
881 hmem = LoadResource( instance, hrsrc );
882 if (!hmem) return 0;
884 p = LockResource(hmem);
885 string_num = resource_id & 0x000f;
886 for (i = 0; i < string_num; i++)
887 p += *p + 1;
889 TRACE("strlen = %d\n", (int)*p );
891 if (buffer == NULL) return *p;
892 i = MIN(buflen - 1, *p);
893 if (i > 0) {
894 memcpy(buffer, p + 1, i * sizeof (WCHAR));
895 buffer[i] = (WCHAR) 0;
896 } else {
897 if (buflen > 1) {
898 buffer[0] = (WCHAR) 0;
899 return 0;
901 #if 0
902 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
903 #endif
906 TRACE("%s loaded !\n", debugstr_w(buffer));
907 return i;
910 /**********************************************************************
911 * LoadString32A (USER32.375)
913 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
914 LPSTR buffer, INT buflen )
916 INT retval;
917 LPWSTR buffer2 = NULL;
918 if (buffer && buflen)
919 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
920 retval = LoadStringW(instance,resource_id,buffer2,buflen);
922 if (buffer2)
924 if (retval) {
925 lstrcpynWtoA( buffer, buffer2, buflen );
926 retval = lstrlenA( buffer );
928 else
929 *buffer = 0;
930 HeapFree( GetProcessHeap(), 0, buffer2 );
932 return retval;
935 /* Messages...used by FormatMessage32* (KERNEL32.something)
937 * They can be specified either directly or using a message ID and
938 * loading them from the resource.
940 * The resourcedata has following format:
941 * start:
942 * 0: DWORD nrofentries
943 * nrofentries * subentry:
944 * 0: DWORD firstentry
945 * 4: DWORD lastentry
946 * 8: DWORD offset from start to the stringentries
948 * (lastentry-firstentry) * stringentry:
949 * 0: WORD len (0 marks end)
950 * 2: WORD flags
951 * 4: CHAR[len-4]
952 * (stringentry i of a subentry refers to the ID 'firstentry+i')
954 * Yes, ANSI strings in win32 resources. Go figure.
957 /**********************************************************************
958 * LoadMessage32A (internal)
960 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
961 LPSTR buffer, INT buflen )
963 HGLOBAL hmem;
964 HRSRC hrsrc;
965 PMESSAGE_RESOURCE_DATA mrd;
966 PMESSAGE_RESOURCE_BLOCK mrb;
967 PMESSAGE_RESOURCE_ENTRY mre;
968 int i,slen;
970 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
972 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
973 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
974 if (!hrsrc) return 0;
975 hmem = LoadResource( instance, hrsrc );
976 if (!hmem) return 0;
978 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
979 mre = NULL;
980 mrb = &(mrd->Blocks[0]);
981 for (i=mrd->NumberOfBlocks;i--;) {
982 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
983 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
984 id -= mrb->LowId;
985 break;
987 mrb++;
989 if (!mre)
990 return 0;
991 for (i=id;i--;) {
992 if (!mre->Length)
993 return 0;
994 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
996 slen=mre->Length;
997 TRACE(" - strlen=%d\n",slen);
998 i = MIN(buflen - 1, slen);
999 if (buffer == NULL)
1000 return slen;
1001 if (i>0) {
1002 lstrcpynA(buffer,(char*)mre->Text,i);
1003 buffer[i]=0;
1004 } else {
1005 if (buflen>1) {
1006 buffer[0]=0;
1007 return 0;
1010 if (buffer)
1011 TRACE("'%s' copied !\n", buffer);
1012 return i;
1015 /**********************************************************************
1016 * LoadMessage32W (internal)
1018 INT WINAPI LoadMessageW( HMODULE instance, UINT id, WORD lang,
1019 LPWSTR buffer, INT buflen )
1021 INT retval;
1022 LPSTR buffer2 = NULL;
1023 if (buffer && buflen)
1024 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
1025 retval = LoadMessageA(instance,id,lang,buffer2,buflen);
1026 if (buffer)
1028 if (retval) {
1029 lstrcpynAtoW( buffer, buffer2, buflen );
1030 retval = lstrlenW( buffer );
1032 HeapFree( GetProcessHeap(), 0, buffer2 );
1034 return retval;
1038 /**********************************************************************
1039 * EnumResourceTypesA (KERNEL32.90)
1041 BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
1042 LONG lParam)
1044 /* FIXME: move WINE_MODREF stuff here */
1045 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
1048 /**********************************************************************
1049 * EnumResourceTypesW (KERNEL32.91)
1051 BOOL WINAPI EnumResourceTypesW( HMODULE hmodule,ENUMRESTYPEPROCW lpfun,
1052 LONG lParam)
1054 /* FIXME: move WINE_MODREF stuff here */
1055 return PE_EnumResourceTypesW(hmodule,lpfun,lParam);
1058 /**********************************************************************
1059 * EnumResourceNamesA (KERNEL32.88)
1061 BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
1062 ENUMRESNAMEPROCA lpfun, LONG lParam )
1064 /* FIXME: move WINE_MODREF stuff here */
1065 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
1067 /**********************************************************************
1068 * EnumResourceNamesW (KERNEL32.89)
1070 BOOL WINAPI EnumResourceNamesW( HMODULE hmodule, LPCWSTR type,
1071 ENUMRESNAMEPROCW lpfun, LONG lParam )
1073 /* FIXME: move WINE_MODREF stuff here */
1074 return PE_EnumResourceNamesW(hmodule,type,lpfun,lParam);
1077 /**********************************************************************
1078 * EnumResourceLanguagesA (KERNEL32.86)
1080 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
1081 LPCSTR name, ENUMRESLANGPROCA lpfun,
1082 LONG lParam)
1084 /* FIXME: move WINE_MODREF stuff here */
1085 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
1087 /**********************************************************************
1088 * EnumResourceLanguagesW (KERNEL32.87)
1090 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmodule, LPCWSTR type,
1091 LPCWSTR name, ENUMRESLANGPROCW lpfun,
1092 LONG lParam)
1094 /* FIXME: move WINE_MODREF stuff here */
1095 return PE_EnumResourceLanguagesW(hmodule,type,name,lpfun,lParam);