4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
17 #include "stackframe.h"
19 #include "flatthunk.h"
22 #include "debugtools.h"
24 DECLARE_DEBUG_CHANNEL(relay
)
25 DECLARE_DEBUG_CHANNEL(system
)
26 DECLARE_DEBUG_CHANNEL(thunk
)
29 extern LONG
CallTo16RegisterShort(const CONTEXT86
*context
, INT offset
);
30 extern LONG
CallTo16RegisterLong(const CONTEXT86
*context
, INT offset
);
32 /* List of the 16-bit callback functions. This list is used */
33 /* by the build program to generate the file if1632/callto16.S */
35 /* ### start build ### */
36 extern WORD
THUNK_CallTo16_word_ (FARPROC16
);
37 extern LONG
THUNK_CallTo16_long_ (FARPROC16
);
38 extern WORD
THUNK_CallTo16_word_w (FARPROC16
,WORD
);
39 extern WORD
THUNK_CallTo16_word_l (FARPROC16
,LONG
);
40 extern LONG
THUNK_CallTo16_long_l (FARPROC16
,LONG
);
41 extern WORD
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
42 extern WORD
THUNK_CallTo16_word_wl (FARPROC16
,WORD
,LONG
);
43 extern WORD
THUNK_CallTo16_word_ll (FARPROC16
,LONG
,LONG
);
44 extern LONG
THUNK_CallTo16_long_ll (FARPROC16
,LONG
,LONG
);
45 extern WORD
THUNK_CallTo16_word_www (FARPROC16
,WORD
,WORD
,WORD
);
46 extern WORD
THUNK_CallTo16_word_wwl (FARPROC16
,WORD
,WORD
,LONG
);
47 extern WORD
THUNK_CallTo16_word_wlw (FARPROC16
,WORD
,LONG
,WORD
);
48 extern LONG
THUNK_CallTo16_long_wwl (FARPROC16
,WORD
,WORD
,LONG
);
49 extern LONG
THUNK_CallTo16_long_lll (FARPROC16
,LONG
,LONG
,LONG
);
50 extern WORD
THUNK_CallTo16_word_llwl (FARPROC16
,LONG
,LONG
,WORD
,LONG
);
51 extern WORD
THUNK_CallTo16_word_lwll (FARPROC16
,LONG
,WORD
,LONG
,LONG
);
52 extern WORD
THUNK_CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
53 extern WORD
THUNK_CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
54 extern WORD
THUNK_CallTo16_word_wwll (FARPROC16
,WORD
,WORD
,LONG
,LONG
);
55 extern WORD
THUNK_CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
56 extern LONG
THUNK_CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
57 extern WORD
THUNK_CallTo16_word_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
58 extern LONG
THUNK_CallTo16_long_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
59 extern WORD
THUNK_CallTo16_word_wllwl(FARPROC16
,WORD
,LONG
,LONG
,WORD
,LONG
);
60 extern WORD
THUNK_CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
61 extern LONG
THUNK_CallTo16_long_lwwll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
);
62 extern WORD
THUNK_CallTo16_word_wwlll(FARPROC16
,WORD
,WORD
,LONG
,LONG
,LONG
);
63 extern WORD
THUNK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
64 extern WORD
THUNK_CallTo16_word_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
65 extern LONG
THUNK_CallTo16_long_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
66 extern LONG
THUNK_CallTo16_long_lllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
);
67 extern LONG
THUNK_CallTo16_long_llllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
68 extern LONG
THUNK_CallTo16_long_lllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
69 extern WORD
THUNK_CallTo16_word_llwwlll(FARPROC16
,LONG
,LONG
,WORD
,WORD
,LONG
,LONG
,LONG
);
70 extern LONG
THUNK_CallTo16_word_lwwlllll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
,
72 extern LONG
THUNK_CallTo16_long_llllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,
74 extern LONG
THUNK_CallTo16_long_lllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
75 LONG
,LONG
,LONG
,LONG
,LONG
);
76 extern LONG
THUNK_CallTo16_long_llllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
77 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
78 extern LONG
THUNK_CallTo16_long_lllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
79 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
81 extern LONG
THUNK_CallTo16_long_llllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
82 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
84 extern LONG
THUNK_CallTo16_long_lwwllwlllllw(FARPROC16
,LONG
,WORD
,WORD
,LONG
,
85 LONG
,WORD
,LONG
,LONG
,LONG
,LONG
,
87 extern LONG
THUNK_CallTo16_long_lllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
88 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
90 extern LONG
THUNK_CallTo16_long_llllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
91 LONG
,LONG
,LONG
,LONG
,LONG
,
92 LONG
,LONG
,LONG
,LONG
,LONG
,
94 extern LONG
THUNK_CallTo16_word_lwwwwlwwwwllll(FARPROC16
,LONG
,WORD
,WORD
,
95 WORD
,WORD
,LONG
,WORD
,WORD
,
96 WORD
,WORD
,LONG
,LONG
,LONG
,
98 extern LONG
THUNK_CallTo16_long_lllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
99 LONG
,LONG
,LONG
,LONG
,LONG
,
100 LONG
,LONG
,LONG
,LONG
,LONG
,
102 extern LONG
THUNK_CallTo16_long_llllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
103 LONG
,LONG
,LONG
,LONG
,LONG
,
104 LONG
,LONG
,LONG
,LONG
,LONG
,
106 extern void THUNK_CallFrom16_p_long_wwwll();
107 /* ### stop build ### */
110 typedef void (*RELAY
)();
112 #include "pshpack1.h"
114 typedef struct tagTHUNK
116 BYTE popl_eax
; /* 0x58 popl %eax (return address)*/
117 BYTE pushl_func
; /* 0x68 pushl $proc */
118 FARPROC proc WINE_PACKED
;
119 BYTE pushl_eax
; /* 0x50 pushl %eax */
120 BYTE jmp
; /* 0xe9 jmp relay (relative jump)*/
121 RELAY relay WINE_PACKED
;
122 struct tagTHUNK
*next WINE_PACKED
;
127 #define DECL_THUNK(aname,aproc,arelay) \
129 aname.popl_eax = 0x58; \
130 aname.pushl_func = 0x68; \
131 aname.proc = (FARPROC) (aproc); \
132 aname.pushl_eax = 0x50; \
134 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
137 static THUNK
*firstThunk
= NULL
;
139 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
140 UINT16 msg
, WPARAM16 wParam
,
142 static BOOL WINAPI
THUNK_WOWCallback16Ex( FARPROC16
,DWORD
,DWORD
,
144 static BOOL
THUNK_ThunkletInit( void );
146 /* Callbacks function table for the emulator */
147 static const CALLBACKS_TABLE CALLBACK_EmulatorTable
=
149 (void *)CallTo16RegisterShort
, /* CallRegisterShortProc */
150 (void *)CallTo16RegisterLong
, /* CallRegisterLongProc */
151 (void*)THUNK_CallFrom16_p_long_wwwll
, /* CallFrom16WndProc */
152 THUNK_CallWndProc16
, /* CallWndProc */
153 (void *)THUNK_CallTo16_long_lwwll
, /* CallDriverProc */
154 (void *)THUNK_CallTo16_word_wwlll
, /* CallDriverCallback */
155 (void *)THUNK_CallTo16_word_wwlll
, /* CallTimeFuncProc */
156 (void *)THUNK_CallTo16_word_w
, /* CallWindowsExitProc */
157 (void *)THUNK_CallTo16_word_lwww
, /* CallWordBreakProc */
158 (void *)THUNK_CallTo16_word_ww
, /* CallBootAppProc */
159 (void *)THUNK_CallTo16_word_www
, /* CallLoadAppSegProc */
160 (void *)THUNK_CallTo16_word_www
, /* CallLocalNotifyFunc */
161 (void *)THUNK_CallTo16_word_www
, /* CallResourceHandlerProc */
162 (void *)THUNK_CallTo16_long_l
, /* CallWOWCallbackProc */
163 THUNK_WOWCallback16Ex
, /* CallWOWCallback16Ex */
164 (void *)THUNK_CallTo16_long_ll
, /* CallUTProc */
165 (void *)THUNK_CallTo16_long_l
, /* CallASPIPostProc */
166 (void *)THUNK_CallTo16_word_lwll
, /* CallDrvControlProc */
167 (void *)THUNK_CallTo16_word_lwlll
, /* CallDrvEnableProc */
168 (void *)THUNK_CallTo16_word_llll
, /* CallDrvEnumDFontsProc */
169 (void *)THUNK_CallTo16_word_lwll
, /* CallDrvEnumObjProc */
170 (void *)THUNK_CallTo16_word_lwwlllll
, /* CallDrvOutputProc */
171 (void *)THUNK_CallTo16_long_lwlll
, /* CallDrvRealizeProc */
172 (void *)THUNK_CallTo16_word_lwwwwlwwwwllll
, /* CallDrvStretchBltProc */
173 (void *)THUNK_CallTo16_long_lwwllwlllllw
, /* CallDrvExtTextOutProc */
174 (void *)THUNK_CallTo16_word_llwwlll
, /* CallDrvGetCharWidth */
175 (void *)THUNK_CallTo16_word_ww
/* CallDrvAbortProc */
179 /***********************************************************************
182 BOOL
THUNK_Init(void)
184 /* Set the window proc calling functions */
185 Callbacks
= &CALLBACK_EmulatorTable
;
186 /* Initialize Thunklets */
187 return THUNK_ThunkletInit();
190 /***********************************************************************
193 static THUNK
*THUNK_Alloc( FARPROC func
, RELAY relay
)
195 THUNK
*thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
198 thunk
->popl_eax
= 0x58;
199 thunk
->pushl_func
= 0x68;
201 thunk
->pushl_eax
= 0x50;
203 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
204 thunk
->next
= firstThunk
;
211 /***********************************************************************
214 static THUNK
*THUNK_Find( FARPROC func
)
216 THUNK
*thunk
= firstThunk
;
217 while (thunk
&& (thunk
->proc
!= func
)) thunk
= thunk
->next
;
222 /***********************************************************************
225 static void THUNK_Free( THUNK
*thunk
)
227 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk
))
229 THUNK
**prev
= &firstThunk
;
230 while (*prev
&& (*prev
!= thunk
)) prev
= &(*prev
)->next
;
234 HeapFree( GetProcessHeap(), 0, thunk
);
238 ERR_(thunk
)("invalid thunk addr %p\n", thunk
);
242 /***********************************************************************
243 * THUNK_CallWndProc16
245 * Call a 16-bit window procedure
247 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
248 UINT16 msg
, WPARAM16 wParam
,
254 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
256 TEB
*teb
= NtCurrentTeb();
259 /* Window procedures want ax = hInstance, ds = es = ss */
261 memset(&context
, '\0', sizeof(context
));
262 DS_reg(&context
) = SELECTOROF(teb
->cur_stack
);
263 ES_reg(&context
) = DS_reg(&context
);
264 EAX_reg(&context
) = wndPtr
? wndPtr
->hInstance
: DS_reg(&context
);
265 CS_reg(&context
) = SELECTOROF(proc
);
266 EIP_reg(&context
) = OFFSETOF(proc
);
267 EBP_reg(&context
) = OFFSETOF(teb
->cur_stack
)
268 + (WORD
)&((STACK16FRAME
*)0)->bp
;
270 WIN_ReleaseWndPtr(wndPtr
);
274 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
275 work if structures passed in lParam are placed in the stack/data
276 segment. Programmers easily make the mistake of converting lParam
277 to a near rather than a far pointer, since Windows apparently
278 allows this. We copy the structures to the 16 bit stack; this is
279 ugly but makes these programs work. */
284 offset
= sizeof(CREATESTRUCT16
); break;
286 offset
= sizeof(DRAWITEMSTRUCT16
); break;
288 offset
= sizeof(COMPAREITEMSTRUCT16
); break;
292 void *s
= PTR_SEG_TO_LIN(lParam
);
293 lParam
= STACK16_PUSH( teb
, offset
);
294 memcpy( PTR_SEG_TO_LIN(lParam
), s
, offset
);
298 iWndsLocks
= WIN_SuspendWndsLock();
300 args
= (WORD
*)THREAD_STACK16(teb
) - 5;
301 args
[0] = LOWORD(lParam
);
302 args
[1] = HIWORD(lParam
);
307 ret
= CallTo16RegisterShort( &context
, 5 * sizeof(WORD
) );
308 if (offset
) STACK16_POP( teb
, offset
);
310 WIN_RestoreWndsLock(iWndsLocks
);
316 /***********************************************************************
317 * THUNK_EnumObjects16 (GDI.71)
319 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
320 GOBJENUMPROC16 func
, LPARAM lParam
)
322 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_ll
);
323 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
327 /*************************************************************************
328 * THUNK_EnumFonts16 (GDI.70)
330 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
331 FONTENUMPROC16 func
, LPARAM lParam
)
333 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
334 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
337 /******************************************************************
338 * THUNK_EnumMetaFile16 (GDI.175)
340 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
341 MFENUMPROC16 func
, LPARAM lParam
)
343 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wllwl
);
344 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
348 /*************************************************************************
349 * THUNK_EnumFontFamilies16 (GDI.330)
351 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
352 FONTENUMPROC16 func
, LPARAM lParam
)
354 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
355 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
359 /*************************************************************************
360 * THUNK_EnumFontFamiliesEx16 (GDI.613)
362 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
363 FONTENUMPROCEX16 func
, LPARAM lParam
,
366 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
367 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
372 /**********************************************************************
373 * THUNK_LineDDA16 (GDI.100)
375 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
376 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
378 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wwl
);
379 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
383 /*******************************************************************
384 * THUNK_EnumWindows16 (USER.54)
386 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
388 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
389 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
393 /**********************************************************************
394 * THUNK_EnumChildWindows16 (USER.55)
396 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
399 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
400 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
404 /**********************************************************************
405 * THUNK_EnumTaskWindows16 (USER.225)
407 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
410 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
411 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
415 /***********************************************************************
416 * THUNK_EnumProps16 (USER.27)
418 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
420 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
421 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
425 /***********************************************************************
426 * THUNK_GrayString16 (USER.185)
428 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
429 GRAYSTRINGPROC16 func
, LPARAM lParam
,
430 INT16 cch
, INT16 x
, INT16 y
,
433 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
435 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
437 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
442 /***********************************************************************
443 * THUNK_SetWindowsHook16 (USER.121)
445 FARPROC16 WINAPI
THUNK_SetWindowsHook16( INT16 id
, HOOKPROC16 proc
)
447 HINSTANCE16 hInst
= FarGetOwner16( HIWORD(proc
) );
448 HTASK16 hTask
= (id
== WH_MSGFILTER
) ? GetCurrentTask() : 0;
449 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
450 if (!thunk
) return 0;
451 return (FARPROC16
)SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
455 /***********************************************************************
456 * THUNK_UnhookWindowsHook16 (USER.234)
458 BOOL16 WINAPI
THUNK_UnhookWindowsHook16( INT16 id
, HOOKPROC16 proc
)
461 THUNK
*thunk
= THUNK_Find( (FARPROC16
)proc
);
462 if (!thunk
) return FALSE
;
463 ret
= UnhookWindowsHook16( id
, (HOOKPROC16
)thunk
);
469 /***********************************************************************
470 * THUNK_SetWindowsHookEx16 (USER.291)
472 HHOOK WINAPI
THUNK_SetWindowsHookEx16( INT16 id
, HOOKPROC16 proc
,
473 HINSTANCE16 hInst
, HTASK16 hTask
)
475 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
476 if (!thunk
) return 0;
477 return SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
481 /***********************************************************************
482 * THUNK_UnhookWindowHookEx16 (USER.292)
484 BOOL16 WINAPI
THUNK_UnhookWindowsHookEx16( HHOOK hhook
)
486 THUNK
*thunk
= (THUNK
*)HOOK_GetProc16( hhook
);
487 BOOL16 ret
= UnhookWindowsHookEx16( hhook
);
488 if (thunk
) THUNK_Free( thunk
);
494 static FARPROC16 defDCHookProc
= NULL
;
496 /***********************************************************************
497 * THUNK_SetDCHook (GDI.190)
499 BOOL16 WINAPI
THUNK_SetDCHook( HDC16 hdc
, FARPROC16 proc
, DWORD dwHookData
)
501 THUNK
*thunk
, *oldThunk
;
503 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
505 HMODULE16 hModule
= GetModuleHandle16( "USER" );
506 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
508 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
) )
509 defDCHookProc
= NE_GetEntryPoint( hModule
, 362 );
511 defDCHookProc
= (FARPROC16
)-1;
516 else if (proc
!= defDCHookProc
)
518 thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwll
);
519 if (!thunk
) return FALSE
;
521 else thunk
= (THUNK
*)DCHook16
;
523 /* Free the previous thunk */
524 GetDCHook( hdc
, (FARPROC16
*)&oldThunk
);
525 if (oldThunk
&& (oldThunk
!= (THUNK
*)DCHook16
)) THUNK_Free( oldThunk
);
527 return SetDCHook( hdc
, (FARPROC16
)thunk
, dwHookData
);
531 /***********************************************************************
532 * THUNK_GetDCHook (GDI.191)
534 DWORD WINAPI
THUNK_GetDCHook( HDC16 hdc
, FARPROC16
*phookProc
)
537 DWORD ret
= GetDCHook( hdc
, (FARPROC16
*)&thunk
);
540 if (thunk
== (THUNK
*)DCHook16
)
542 /* Note: we can only get here when running built-in USER */
544 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
545 defDCHookProc
= NE_GetEntryPoint(GetModuleHandle16("USER"),362);
547 *phookProc
= defDCHookProc
;
549 else *phookProc
= thunk
->proc
;
555 /***********************************************************************
556 * THUNK_SetTaskSignalProc (KERNEL.38)
558 FARPROC16 WINAPI
THUNK_SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
560 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwwww
);
561 if ( !thunk
) return NULL
;
563 thunk
= (THUNK
*)SetTaskSignalProc( hTask
, (FARPROC16
)thunk
);
564 if ( !thunk
) return NULL
;
571 /***********************************************************************
572 * THUNK_CreateThread16 (KERNEL.441)
574 static DWORD CALLBACK
THUNK_StartThread16( LPVOID threadArgs
)
576 FARPROC16 start
= ((FARPROC16
*)threadArgs
)[0];
577 DWORD param
= ((DWORD
*)threadArgs
)[1];
578 HeapFree( GetProcessHeap(), 0, threadArgs
);
580 return THUNK_CallTo16_long_l( start
, param
);
582 HANDLE WINAPI
THUNK_CreateThread16( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
583 FARPROC16 start
, SEGPTR param
,
584 DWORD flags
, LPDWORD id
)
586 DWORD
*threadArgs
= HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD
) );
587 if (!threadArgs
) return INVALID_HANDLE_VALUE
;
588 threadArgs
[0] = (DWORD
)start
;
589 threadArgs
[1] = (DWORD
)param
;
591 return CreateThread( sa
, stack
, THUNK_StartThread16
, threadArgs
, flags
, id
);
594 /***********************************************************************
595 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
596 * Generic thunking routine to call 16 bit functions from 32bit code.
599 * TRUE if the call was done
601 static BOOL WINAPI
THUNK_WOWCallback16Ex(
602 FARPROC16 proc
, /* [in] 16bit function to call */
603 DWORD dwFlags
, /* [in] flags (WCB_*) */
604 DWORD cbArgs
, /* [in] number of arguments */
605 LPVOID xargs
, /* [in/out] arguments */
606 LPDWORD pdwret
/* [out] return value of the 16bit call */
608 LPDWORD args
= (LPDWORD
)xargs
;
611 TRACE_(relay
)("(%p,0x%08lx,%ld,%p,%p)\n",
612 proc
,dwFlags
,cbArgs
,xargs
,pdwret
614 if (dwFlags
== WCB16_CDECL
) {
615 /* swap the arguments */
616 args
= HeapAlloc(GetProcessHeap(),0,cbArgs
*sizeof(DWORD
));
617 for (i
=0;i
<cbArgs
;i
++)
618 args
[i
] = ((DWORD
*)xargs
)[cbArgs
-i
-1];
621 case 0: ret
= THUNK_CallTo16_long_(proc
);break;
622 case 1: ret
= THUNK_CallTo16_long_l(proc
,args
[0]);break;
623 case 2: ret
= THUNK_CallTo16_long_ll(proc
,args
[0],args
[1]);break;
624 case 3: ret
= THUNK_CallTo16_long_lll(proc
,args
[0],args
[1],args
[2]);break;
625 case 4: ret
= THUNK_CallTo16_long_llll(proc
,args
[0],args
[1],args
[2],args
[3]);
627 case 5: ret
= THUNK_CallTo16_long_lllll(proc
,args
[0],args
[1],args
[2],args
[3],
631 case 6: ret
= THUNK_CallTo16_long_llllll(proc
,args
[0],args
[1],args
[2],args
[3],
635 case 7: ret
= THUNK_CallTo16_long_lllllll(proc
,args
[0],args
[1],args
[2],args
[3],
636 args
[4],args
[5],args
[6]
639 case 8: ret
= THUNK_CallTo16_long_llllllll(proc
,args
[0],args
[1],args
[2],args
[3],
640 args
[4],args
[5],args
[6],args
[7]
643 case 9: ret
= THUNK_CallTo16_long_lllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
644 args
[4],args
[5],args
[6],args
[7],args
[8]
647 case 10:ret
= THUNK_CallTo16_long_llllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
648 args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]
651 case 11:ret
= THUNK_CallTo16_long_lllllllllll(proc
,args
[0],args
[1],args
[2],
652 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]
655 case 12:ret
= THUNK_CallTo16_long_llllllllllll(proc
,args
[0],args
[1],args
[2],
656 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
660 case 13:ret
= THUNK_CallTo16_long_lllllllllllll(proc
,args
[0],args
[1],args
[2],
661 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
662 args
[10],args
[11],args
[12]
665 case 14:ret
= THUNK_CallTo16_long_llllllllllllll(proc
,args
[0],args
[1],args
[2],
666 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
667 args
[10],args
[11],args
[12],args
[13]
670 case 15:ret
= THUNK_CallTo16_long_lllllllllllllll(proc
,args
[0],args
[1],args
[2],
671 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
672 args
[10],args
[11],args
[12],args
[13],args
[14]
675 case 16:ret
= THUNK_CallTo16_long_llllllllllllllll(proc
,args
[0],args
[1],args
[2],
676 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
677 args
[10],args
[11],args
[12],args
[13],args
[14],args
[15]
681 ERR_(thunk
)("%ld arguments not supported.\n",cbArgs
);
682 if (dwFlags
== WCB16_CDECL
)
683 HeapFree(GetProcessHeap(),0,args
);
686 if (dwFlags
== WCB16_CDECL
)
687 HeapFree(GetProcessHeap(),0,args
);
693 /***********************************************************************
694 * THUNK_MOUSE_Enable (MOUSE.2)
696 static VOID WINAPI
THUNK_CallMouseEventProc( FARPROC16 proc
,
697 DWORD dwFlags
, DWORD dx
, DWORD dy
,
698 DWORD cButtons
, DWORD dwExtraInfo
)
702 memset( &context
, 0, sizeof(context
) );
703 CS_reg(&context
) = SELECTOROF( proc
);
704 EIP_reg(&context
) = OFFSETOF( proc
);
705 AX_reg(&context
) = (WORD
)dwFlags
;
706 BX_reg(&context
) = (WORD
)dx
;
707 CX_reg(&context
) = (WORD
)dy
;
708 DX_reg(&context
) = (WORD
)cButtons
;
709 SI_reg(&context
) = LOWORD( dwExtraInfo
);
710 DI_reg(&context
) = HIWORD( dwExtraInfo
);
712 CallTo16RegisterShort( &context
, 0 );
714 VOID WINAPI
THUNK_MOUSE_Enable( FARPROC16 proc
)
716 static THUNK
*lastThunk
= NULL
;
717 static FARPROC16 lastProc
= NULL
;
719 if ( lastProc
!= proc
)
722 THUNK_Free( lastThunk
);
727 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallMouseEventProc
);
732 MOUSE_Enable( (LPMOUSE_EVENT_PROC
)lastThunk
);
735 /***********************************************************************
736 * GetMouseEventProc (USER.337)
738 FARPROC16 WINAPI
GetMouseEventProc16(void)
740 HMODULE16 hmodule
= GetModuleHandle16("USER");
741 return NE_GetEntryPoint( hmodule
, NE_GetOrdinal( hmodule
, "mouse_event" ));
745 /***********************************************************************
746 * WIN16_mouse_event (USER.299)
748 void WINAPI
WIN16_mouse_event( CONTEXT86
*context
)
750 mouse_event( AX_reg(context
), BX_reg(context
), CX_reg(context
),
751 DX_reg(context
), MAKELONG(SI_reg(context
), DI_reg(context
)) );
755 /***********************************************************************
756 * THUNK_KEYBD_Enable (KEYBOARD.2)
758 static VOID WINAPI
THUNK_CallKeybdEventProc( FARPROC16 proc
,
759 BYTE bVk
, BYTE bScan
,
760 DWORD dwFlags
, DWORD dwExtraInfo
)
764 memset( &context
, 0, sizeof(context
) );
765 CS_reg(&context
) = SELECTOROF( proc
);
766 EIP_reg(&context
) = OFFSETOF( proc
);
767 AH_reg(&context
) = (dwFlags
& KEYEVENTF_KEYUP
)? 0x80 : 0;
768 AL_reg(&context
) = bVk
;
769 BH_reg(&context
) = (dwFlags
& KEYEVENTF_EXTENDEDKEY
)? 1 : 0;
770 BL_reg(&context
) = bScan
;
771 SI_reg(&context
) = LOWORD( dwExtraInfo
);
772 DI_reg(&context
) = HIWORD( dwExtraInfo
);
774 CallTo16RegisterShort( &context
, 0 );
776 VOID WINAPI
THUNK_KEYBOARD_Enable( FARPROC16 proc
, LPBYTE lpKeyState
)
778 static THUNK
*lastThunk
= NULL
;
779 static FARPROC16 lastProc
= NULL
;
781 if ( lastProc
!= proc
)
784 THUNK_Free( lastThunk
);
789 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallKeybdEventProc
);
794 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC
)lastThunk
, lpKeyState
);
797 /***********************************************************************
798 * WIN16_keybd_event (USER.289)
800 void WINAPI
WIN16_keybd_event( CONTEXT86
*context
)
804 if (AH_reg(context
) & 0x80) dwFlags
|= KEYEVENTF_KEYUP
;
805 if (BH_reg(context
) & 1 ) dwFlags
|= KEYEVENTF_EXTENDEDKEY
;
807 keybd_event( AL_reg(context
), BL_reg(context
),
808 dwFlags
, MAKELONG(SI_reg(context
), DI_reg(context
)) );
812 /***********************************************************************
813 * WIN16_CreateSystemTimer (SYSTEM.2)
815 static void THUNK_CallSystemTimerProc( FARPROC16 proc
, WORD timer
)
818 memset( &context
, '\0', sizeof(context
) );
820 CS_reg( &context
) = SELECTOROF( proc
);
821 IP_reg( &context
) = OFFSETOF( proc
);
822 BP_reg( &context
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
823 + (WORD
)&((STACK16FRAME
*)0)->bp
;
825 AX_reg( &context
) = timer
;
827 if ( _ConfirmWin16Lock() )
829 FIXME_(system
)("Skipping timer %d callback because timer signal "
830 "arrived while we own the Win16Lock!\n", timer
);
834 CallTo16RegisterShort( &context
, 0 );
836 /* FIXME: This does not work if the signal occurs while this thread
837 is currently in 16-bit code. With the current structure
838 of the Wine thunking code, this seems to be hard to fix ... */
840 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
842 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallSystemTimerProc
);
843 WORD timer
= CreateSystemTimer( rate
, (SYSTEMTIMERPROC
)thunk
);
844 if (!timer
) THUNK_Free( thunk
);
848 /***********************************************************************
851 void THUNK_InitCallout(void)
857 hModule
= GetModuleHandleA( "USER32" );
858 wm
= MODULE32_LookupHMODULE( hModule
);
859 if ( wm
&& !(wm
->flags
& WINE_MODREF_INTERNAL
) )
861 #define GETADDR( var, name ) \
862 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
864 GETADDR( PeekMessageA
, "PeekMessageA" );
865 GETADDR( PeekMessageW
, "PeekMessageW" );
866 GETADDR( GetMessageA
, "GetMessageA" );
867 GETADDR( GetMessageW
, "GetMessageW" );
868 GETADDR( SendMessageA
, "SendMessageA" );
869 GETADDR( SendMessageW
, "SendMessageW" );
870 GETADDR( PostMessageA
, "PostMessageA" );
871 GETADDR( PostMessageW
, "PostMessageW" );
872 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
873 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
874 GETADDR( TranslateMessage
, "TranslateMessage" );
875 GETADDR( DispatchMessageW
, "DispatchMessageW" );
876 GETADDR( DispatchMessageA
, "DispatchMessageA" );
877 GETADDR( RedrawWindow
, "RedrawWindow" );
878 GETADDR( UserSignalProc
, "UserSignalProc" );
883 hModule
= GetModuleHandle16( "USER" );
884 pModule
= NE_GetPtr( hModule
);
885 if ( pModule
&& !(pModule
->flags
& NE_FFLAGS_BUILTIN
) )
887 #define GETADDR( var, name, thk ) \
888 *(FARPROC *)&Callout.##var = (FARPROC) \
889 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
890 (RELAY)THUNK_CallTo16_##thk )
892 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
893 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
894 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
895 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
896 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
897 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
898 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
899 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
900 GETADDR( InitApp16
, "InitApp", word_w
);
901 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
902 GETADDR( UserYield16
, "UserYield", word_
);
903 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
909 /***********************************************************************
910 * 16->32 Flat Thunk routines:
913 /***********************************************************************
914 * ThunkConnect16 (KERNEL.651)
915 * Connects a 32bit and a 16bit thunkbuffer.
917 UINT WINAPI
ThunkConnect16(
918 LPSTR module16
, /* [in] name of win16 dll */
919 LPSTR module32
, /* [in] name of win32 dll */
920 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
921 DWORD dwReason
, /* [in] initialisation argument */
922 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
923 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
924 WORD cs
/* [in] CS of win16 dll */
928 if (!lstrncmpA(TD
->magic
, "SL01", 4))
932 TRACE_(thunk
)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
933 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
935 else if (!lstrncmpA(TD
->magic
, "LS01", 4))
939 TRACE_(thunk
)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
940 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
944 ERR_(thunk
)("Invalid magic %c%c%c%c\n",
945 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
951 case DLL_PROCESS_ATTACH
:
954 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
955 struct ThunkDataSL
*SL
= SL16
->fpData
;
959 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
961 SL
->common
= SL16
->common
;
962 SL
->flags1
= SL16
->flags1
;
963 SL
->flags2
= SL16
->flags2
;
965 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
968 lstrcpynA(SL
->pszDll16
, module16
, 255);
969 lstrcpynA(SL
->pszDll32
, module32
, 255);
971 /* We should create a SEGPTR to the ThunkDataSL,
972 but since the contents are not in the original format,
973 any access to this by 16-bit code would crash anyway. */
979 if (SL
->flags2
& 0x80000000)
981 TRACE_(thunk
)("Preloading 32-bit library\n");
982 LoadLibraryA(module32
);
991 case DLL_PROCESS_DETACH
:
1000 /***********************************************************************
1001 * C16ThkSL (KERNEL.630)
1004 void WINAPI
C16ThkSL(CONTEXT86
*context
)
1006 extern void CallFrom16Thunk(void);
1007 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1012 /* We produce the following code:
1017 * mov edx, es:[ecx + $EDX]
1020 * call __FLATCS:CallFrom16_t_long_
1023 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
1024 *x
++ = 0x8E; *x
++ = 0xC0;
1025 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1026 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1027 *x
++ = 0x91; *((DWORD
*)x
)++ = EDX_reg(context
);
1030 *x
++ = 0x66; *x
++ = 0x52;
1031 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1032 *((WORD
*)x
)++ = cs
;
1034 /* Jump to the stub code just created */
1035 IP_reg(context
) = LOWORD(EAX_reg(context
));
1036 CS_reg(context
) = HIWORD(EAX_reg(context
));
1038 /* Since C16ThkSL got called by a jmp, we need to leave the
1039 orginal return address on the stack */
1040 SP_reg(context
) -= 4;
1043 /***********************************************************************
1044 * C16ThkSL01 (KERNEL.631)
1047 void WINAPI
C16ThkSL01(CONTEXT86
*context
)
1049 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1053 struct ThunkDataSL16
*SL16
= PTR_SEG_TO_LIN(EDX_reg(context
));
1054 struct ThunkDataSL
*td
= SL16
->fpData
;
1056 extern void CallFrom16Thunk(void);
1057 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1063 ERR_(thunk
)("ThunkConnect16 was not called!\n");
1067 TRACE_(thunk
)("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
1070 /* We produce the following code:
1077 * call __FLATCS:CallFrom16_t_long_
1080 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1081 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
1082 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
1085 *x
++ = 0x66; *x
++ = 0x52;
1086 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1087 *((WORD
*)x
)++ = cs
;
1089 /* Jump to the stub code just created */
1090 IP_reg(context
) = LOWORD(EAX_reg(context
));
1091 CS_reg(context
) = HIWORD(EAX_reg(context
));
1093 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1094 orginal return address on the stack */
1095 SP_reg(context
) -= 4;
1099 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
1100 DWORD targetNr
= CX_reg(context
) / 4;
1101 struct SLTargetDB
*tdb
;
1103 TRACE_(thunk
)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1104 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
1106 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1107 if (tdb
->process
== PROCESS_Current())
1112 TRACE_(thunk
)("Loading 32-bit library %s\n", td
->pszDll32
);
1113 LoadLibraryA(td
->pszDll32
);
1115 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1116 if (tdb
->process
== PROCESS_Current())
1122 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
1124 TRACE_(thunk
)("Call target is %08lx\n", EDX_reg(context
));
1128 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), SP_reg(context
));
1129 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1130 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1131 IP_reg(context
) = stack
[2];
1132 CS_reg(context
) = stack
[3];
1133 SP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1135 ERR_(thunk
)("Process %08lx did not ThunkConnect32 %s to %s\n",
1136 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
1142 WOW16Call(WORD x
,WORD y
,WORD z
) {
1145 FIXME_(thunk
)("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
1147 for (i
=0;i
<x
/2;i
++) {
1148 WORD a
= STACK16_POP(NtCurrentTeb(),2);
1151 calladdr
= STACK16_POP(NtCurrentTeb(),4);
1152 DPRINTF(") calling address was 0x%08lx\n",calladdr
);
1157 /***********************************************************************
1158 * 16<->32 Thunklet/Callback API:
1161 #include "pshpack1.h"
1162 typedef struct _THUNKLET
1177 struct _THUNKLET
*next
;
1179 #include "poppack.h"
1181 #define THUNKLET_TYPE_LS 1
1182 #define THUNKLET_TYPE_SL 2
1184 static HANDLE ThunkletHeap
= 0;
1185 static THUNKLET
*ThunkletAnchor
= NULL
;
1187 static FARPROC ThunkletSysthunkGlueLS
= 0;
1188 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1190 static FARPROC ThunkletCallbackGlueLS
= 0;
1191 static SEGPTR ThunkletCallbackGlueSL
= 0;
1193 /***********************************************************************
1194 * THUNK_ThunkletInit
1196 static BOOL
THUNK_ThunkletInit( void )
1200 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
1201 if (!ThunkletHeap
) return FALSE
;
1203 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1204 if (!thunk
) return FALSE
;
1206 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1207 *thunk
++ = 0x58; /* popl eax */
1208 *thunk
++ = 0xC3; /* ret */
1210 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1211 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1212 *thunk
++ = 0xCB; /* lret */
1217 /***********************************************************************
1218 * SetThunkletCallbackGlue (KERNEL.560)
1220 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1222 ThunkletCallbackGlueLS
= glueLS
;
1223 ThunkletCallbackGlueSL
= glueSL
;
1227 /***********************************************************************
1228 * THUNK_FindThunklet
1230 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1231 DWORD glue
, BYTE type
)
1235 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1236 if ( thunk
->type
== type
1237 && thunk
->target
== target
1238 && thunk
->relay
== relay
1239 && ( type
== THUNKLET_TYPE_LS
?
1240 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
1241 : ( thunk
->glue
== glue
) ) )
1247 /***********************************************************************
1248 * THUNK_AllocLSThunklet
1250 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1251 FARPROC glue
, HTASK16 owner
)
1253 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1257 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1259 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1262 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1263 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1264 thunk
->jmp_glue
= 0xE9;
1266 thunk
->target
= (DWORD
)target
;
1267 thunk
->relay
= (DWORD
)relay
;
1268 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1270 thunk
->type
= THUNKLET_TYPE_LS
;
1271 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1273 thunk
->next
= ThunkletAnchor
;
1274 ThunkletAnchor
= thunk
;
1277 return (FARPROC
)thunk
;
1280 /***********************************************************************
1281 * THUNK_AllocSLThunklet
1283 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1284 SEGPTR glue
, HTASK16 owner
)
1286 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1290 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1292 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1295 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1296 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1297 thunk
->jmp_glue
= 0xEA;
1299 thunk
->target
= (DWORD
)target
;
1300 thunk
->relay
= (DWORD
)relay
;
1301 thunk
->glue
= (DWORD
)glue
;
1303 thunk
->type
= THUNKLET_TYPE_SL
;
1304 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1306 thunk
->next
= ThunkletAnchor
;
1307 ThunkletAnchor
= thunk
;
1310 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1313 /**********************************************************************
1316 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
1318 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1319 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1320 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1323 /**********************************************************************
1324 * IsSLThunklet (KERNEL.612)
1326 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1328 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1329 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1330 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1335 /***********************************************************************
1336 * AllocLSThunkletSysthunk (KERNEL.607)
1338 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1339 FARPROC relay
, DWORD dummy
)
1341 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1342 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1345 /***********************************************************************
1346 * AllocSLThunkletSysthunk (KERNEL.608)
1348 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1349 SEGPTR relay
, DWORD dummy
)
1351 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1352 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1356 /***********************************************************************
1357 * AllocLSThunkletCallbackEx (KERNEL.567)
1359 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1360 DWORD relay
, HTASK16 task
)
1362 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1363 if ( !thunk
) return NULL
;
1365 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1366 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1367 return (FARPROC
)thunk
->target
;
1369 return THUNK_AllocLSThunklet( target
, relay
,
1370 ThunkletCallbackGlueLS
, task
);
1373 /***********************************************************************
1374 * AllocSLThunkletCallbackEx (KERNEL.568)
1376 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1377 DWORD relay
, HTASK16 task
)
1379 THUNKLET
*thunk
= (THUNKLET
*)target
;
1380 if ( !thunk
) return 0;
1382 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1383 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1384 return (SEGPTR
)thunk
->target
;
1386 return THUNK_AllocSLThunklet( target
, relay
,
1387 ThunkletCallbackGlueSL
, task
);
1390 /***********************************************************************
1391 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1393 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1395 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1398 /***********************************************************************
1399 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1401 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1403 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1406 /***********************************************************************
1407 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1409 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1411 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1412 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1413 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1414 return (FARPROC
)thunk
->target
;
1416 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1417 (DWORD
)ThunkletCallbackGlueLS
,
1419 return (FARPROC
)thunk
;
1422 /***********************************************************************
1423 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1425 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1427 THUNKLET
*thunk
= (THUNKLET
*)target
;
1428 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1429 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1430 return (SEGPTR
)thunk
->target
;
1432 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1433 (DWORD
)ThunkletCallbackGlueSL
,
1435 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1439 /***********************************************************************
1440 * FreeThunklet16 (KERNEL.611)
1442 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
1447 /***********************************************************************
1448 * Callback Client API
1451 #define N_CBC_FIXED 20
1452 #define N_CBC_VARIABLE 10
1453 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1455 static SEGPTR
*CBClientRelay16
[ N_CBC_TOTAL
];
1456 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1458 /***********************************************************************
1459 * RegisterCBClient (KERNEL.619)
1461 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1462 SEGPTR
*relay16
, FARPROC
*relay32
)
1464 /* Search for free Callback ID */
1466 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1467 if ( !CBClientRelay16
[ wCBCId
] )
1470 /* Register Callback ID */
1471 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1473 CBClientRelay16
[ wCBCId
] = relay16
;
1474 CBClientRelay32
[ wCBCId
] = relay32
;
1482 /***********************************************************************
1483 * UnRegisterCBClient (KERNEL.622)
1485 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1486 SEGPTR
*relay16
, FARPROC
*relay32
)
1488 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1489 && CBClientRelay16
[ wCBCId
] == relay16
1490 && CBClientRelay32
[ wCBCId
] == relay32
)
1492 CBClientRelay16
[ wCBCId
] = 0;
1493 CBClientRelay32
[ wCBCId
] = 0;
1502 /***********************************************************************
1503 * InitCBClient (KERNEL.623)
1505 void WINAPI
InitCBClient16( FARPROC glueLS
)
1507 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1508 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
1510 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1513 /***********************************************************************
1514 * CBClientGlueSL (KERNEL.604)
1516 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1518 /* Create stack frame */
1519 SEGPTR stackSeg
= STACK16_PUSH( NtCurrentTeb(), 12 );
1520 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1523 stackLin
[3] = BP_reg( context
);
1524 stackLin
[2] = SI_reg( context
);
1525 stackLin
[1] = DI_reg( context
);
1526 stackLin
[0] = DS_reg( context
);
1528 BP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1529 SP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1530 GS_reg( context
) = 0;
1532 /* Jump to 16-bit relay code */
1533 glue
= CBClientRelay16
[ stackLin
[5] ][ stackLin
[4] ];
1534 CS_reg ( context
) = SELECTOROF( glue
);
1535 EIP_reg( context
) = OFFSETOF ( glue
);
1538 /***********************************************************************
1539 * CBClientThunkSL (KERNEL.620)
1541 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1543 /* Call 32-bit relay code */
1544 extern DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1546 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1547 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1549 EAX_reg(context
) = CALL32_CBClient( proc
, args
, &ESI_reg( context
) );
1552 /***********************************************************************
1553 * CBClientThunkSLEx (KERNEL.621)
1555 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1557 /* Call 32-bit relay code */
1558 extern DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
, LPWORD args
,
1559 DWORD
*esi
, INT
*nArgs
);
1561 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1562 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1566 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &ESI_reg( context
), &nArgs
);
1568 /* Restore registers saved by CBClientGlueSL */
1569 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1570 BP_reg( context
) = stackLin
[3];
1571 SI_reg( context
) = stackLin
[2];
1572 DI_reg( context
) = stackLin
[1];
1573 DS_reg( context
) = stackLin
[0];
1574 SP_reg( context
) += 16+nArgs
;
1576 /* Return to caller of CBClient thunklet */
1577 CS_reg ( context
) = stackLin
[9];
1578 EIP_reg( context
) = stackLin
[8];