4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
29 #define PrintId(name) \
30 if (HIWORD((DWORD)name)) \
31 dprintf_resource( stddeb, "'%s'", (char *)PTR_SEG_TO_LIN(name)); \
33 dprintf_resource( stddeb, "#%04x", LOWORD(name));
36 /**********************************************************************
37 * FindResource16 (KERNEL.60)
39 HRSRC16 FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
43 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
44 dprintf_resource(stddeb, "FindResource16: module=%04x type=", hModule );
46 if (HIWORD(name)) /* Check for '#xxx' name */
48 char *ptr = PTR_SEG_TO_LIN( name );
50 if (!(name = (SEGPTR)atoi( ptr + 1 ))) return 0;
53 dprintf_resource( stddeb, " name=" );
55 dprintf_resource( stddeb, "\n" );
56 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
58 if (pModule->flags & NE_FFLAGS_WIN32)
60 fprintf(stderr,"Don't know how to FindResource16() for Win32 module\n");
63 return NE_FindResource( hModule, type, name );
65 return LIBRES_FindResource( hModule, name, type );
70 /**********************************************************************
71 * FindResource32A (KERNEL32.128)
73 HANDLE32 FindResource32A( HINSTANCE32 hModule, LPCSTR name, LPCSTR type )
78 if (HIWORD((DWORD)name)) xname = STRING32_DupAnsiToUni(name);
79 else xname = (LPWSTR)name;
80 if (HIWORD((DWORD)type)) xtype = STRING32_DupAnsiToUni(type);
81 else xtype = (LPWSTR)type;
82 ret = FindResource32W(hModule,xname,xtype);
83 if (HIWORD((DWORD)name)) free(xname);
84 if (HIWORD((DWORD)type)) free(xtype);
89 /**********************************************************************
90 * FindResource32W (KERNEL32.131)
92 HRSRC32 FindResource32W( HINSTANCE32 hModule, LPCWSTR name, LPCWSTR type )
97 /* Sometimes we get passed hModule = 0x00000000. FIXME: is GetTaskDS()
100 if (!hModule) hModule = GetTaskDS();
101 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
102 dprintf_resource(stddeb, "FindResource32W: module=%08x type=", hModule );
104 dprintf_resource( stddeb, " name=" );
106 dprintf_resource( stddeb, "\n" );
107 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
108 if (!(pModule->flags & NE_FFLAGS_WIN32)) return 0;
109 return PE_FindResource32W(hModule,name,type);
111 return LIBRES_FindResource( hModule, name, type );
116 /**********************************************************************
117 * LoadResource16 (KERNEL.61)
119 HGLOBAL16 LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
123 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
124 dprintf_resource(stddeb, "LoadResource16: module=%04x res=%04x\n",
126 if (!hRsrc) return 0;
127 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
129 if (pModule->flags & NE_FFLAGS_WIN32)
131 fprintf(stderr,"Don't know how to LoadResource16() for Win32 module\n");
134 return NE_LoadResource( hModule, hRsrc );
136 return LIBRES_LoadResource( hModule, hRsrc );
140 /**********************************************************************
141 * LoadResource32 (KERNEL32.370)
143 HGLOBAL32 LoadResource32( HINSTANCE32 hModule, HRSRC32 hRsrc )
148 if (!hModule) hModule = GetTaskDS(); /* FIXME: see FindResource32W */
149 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
150 dprintf_resource(stddeb, "LoadResource32: module=%04x res=%04x\n",
152 if (!hRsrc) return 0;
154 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
155 if (!(pModule->flags & NE_FFLAGS_WIN32))
157 fprintf(stderr,"LoadResource32: tried to load a non win32 resource.\n");
158 return 0; /* FIXME? */
160 return PE_LoadResource32(hModule,hRsrc);
162 return LIBRES_LoadResource( hModule, hRsrc );
167 /**********************************************************************
168 * LockResource (KERNEL.62)
171 SEGPTR WIN16_LockResource16(HGLOBAL16 handle)
177 dprintf_resource(stddeb, "LockResource: handle=%04x\n", handle );
178 if (!handle) return (SEGPTR)0;
179 hModule = GetExePtr( handle );
180 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
181 if (pModule->flags & NE_FFLAGS_WIN32)
183 fprintf(stderr,"Don't know how to LockResource() for Win32 module\n");
186 return NE_LockResource( hModule, handle );
188 return LIBRES_LockResource( handle );
192 /* WINELIB 16-bit version */
193 LPVOID LockResource16( HGLOBAL16 handle )
199 dprintf_resource(stddeb, "LockResource: handle=%04x\n", handle );
200 if (!handle) return NULL;
201 hModule = GetExePtr( handle );
202 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
203 if (pModule->flags & NE_FFLAGS_WIN32)
205 fprintf(stderr,"Don't know how to LockResource16() for Win32 module\n");
208 return (LPSTR)PTR_SEG_TO_LIN( NE_LockResource( hModule, handle ) );
210 return LIBRES_LockResource( handle );
215 /**********************************************************************
216 * LockResource32 (KERNEL32.384)
218 LPVOID LockResource32( HGLOBAL32 handle )
220 return (LPVOID)handle;
224 /**********************************************************************
225 * FreeResource16 (KERNEL.63)
227 BOOL16 FreeResource16( HGLOBAL16 handle )
233 dprintf_resource(stddeb, "FreeResource16: handle=%04x\n", handle );
234 if (!handle) return FALSE;
235 hModule = GetExePtr( handle );
236 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
237 if (pModule->flags & NE_FFLAGS_WIN32)
239 fprintf(stderr,"Don't know how to FreeResource16() for Win32 module\n");
242 return NE_FreeResource( hModule, handle );
244 return LIBRES_FreeResource( handle );
248 /**********************************************************************
249 * FreeResource32 (KERNEL32.145)
251 BOOL32 FreeResource32( HGLOBAL32 handle )
253 /* no longer used in Win32 */
258 /**********************************************************************
259 * AccessResource16 (KERNEL.64)
261 INT16 AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
265 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
266 dprintf_resource(stddeb, "AccessResource16: module=%04x res=%04x\n",
268 if (!hRsrc) return 0;
269 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
271 if (pModule->flags & NE_FFLAGS_WIN32)
273 fprintf(stderr,"Don't know how to AccessResource16() for Win32 module\n");
276 return NE_AccessResource( hModule, hRsrc );
278 return LIBRES_AccessResource( hModule, hRsrc );
283 /**********************************************************************
284 * AccessResource32 (KERNEL32.64)
286 INT32 AccessResource32( HINSTANCE32 hModule, HRSRC32 hRsrc )
288 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
289 dprintf_resource(stddeb, "AccessResource: module=%04x res=%04x\n",
291 if (!hRsrc) return 0;
292 fprintf(stderr,"AccessResource32: not implemented\n");
297 /**********************************************************************
298 * SizeofResource16 (KERNEL.65)
300 DWORD SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
304 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
305 dprintf_resource(stddeb, "SizeofResource16: module=%04x res=%04x\n",
307 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
309 if (pModule->flags & NE_FFLAGS_WIN32)
311 fprintf(stderr,"Don't know how to SizeOfResource16() for Win32 module\n");
314 return NE_SizeofResource( hModule, hRsrc );
316 return LIBRES_SizeofResource( hModule, hRsrc );
321 /**********************************************************************
322 * SizeofResource32 (KERNEL32.522)
324 DWORD SizeofResource32( HINSTANCE32 hModule, HRSRC32 hRsrc )
326 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
327 dprintf_resource(stddeb, "SizeofResource32: module=%04x res=%04x\n",
329 fprintf(stderr,"SizeofResource32: not implemented\n");
334 /**********************************************************************
335 * AllocResource16 (KERNEL.66)
337 HGLOBAL16 AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size )
341 hModule = GetExePtr( hModule ); /* In case we were passed an hInstance */
342 dprintf_resource(stddeb, "AllocResource: module=%04x res=%04x size=%ld\n",
343 hModule, hRsrc, size );
344 if (!hRsrc) return 0;
345 if (!(pModule = MODULE_GetPtr( hModule ))) return 0;
347 if (pModule->flags & NE_FFLAGS_WIN32)
349 fprintf(stderr,"Don't know how to AllocResource() for Win32 module\n");
352 return NE_AllocResource( hModule, hRsrc, size );
354 return LIBRES_AllocResource( hModule, hRsrc, size );
358 /**********************************************************************
359 * DirectResAlloc (KERNEL.168)
361 * Check Schulman, p. 232 for details
363 HANDLE DirectResAlloc(HANDLE hInstance, WORD wType, WORD wSize)
365 dprintf_resource(stddeb,"DirectResAlloc(%04x,%04x,%04x)\n",
366 hInstance, wType, wSize );
367 hInstance = GetExePtr(hInstance);
368 if(!hInstance)return 0;
369 if(wType != 0x10) /* 0x10 is the only observed value, passed from
370 CreateCursorIndirect. */
371 fprintf(stderr, "DirectResAlloc: wType = %x\n", wType);
372 return GLOBAL_Alloc(GMEM_MOVEABLE, wSize, hInstance, FALSE, FALSE, FALSE);
376 /**********************************************************************
377 * LoadAccelerators16 [USER.177]
379 HACCEL16 LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
385 ACCELHEADER *lpAccelTbl;
388 if (HIWORD(lpTableName))
389 dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
390 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
392 dprintf_accel( stddeb, "LoadAccelerators: %04x %04x\n",
393 instance, LOWORD(lpTableName) );
395 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR )))
397 if (!(rsc_mem = LoadResource16( instance, hRsrc ))) return 0;
399 lp = (BYTE *)LockResource16(rsc_mem);
400 n = SizeofResource16(instance,hRsrc)/sizeof(ACCELENTRY);
401 hAccel = GlobalAlloc16(GMEM_MOVEABLE,
402 sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
403 lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
404 lpAccelTbl->wCount = 0;
405 for (i = 0; i < n; i++) {
406 lpAccelTbl->tbl[i].type = *(lp++);
407 lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
409 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
411 if (lpAccelTbl->tbl[i].wEvent == 0) break;
412 dprintf_accel(stddeb,
413 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
414 i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval,
415 lpAccelTbl->tbl[i].type);
416 lpAccelTbl->wCount++;
418 GlobalUnlock16(hAccel);
419 FreeResource16( rsc_mem );
423 /**********************************************************************
424 * LoadAccelerators32W [USER.177]
426 HACCEL32 LoadAccelerators32W(HINSTANCE32 instance,LPCWSTR lpTableName)
433 ACCELHEADER *lpAccelTbl;
436 if (HIWORD(lpTableName))
437 dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
438 instance, (char *)( lpTableName ) );
440 dprintf_accel( stddeb, "LoadAccelerators: %04x %04x\n",
441 instance, LOWORD(lpTableName) );
443 if (!(hRsrc = FindResource32W( instance, lpTableName,
444 (LPCWSTR)RT_ACCELERATOR )))
446 if (!(rsc_mem = LoadResource32( instance, hRsrc ))) return 0;
448 lp = (BYTE *)LockResource32(rsc_mem);
449 n = SizeofResource32(instance,hRsrc)/sizeof(ACCELENTRY);
450 hAccel = GlobalAlloc16(GMEM_MOVEABLE,
451 sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
452 lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
453 lpAccelTbl->wCount = 0;
454 for (i = 0; i < n; i++) {
455 lpAccelTbl->tbl[i].type = *(lp++);
456 lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
458 lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
460 if (lpAccelTbl->tbl[i].wEvent == 0) break;
461 dprintf_accel(stddeb,
462 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
463 i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval,
464 lpAccelTbl->tbl[i].type);
465 lpAccelTbl->wCount++;
467 GlobalUnlock16(hAccel);
468 FreeResource32(rsc_mem);
471 fprintf(stderr,"LoadAcceleratorsW: not implemented\n");
472 return 0x100; /* Return something anyway */
476 HACCEL32 LoadAccelerators32A(HINSTANCE32 instance,LPCSTR lpTableName)
480 if (HIWORD(lpTableName))
481 uni=STRING32_DupAnsiToUni(lpTableName);
483 uni=(LPWSTR)lpTableName;
484 result=LoadAccelerators32W(instance,uni);
491 /**********************************************************************
492 * TranslateAccelerator [USER.178]
494 int TranslateAccelerator(HWND hWnd, HANDLE hAccel, LPMSG16 msg)
496 ACCELHEADER *lpAccelTbl;
499 if (hAccel == 0 || msg == NULL) return 0;
500 if (msg->message != WM_KEYDOWN &&
501 msg->message != WM_KEYUP &&
502 msg->message != WM_SYSKEYDOWN &&
503 msg->message != WM_SYSKEYUP &&
504 msg->message != WM_CHAR) return 0;
506 dprintf_accel(stddeb, "TranslateAccelerators hAccel=%04x !\n", hAccel);
508 lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
509 for (i = 0; i < lpAccelTbl->wCount; i++) {
510 if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) {
511 if(msg->wParam == lpAccelTbl->tbl[i].wEvent &&
512 (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)) {
515 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= SHIFT_ACCEL;
516 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= CONTROL_ACCEL;
517 if(GetKeyState(VK_MENU) & 0x8000) mask |= ALT_ACCEL;
518 if(mask == (lpAccelTbl->tbl[i].type &
519 (SHIFT_ACCEL | CONTROL_ACCEL | ALT_ACCEL))) {
520 SendMessage16(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval,
522 GlobalUnlock16(hAccel);
525 if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
530 if (msg->wParam == lpAccelTbl->tbl[i].wEvent &&
531 msg->message == WM_CHAR) {
532 SendMessage16(hWnd, WM_COMMAND, lpAccelTbl->tbl[i].wIDval, 0x00010000L);
533 GlobalUnlock16(hAccel);
538 GlobalUnlock16(hAccel);
542 /**********************************************************************
546 LoadString16(HINSTANCE16 instance,UINT16 resource_id,LPSTR buffer,INT16 buflen)
554 dprintf_resource(stddeb,"LoadString: inst=%04x id=%04x buff=%08x len=%d\n",
555 instance, resource_id, (int) buffer, buflen);
557 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING );
558 if (!hrsrc) return 0;
559 hmem = LoadResource16( instance, hrsrc );
562 p = LockResource16(hmem);
563 string_num = resource_id & 0x000f;
564 for (i = 0; i < string_num; i++)
567 dprintf_resource( stddeb, "strlen = %d\n", (int)*p );
569 i = MIN(buflen - 1, *p);
573 memcpy(buffer, p + 1, i);
580 fprintf(stderr,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen, *p);
581 fprintf(stderr,"LoadString // and try to obtain string '%s'\n", p + 1);
583 FreeResource16( hmem );
585 dprintf_resource(stddeb,"LoadString // '%s' copied !\n", buffer);
589 /**********************************************************************
590 * LoadString32W (USER32.375)
593 LoadString32W(HINSTANCE32 instance,UINT32 resource_id,LPWSTR buffer,int buflen)
601 dprintf_resource(stddeb, "LoadString: instance = %04x, id = %04x, buffer = %08x, "
602 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
604 hrsrc = FindResource32W( instance, (LPCWSTR)((resource_id>>4)+1),
605 (LPCWSTR)RT_STRING );
606 if (!hrsrc) return 0;
607 hmem = LoadResource32( instance, hrsrc );
610 p = LockResource32(hmem);
611 string_num = resource_id & 0x000f;
612 for (i = 0; i < string_num; i++)
615 dprintf_resource( stddeb, "strlen = %d\n", (int)*p );
617 i = MIN(buflen - 1, *p);
621 memcpy(buffer, p + 1, i * sizeof (WCHAR));
622 buffer[i] = (WCHAR) 0;
625 buffer[0] = (WCHAR) 0;
629 fprintf(stderr,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen, *p);
630 fprintf(stderr,"LoadString // and try to obtain string '%s'\n", p + 1);
634 dprintf_resource(stddeb,"LoadString // '%s' copied !\n", buffer);
639 /**********************************************************************
640 * LoadString32A (USER32.374)
643 LoadString32A(HINSTANCE32 instance,UINT32 resource_id,LPSTR buffer,int buflen)
645 LPWSTR buffer2 = (LPWSTR)xmalloc(buflen*2);
646 INT32 retval = LoadString32W(instance,resource_id,buffer2,buflen);
648 STRING32_UniToAnsi(buffer,buffer2);