4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
11 #include <sys/types.h>
28 #define PrintId(name) \
29 if (HIWORD((DWORD)name)) \
30 dprintf_resource( stddeb, "'%s'", (char *)PTR_SEG_TO_LIN(name)); \
32 dprintf_resource( stddeb, "#%04x", LOWORD(name));
35 /**********************************************************************
36 * FindResource (KERNEL.60)
38 HRSRC
FindResource( HMODULE hModule
, SEGPTR name
, SEGPTR type
)
42 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
43 dprintf_resource(stddeb
, "FindResource: module="NPFMT
" type=", hModule
);
45 if (HIWORD(name
)) /* Check for '#xxx' name */
47 char *ptr
= PTR_SEG_TO_LIN( name
);
49 if (!(name
= (SEGPTR
)atoi( ptr
+ 1 ))) return 0;
52 dprintf_resource( stddeb
, " name=" );
54 dprintf_resource( stddeb
, "\n" );
55 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
60 return NE_FindResource( hModule
, type
, name
);
67 return LIBRES_FindResource( hModule
, name
, type
);
72 /**********************************************************************
73 * LoadResource (KERNEL.61)
75 HGLOBAL
LoadResource( HMODULE hModule
, HRSRC hRsrc
)
79 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
80 dprintf_resource(stddeb
, "LoadResource: module="NPFMT
" res="NPFMT
"\n",
83 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
88 return NE_LoadResource( hModule
, hRsrc
);
95 return LIBRES_LoadResource( hModule
, hRsrc
);
100 /**********************************************************************
101 * LockResource (KERNEL.62)
104 SEGPTR
WIN16_LockResource( HGLOBAL handle
)
110 dprintf_resource(stddeb
, "LockResource: handle="NPFMT
"\n", handle
);
111 if (!handle
) return (SEGPTR
)0;
112 hModule
= GetExePtr( handle
);
113 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
117 return NE_LockResource( hModule
, handle
);
124 return LIBRES_LockResource( handle
);
129 LPVOID
LockResource( HGLOBAL handle
)
135 dprintf_resource(stddeb
, "LockResource: handle="NPFMT
"\n", handle
);
136 if (!handle
) return NULL
;
137 hModule
= GetExePtr( handle
);
138 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
142 return (LPSTR
)PTR_SEG_TO_LIN( NE_LockResource( hModule
, handle
) );
149 return LIBRES_LockResource( handle
);
154 /**********************************************************************
155 * FreeResource (KERNEL.63)
157 BOOL
FreeResource( HGLOBAL handle
)
163 dprintf_resource(stddeb
, "FreeResource: handle="NPFMT
"\n", handle
);
164 if (!handle
) return FALSE
;
165 hModule
= GetExePtr( handle
);
166 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
170 return NE_FreeResource( hModule
, handle
);
177 return LIBRES_FreeResource( handle
);
182 /**********************************************************************
183 * AccessResource (KERNEL.64)
185 INT
AccessResource( HINSTANCE hModule
, HRSRC hRsrc
)
189 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
190 dprintf_resource(stddeb
, "AccessResource: module="NPFMT
" res="NPFMT
"\n",
192 if (!hRsrc
) return 0;
193 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
198 return NE_AccessResource( hModule
, hRsrc
);
205 return LIBRES_AccessResource( hModule
, hRsrc
);
210 /**********************************************************************
211 * SizeofResource (KERNEL.65)
213 DWORD
SizeofResource( HMODULE hModule
, HRSRC hRsrc
)
217 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
218 dprintf_resource(stddeb
, "SizeofResource: module="NPFMT
" res="NPFMT
"\n",
220 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
225 return NE_SizeofResource( hModule
, hRsrc
);
232 return LIBRES_SizeofResource( hModule
, hRsrc
);
237 /**********************************************************************
238 * AllocResource (KERNEL.66)
240 HGLOBAL
AllocResource( HMODULE hModule
, HRSRC hRsrc
, DWORD size
)
244 hModule
= GetExePtr( hModule
); /* In case we were passed an hInstance */
245 dprintf_resource(stddeb
, "AllocResource: module="NPFMT
" res="NPFMT
" size=%ld\n",
246 hModule
, hRsrc
, size
);
247 if (!hRsrc
) return 0;
248 if (!(pModule
= (WORD
*)GlobalLock( hModule
))) return 0;
253 return NE_AllocResource( hModule
, hRsrc
, size
);
260 return LIBRES_AllocResource( hModule
, hRsrc
, size
);
264 /**********************************************************************
265 * DirectResAlloc (KERNEL.168)
267 * Check Schulman, p. 232 for details
269 HANDLE
DirectResAlloc(HANDLE hInstance
, WORD wType
, WORD wSize
)
271 dprintf_resource(stddeb
,"DirectResAlloc("NPFMT
",%x,%x)\n",hInstance
,wType
,wSize
);
272 hInstance
= GetExePtr(hInstance
);
273 if(!hInstance
)return 0;
274 if(wType
!= 0x10) /* 0x10 is the only observed value, passed from
275 CreateCursorIndirect. */
276 fprintf(stderr
, "DirectResAlloc: wType = %x\n", wType
);
277 return GLOBAL_Alloc(GMEM_MOVEABLE
, wSize
, hInstance
, FALSE
, FALSE
, FALSE
);
281 /**********************************************************************
282 * LoadAccelerators [USER.177]
284 HANDLE
LoadAccelerators(HANDLE instance
, SEGPTR lpTableName
)
290 ACCELHEADER
*lpAccelTbl
;
293 if (HIWORD(lpTableName
))
294 dprintf_accel( stddeb
, "LoadAccelerators: "NPFMT
" '%s'\n",
295 instance
, (char *)PTR_SEG_TO_LIN( lpTableName
) );
297 dprintf_accel( stddeb
, "LoadAccelerators: "NPFMT
" %04x\n",
298 instance
, LOWORD(lpTableName
) );
300 if (!(hRsrc
= FindResource( instance
, lpTableName
, RT_ACCELERATOR
)))
302 if (!(rsc_mem
= LoadResource( instance
, hRsrc
))) return 0;
304 lp
= (BYTE
*)LockResource(rsc_mem
);
305 n
= SizeofResource( instance
, hRsrc
) / sizeof(ACCELENTRY
);
306 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
307 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
308 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
309 lpAccelTbl
->wCount
= 0;
310 for (i
= 0; i
< n
; i
++) {
311 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
312 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
314 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
316 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
317 dprintf_accel(stddeb
,
318 "Accelerator #%u / event=%04X id=%04X type=%02X \n",
319 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
320 lpAccelTbl
->tbl
[i
].type
);
321 lpAccelTbl
->wCount
++;
323 GlobalUnlock(hAccel
);
324 FreeResource( rsc_mem
);
328 /**********************************************************************
329 * TranslateAccelerator [USER.178]
331 int TranslateAccelerator(HWND hWnd
, HANDLE hAccel
, LPMSG msg
)
333 ACCELHEADER
*lpAccelTbl
;
336 if (hAccel
== 0 || msg
== NULL
) return 0;
337 if (msg
->message
!= WM_KEYDOWN
&&
338 msg
->message
!= WM_KEYUP
&&
339 msg
->message
!= WM_SYSKEYDOWN
&&
340 msg
->message
!= WM_SYSKEYUP
&&
341 msg
->message
!= WM_CHAR
) return 0;
343 dprintf_accel(stddeb
, "TranslateAccelerators hAccel="NPFMT
" !\n", hAccel
);
345 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
346 for (i
= 0; i
< lpAccelTbl
->wCount
; i
++) {
347 if(lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) {
348 if(msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
349 (msg
->message
== WM_KEYDOWN
|| msg
->message
== WM_SYSKEYDOWN
)) {
352 if(GetKeyState(VK_SHIFT
) & 0xf) mask
|= SHIFT_ACCEL
;
353 if(GetKeyState(VK_CONTROL
) & 0xf) mask
|= CONTROL_ACCEL
;
354 if(GetKeyState(VK_MENU
) & 0xf) mask
|= ALT_ACCEL
;
355 if(mask
== (lpAccelTbl
->tbl
[i
].type
&
356 (SHIFT_ACCEL
| CONTROL_ACCEL
| ALT_ACCEL
))) {
357 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
,
359 GlobalUnlock(hAccel
);
362 if (msg
->message
== WM_KEYUP
|| msg
->message
== WM_SYSKEYUP
)
367 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
368 msg
->message
== WM_CHAR
) {
369 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
370 GlobalUnlock(hAccel
);
375 GlobalUnlock(hAccel
);
379 /**********************************************************************
383 LoadString(HANDLE instance
, WORD resource_id
, LPSTR buffer
, int buflen
)
390 dprintf_resource(stddeb
, "LoadString: instance = "NPFMT
", id = %04x, buffer = %08x, "
391 "length = %d\n", instance
, resource_id
, (int) buffer
, buflen
);
393 hrsrc
= FindResource( instance
, (SEGPTR
)((resource_id
>>4)+1), RT_STRING
);
394 if (!hrsrc
) return 0;
395 hmem
= LoadResource( instance
, hrsrc
);
398 p
= LockResource(hmem
);
399 string_num
= resource_id
& 0x000f;
400 for (i
= 0; i
< string_num
; i
++)
403 dprintf_resource( stddeb
, "strlen = %d\n", (int)*p
);
405 i
= MIN(buflen
- 1, *p
);
409 memcpy(buffer
, p
+ 1, i
);
416 fprintf(stderr
,"LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
417 fprintf(stderr
,"LoadString // and try to obtain string '%s'\n", p
+ 1);
419 FreeResource( hmem
);
421 dprintf_resource(stddeb
,"LoadString // '%s' copied !\n", buffer
);