4 * Copyright 1995 Thomas Sandford
6 * Based on the Win16 resource handling code in loader/resource.c
7 * Copyright 1993 Robert J. Amstadt
8 * Copyright 1995 Alexandre Julliard
10 * This is not even at ALPHA level yet. Don't expect it to work!
13 #include <sys/types.h>
19 #include "resource32.h"
26 int language
= 0x0409;
28 #define PrintId(name) \
29 if (HIWORD((DWORD)name)) \
30 dprintf_resource( stddeb, "'%s'", name); \
32 dprintf_resource( stddeb, "#%04x", LOWORD(name));
34 /**********************************************************************
37 * Helper function - goes down one level of PE resource tree
40 PIMAGE_RESOURCE_DIRECTORY
GetResDirEntry(PIMAGE_RESOURCE_DIRECTORY resdirptr
,
45 PIMAGE_RESOURCE_DIRECTORY_ENTRY entryTable
;
48 /* FIXME: what about #xxx names? */
49 entryTable
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
) (
51 sizeof(IMAGE_RESOURCE_DIRECTORY
));
52 for (entrynum
= 0; entrynum
< resdirptr
->NumberOfNamedEntries
; entrynum
++)
56 entryTable
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
) (
58 sizeof(IMAGE_RESOURCE_DIRECTORY
) +
59 resdirptr
->NumberOfNamedEntries
* sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY
));
60 for (entrynum
= 0; entrynum
< resdirptr
->NumberOfIdEntries
; entrynum
++)
61 if ((DWORD
)entryTable
[entrynum
].Name
== (DWORD
)name
)
62 return (PIMAGE_RESOURCE_DIRECTORY
) (
64 (entryTable
[entrynum
].OffsetToData
& 0x7fffffff));
69 /**********************************************************************
70 * FindResource (KERNEL.60)
72 HANDLE32
FindResource32( HINSTANCE hModule
, LPCTSTR name
, LPCTSTR type
)
75 struct w_files
*wptr
= wine_files
;
76 PIMAGE_RESOURCE_DIRECTORY resdirptr
;
80 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
82 dprintf_resource(stddeb
, "FindResource: module=%08x type=", hModule
);
84 dprintf_resource( stddeb
, " name=" );
86 dprintf_resource( stddeb
, "\n" );
87 while (wptr
!= NULL
&& (wptr
->hModule
!= hModule
))
89 if ((wptr
== NULL
) || (wptr
->pe
== NULL
) || (wptr
->pe
->pe_resource
== NULL
))
91 resdirptr
= (PIMAGE_RESOURCE_DIRECTORY
) wptr
->pe
->pe_resource
;
92 root
= (DWORD
) resdirptr
;
93 if ((resdirptr
= GetResDirEntry(resdirptr
, type
, root
)) == NULL
)
95 if ((resdirptr
= GetResDirEntry(resdirptr
, name
, root
)) == NULL
)
97 return (HANDLE32
) GetResDirEntry(resdirptr
, (LPCSTR
)language
, root
);
99 return LIBRES_FindResource( hModule
, name
, type
);
104 /**********************************************************************
105 * LoadResource (KERNEL.61)
107 HANDLE32
LoadResource32( HINSTANCE hModule
, HANDLE32 hRsrc
)
110 struct w_files
*wptr
= wine_files
;
113 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
115 dprintf_resource(stddeb
, "LoadResource: module="NPFMT
" res="NPFMT
"\n",
117 if (!hRsrc
) return 0;
118 while (wptr
!= NULL
&& (wptr
->hModule
!= hModule
))
120 if ((wptr
== NULL
) || (wptr
->pe
== NULL
) || (wptr
->pe
->pe_resource
== NULL
))
122 return (HANDLE32
) (wptr
->load_addr
+((PIMAGE_RESOURCE_DATA_ENTRY
)hRsrc
)->OffsetToData
);
124 return LIBRES_LoadResource( hModule
, hRsrc
);
129 /**********************************************************************
130 * LockResource (KERNEL.62)
132 LPVOID
LockResource32( HANDLE32 handle
)
134 return (LPVOID
) handle
;
137 /**********************************************************************
138 * FreeResource (KERNEL.63)
140 BOOL
FreeResource32( HANDLE32 handle
)
142 /* no longer used in Win32 */
146 /**********************************************************************
147 * AccessResource (KERNEL.64)
149 INT
AccessResource32( HINSTANCE hModule
, HRSRC hRsrc
)
153 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
154 dprintf_resource(stddeb
, "AccessResource: module="NPFMT
" res="NPFMT
"\n",
156 if (!hRsrc
) return 0;
157 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
162 return NE_AccessResource( hModule
, hRsrc
);
169 return LIBRES_AccessResource( hModule
, hRsrc
);
174 /**********************************************************************
175 * SizeofResource (KERNEL.65)
177 DWORD
SizeofResource32( HINSTANCE hModule
, HRSRC hRsrc
)
181 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
182 dprintf_resource(stddeb
, "SizeofResource: module="NPFMT
" res="NPFMT
"\n",
184 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
189 return NE_SizeofResource( hModule
, hRsrc
);
196 return LIBRES_SizeofResource( hModule
, hRsrc
);
200 /**********************************************************************
201 * LoadAccelerators [USER.177]
203 HANDLE
LoadAccelerators32(HINSTANCE instance
, LPCTSTR lpTableName
)
209 ACCELHEADER
*lpAccelTbl
;
212 if (HIWORD(lpTableName
))
213 dprintf_accel( stddeb
, "LoadAccelerators: "NPFMT
" '%s'\n",
214 instance
, (char *)( lpTableName
) );
216 dprintf_accel( stddeb
, "LoadAccelerators: "NPFMT
" %04x\n",
217 instance
, LOWORD(lpTableName
) );
219 if (!(hRsrc
= FindResource( instance
, lpTableName
, RT_ACCELERATOR
)))
221 if (!(rsc_mem
= LoadResource( instance
, hRsrc
))) return 0;
223 lp
= (BYTE
*)LockResource(rsc_mem
);
224 n
= SizeofResource( instance
, hRsrc
) / sizeof(ACCELENTRY
);
225 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
226 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
227 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
228 lpAccelTbl
->wCount
= 0;
229 for (i
= 0; i
< n
; i
++) {
230 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
231 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
233 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
235 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
236 dprintf_accel(stddeb
,
237 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
238 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
239 lpAccelTbl
->tbl
[i
].type
);
240 lpAccelTbl
->wCount
++;
242 GlobalUnlock(hAccel
);
243 FreeResource( rsc_mem
);
247 /**********************************************************************
251 LoadString32(HINSTANCE instance
, DWORD resource_id
, LPTSTR buffer
, int buflen
)
253 HANDLE32 hmem
, hrsrc
;
258 dprintf_resource(stddeb
, "LoadString: instance = "NPFMT
", id = %04x, buffer = %08x, "
259 "length = %d\n", instance
, resource_id
, (int) buffer
, buflen
);
261 hrsrc
= FindResource32( instance
, (resource_id
>>4)+1, RT_STRING
);
262 if (!hrsrc
) return 0;
263 hmem
= LoadResource32( instance
, hrsrc
);
266 p
= LockResource32(hmem
);
267 string_num
= resource_id
& 0x000f;
268 for (i
= 0; i
< string_num
; i
++)
271 dprintf_resource( stddeb
, "strlen = %d\n", (int)*p
);
273 i
= MIN(buflen
- 1, *p
);
277 memcpy(buffer
, p
+ 1, i
* sizeof (WCHAR
));
278 buffer
[i
] = (WCHAR
) 0;
281 buffer
[0] = (WCHAR
) 0;
284 fprintf(stderr
,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
285 fprintf(stderr
,"LoadString // and try to obtain string '%s'\n", p
+ 1);
287 dprintf_resource(stddeb
,"LoadString // '%s' copied !\n", buffer
);
291 /**********************************************************************
295 LoadStringA32(HINSTANCE instance
, DWORD resource_id
, LPSTR buffer
, int buflen
)
297 WCHAR
*buffer2
= xmalloc(buflen
*2);
298 int retval
= LoadString32(instance
, resource_id
, buffer2
, buflen
);
301 *buffer
++ = (char) *buffer2
++;
306 HICON
LoadIconW32(HINSTANCE hisnt
, LPCTSTR lpszIcon
)
309 return LoadIcon(0, IDI_APPLICATION
);
312 HICON
LoadIconA32(HINSTANCE hinst
, LPCTSTR lpszIcon
)
315 return LoadIconW32(hinst
, lpszIcon
);
317 /**********************************************************************
320 HBITMAP
LoadBitmapW32( HANDLE instance
, LPCTSTR name
)
328 if (!instance
) /* OEM bitmap */
330 if (HIWORD((int)name
)) return 0;
331 return OBM_LoadBitmap( LOWORD((int)name
) );
334 if (!(hRsrc
= FindResource32( instance
, name
, RT_BITMAP
))) return 0;
335 if (!(handle
= LoadResource32( instance
, hRsrc
))) return 0;
337 info
= (BITMAPINFO
*)LockResource32( handle
);
338 if ((hdc
= GetDC(0)) != 0)
340 char *bits
= (char *)info
+ DIB_BitmapInfoSize( info
, DIB_RGB_COLORS
);
341 hbitmap
= CreateDIBitmap( hdc
, &info
->bmiHeader
, CBM_INIT
,
342 bits
, info
, DIB_RGB_COLORS
);
347 /**********************************************************************
350 HBITMAP
LoadBitmapA32( HANDLE instance
, LPCTSTR name
)
352 return LoadBitmapW32(instance
, name
);