Release 960114
[wine/gsoc-2012-control.git] / win32 / resource.c
blob5add44060393bc663554a5ff2a779cb83f563da6
1 /*
2 * Win32 Resources
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>
14 #include "wintypes.h"
15 #include "windows.h"
16 #include "kernel32.h"
17 #include "pe_image.h"
18 #include "handle32.h"
19 #include "resource32.h"
20 #include "neexe.h"
21 #include "accel.h"
22 #include "xmalloc.h"
23 #include "stddebug.h"
24 #include "debug.h"
26 int language = 0x0409;
28 #define PrintId(name) \
29 if (HIWORD((DWORD)name)) \
30 dprintf_resource( stddeb, "'%s'", name); \
31 else \
32 dprintf_resource( stddeb, "#%04x", LOWORD(name));
34 /**********************************************************************
35 * GetResDirEntry
37 * Helper function - goes down one level of PE resource tree
40 PIMAGE_RESOURCE_DIRECTORY GetResDirEntry(PIMAGE_RESOURCE_DIRECTORY resdirptr,
41 LPCSTR name,
42 DWORD root)
44 int entrynum;
45 PIMAGE_RESOURCE_DIRECTORY_ENTRY entryTable;
47 if (HIWORD(name)) {
48 /* FIXME: what about #xxx names? */
49 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
50 (BYTE *) resdirptr +
51 sizeof(IMAGE_RESOURCE_DIRECTORY));
52 for (entrynum = 0; entrynum < resdirptr->NumberOfNamedEntries; entrynum++)
53 /* do what??? */ ;
54 return NULL;
55 } else {
56 entryTable = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (
57 (BYTE *) resdirptr +
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) (
63 root +
64 (entryTable[entrynum].OffsetToData & 0x7fffffff));
65 return NULL;
69 /**********************************************************************
70 * FindResource (KERNEL.60)
72 HANDLE32 FindResource32( HINSTANCE hModule, LPCTSTR name, LPCTSTR type )
74 #ifndef WINELIB
75 struct w_files *wptr = wine_files;
76 PIMAGE_RESOURCE_DIRECTORY resdirptr;
77 DWORD root;
79 #if 0
80 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
81 #endif
82 dprintf_resource(stddeb, "FindResource: module=%08x type=", hModule );
83 PrintId( type );
84 dprintf_resource( stddeb, " name=" );
85 PrintId( name );
86 dprintf_resource( stddeb, "\n" );
87 while (wptr != NULL && (wptr->hModule != hModule))
88 wptr = wptr->next;
89 if ((wptr == NULL) || (wptr->pe == NULL) || (wptr->pe->pe_resource == NULL))
90 return 0;
91 resdirptr = (PIMAGE_RESOURCE_DIRECTORY) wptr->pe->pe_resource;
92 root = (DWORD) resdirptr;
93 if ((resdirptr = GetResDirEntry(resdirptr, type, root)) == NULL)
94 return 0;
95 if ((resdirptr = GetResDirEntry(resdirptr, name, root)) == NULL)
96 return 0;
97 return (HANDLE32) GetResDirEntry(resdirptr, (LPCSTR)language, root);
98 #else
99 return LIBRES_FindResource( hModule, name, type );
100 #endif
104 /**********************************************************************
105 * LoadResource (KERNEL.61)
107 HANDLE32 LoadResource32( HINSTANCE hModule, HANDLE32 hRsrc )
109 #ifndef WINELIB
110 struct w_files *wptr = wine_files;
112 #if 0
113 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
114 #endif
115 dprintf_resource(stddeb, "LoadResource: module="NPFMT" res="NPFMT"\n",
116 hModule, hRsrc );
117 if (!hRsrc) return 0;
118 while (wptr != NULL && (wptr->hModule != hModule))
119 wptr = wptr->next;
120 if ((wptr == NULL) || (wptr->pe == NULL) || (wptr->pe->pe_resource == NULL))
121 return 0;
122 return (HANDLE32) (wptr->load_addr+((PIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->OffsetToData);
123 #else
124 return LIBRES_LoadResource( hModule, hRsrc );
125 #endif
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 */
143 return TRUE;
146 /**********************************************************************
147 * AccessResource (KERNEL.64)
149 INT AccessResource32( HINSTANCE hModule, HRSRC hRsrc )
151 WORD *pModule;
153 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
154 dprintf_resource(stddeb, "AccessResource: module="NPFMT" res="NPFMT"\n",
155 hModule, hRsrc );
156 if (!hRsrc) return 0;
157 if (!(pModule = (WORD *)GlobalLock( hModule ))) return 0;
158 #ifndef WINELIB
159 switch(*pModule)
161 case NE_SIGNATURE:
162 return NE_AccessResource( hModule, hRsrc );
163 case PE_SIGNATURE:
164 return 0;
165 default:
166 return 0;
168 #else
169 return LIBRES_AccessResource( hModule, hRsrc );
170 #endif
174 /**********************************************************************
175 * SizeofResource (KERNEL.65)
177 DWORD SizeofResource32( HINSTANCE hModule, HRSRC hRsrc )
179 WORD *pModule;
181 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
182 dprintf_resource(stddeb, "SizeofResource: module="NPFMT" res="NPFMT"\n",
183 hModule, hRsrc );
184 if (!(pModule = (WORD *)GlobalLock( hModule ))) return 0;
185 #ifndef WINELIB
186 switch(*pModule)
188 case NE_SIGNATURE:
189 return NE_SizeofResource( hModule, hRsrc );
190 case PE_SIGNATURE:
191 return 0;
192 default:
193 return 0;
195 #else
196 return LIBRES_SizeofResource( hModule, hRsrc );
197 #endif
200 /**********************************************************************
201 * LoadAccelerators [USER.177]
203 HANDLE LoadAccelerators32(HINSTANCE instance, LPCTSTR lpTableName)
205 HANDLE hAccel;
206 HANDLE rsc_mem;
207 HRSRC hRsrc;
208 BYTE *lp;
209 ACCELHEADER *lpAccelTbl;
210 int i, n;
212 if (HIWORD(lpTableName))
213 dprintf_accel( stddeb, "LoadAccelerators: "NPFMT" '%s'\n",
214 instance, (char *)( lpTableName ) );
215 else
216 dprintf_accel( stddeb, "LoadAccelerators: "NPFMT" %04x\n",
217 instance, LOWORD(lpTableName) );
219 if (!(hRsrc = FindResource( instance, lpTableName, RT_ACCELERATOR )))
220 return 0;
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);
232 lp += 2;
233 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
234 lp += 2;
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 );
244 return hAccel;
247 /**********************************************************************
248 * LoadString
251 LoadString32(HINSTANCE instance, DWORD resource_id, LPTSTR buffer, int buflen)
253 HANDLE32 hmem, hrsrc;
254 WCHAR *p;
255 int string_num;
256 int i;
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 );
264 if (!hmem) return 0;
266 p = LockResource32(hmem);
267 string_num = resource_id & 0x000f;
268 for (i = 0; i < string_num; i++)
269 p += *p + 1;
271 dprintf_resource( stddeb, "strlen = %d\n", (int)*p );
273 i = MIN(buflen - 1, *p);
274 if (buffer == NULL)
275 return i;
276 if (i > 0) {
277 memcpy(buffer, p + 1, i * sizeof (WCHAR));
278 buffer[i] = (WCHAR) 0;
279 } else {
280 if (buflen > 1) {
281 buffer[0] = (WCHAR) 0;
282 return 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);
288 return i;
291 /**********************************************************************
292 * LoadStringA
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);
300 while (*buffer2)
301 *buffer++ = (char) *buffer2++;
302 *buffer = 0;
303 return retval;
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 /**********************************************************************
318 * LoadBitmapW
320 HBITMAP LoadBitmapW32( HANDLE instance, LPCTSTR name )
322 HBITMAP hbitmap = 0;
323 HDC hdc;
324 HANDLE32 hRsrc;
325 HANDLE32 handle;
326 BITMAPINFO *info;
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 );
343 ReleaseDC( 0, hdc );
345 return hbitmap;
347 /**********************************************************************
348 * LoadBitmapA
350 HBITMAP LoadBitmapA32( HANDLE instance, LPCTSTR name )
352 return LoadBitmapW32(instance, name);