Francois Boisvert
[wine.git] / loader / resource.c
bloba36fe7ecb9da5fb54262eb881567bfaa103609ac
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 "wine/exception.h"
22 #include "ldt.h"
23 #include "global.h"
24 #include "heap.h"
25 #include "callback.h"
26 #include "cursoricon.h"
27 #include "neexe.h"
28 #include "task.h"
29 #include "process.h"
30 #include "module.h"
31 #include "file.h"
32 #include "debugtools.h"
33 #include "winerror.h"
34 #include "winnls.h"
36 DEFAULT_DEBUG_CHANNEL(resource);
37 DECLARE_DEBUG_CHANNEL(accel);
39 extern WORD WINE_LanguageId;
41 #define HRSRC_MAP_BLOCKSIZE 16
43 typedef struct _HRSRC_ELEM
45 HANDLE hRsrc;
46 WORD type;
47 } HRSRC_ELEM;
49 typedef struct _HRSRC_MAP
51 int nAlloc;
52 int nUsed;
53 HRSRC_ELEM *elem;
54 } HRSRC_MAP;
56 /**********************************************************************
57 * MapHRsrc32To16
59 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
61 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
62 HRSRC_ELEM *newElem;
63 int i;
65 /* On first call, initialize HRSRC map */
66 if ( !map )
68 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
69 sizeof(HRSRC_MAP) ) ) )
71 ERR("Cannot allocate HRSRC map\n" );
72 return 0;
74 pModule->hRsrcMap = (LPVOID)map;
77 /* Check whether HRSRC32 already in map */
78 for ( i = 0; i < map->nUsed; i++ )
79 if ( map->elem[i].hRsrc == hRsrc32 )
80 return (HRSRC16)(i + 1);
82 /* If no space left, grow table */
83 if ( map->nUsed == map->nAlloc )
85 if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
86 map->elem,
87 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
88 * sizeof(HRSRC_ELEM) ) ))
90 ERR("Cannot grow HRSRC map\n" );
91 return 0;
93 map->elem = newElem;
94 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
97 /* Add HRSRC32 to table */
98 map->elem[map->nUsed].hRsrc = hRsrc32;
99 map->elem[map->nUsed].type = type;
100 map->nUsed++;
102 return (HRSRC16)map->nUsed;
105 /**********************************************************************
106 * MapHRsrc16To32
108 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
110 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
111 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
113 return map->elem[(int)hRsrc16-1].hRsrc;
116 /**********************************************************************
117 * MapHRsrc16ToType
119 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
121 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
122 if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
124 return map->elem[(int)hRsrc16-1].type;
128 /* filter for page-fault exceptions */
129 static WINE_EXCEPTION_FILTER(page_fault)
131 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
132 return EXCEPTION_EXECUTE_HANDLER;
133 return EXCEPTION_CONTINUE_SEARCH;
136 static HRSRC RES_FindResource2( HMODULE hModule, LPCSTR type,
137 LPCSTR name, WORD lang,
138 BOOL bUnicode, BOOL bRet16 )
140 HRSRC hRsrc = 0;
141 HMODULE16 hMod16 = MapHModuleLS( hModule );
142 NE_MODULE *pModule = NE_GetPtr( hMod16 );
143 WINE_MODREF *wm = pModule && pModule->module32?
144 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
146 TRACE("(%08x %s, %08x%s, %08x%s, %04x, %s, %s)\n",
147 hModule,
148 pModule ? (char *)NE_MODULE_NAME(pModule) : "NULL dereference",
149 (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
150 (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
151 lang,
152 bUnicode? "W" : "A",
153 bRet16? "NE" : "PE" );
155 if (pModule)
157 if ( wm )
159 /* 32-bit PE module */
160 LPWSTR typeStr, nameStr;
162 if ( HIWORD( type ) && !bUnicode )
163 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
164 else
165 typeStr = (LPWSTR)type;
166 if ( HIWORD( name ) && !bUnicode )
167 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
168 else
169 nameStr = (LPWSTR)name;
171 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
173 if ( HIWORD( type ) && !bUnicode )
174 HeapFree( GetProcessHeap(), 0, typeStr );
175 if ( HIWORD( name ) && !bUnicode )
176 HeapFree( GetProcessHeap(), 0, nameStr );
179 /* If we need to return 16-bit HRSRC, perform conversion */
180 if ( bRet16 )
181 hRsrc = MapHRsrc32To16( pModule, hRsrc,
182 HIWORD( type )? 0 : LOWORD( type ) );
184 else
186 /* 16-bit NE module */
187 LPSTR typeStr, nameStr;
189 if ( HIWORD( type ) && bUnicode )
190 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
191 else
192 typeStr = (LPSTR)type;
193 if ( HIWORD( name ) && bUnicode )
194 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
195 else
196 nameStr = (LPSTR)name;
198 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
200 if ( HIWORD( type ) && bUnicode )
201 HeapFree( GetProcessHeap(), 0, typeStr );
202 if ( HIWORD( name ) && bUnicode )
203 HeapFree( GetProcessHeap(), 0, nameStr );
206 /* If we need to return 32-bit HRSRC, no conversion is necessary,
207 we simply use the 16-bit HRSRC as 32-bit HRSRC */
210 return hRsrc;
213 /**********************************************************************
214 * RES_FindResource
217 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
218 LPCSTR name, WORD lang,
219 BOOL bUnicode, BOOL bRet16 )
221 HRSRC hRsrc;
222 __TRY
224 hRsrc = RES_FindResource2(hModule, type, name, lang, bUnicode, bRet16);
226 __EXCEPT(page_fault)
228 WARN("page fault\n");
229 SetLastError(ERROR_INVALID_PARAMETER);
230 return 0;
232 __ENDTRY
233 return hRsrc;
236 /**********************************************************************
237 * RES_SizeofResource
239 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
241 DWORD size = 0;
243 HMODULE16 hMod16 = MapHModuleLS( hModule );
244 NE_MODULE *pModule = NE_GetPtr( hMod16 );
245 WINE_MODREF *wm = pModule && pModule->module32?
246 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
248 TRACE("(%08x %s, %08x, %s)\n",
249 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
251 if ( !pModule || !hRsrc ) return 0;
253 if ( wm )
255 /* 32-bit PE module */
257 /* If we got a 16-bit hRsrc, convert it */
258 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
260 size = PE_SizeofResource( hModule, hRsrc32 );
262 else
264 /* 16-bit NE module */
266 /* If we got a 32-bit hRsrc, we don't need to convert it */
268 size = NE_SizeofResource( pModule, hRsrc );
271 return size;
274 /**********************************************************************
275 * RES_AccessResource
277 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
279 HFILE hFile = HFILE_ERROR;
281 HMODULE16 hMod16 = MapHModuleLS( hModule );
282 NE_MODULE *pModule = NE_GetPtr( hMod16 );
283 WINE_MODREF *wm = pModule && pModule->module32?
284 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
286 TRACE("(%08x %s, %08x, %s)\n",
287 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
289 if ( !pModule || !hRsrc ) return HFILE_ERROR;
291 if ( wm )
293 /* 32-bit PE module */
294 #if 0
295 /* If we got a 16-bit hRsrc, convert it */
296 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
297 #endif
299 FIXME("32-bit modules not yet supported.\n" );
300 hFile = HFILE_ERROR;
302 /* If we need to return a 16-bit file handle, convert it */
303 if ( bRet16 )
304 hFile = FILE_AllocDosHandle( hFile );
306 else
308 /* 16-bit NE module */
310 /* If we got a 32-bit hRsrc, we don't need to convert it */
312 hFile = NE_AccessResource( pModule, hRsrc );
314 /* If we are to return a 32-bit file handle, convert it */
315 if ( !bRet16 )
316 hFile = FILE_GetHandle( hFile );
319 return hFile;
322 /**********************************************************************
323 * RES_LoadResource
325 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
327 HGLOBAL hMem = 0;
329 HMODULE16 hMod16 = MapHModuleLS( hModule );
330 NE_MODULE *pModule = NE_GetPtr( hMod16 );
331 WINE_MODREF *wm = pModule && pModule->module32?
332 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
334 TRACE("(%08x %s, %08x, %s)\n",
335 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
337 if ( !pModule || !hRsrc ) return 0;
339 if ( wm )
341 /* 32-bit PE module */
343 /* If we got a 16-bit hRsrc, convert it */
344 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
346 hMem = PE_LoadResource( wm, hRsrc32 );
348 /* If we need to return a 16-bit resource, convert it */
349 if ( bRet16 )
351 WORD type = MapHRsrc16ToType( pModule, hRsrc );
352 DWORD size = SizeofResource( hModule, hRsrc );
353 LPVOID bits = LockResource( hMem );
355 hMem = NE_LoadPEResource( pModule, type, bits, size );
358 else
360 /* 16-bit NE module */
362 /* If we got a 32-bit hRsrc, we don't need to convert it */
364 hMem = NE_LoadResource( pModule, hRsrc );
366 /* If we are to return a 32-bit resource, we should probably
367 convert it but we don't for now. FIXME !!! */
370 return hMem;
373 /**********************************************************************
374 * RES_LockResource
376 static LPVOID RES_LockResource( HGLOBAL handle, BOOL bRet16 )
378 LPVOID bits = NULL;
380 TRACE("(%08x, %s)\n", handle, bRet16? "NE" : "PE" );
382 if ( HIWORD( handle ) )
384 /* 32-bit memory handle */
386 if ( bRet16 )
387 FIXME("can't return SEGPTR to 32-bit resource %08x.\n", handle );
388 else
389 bits = (LPVOID)handle;
391 else
393 /* 16-bit memory handle */
395 /* May need to reload the resource if discarded */
396 SEGPTR segPtr = WIN16_GlobalLock16( handle );
398 if ( bRet16 )
399 bits = (LPVOID)segPtr;
400 else
401 bits = PTR_SEG_TO_LIN( segPtr );
404 return bits;
407 /**********************************************************************
408 * RES_FreeResource
410 static BOOL RES_FreeResource( HGLOBAL handle )
412 HGLOBAL retv = handle;
414 TRACE("(%08x)\n", handle );
416 if ( HIWORD( handle ) )
418 /* 32-bit memory handle: nothing to do */
420 else
422 /* 16-bit memory handle */
423 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( 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
430 GlobalFree16() */
431 if ( retv ) {
432 if ( Callout.DestroyIcon32 )
433 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
434 else
435 retv = GlobalFree16( handle );
439 return (BOOL)retv;
443 /**********************************************************************
444 * FindResource16 (KERNEL.60)
446 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
448 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
449 LPCSTR typeStr = HIWORD(type)? PTR_SEG_TO_LIN(type) : (LPCSTR)type;
451 return RES_FindResource( hModule, typeStr, nameStr,
452 WINE_LanguageId, FALSE, TRUE );
455 /**********************************************************************
456 * FindResourceA (KERNEL32.128)
458 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
460 return RES_FindResource( hModule, type, name,
461 WINE_LanguageId, FALSE, FALSE );
464 /**********************************************************************
465 * FindResourceExA (KERNEL32.129)
467 HANDLE WINAPI FindResourceExA( HMODULE hModule,
468 LPCSTR type, LPCSTR name, WORD lang )
470 return RES_FindResource( hModule, type, name,
471 lang, FALSE, FALSE );
474 /**********************************************************************
475 * FindResourceExW (KERNEL32.130)
477 HRSRC WINAPI FindResourceExW( HMODULE hModule,
478 LPCWSTR type, LPCWSTR name, WORD lang )
480 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
481 lang, TRUE, FALSE );
484 /**********************************************************************
485 * FindResourceW (KERNEL32.131)
487 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
489 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
490 WINE_LanguageId, TRUE, FALSE );
493 /**********************************************************************
494 * LoadResource16 (KERNEL.61)
496 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
498 return RES_LoadResource( hModule, hRsrc, TRUE );
501 /**********************************************************************
502 * LoadResource (KERNEL32.370)
504 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
506 return RES_LoadResource( hModule, hRsrc, FALSE );
509 /**********************************************************************
510 * LockResource16 (KERNEL.62)
512 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
514 return (SEGPTR)RES_LockResource( handle, TRUE );
516 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
518 return RES_LockResource( handle, FALSE );
521 /**********************************************************************
522 * LockResource (KERNEL32.384)
524 LPVOID WINAPI LockResource( HGLOBAL handle )
526 return RES_LockResource( handle, FALSE );
529 /**********************************************************************
530 * FreeResource16 (KERNEL.63)
532 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
534 return RES_FreeResource( handle );
537 /**********************************************************************
538 * FreeResource (KERNEL32.145)
540 BOOL WINAPI FreeResource( HGLOBAL handle )
542 return RES_FreeResource( handle );
545 /**********************************************************************
546 * AccessResource16 (KERNEL.64)
548 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
550 return RES_AccessResource( hModule, hRsrc, TRUE );
553 /**********************************************************************
554 * AccessResource (KERNEL32.64)
556 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
558 return RES_AccessResource( hModule, hRsrc, FALSE );
561 /**********************************************************************
562 * SizeofResource16 (KERNEL.65)
564 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
566 return RES_SizeofResource( hModule, hRsrc, TRUE );
569 /**********************************************************************
570 * SizeofResource (KERNEL32.522)
572 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
574 return RES_SizeofResource( hModule, hRsrc, FALSE );
579 /**********************************************************************
580 * LoadAccelerators16 [USER.177]
582 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
584 HRSRC16 hRsrc;
586 if (HIWORD(lpTableName))
587 TRACE_(accel)("%04x '%s'\n",
588 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
589 else
590 TRACE_(accel)("%04x %04x\n",
591 instance, LOWORD(lpTableName) );
593 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
594 WARN_(accel)("couldn't find accelerator table resource\n");
595 return 0;
598 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
599 return LoadResource16(instance,hRsrc);
602 /**********************************************************************
603 * LoadAcceleratorsW [USER.177]
604 * The image layout seems to look like this (not 100% sure):
605 * 00: BYTE type type of accelerator
606 * 01: BYTE pad (to WORD boundary)
607 * 02: WORD event
608 * 04: WORD IDval
609 * 06: WORD pad (to DWORD boundary)
611 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
613 HRSRC hRsrc;
614 HACCEL hMem,hRetval=0;
615 DWORD size;
617 if (HIWORD(lpTableName))
618 TRACE_(accel)("%p '%s'\n",
619 (LPVOID)instance, (char *)( lpTableName ) );
620 else
621 TRACE_(accel)("%p 0x%04x\n",
622 (LPVOID)instance, LOWORD(lpTableName) );
624 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
626 WARN_(accel)("couldn't find accelerator table resource\n");
627 } else {
628 hMem = LoadResource( instance, hRsrc );
629 size = SizeofResource( instance, hRsrc );
630 if(size>=sizeof(PE_ACCEL))
632 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
633 LPACCEL16 accel16;
634 int i,nrofaccells = size/sizeof(PE_ACCEL);
636 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
637 accel16 = (LPACCEL16)GlobalLock16(hRetval);
638 for (i=0;i<nrofaccells;i++) {
639 accel16[i].fVirt = accel_table[i].fVirt;
640 accel16[i].key = accel_table[i].key;
641 accel16[i].cmd = accel_table[i].cmd;
643 accel16[i-1].fVirt |= 0x80;
646 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
647 return hRetval;
650 /***********************************************************************
651 * LoadAcceleratorsA
653 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
655 LPWSTR uni;
656 HACCEL result;
657 if (HIWORD(lpTableName))
658 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
659 else
660 uni = (LPWSTR)lpTableName;
661 result = LoadAcceleratorsW(instance,uni);
662 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
663 return result;
666 /**********************************************************************
667 * CopyAcceleratorTableA (USER32.58)
669 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
671 return CopyAcceleratorTableW(src, dst, entries);
674 /**********************************************************************
675 * CopyAcceleratorTableW (USER32.59)
677 * By mortene@pvv.org 980321
679 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
680 INT entries)
682 int i,xsize;
683 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
684 BOOL done = FALSE;
686 /* Do parameter checking to avoid the explosions and the screaming
687 as far as possible. */
688 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
689 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
690 (LPVOID)src, (LPVOID)dst, entries);
691 return 0;
693 xsize = GlobalSize16(src)/sizeof(ACCEL16);
694 if (xsize>entries) entries=xsize;
696 i=0;
697 while(!done) {
698 /* Spit out some debugging information. */
699 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
700 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
702 /* Copy data to the destination structure array (if dst == NULL,
703 we're just supposed to count the number of entries). */
704 if(dst) {
705 dst[i].fVirt = accel[i].fVirt;
706 dst[i].key = accel[i].key;
707 dst[i].cmd = accel[i].cmd;
709 /* Check if we've reached the end of the application supplied
710 accelerator table. */
711 if(i+1 == entries) {
712 /* Turn off the high order bit, just in case. */
713 dst[i].fVirt &= 0x7f;
714 done = TRUE;
718 /* The highest order bit seems to mark the end of the accelerator
719 resource table, but not always. Use GlobalSize() check too. */
720 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
722 i++;
725 return i;
728 /*********************************************************************
729 * CreateAcceleratorTableA (USER32.64)
731 * By mortene@pvv.org 980321
733 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
735 HACCEL hAccel;
736 LPACCEL16 accel;
737 int i;
739 /* Do parameter checking just in case someone's trying to be
740 funny. */
741 if(cEntries < 1) {
742 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
743 lpaccel, cEntries);
744 SetLastError(ERROR_INVALID_PARAMETER);
745 return (HACCEL)NULL;
747 FIXME_(accel)("should check that the accelerator descriptions are valid,"
748 " return NULL and SetLastError() if not.\n");
751 /* Allocate memory and copy the table. */
752 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
754 TRACE_(accel)("handle %x\n", hAccel);
755 if(!hAccel) {
756 ERR_(accel)("Out of memory.\n");
757 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
758 return (HACCEL)NULL;
760 accel = GlobalLock16(hAccel);
761 for (i=0;i<cEntries;i++) {
762 accel[i].fVirt = lpaccel[i].fVirt;
763 accel[i].key = lpaccel[i].key;
764 accel[i].cmd = lpaccel[i].cmd;
766 /* Set the end-of-table terminator. */
767 accel[cEntries-1].fVirt |= 0x80;
769 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
770 return hAccel;
773 /*********************************************************************
774 * CreateAcceleratorTableW (USER32.64)
778 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
780 HACCEL hAccel;
781 LPACCEL16 accel;
782 int i;
783 char ckey;
785 /* Do parameter checking just in case someone's trying to be
786 funny. */
787 if(cEntries < 1) {
788 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
789 lpaccel, cEntries);
790 SetLastError(ERROR_INVALID_PARAMETER);
791 return (HACCEL)NULL;
793 FIXME_(accel)("should check that the accelerator descriptions are valid,"
794 " return NULL and SetLastError() if not.\n");
797 /* Allocate memory and copy the table. */
798 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
800 TRACE_(accel)("handle %x\n", hAccel);
801 if(!hAccel) {
802 ERR_(accel)("Out of memory.\n");
803 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
804 return (HACCEL)NULL;
806 accel = GlobalLock16(hAccel);
809 for (i=0;i<cEntries;i++) {
810 accel[i].fVirt = lpaccel[i].fVirt;
811 if( !(accel[i].fVirt & FVIRTKEY) ) {
812 ckey = (char) lpaccel[i].key;
813 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
814 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
816 else
817 accel[i].key = lpaccel[i].key;
818 accel[i].cmd = lpaccel[i].cmd;
821 /* Set the end-of-table terminator. */
822 accel[cEntries-1].fVirt |= 0x80;
824 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
825 return hAccel;
828 /******************************************************************************
829 * DestroyAcceleratorTable [USER32.130]
830 * Destroys an accelerator table
832 * NOTES
833 * By mortene@pvv.org 980321
835 * PARAMS
836 * handle [I] Handle to accelerator table
838 * RETURNS STD
840 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
842 return GlobalFree16(handle);
845 /**********************************************************************
846 * LoadString16
848 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
849 LPSTR buffer, INT16 buflen )
851 HGLOBAL16 hmem;
852 HRSRC16 hrsrc;
853 unsigned char *p;
854 int string_num;
855 int i;
857 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
858 instance, resource_id, (int) buffer, buflen);
860 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
861 if (!hrsrc) return 0;
862 hmem = LoadResource16( instance, hrsrc );
863 if (!hmem) return 0;
865 p = LockResource16(hmem);
866 string_num = resource_id & 0x000f;
867 for (i = 0; i < string_num; i++)
868 p += *p + 1;
870 TRACE("strlen = %d\n", (int)*p );
872 if (buffer == NULL) return *p;
873 i = min(buflen - 1, *p);
874 if (i > 0) {
875 memcpy(buffer, p + 1, i);
876 buffer[i] = '\0';
877 } else {
878 if (buflen > 1) {
879 buffer[0] = '\0';
880 return 0;
882 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
884 FreeResource16( hmem );
886 TRACE("'%s' loaded !\n", buffer);
887 return i;
890 /**********************************************************************
891 * LoadStringW (USER32.376)
893 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
894 LPWSTR buffer, INT buflen )
896 HGLOBAL hmem;
897 HRSRC hrsrc;
898 WCHAR *p;
899 int string_num;
900 int i;
902 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
903 resource_id = (UINT)(-((INT)resource_id));
904 TRACE("instance = %04x, id = %04x, buffer = %08x, "
905 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
907 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
908 * 20 - 31. */
909 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
910 RT_STRINGW );
911 if (!hrsrc) return 0;
912 hmem = LoadResource( instance, hrsrc );
913 if (!hmem) return 0;
915 p = LockResource(hmem);
916 string_num = resource_id & 0x000f;
917 for (i = 0; i < string_num; i++)
918 p += *p + 1;
920 TRACE("strlen = %d\n", (int)*p );
922 if (buffer == NULL) return *p;
923 i = min(buflen - 1, *p);
924 if (i > 0) {
925 memcpy(buffer, p + 1, i * sizeof (WCHAR));
926 buffer[i] = (WCHAR) 0;
927 } else {
928 if (buflen > 1) {
929 buffer[0] = (WCHAR) 0;
930 return 0;
932 #if 0
933 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
934 #endif
937 TRACE("%s loaded !\n", debugstr_w(buffer));
938 return i;
941 /**********************************************************************
942 * LoadStringA (USER32.375)
944 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
945 LPSTR buffer, INT buflen )
947 INT retval;
948 INT wbuflen;
949 INT abuflen;
950 LPWSTR wbuf = NULL;
951 LPSTR abuf = NULL;
953 if ( buffer != NULL && buflen > 0 )
954 *buffer = 0;
956 wbuflen = LoadStringW(instance,resource_id,NULL,0);
957 if ( !wbuflen )
958 return 0;
959 wbuflen ++;
961 retval = 0;
962 wbuf = HeapAlloc( GetProcessHeap(), 0, wbuflen * sizeof(WCHAR) );
963 wbuflen = LoadStringW(instance,resource_id,wbuf,wbuflen);
964 if ( wbuflen > 0 )
966 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,NULL,0,NULL,NULL);
967 if ( abuflen > 0 )
969 if ( buffer == NULL || buflen == 0 )
970 retval = abuflen;
971 else
973 abuf = HeapAlloc( GetProcessHeap(), 0, abuflen * sizeof(CHAR) );
974 abuflen = WideCharToMultiByte(CP_ACP,0,wbuf,wbuflen,abuf,abuflen,NULL,NULL);
975 if ( abuflen > 0 )
977 abuflen = min(abuflen,buflen - 1);
978 memcpy( buffer, abuf, abuflen );
979 buffer[abuflen] = 0;
980 retval = abuflen;
982 HeapFree( GetProcessHeap(), 0, abuf );
986 HeapFree( GetProcessHeap(), 0, wbuf );
988 return retval;
991 /* Messages...used by FormatMessage32* (KERNEL32.something)
993 * They can be specified either directly or using a message ID and
994 * loading them from the resource.
996 * The resourcedata has following format:
997 * start:
998 * 0: DWORD nrofentries
999 * nrofentries * subentry:
1000 * 0: DWORD firstentry
1001 * 4: DWORD lastentry
1002 * 8: DWORD offset from start to the stringentries
1004 * (lastentry-firstentry) * stringentry:
1005 * 0: WORD len (0 marks end)
1006 * 2: WORD flags
1007 * 4: CHAR[len-4]
1008 * (stringentry i of a subentry refers to the ID 'firstentry+i')
1010 * Yes, ANSI strings in win32 resources. Go figure.
1013 /**********************************************************************
1014 * LoadMessageA (internal)
1016 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
1017 LPSTR buffer, INT buflen )
1019 HGLOBAL hmem;
1020 HRSRC hrsrc;
1021 PMESSAGE_RESOURCE_DATA mrd;
1022 PMESSAGE_RESOURCE_BLOCK mrb;
1023 PMESSAGE_RESOURCE_ENTRY mre;
1024 int i,slen;
1026 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
1028 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
1029 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
1030 if (!hrsrc) return 0;
1031 hmem = LoadResource( instance, hrsrc );
1032 if (!hmem) return 0;
1034 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
1035 mre = NULL;
1036 mrb = &(mrd->Blocks[0]);
1037 for (i=mrd->NumberOfBlocks;i--;) {
1038 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
1039 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
1040 id -= mrb->LowId;
1041 break;
1043 mrb++;
1045 if (!mre)
1046 return 0;
1047 for (i=id;i--;) {
1048 if (!mre->Length)
1049 return 0;
1050 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
1052 slen=mre->Length;
1053 TRACE(" - strlen=%d\n",slen);
1054 i = min(buflen - 1, slen);
1055 if (buffer == NULL)
1056 return slen;
1057 if (i>0) {
1058 lstrcpynA(buffer,(char*)mre->Text,i);
1059 buffer[i]=0;
1060 } else {
1061 if (buflen>1) {
1062 buffer[0]=0;
1063 return 0;
1066 if (buffer)
1067 TRACE("'%s' copied !\n", buffer);
1068 return i;
1071 /**********************************************************************
1072 * LoadMessageW (internal)
1074 INT WINAPI LoadMessageW( HMODULE instance, UINT id, WORD lang,
1075 LPWSTR buffer, INT buflen )
1077 INT retval;
1078 LPSTR buffer2 = NULL;
1079 if (buffer && buflen)
1080 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
1081 retval = LoadMessageA(instance,id,lang,buffer2,buflen);
1082 if (buffer)
1084 if (retval) {
1085 lstrcpynAtoW( buffer, buffer2, buflen );
1086 retval = lstrlenW( buffer );
1088 HeapFree( GetProcessHeap(), 0, buffer2 );
1090 return retval;
1094 /**********************************************************************
1095 * EnumResourceTypesA (KERNEL32.90)
1097 BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
1098 LONG lParam)
1100 /* FIXME: move WINE_MODREF stuff here */
1101 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
1104 /**********************************************************************
1105 * EnumResourceTypesW (KERNEL32.91)
1107 BOOL WINAPI EnumResourceTypesW( HMODULE hmodule,ENUMRESTYPEPROCW lpfun,
1108 LONG lParam)
1110 /* FIXME: move WINE_MODREF stuff here */
1111 return PE_EnumResourceTypesW(hmodule,lpfun,lParam);
1114 /**********************************************************************
1115 * EnumResourceNamesA (KERNEL32.88)
1117 BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
1118 ENUMRESNAMEPROCA lpfun, LONG lParam )
1120 /* FIXME: move WINE_MODREF stuff here */
1121 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
1123 /**********************************************************************
1124 * EnumResourceNamesW (KERNEL32.89)
1126 BOOL WINAPI EnumResourceNamesW( HMODULE hmodule, LPCWSTR type,
1127 ENUMRESNAMEPROCW lpfun, LONG lParam )
1129 /* FIXME: move WINE_MODREF stuff here */
1130 return PE_EnumResourceNamesW(hmodule,type,lpfun,lParam);
1133 /**********************************************************************
1134 * EnumResourceLanguagesA (KERNEL32.86)
1136 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
1137 LPCSTR name, ENUMRESLANGPROCA lpfun,
1138 LONG lParam)
1140 /* FIXME: move WINE_MODREF stuff here */
1141 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
1143 /**********************************************************************
1144 * EnumResourceLanguagesW (KERNEL32.87)
1146 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmodule, LPCWSTR type,
1147 LPCWSTR name, ENUMRESLANGPROCW lpfun,
1148 LONG lParam)
1150 /* FIXME: move WINE_MODREF stuff here */
1151 return PE_EnumResourceLanguagesW(hmodule,type,name,lpfun,lParam);