4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
18 #include "wine/winbase16.h"
19 #include "wine/winuser16.h"
24 #include "cursoricon.h"
30 #include "debugtools.h"
36 DECLARE_DEBUG_CHANNEL(accel)
37 DECLARE_DEBUG_CHANNEL(resource)
39 extern WORD WINE_LanguageId;
41 #define HRSRC_MAP_BLOCKSIZE 16
43 typedef struct _HRSRC_ELEM
49 typedef struct _HRSRC_MAP
56 /**********************************************************************
59 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
61 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
65 /* On first call, initialize HRSRC map */
68 if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
69 sizeof(HRSRC_MAP) ) ) )
71 ERR_(resource)("Cannot allocate HRSRC map\n" );
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,
87 (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
88 * sizeof(HRSRC_ELEM) ) ))
90 ERR_(resource)("Cannot grow HRSRC map\n" );
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;
102 return (HRSRC16)map->nUsed;
105 /**********************************************************************
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 /**********************************************************************
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 /**********************************************************************
131 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
132 LPCSTR name, WORD lang,
133 BOOL bUnicode, BOOL bRet16 )
137 HMODULE16 hMod16 = MapHModuleLS( hModule );
138 NE_MODULE *pModule = NE_GetPtr( hMod16 );
139 WINE_MODREF *wm = pModule && pModule->module32?
140 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
142 TRACE_(resource)("(%08x %s, %08x%s, %08x%s, %04x, %s, %s)\n",
143 hModule, 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)) : "",
148 bRet16? "NE" : "PE" );
150 if ( !pModule ) return 0;
154 /* 32-bit PE/ELF module */
155 LPWSTR typeStr, nameStr;
157 if ( HIWORD( type ) && !bUnicode )
158 typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
160 typeStr = (LPWSTR)type;
161 if ( HIWORD( name ) && !bUnicode )
162 nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
164 nameStr = (LPWSTR)name;
169 hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
173 hRsrc = LIBRES_FindResource( hModule, nameStr, typeStr );
177 ERR_(resource)("unknown module type %d\n", wm->type );
181 if ( HIWORD( type ) && !bUnicode )
182 HeapFree( GetProcessHeap(), 0, typeStr );
183 if ( HIWORD( name ) && !bUnicode )
184 HeapFree( GetProcessHeap(), 0, nameStr );
187 /* If we need to return 16-bit HRSRC, perform conversion */
189 hRsrc = MapHRsrc32To16( pModule, hRsrc,
190 HIWORD( type )? 0 : LOWORD( type ) );
194 /* 16-bit NE module */
195 LPSTR typeStr, nameStr;
197 if ( HIWORD( type ) && bUnicode )
198 typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
200 typeStr = (LPSTR)type;
201 if ( HIWORD( name ) && bUnicode )
202 nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
204 nameStr = (LPSTR)name;
206 hRsrc = NE_FindResource( pModule, nameStr, typeStr );
208 if ( HIWORD( type ) && bUnicode )
209 HeapFree( GetProcessHeap(), 0, typeStr );
210 if ( HIWORD( name ) && bUnicode )
211 HeapFree( GetProcessHeap(), 0, nameStr );
214 /* If we need to return 32-bit HRSRC, no conversion is necessary,
215 we simply use the 16-bit HRSRC as 32-bit HRSRC */
221 /**********************************************************************
224 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
228 HMODULE16 hMod16 = MapHModuleLS( hModule );
229 NE_MODULE *pModule = NE_GetPtr( hMod16 );
230 WINE_MODREF *wm = pModule && pModule->module32?
231 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
233 TRACE_(resource)("(%08x %s, %08x, %s)\n",
234 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
236 if ( !pModule || !hRsrc ) return 0;
240 /* 32-bit PE/ELF module */
242 /* If we got a 16-bit hRsrc, convert it */
243 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
248 size = PE_SizeofResource( hModule, hRsrc32 );
252 size = LIBRES_SizeofResource( hModule, hRsrc32 );
256 ERR_(resource)("unknown module type %d\n", wm->type );
262 /* 16-bit NE module */
264 /* If we got a 32-bit hRsrc, we don't need to convert it */
266 size = NE_SizeofResource( pModule, hRsrc );
272 /**********************************************************************
275 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
277 HFILE hFile = HFILE_ERROR;
279 HMODULE16 hMod16 = MapHModuleLS( hModule );
280 NE_MODULE *pModule = NE_GetPtr( hMod16 );
281 WINE_MODREF *wm = pModule && pModule->module32?
282 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
284 TRACE_(resource)("(%08x %s, %08x, %s)\n",
285 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
287 if ( !pModule || !hRsrc ) return HFILE_ERROR;
291 /* 32-bit PE/ELF module */
293 /* If we got a 16-bit hRsrc, convert it */
294 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
297 FIXME_(resource)("32-bit modules not yet supported.\n" );
300 /* If we need to return a 16-bit file handle, convert it */
302 hFile = FILE_AllocDosHandle( hFile );
306 /* 16-bit NE module */
308 /* If we got a 32-bit hRsrc, we don't need to convert it */
310 hFile = NE_AccessResource( pModule, hRsrc );
312 /* If we are to return a 32-bit file handle, convert it */
314 hFile = FILE_GetHandle( hFile );
320 /**********************************************************************
323 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
327 HMODULE16 hMod16 = MapHModuleLS( hModule );
328 NE_MODULE *pModule = NE_GetPtr( hMod16 );
329 WINE_MODREF *wm = pModule && pModule->module32?
330 MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
332 TRACE_(resource)("(%08x %s, %08x, %s)\n",
333 hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
335 if ( !pModule || !hRsrc ) return 0;
339 /* 32-bit PE/ELF module */
341 /* If we got a 16-bit hRsrc, convert it */
342 HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
347 hMem = PE_LoadResource( wm, hRsrc32 );
351 hMem = LIBRES_LoadResource( hModule, hRsrc32 );
355 ERR_(resource)("unknown module type %d\n", wm->type );
359 /* If we need to return a 16-bit resource, convert it */
362 WORD type = MapHRsrc16ToType( pModule, hRsrc );
363 DWORD size = SizeofResource( hModule, hRsrc );
364 LPVOID bits = LockResource( hMem );
366 hMem = NE_LoadPEResource( pModule, type, bits, size );
371 /* 16-bit NE module */
373 /* If we got a 32-bit hRsrc, we don't need to convert it */
375 hMem = NE_LoadResource( pModule, hRsrc );
377 /* If we are to return a 32-bit resource, we should probably
378 convert it but we don't for now. FIXME !!! */
384 /**********************************************************************
387 static LPVOID RES_LockResource( HGLOBAL handle, BOOL bRet16 )
391 TRACE_(resource)("(%08x, %s)\n", handle, bRet16? "NE" : "PE" );
393 if ( HIWORD( handle ) )
395 /* 32-bit memory handle */
398 FIXME_(resource)("can't return SEGPTR to 32-bit resource %08x.\n", handle );
400 bits = (LPVOID)handle;
404 /* 16-bit memory handle */
406 /* May need to reload the resource if discarded */
407 SEGPTR segPtr = WIN16_GlobalLock16( handle );
410 bits = (LPVOID)segPtr;
412 bits = PTR_SEG_TO_LIN( segPtr );
418 /**********************************************************************
421 static BOOL RES_FreeResource( HGLOBAL handle )
423 HGLOBAL retv = handle;
425 TRACE_(resource)("(%08x)\n", handle );
427 if ( HIWORD( handle ) )
429 /* 32-bit memory handle: nothing to do */
433 /* 16-bit memory handle */
434 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
436 /* Try NE resource first */
437 retv = NE_FreeResource( pModule, handle );
439 /* If this failed, call USER.DestroyIcon32; this will check
440 whether it is a shared cursor/icon; if not it will call
443 if ( Callout.DestroyIcon32 )
444 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
446 retv = GlobalFree16( handle );
454 /**********************************************************************
455 * FindResource16 (KERNEL.60)
457 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
459 LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
460 LPCSTR typeStr = HIWORD(type)? PTR_SEG_TO_LIN(type) : (LPCSTR)type;
462 return RES_FindResource( hModule, typeStr, nameStr,
463 WINE_LanguageId, FALSE, TRUE );
466 /**********************************************************************
467 * FindResourceA (KERNEL32.128)
469 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
471 return RES_FindResource( hModule, type, name,
472 WINE_LanguageId, FALSE, FALSE );
475 /**********************************************************************
476 * FindResourceExA (KERNEL32.129)
478 HANDLE WINAPI FindResourceExA( HMODULE hModule,
479 LPCSTR type, LPCSTR name, WORD lang )
481 return RES_FindResource( hModule, type, name,
482 lang, FALSE, FALSE );
485 /**********************************************************************
486 * FindResourceExW (KERNEL32.130)
488 HRSRC WINAPI FindResourceExW( HMODULE hModule,
489 LPCWSTR type, LPCWSTR name, WORD lang )
491 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
495 /**********************************************************************
496 * FindResourceW (KERNEL32.131)
498 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
500 return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name,
501 WINE_LanguageId, TRUE, FALSE );
504 /**********************************************************************
505 * LoadResource16 (KERNEL.61)
507 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
509 return RES_LoadResource( hModule, hRsrc, TRUE );
512 /**********************************************************************
513 * LoadResource (KERNEL32.370)
515 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
517 return RES_LoadResource( hModule, hRsrc, FALSE );
520 /**********************************************************************
521 * LockResource16 (KERNEL.62)
523 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
525 return (SEGPTR)RES_LockResource( handle, TRUE );
527 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
529 return RES_LockResource( handle, FALSE );
532 /**********************************************************************
533 * LockResource (KERNEL32.384)
535 LPVOID WINAPI LockResource( HGLOBAL handle )
537 return RES_LockResource( handle, FALSE );
540 /**********************************************************************
541 * FreeResource16 (KERNEL.63)
543 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
545 return RES_FreeResource( handle );
548 /**********************************************************************
549 * FreeResource (KERNEL32.145)
551 BOOL WINAPI FreeResource( HGLOBAL handle )
553 return RES_FreeResource( handle );
556 /**********************************************************************
557 * AccessResource16 (KERNEL.64)
559 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
561 return RES_AccessResource( hModule, hRsrc, TRUE );
564 /**********************************************************************
565 * AccessResource (KERNEL32.64)
567 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
569 return RES_AccessResource( hModule, hRsrc, FALSE );
572 /**********************************************************************
573 * SizeofResource16 (KERNEL.65)
575 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
577 return RES_SizeofResource( hModule, hRsrc, TRUE );
580 /**********************************************************************
581 * SizeofResource (KERNEL32.522)
583 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
585 return RES_SizeofResource( hModule, hRsrc, FALSE );
590 /**********************************************************************
591 * LoadAccelerators16 [USER.177]
593 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
597 if (HIWORD(lpTableName))
598 TRACE_(accel)("%04x '%s'\n",
599 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
601 TRACE_(accel)("%04x %04x\n",
602 instance, LOWORD(lpTableName) );
604 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
605 WARN_(accel)("couldn't find accelerator table resource\n");
609 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
610 return LoadResource16(instance,hRsrc);
613 /**********************************************************************
614 * LoadAccelerators32W [USER.177]
615 * The image layout seems to look like this (not 100% sure):
616 * 00: BYTE type type of accelerator
617 * 01: BYTE pad (to WORD boundary)
620 * 06: WORD pad (to DWORD boundary)
622 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
625 HACCEL hMem,hRetval=0;
628 if (HIWORD(lpTableName))
629 TRACE_(accel)("%p '%s'\n",
630 (LPVOID)instance, (char *)( lpTableName ) );
632 TRACE_(accel)("%p 0x%04x\n",
633 (LPVOID)instance, LOWORD(lpTableName) );
635 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
637 WARN_(accel)("couldn't find accelerator table resource\n");
639 hMem = LoadResource( instance, hRsrc );
640 size = SizeofResource( instance, hRsrc );
641 if(size>=sizeof(PE_ACCEL))
643 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
645 int i,nrofaccells = size/sizeof(PE_ACCEL);
647 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
648 accel16 = (LPACCEL16)GlobalLock16(hRetval);
649 for (i=0;i<nrofaccells;i++) {
650 accel16[i].fVirt = accel_table[i].fVirt;
651 accel16[i].key = accel_table[i].key;
652 accel16[i].cmd = accel_table[i].cmd;
654 accel16[i-1].fVirt |= 0x80;
657 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
661 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
665 if (HIWORD(lpTableName))
666 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
668 uni = (LPWSTR)lpTableName;
669 result = LoadAcceleratorsW(instance,uni);
670 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
674 /**********************************************************************
675 * CopyAcceleratorTable32A (USER32.58)
677 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
679 return CopyAcceleratorTableW(src, dst, entries);
682 /**********************************************************************
683 * CopyAcceleratorTable32W (USER32.59)
685 * By mortene@pvv.org 980321
687 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
691 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
694 /* Do parameter checking to avoid the explosions and the screaming
695 as far as possible. */
696 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
697 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
698 (LPVOID)src, (LPVOID)dst, entries);
701 xsize = GlobalSize16(src)/sizeof(ACCEL16);
702 if (xsize>entries) entries=xsize;
706 /* Spit out some debugging information. */
707 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
708 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
710 /* Copy data to the destination structure array (if dst == NULL,
711 we're just supposed to count the number of entries). */
713 dst[i].fVirt = accel[i].fVirt;
714 dst[i].key = accel[i].key;
715 dst[i].cmd = accel[i].cmd;
717 /* Check if we've reached the end of the application supplied
718 accelerator table. */
720 /* Turn off the high order bit, just in case. */
721 dst[i].fVirt &= 0x7f;
726 /* The highest order bit seems to mark the end of the accelerator
727 resource table, but not always. Use GlobalSize() check too. */
728 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
736 /*********************************************************************
737 * CreateAcceleratorTable (USER32.64)
739 * By mortene@pvv.org 980321
741 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
747 /* Do parameter checking just in case someone's trying to be
750 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
752 SetLastError(ERROR_INVALID_PARAMETER);
755 FIXME_(accel)("should check that the accelerator descriptions are valid,"
756 " return NULL and SetLastError() if not.\n");
759 /* Allocate memory and copy the table. */
760 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
762 TRACE_(accel)("handle %x\n", hAccel);
764 ERR_(accel)("Out of memory.\n");
765 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
768 accel = GlobalLock16(hAccel);
769 for (i=0;i<cEntries;i++) {
770 accel[i].fVirt = lpaccel[i].fVirt;
771 accel[i].key = lpaccel[i].key;
772 accel[i].cmd = lpaccel[i].cmd;
774 /* Set the end-of-table terminator. */
775 accel[cEntries-1].fVirt |= 0x80;
777 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
781 /*********************************************************************
782 * CreateAcceleratorTableW (USER32.64)
786 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
793 /* Do parameter checking just in case someone's trying to be
796 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
798 SetLastError(ERROR_INVALID_PARAMETER);
801 FIXME_(accel)("should check that the accelerator descriptions are valid,"
802 " return NULL and SetLastError() if not.\n");
805 /* Allocate memory and copy the table. */
806 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
808 TRACE_(accel)("handle %x\n", hAccel);
810 ERR_(accel)("Out of memory.\n");
811 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
814 accel = GlobalLock16(hAccel);
817 for (i=0;i<cEntries;i++) {
818 accel[i].fVirt = lpaccel[i].fVirt;
819 if( !(accel[i].fVirt & FVIRTKEY) ) {
820 ckey = (char) lpaccel[i].key;
821 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
822 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
825 accel[i].key = lpaccel[i].key;
826 accel[i].cmd = lpaccel[i].cmd;
829 /* Set the end-of-table terminator. */
830 accel[cEntries-1].fVirt |= 0x80;
832 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
836 /******************************************************************************
837 * DestroyAcceleratorTable [USER32.130]
838 * Destroys an accelerator table
841 * By mortene@pvv.org 980321
844 * handle [I] Handle to accelerator table
848 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
850 return GlobalFree16(handle);
853 /**********************************************************************
856 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
857 LPSTR buffer, INT16 buflen )
865 TRACE_(resource)("inst=%04x id=%04x buff=%08x len=%d\n",
866 instance, resource_id, (int) buffer, buflen);
868 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
869 if (!hrsrc) return 0;
870 hmem = LoadResource16( instance, hrsrc );
873 p = LockResource16(hmem);
874 string_num = resource_id & 0x000f;
875 for (i = 0; i < string_num; i++)
878 TRACE_(resource)("strlen = %d\n", (int)*p );
880 if (buffer == NULL) return *p;
881 i = MIN(buflen - 1, *p);
883 memcpy(buffer, p + 1, i);
890 WARN_(resource)("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
892 FreeResource16( hmem );
894 TRACE_(resource)("'%s' loaded !\n", buffer);
898 /**********************************************************************
899 * LoadString32W (USER32.376)
901 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
902 LPWSTR buffer, INT buflen )
910 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
911 resource_id = (UINT)(-((INT)resource_id));
912 TRACE_(resource)("instance = %04x, id = %04x, buffer = %08x, "
913 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
915 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
917 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
919 if (!hrsrc) return 0;
920 hmem = LoadResource( instance, hrsrc );
923 p = LockResource(hmem);
924 string_num = resource_id & 0x000f;
925 for (i = 0; i < string_num; i++)
928 TRACE_(resource)("strlen = %d\n", (int)*p );
930 if (buffer == NULL) return *p;
931 i = MIN(buflen - 1, *p);
933 memcpy(buffer, p + 1, i * sizeof (WCHAR));
934 buffer[i] = (WCHAR) 0;
937 buffer[0] = (WCHAR) 0;
941 WARN_(resource)("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
945 TRACE_(resource)("%s loaded !\n", debugstr_w(buffer));
949 /**********************************************************************
950 * LoadString32A (USER32.375)
952 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
953 LPSTR buffer, INT buflen )
956 LPWSTR buffer2 = NULL;
957 if (buffer && buflen)
958 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
959 retval = LoadStringW(instance,resource_id,buffer2,buflen);
964 lstrcpynWtoA( buffer, buffer2, buflen );
965 retval = lstrlenA( buffer );
969 HeapFree( GetProcessHeap(), 0, buffer2 );
974 /* Messages...used by FormatMessage32* (KERNEL32.something)
976 * They can be specified either directly or using a message ID and
977 * loading them from the resource.
979 * The resourcedata has following format:
981 * 0: DWORD nrofentries
982 * nrofentries * subentry:
983 * 0: DWORD firstentry
985 * 8: DWORD offset from start to the stringentries
987 * (lastentry-firstentry) * stringentry:
988 * 0: WORD len (0 marks end)
991 * (stringentry i of a subentry refers to the ID 'firstentry+i')
993 * Yes, ANSI strings in win32 resources. Go figure.
996 /**********************************************************************
997 * LoadMessage32A (internal)
999 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
1000 LPSTR buffer, INT buflen )
1004 PMESSAGE_RESOURCE_DATA mrd;
1005 PMESSAGE_RESOURCE_BLOCK mrb;
1006 PMESSAGE_RESOURCE_ENTRY mre;
1009 TRACE_(resource)("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
1011 /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
1012 hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
1013 if (!hrsrc) return 0;
1014 hmem = LoadResource( instance, hrsrc );
1015 if (!hmem) return 0;
1017 mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
1019 mrb = &(mrd->Blocks[0]);
1020 for (i=mrd->NumberOfBlocks;i--;) {
1021 if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
1022 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
1033 mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
1036 TRACE_(resource)(" - strlen=%d\n",slen);
1037 i = MIN(buflen - 1, slen);
1041 lstrcpynA(buffer,(char*)mre->Text,i);
1050 TRACE_(resource)("'%s' copied !\n", buffer);
1054 /**********************************************************************
1055 * LoadMessage32W (internal)
1057 INT WINAPI LoadMessageW( HMODULE instance, UINT id, WORD lang,
1058 LPWSTR buffer, INT buflen )
1061 LPSTR buffer2 = NULL;
1062 if (buffer && buflen)
1063 buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
1064 retval = LoadMessageA(instance,id,lang,buffer2,buflen);
1068 lstrcpynAtoW( buffer, buffer2, buflen );
1069 retval = lstrlenW( buffer );
1071 HeapFree( GetProcessHeap(), 0, buffer2 );
1077 /**********************************************************************
1078 * EnumResourceTypesA (KERNEL32.90)
1080 BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
1083 /* FIXME: move WINE_MODREF stuff here */
1084 return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
1087 /**********************************************************************
1088 * EnumResourceTypesW (KERNEL32.91)
1090 BOOL WINAPI EnumResourceTypesW( HMODULE hmodule,ENUMRESTYPEPROCW lpfun,
1093 /* FIXME: move WINE_MODREF stuff here */
1094 return PE_EnumResourceTypesW(hmodule,lpfun,lParam);
1097 /**********************************************************************
1098 * EnumResourceNamesA (KERNEL32.88)
1100 BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
1101 ENUMRESNAMEPROCA lpfun, LONG lParam )
1103 /* FIXME: move WINE_MODREF stuff here */
1104 return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
1106 /**********************************************************************
1107 * EnumResourceNamesW (KERNEL32.89)
1109 BOOL WINAPI EnumResourceNamesW( HMODULE hmodule, LPCWSTR type,
1110 ENUMRESNAMEPROCW lpfun, LONG lParam )
1112 /* FIXME: move WINE_MODREF stuff here */
1113 return PE_EnumResourceNamesW(hmodule,type,lpfun,lParam);
1116 /**********************************************************************
1117 * EnumResourceLanguagesA (KERNEL32.86)
1119 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
1120 LPCSTR name, ENUMRESLANGPROCA lpfun,
1123 /* FIXME: move WINE_MODREF stuff here */
1124 return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
1126 /**********************************************************************
1127 * EnumResourceLanguagesW (KERNEL32.87)
1129 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmodule, LPCWSTR type,
1130 LPCWSTR name, ENUMRESLANGPROCW lpfun,
1133 /* FIXME: move WINE_MODREF stuff here */
1134 return PE_EnumResourceLanguagesW(hmodule,type,name,lpfun,lParam);