4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
13 #include "builtin16.h"
18 #include "stackframe.h"
20 #include "flatthunk.h"
23 #include "debugtools.h"
25 DECLARE_DEBUG_CHANNEL(relay
)
26 DECLARE_DEBUG_CHANNEL(system
)
27 DECLARE_DEBUG_CHANNEL(thunk
)
30 extern LONG CALLBACK
CallTo16RegisterShort(const CONTEXT86
*context
, INT offset
);
31 extern LONG CALLBACK
CallTo16RegisterLong(const CONTEXT86
*context
, INT offset
);
33 /* List of the 16-bit callback functions. This list is used */
34 /* by the build program to generate the file if1632/callto16.S */
36 /* ### start build ### */
37 extern WORD CALLBACK
THUNK_CallTo16_word_ (FARPROC16
);
38 extern LONG CALLBACK
THUNK_CallTo16_long_ (FARPROC16
);
39 extern WORD CALLBACK
THUNK_CallTo16_word_w (FARPROC16
,WORD
);
40 extern WORD CALLBACK
THUNK_CallTo16_word_l (FARPROC16
,LONG
);
41 extern LONG CALLBACK
THUNK_CallTo16_long_l (FARPROC16
,LONG
);
42 extern WORD CALLBACK
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
43 extern WORD CALLBACK
THUNK_CallTo16_word_wl (FARPROC16
,WORD
,LONG
);
44 extern WORD CALLBACK
THUNK_CallTo16_word_ll (FARPROC16
,LONG
,LONG
);
45 extern LONG CALLBACK
THUNK_CallTo16_long_ll (FARPROC16
,LONG
,LONG
);
46 extern WORD CALLBACK
THUNK_CallTo16_word_www (FARPROC16
,WORD
,WORD
,WORD
);
47 extern WORD CALLBACK
THUNK_CallTo16_word_wwl (FARPROC16
,WORD
,WORD
,LONG
);
48 extern WORD CALLBACK
THUNK_CallTo16_word_wlw (FARPROC16
,WORD
,LONG
,WORD
);
49 extern LONG CALLBACK
THUNK_CallTo16_long_wwl (FARPROC16
,WORD
,WORD
,LONG
);
50 extern LONG CALLBACK
THUNK_CallTo16_long_lll (FARPROC16
,LONG
,LONG
,LONG
);
51 extern WORD CALLBACK
THUNK_CallTo16_word_llwl (FARPROC16
,LONG
,LONG
,WORD
,LONG
);
52 extern WORD CALLBACK
THUNK_CallTo16_word_lwll (FARPROC16
,LONG
,WORD
,LONG
,LONG
);
53 extern WORD CALLBACK
THUNK_CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
54 extern WORD CALLBACK
THUNK_CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
55 extern WORD CALLBACK
THUNK_CallTo16_word_wwll (FARPROC16
,WORD
,WORD
,LONG
,LONG
);
56 extern WORD CALLBACK
THUNK_CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
57 extern LONG CALLBACK
THUNK_CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
58 extern WORD CALLBACK
THUNK_CallTo16_word_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
59 extern LONG CALLBACK
THUNK_CallTo16_long_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
60 extern WORD CALLBACK
THUNK_CallTo16_word_wllwl(FARPROC16
,WORD
,LONG
,LONG
,WORD
,LONG
);
61 extern WORD CALLBACK
THUNK_CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
62 extern LONG CALLBACK
THUNK_CallTo16_long_lwwll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
);
63 extern WORD CALLBACK
THUNK_CallTo16_word_wwlll(FARPROC16
,WORD
,WORD
,LONG
,LONG
,LONG
);
64 extern WORD CALLBACK
THUNK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
65 extern WORD CALLBACK
THUNK_CallTo16_word_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
66 extern LONG CALLBACK
THUNK_CallTo16_long_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
67 extern LONG CALLBACK
THUNK_CallTo16_long_lllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
);
68 extern LONG CALLBACK
THUNK_CallTo16_long_llllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
69 extern LONG CALLBACK
THUNK_CallTo16_long_lllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
70 extern WORD CALLBACK
THUNK_CallTo16_word_llwwlll(FARPROC16
,LONG
,LONG
,WORD
,WORD
,LONG
,LONG
,LONG
);
71 extern LONG CALLBACK
THUNK_CallTo16_word_lwwlllll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
,
73 extern LONG CALLBACK
THUNK_CallTo16_long_llllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,
75 extern LONG CALLBACK
THUNK_CallTo16_long_lllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
76 LONG
,LONG
,LONG
,LONG
,LONG
);
77 extern LONG CALLBACK
THUNK_CallTo16_long_llllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
78 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
79 extern LONG CALLBACK
THUNK_CallTo16_long_lllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
80 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
82 extern LONG CALLBACK
THUNK_CallTo16_long_llllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
83 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
85 extern LONG CALLBACK
THUNK_CallTo16_long_lwwllwlllllw(FARPROC16
,LONG
,WORD
,WORD
,LONG
,
86 LONG
,WORD
,LONG
,LONG
,LONG
,LONG
,
88 extern LONG CALLBACK
THUNK_CallTo16_long_lllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
89 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
91 extern LONG CALLBACK
THUNK_CallTo16_long_llllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
92 LONG
,LONG
,LONG
,LONG
,LONG
,
93 LONG
,LONG
,LONG
,LONG
,LONG
,
95 extern LONG CALLBACK
THUNK_CallTo16_word_lwwwwlwwwwllll(FARPROC16
,LONG
,WORD
,WORD
,
96 WORD
,WORD
,LONG
,WORD
,WORD
,
97 WORD
,WORD
,LONG
,LONG
,LONG
,
99 extern LONG CALLBACK
THUNK_CallTo16_long_lllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
100 LONG
,LONG
,LONG
,LONG
,LONG
,
101 LONG
,LONG
,LONG
,LONG
,LONG
,
103 extern LONG CALLBACK
THUNK_CallTo16_long_llllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
104 LONG
,LONG
,LONG
,LONG
,LONG
,
105 LONG
,LONG
,LONG
,LONG
,LONG
,
107 extern void CALLBACK
THUNK_CallFrom16_p_long_wwwll();
108 /* ### stop build ### */
111 typedef void (*RELAY
)();
113 #include "pshpack1.h"
115 typedef struct tagTHUNK
117 BYTE popl_eax
; /* 0x58 popl %eax (return address)*/
118 BYTE pushl_func
; /* 0x68 pushl $proc */
119 FARPROC proc WINE_PACKED
;
120 BYTE pushl_eax
; /* 0x50 pushl %eax */
121 BYTE jmp
; /* 0xe9 jmp relay (relative jump)*/
122 RELAY relay WINE_PACKED
;
123 struct tagTHUNK
*next WINE_PACKED
;
128 #define DECL_THUNK(aname,aproc,arelay) \
130 aname.popl_eax = 0x58; \
131 aname.pushl_func = 0x68; \
132 aname.proc = (FARPROC) (aproc); \
133 aname.pushl_eax = 0x50; \
135 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
138 static THUNK
*firstThunk
= NULL
;
140 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
141 UINT16 msg
, WPARAM16 wParam
,
143 static BOOL WINAPI
THUNK_WOWCallback16Ex( FARPROC16
,DWORD
,DWORD
,
145 static BOOL
THUNK_ThunkletInit( void );
147 /* Callbacks function table for the emulator */
148 static const CALLBACKS_TABLE CALLBACK_EmulatorTable
=
150 (void *)CallTo16RegisterShort
, /* CallRegisterShortProc */
151 (void *)CallTo16RegisterLong
, /* CallRegisterLongProc */
152 (void*)THUNK_CallFrom16_p_long_wwwll
, /* CallFrom16WndProc */
153 THUNK_CallWndProc16
, /* CallWndProc */
154 (void *)THUNK_CallTo16_long_lwwll
, /* CallDriverProc */
155 (void *)THUNK_CallTo16_word_wwlll
, /* CallDriverCallback */
156 (void *)THUNK_CallTo16_word_wwlll
, /* CallTimeFuncProc */
157 (void *)THUNK_CallTo16_word_w
, /* CallWindowsExitProc */
158 (void *)THUNK_CallTo16_word_lwww
, /* CallWordBreakProc */
159 (void *)THUNK_CallTo16_word_ww
, /* CallBootAppProc */
160 (void *)THUNK_CallTo16_word_www
, /* CallLoadAppSegProc */
161 (void *)THUNK_CallTo16_word_www
, /* CallLocalNotifyFunc */
162 (void *)THUNK_CallTo16_word_www
, /* CallResourceHandlerProc */
163 (void *)THUNK_CallTo16_long_l
, /* CallWOWCallbackProc */
164 THUNK_WOWCallback16Ex
, /* CallWOWCallback16Ex */
165 (void *)THUNK_CallTo16_long_ll
, /* CallUTProc */
166 (void *)THUNK_CallTo16_long_l
, /* CallASPIPostProc */
167 (void *)THUNK_CallTo16_word_lwll
, /* CallDrvControlProc */
168 (void *)THUNK_CallTo16_word_lwlll
, /* CallDrvEnableProc */
169 (void *)THUNK_CallTo16_word_llll
, /* CallDrvEnumDFontsProc */
170 (void *)THUNK_CallTo16_word_lwll
, /* CallDrvEnumObjProc */
171 (void *)THUNK_CallTo16_word_lwwlllll
, /* CallDrvOutputProc */
172 (void *)THUNK_CallTo16_long_lwlll
, /* CallDrvRealizeProc */
173 (void *)THUNK_CallTo16_word_lwwwwlwwwwllll
, /* CallDrvStretchBltProc */
174 (void *)THUNK_CallTo16_long_lwwllwlllllw
, /* CallDrvExtTextOutProc */
175 (void *)THUNK_CallTo16_word_llwwlll
, /* CallDrvGetCharWidth */
176 (void *)THUNK_CallTo16_word_ww
/* CallDrvAbortProc */
179 const CALLBACKS_TABLE
*Callbacks
= &CALLBACK_EmulatorTable
;
181 CALLOUT_TABLE Callout
= { 0 };
184 /***********************************************************************
187 BOOL
THUNK_Init(void)
189 /* Initialize Thunklets */
190 return THUNK_ThunkletInit();
193 /***********************************************************************
196 static THUNK
*THUNK_Alloc( FARPROC func
, RELAY relay
)
198 THUNK
*thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
201 thunk
->popl_eax
= 0x58;
202 thunk
->pushl_func
= 0x68;
204 thunk
->pushl_eax
= 0x50;
206 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
207 thunk
->next
= firstThunk
;
214 /***********************************************************************
217 static THUNK
*THUNK_Find( FARPROC func
)
219 THUNK
*thunk
= firstThunk
;
220 while (thunk
&& (thunk
->proc
!= func
)) thunk
= thunk
->next
;
225 /***********************************************************************
228 static void THUNK_Free( THUNK
*thunk
)
230 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk
))
232 THUNK
**prev
= &firstThunk
;
233 while (*prev
&& (*prev
!= thunk
)) prev
= &(*prev
)->next
;
237 HeapFree( GetProcessHeap(), 0, thunk
);
241 ERR_(thunk
)("invalid thunk addr %p\n", thunk
);
245 /***********************************************************************
246 * THUNK_CallWndProc16
248 * Call a 16-bit window procedure
250 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
251 UINT16 msg
, WPARAM16 wParam
,
257 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
259 TEB
*teb
= NtCurrentTeb();
262 /* Window procedures want ax = hInstance, ds = es = ss */
264 memset(&context
, '\0', sizeof(context
));
265 DS_reg(&context
) = SELECTOROF(teb
->cur_stack
);
266 ES_reg(&context
) = DS_reg(&context
);
267 EAX_reg(&context
) = wndPtr
? wndPtr
->hInstance
: DS_reg(&context
);
268 CS_reg(&context
) = SELECTOROF(proc
);
269 EIP_reg(&context
) = OFFSETOF(proc
);
270 EBP_reg(&context
) = OFFSETOF(teb
->cur_stack
)
271 + (WORD
)&((STACK16FRAME
*)0)->bp
;
273 WIN_ReleaseWndPtr(wndPtr
);
277 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
278 work if structures passed in lParam are placed in the stack/data
279 segment. Programmers easily make the mistake of converting lParam
280 to a near rather than a far pointer, since Windows apparently
281 allows this. We copy the structures to the 16 bit stack; this is
282 ugly but makes these programs work. */
287 offset
= sizeof(CREATESTRUCT16
); break;
289 offset
= sizeof(DRAWITEMSTRUCT16
); break;
291 offset
= sizeof(COMPAREITEMSTRUCT16
); break;
295 void *s
= PTR_SEG_TO_LIN(lParam
);
296 lParam
= STACK16_PUSH( teb
, offset
);
297 memcpy( PTR_SEG_TO_LIN(lParam
), s
, offset
);
301 iWndsLocks
= WIN_SuspendWndsLock();
303 args
= (WORD
*)THREAD_STACK16(teb
) - 5;
304 args
[0] = LOWORD(lParam
);
305 args
[1] = HIWORD(lParam
);
310 ret
= CallTo16RegisterShort( &context
, 5 * sizeof(WORD
) );
311 if (offset
) STACK16_POP( teb
, offset
);
313 WIN_RestoreWndsLock(iWndsLocks
);
319 /***********************************************************************
320 * THUNK_EnumObjects16 (GDI.71)
322 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
323 GOBJENUMPROC16 func
, LPARAM lParam
)
325 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_ll
);
326 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
330 /*************************************************************************
331 * THUNK_EnumFonts16 (GDI.70)
333 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
334 FONTENUMPROC16 func
, LPARAM lParam
)
336 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
337 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
340 /******************************************************************
341 * THUNK_EnumMetaFile16 (GDI.175)
343 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
344 MFENUMPROC16 func
, LPARAM lParam
)
346 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wllwl
);
347 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
351 /*************************************************************************
352 * THUNK_EnumFontFamilies16 (GDI.330)
354 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
355 FONTENUMPROC16 func
, LPARAM lParam
)
357 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
358 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
362 /*************************************************************************
363 * THUNK_EnumFontFamiliesEx16 (GDI.613)
365 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
366 FONTENUMPROCEX16 func
, LPARAM lParam
,
369 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
370 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
375 /**********************************************************************
376 * THUNK_LineDDA16 (GDI.100)
378 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
379 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
381 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wwl
);
382 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
386 /*******************************************************************
387 * THUNK_EnumWindows16 (USER.54)
389 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
391 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
392 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
396 /**********************************************************************
397 * THUNK_EnumChildWindows16 (USER.55)
399 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
402 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
403 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
407 /**********************************************************************
408 * THUNK_EnumTaskWindows16 (USER.225)
410 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
413 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
414 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
418 /***********************************************************************
419 * THUNK_EnumProps16 (USER.27)
421 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
423 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
424 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
428 /***********************************************************************
429 * THUNK_GrayString16 (USER.185)
431 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
432 GRAYSTRINGPROC16 func
, LPARAM lParam
,
433 INT16 cch
, INT16 x
, INT16 y
,
436 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
438 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
440 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
445 /***********************************************************************
446 * THUNK_SetWindowsHook16 (USER.121)
448 FARPROC16 WINAPI
THUNK_SetWindowsHook16( INT16 id
, HOOKPROC16 proc
)
450 HINSTANCE16 hInst
= FarGetOwner16( HIWORD(proc
) );
451 HTASK16 hTask
= (id
== WH_MSGFILTER
) ? GetCurrentTask() : 0;
452 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
453 if (!thunk
) return 0;
454 return (FARPROC16
)SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
458 /***********************************************************************
459 * THUNK_UnhookWindowsHook16 (USER.234)
461 BOOL16 WINAPI
THUNK_UnhookWindowsHook16( INT16 id
, HOOKPROC16 proc
)
464 THUNK
*thunk
= THUNK_Find( (FARPROC16
)proc
);
465 if (!thunk
) return FALSE
;
466 ret
= UnhookWindowsHook16( id
, (HOOKPROC16
)thunk
);
472 /***********************************************************************
473 * THUNK_SetWindowsHookEx16 (USER.291)
475 HHOOK WINAPI
THUNK_SetWindowsHookEx16( INT16 id
, HOOKPROC16 proc
,
476 HINSTANCE16 hInst
, HTASK16 hTask
)
478 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
479 if (!thunk
) return 0;
480 return SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
484 /***********************************************************************
485 * THUNK_UnhookWindowHookEx16 (USER.292)
487 BOOL16 WINAPI
THUNK_UnhookWindowsHookEx16( HHOOK hhook
)
489 THUNK
*thunk
= (THUNK
*)HOOK_GetProc16( hhook
);
490 BOOL16 ret
= UnhookWindowsHookEx16( hhook
);
491 if (thunk
) THUNK_Free( thunk
);
497 static FARPROC16 defDCHookProc
= NULL
;
499 /***********************************************************************
500 * THUNK_SetDCHook (GDI.190)
502 BOOL16 WINAPI
THUNK_SetDCHook( HDC16 hdc
, FARPROC16 proc
, DWORD dwHookData
)
504 THUNK
*thunk
, *oldThunk
;
506 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
508 HMODULE16 hModule
= GetModuleHandle16( "USER" );
509 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
511 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
) )
512 defDCHookProc
= NE_GetEntryPoint( hModule
, 362 );
514 defDCHookProc
= (FARPROC16
)-1;
519 else if (proc
!= defDCHookProc
)
521 thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwll
);
522 if (!thunk
) return FALSE
;
524 else thunk
= (THUNK
*)DCHook16
;
526 /* Free the previous thunk */
527 GetDCHook( hdc
, (FARPROC16
*)&oldThunk
);
528 if (oldThunk
&& (oldThunk
!= (THUNK
*)DCHook16
)) THUNK_Free( oldThunk
);
530 return SetDCHook( hdc
, (FARPROC16
)thunk
, dwHookData
);
534 /***********************************************************************
535 * THUNK_GetDCHook (GDI.191)
537 DWORD WINAPI
THUNK_GetDCHook( HDC16 hdc
, FARPROC16
*phookProc
)
540 DWORD ret
= GetDCHook( hdc
, (FARPROC16
*)&thunk
);
543 if (thunk
== (THUNK
*)DCHook16
)
545 /* Note: we can only get here when running built-in USER */
547 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
548 defDCHookProc
= NE_GetEntryPoint(GetModuleHandle16("USER"),362);
550 *phookProc
= defDCHookProc
;
552 else *phookProc
= thunk
->proc
;
558 /***********************************************************************
559 * THUNK_SetTaskSignalProc (KERNEL.38)
561 FARPROC16 WINAPI
THUNK_SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
563 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwwww
);
564 if ( !thunk
) return NULL
;
566 thunk
= (THUNK
*)SetTaskSignalProc( hTask
, (FARPROC16
)thunk
);
567 if ( !thunk
) return NULL
;
574 /***********************************************************************
575 * THUNK_CreateThread16 (KERNEL.441)
577 static DWORD CALLBACK
THUNK_StartThread16( LPVOID threadArgs
)
579 FARPROC16 start
= ((FARPROC16
*)threadArgs
)[0];
580 DWORD param
= ((DWORD
*)threadArgs
)[1];
581 HeapFree( GetProcessHeap(), 0, threadArgs
);
583 return THUNK_CallTo16_long_l( start
, param
);
585 HANDLE WINAPI
THUNK_CreateThread16( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
586 FARPROC16 start
, SEGPTR param
,
587 DWORD flags
, LPDWORD id
)
589 DWORD
*threadArgs
= HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD
) );
590 if (!threadArgs
) return INVALID_HANDLE_VALUE
;
591 threadArgs
[0] = (DWORD
)start
;
592 threadArgs
[1] = (DWORD
)param
;
594 return CreateThread( sa
, stack
, THUNK_StartThread16
, threadArgs
, flags
, id
);
597 /***********************************************************************
598 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
599 * Generic thunking routine to call 16 bit functions from 32bit code.
602 * TRUE if the call was done
604 static BOOL WINAPI
THUNK_WOWCallback16Ex(
605 FARPROC16 proc
, /* [in] 16bit function to call */
606 DWORD dwFlags
, /* [in] flags (WCB_*) */
607 DWORD cbArgs
, /* [in] number of arguments */
608 LPVOID xargs
, /* [in/out] arguments */
609 LPDWORD pdwret
/* [out] return value of the 16bit call */
611 LPDWORD args
= (LPDWORD
)xargs
;
614 TRACE_(relay
)("(%p,0x%08lx,%ld,%p,%p)\n",
615 proc
,dwFlags
,cbArgs
,xargs
,pdwret
617 if (dwFlags
== WCB16_CDECL
) {
618 /* swap the arguments */
619 args
= HeapAlloc(GetProcessHeap(),0,cbArgs
*sizeof(DWORD
));
620 for (i
=0;i
<cbArgs
;i
++)
621 args
[i
] = ((DWORD
*)xargs
)[cbArgs
-i
-1];
624 case 0: ret
= THUNK_CallTo16_long_(proc
);break;
625 case 1: ret
= THUNK_CallTo16_long_l(proc
,args
[0]);break;
626 case 2: ret
= THUNK_CallTo16_long_ll(proc
,args
[0],args
[1]);break;
627 case 3: ret
= THUNK_CallTo16_long_lll(proc
,args
[0],args
[1],args
[2]);break;
628 case 4: ret
= THUNK_CallTo16_long_llll(proc
,args
[0],args
[1],args
[2],args
[3]);
630 case 5: ret
= THUNK_CallTo16_long_lllll(proc
,args
[0],args
[1],args
[2],args
[3],
634 case 6: ret
= THUNK_CallTo16_long_llllll(proc
,args
[0],args
[1],args
[2],args
[3],
638 case 7: ret
= THUNK_CallTo16_long_lllllll(proc
,args
[0],args
[1],args
[2],args
[3],
639 args
[4],args
[5],args
[6]
642 case 8: ret
= THUNK_CallTo16_long_llllllll(proc
,args
[0],args
[1],args
[2],args
[3],
643 args
[4],args
[5],args
[6],args
[7]
646 case 9: ret
= THUNK_CallTo16_long_lllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
647 args
[4],args
[5],args
[6],args
[7],args
[8]
650 case 10:ret
= THUNK_CallTo16_long_llllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
651 args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]
654 case 11:ret
= THUNK_CallTo16_long_lllllllllll(proc
,args
[0],args
[1],args
[2],
655 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]
658 case 12:ret
= THUNK_CallTo16_long_llllllllllll(proc
,args
[0],args
[1],args
[2],
659 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
663 case 13:ret
= THUNK_CallTo16_long_lllllllllllll(proc
,args
[0],args
[1],args
[2],
664 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
665 args
[10],args
[11],args
[12]
668 case 14:ret
= THUNK_CallTo16_long_llllllllllllll(proc
,args
[0],args
[1],args
[2],
669 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
670 args
[10],args
[11],args
[12],args
[13]
673 case 15:ret
= THUNK_CallTo16_long_lllllllllllllll(proc
,args
[0],args
[1],args
[2],
674 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
675 args
[10],args
[11],args
[12],args
[13],args
[14]
678 case 16:ret
= THUNK_CallTo16_long_llllllllllllllll(proc
,args
[0],args
[1],args
[2],
679 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
680 args
[10],args
[11],args
[12],args
[13],args
[14],args
[15]
684 ERR_(thunk
)("%ld arguments not supported.\n",cbArgs
);
685 if (dwFlags
== WCB16_CDECL
)
686 HeapFree(GetProcessHeap(),0,args
);
689 if (dwFlags
== WCB16_CDECL
)
690 HeapFree(GetProcessHeap(),0,args
);
696 /***********************************************************************
697 * THUNK_MOUSE_Enable (MOUSE.2)
699 static VOID WINAPI
THUNK_CallMouseEventProc( FARPROC16 proc
,
700 DWORD dwFlags
, DWORD dx
, DWORD dy
,
701 DWORD cButtons
, DWORD dwExtraInfo
)
705 memset( &context
, 0, sizeof(context
) );
706 CS_reg(&context
) = SELECTOROF( proc
);
707 EIP_reg(&context
) = OFFSETOF( proc
);
708 AX_reg(&context
) = (WORD
)dwFlags
;
709 BX_reg(&context
) = (WORD
)dx
;
710 CX_reg(&context
) = (WORD
)dy
;
711 DX_reg(&context
) = (WORD
)cButtons
;
712 SI_reg(&context
) = LOWORD( dwExtraInfo
);
713 DI_reg(&context
) = HIWORD( dwExtraInfo
);
715 CallTo16RegisterShort( &context
, 0 );
717 VOID WINAPI
THUNK_MOUSE_Enable( FARPROC16 proc
)
719 static THUNK
*lastThunk
= NULL
;
720 static FARPROC16 lastProc
= NULL
;
722 if ( lastProc
!= proc
)
725 THUNK_Free( lastThunk
);
730 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallMouseEventProc
);
735 MOUSE_Enable( (LPMOUSE_EVENT_PROC
)lastThunk
);
738 /***********************************************************************
739 * GetMouseEventProc (USER.337)
741 FARPROC16 WINAPI
GetMouseEventProc16(void)
743 HMODULE16 hmodule
= GetModuleHandle16("USER");
744 return NE_GetEntryPoint( hmodule
, NE_GetOrdinal( hmodule
, "mouse_event" ));
748 /***********************************************************************
749 * WIN16_mouse_event (USER.299)
751 void WINAPI
WIN16_mouse_event( CONTEXT86
*context
)
753 mouse_event( AX_reg(context
), BX_reg(context
), CX_reg(context
),
754 DX_reg(context
), MAKELONG(SI_reg(context
), DI_reg(context
)) );
758 /***********************************************************************
759 * THUNK_KEYBD_Enable (KEYBOARD.2)
761 static VOID WINAPI
THUNK_CallKeybdEventProc( FARPROC16 proc
,
762 BYTE bVk
, BYTE bScan
,
763 DWORD dwFlags
, DWORD dwExtraInfo
)
767 memset( &context
, 0, sizeof(context
) );
768 CS_reg(&context
) = SELECTOROF( proc
);
769 EIP_reg(&context
) = OFFSETOF( proc
);
770 AH_reg(&context
) = (dwFlags
& KEYEVENTF_KEYUP
)? 0x80 : 0;
771 AL_reg(&context
) = bVk
;
772 BH_reg(&context
) = (dwFlags
& KEYEVENTF_EXTENDEDKEY
)? 1 : 0;
773 BL_reg(&context
) = bScan
;
774 SI_reg(&context
) = LOWORD( dwExtraInfo
);
775 DI_reg(&context
) = HIWORD( dwExtraInfo
);
777 CallTo16RegisterShort( &context
, 0 );
779 VOID WINAPI
THUNK_KEYBOARD_Enable( FARPROC16 proc
, LPBYTE lpKeyState
)
781 static THUNK
*lastThunk
= NULL
;
782 static FARPROC16 lastProc
= NULL
;
784 if ( lastProc
!= proc
)
787 THUNK_Free( lastThunk
);
792 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallKeybdEventProc
);
797 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC
)lastThunk
, lpKeyState
);
800 /***********************************************************************
801 * WIN16_keybd_event (USER.289)
803 void WINAPI
WIN16_keybd_event( CONTEXT86
*context
)
807 if (AH_reg(context
) & 0x80) dwFlags
|= KEYEVENTF_KEYUP
;
808 if (BH_reg(context
) & 1 ) dwFlags
|= KEYEVENTF_EXTENDEDKEY
;
810 keybd_event( AL_reg(context
), BL_reg(context
),
811 dwFlags
, MAKELONG(SI_reg(context
), DI_reg(context
)) );
815 /***********************************************************************
816 * WIN16_CreateSystemTimer (SYSTEM.2)
818 static void THUNK_CallSystemTimerProc( FARPROC16 proc
, WORD timer
)
821 memset( &context
, '\0', sizeof(context
) );
823 CS_reg( &context
) = SELECTOROF( proc
);
824 IP_reg( &context
) = OFFSETOF( proc
);
825 BP_reg( &context
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
826 + (WORD
)&((STACK16FRAME
*)0)->bp
;
828 AX_reg( &context
) = timer
;
830 if ( _ConfirmWin16Lock() )
832 FIXME_(system
)("Skipping timer %d callback because timer signal "
833 "arrived while we own the Win16Lock!\n", timer
);
837 CallTo16RegisterShort( &context
, 0 );
839 /* FIXME: This does not work if the signal occurs while this thread
840 is currently in 16-bit code. With the current structure
841 of the Wine thunking code, this seems to be hard to fix ... */
843 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
845 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallSystemTimerProc
);
846 WORD timer
= CreateSystemTimer( rate
, (SYSTEMTIMERPROC
)thunk
);
847 if (!timer
) THUNK_Free( thunk
);
851 /***********************************************************************
852 * THUNK_GetCalloutThunk
854 * Retrieve API entry point with given name from given module.
855 * If module is builtin, return the 32-bit entry point, otherwise
856 * create a 32->16 thunk to the 16-bit entry point, using the
860 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
862 FARPROC16 proc
= WIN32_GetProcAddress16( pModule
->self
, name
);
863 if ( !proc
) return 0;
865 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
866 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
868 return (FARPROC
)THUNK_Alloc( proc
, relay
);
871 /***********************************************************************
874 void THUNK_InitCallout(void)
879 hModule
= GetModuleHandleA( "USER32" );
882 #define GETADDR( var, name ) \
883 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
885 GETADDR( PeekMessageA
, "PeekMessageA" );
886 GETADDR( PeekMessageW
, "PeekMessageW" );
887 GETADDR( GetMessageA
, "GetMessageA" );
888 GETADDR( GetMessageW
, "GetMessageW" );
889 GETADDR( SendMessageA
, "SendMessageA" );
890 GETADDR( SendMessageW
, "SendMessageW" );
891 GETADDR( PostMessageA
, "PostMessageA" );
892 GETADDR( PostMessageW
, "PostMessageW" );
893 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
894 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
895 GETADDR( TranslateMessage
, "TranslateMessage" );
896 GETADDR( DispatchMessageW
, "DispatchMessageW" );
897 GETADDR( DispatchMessageA
, "DispatchMessageA" );
898 GETADDR( RedrawWindow
, "RedrawWindow" );
899 GETADDR( UserSignalProc
, "UserSignalProc" );
904 pModule
= NE_GetPtr( GetModuleHandle16( "USER" ) );
907 #define GETADDR( var, name, thk ) \
908 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
909 (RELAY)THUNK_CallTo16_##thk )
911 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
912 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
913 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
914 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
915 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
916 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
917 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
918 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
919 GETADDR( FinalUserInit16
, "FinalUserInit", word_
);
920 GETADDR( InitApp16
, "InitApp", word_w
);
921 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
922 GETADDR( UserYield16
, "UserYield", word_
);
923 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
929 /***********************************************************************
930 * 16->32 Flat Thunk routines:
933 /***********************************************************************
934 * ThunkConnect16 (KERNEL.651)
935 * Connects a 32bit and a 16bit thunkbuffer.
937 UINT WINAPI
ThunkConnect16(
938 LPSTR module16
, /* [in] name of win16 dll */
939 LPSTR module32
, /* [in] name of win32 dll */
940 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
941 DWORD dwReason
, /* [in] initialisation argument */
942 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
943 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
944 WORD cs
/* [in] CS of win16 dll */
948 if (!lstrncmpA(TD
->magic
, "SL01", 4))
952 TRACE_(thunk
)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
953 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
955 else if (!lstrncmpA(TD
->magic
, "LS01", 4))
959 TRACE_(thunk
)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
960 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
964 ERR_(thunk
)("Invalid magic %c%c%c%c\n",
965 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
971 case DLL_PROCESS_ATTACH
:
974 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
975 struct ThunkDataSL
*SL
= SL16
->fpData
;
979 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
981 SL
->common
= SL16
->common
;
982 SL
->flags1
= SL16
->flags1
;
983 SL
->flags2
= SL16
->flags2
;
985 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
988 lstrcpynA(SL
->pszDll16
, module16
, 255);
989 lstrcpynA(SL
->pszDll32
, module32
, 255);
991 /* We should create a SEGPTR to the ThunkDataSL,
992 but since the contents are not in the original format,
993 any access to this by 16-bit code would crash anyway. */
999 if (SL
->flags2
& 0x80000000)
1001 TRACE_(thunk
)("Preloading 32-bit library\n");
1002 LoadLibraryA(module32
);
1011 case DLL_PROCESS_DETACH
:
1012 /* FIXME: cleanup */
1020 /***********************************************************************
1021 * C16ThkSL (KERNEL.630)
1024 void WINAPI
C16ThkSL(CONTEXT86
*context
)
1026 extern void CallFrom16Thunk(void);
1027 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1032 /* We produce the following code:
1037 * mov edx, es:[ecx + $EDX]
1040 * call __FLATCS:CallFrom16_t_long_
1043 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
1044 *x
++ = 0x8E; *x
++ = 0xC0;
1045 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1046 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1047 *x
++ = 0x91; *((DWORD
*)x
)++ = EDX_reg(context
);
1050 *x
++ = 0x66; *x
++ = 0x52;
1051 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1052 *((WORD
*)x
)++ = cs
;
1054 /* Jump to the stub code just created */
1055 IP_reg(context
) = LOWORD(EAX_reg(context
));
1056 CS_reg(context
) = HIWORD(EAX_reg(context
));
1058 /* Since C16ThkSL got called by a jmp, we need to leave the
1059 orginal return address on the stack */
1060 SP_reg(context
) -= 4;
1063 /***********************************************************************
1064 * C16ThkSL01 (KERNEL.631)
1067 void WINAPI
C16ThkSL01(CONTEXT86
*context
)
1069 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1073 struct ThunkDataSL16
*SL16
= PTR_SEG_TO_LIN(EDX_reg(context
));
1074 struct ThunkDataSL
*td
= SL16
->fpData
;
1076 extern void CallFrom16Thunk(void);
1077 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1083 ERR_(thunk
)("ThunkConnect16 was not called!\n");
1087 TRACE_(thunk
)("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
1090 /* We produce the following code:
1097 * call __FLATCS:CallFrom16_t_long_
1100 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1101 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
1102 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
1105 *x
++ = 0x66; *x
++ = 0x52;
1106 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1107 *((WORD
*)x
)++ = cs
;
1109 /* Jump to the stub code just created */
1110 IP_reg(context
) = LOWORD(EAX_reg(context
));
1111 CS_reg(context
) = HIWORD(EAX_reg(context
));
1113 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1114 orginal return address on the stack */
1115 SP_reg(context
) -= 4;
1119 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
1120 DWORD targetNr
= CX_reg(context
) / 4;
1121 struct SLTargetDB
*tdb
;
1123 TRACE_(thunk
)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1124 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
1126 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1127 if (tdb
->process
== PROCESS_Current())
1132 TRACE_(thunk
)("Loading 32-bit library %s\n", td
->pszDll32
);
1133 LoadLibraryA(td
->pszDll32
);
1135 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1136 if (tdb
->process
== PROCESS_Current())
1142 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
1144 TRACE_(thunk
)("Call target is %08lx\n", EDX_reg(context
));
1148 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), SP_reg(context
));
1149 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1150 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1151 IP_reg(context
) = stack
[2];
1152 CS_reg(context
) = stack
[3];
1153 SP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1155 ERR_(thunk
)("Process %08lx did not ThunkConnect32 %s to %s\n",
1156 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
1162 WOW16Call(WORD x
,WORD y
,WORD z
) {
1165 FIXME_(thunk
)("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
1167 for (i
=0;i
<x
/2;i
++) {
1168 WORD a
= STACK16_POP(NtCurrentTeb(),2);
1171 calladdr
= STACK16_POP(NtCurrentTeb(),4);
1172 DPRINTF(") calling address was 0x%08lx\n",calladdr
);
1177 /***********************************************************************
1178 * 16<->32 Thunklet/Callback API:
1181 #include "pshpack1.h"
1182 typedef struct _THUNKLET
1197 struct _THUNKLET
*next
;
1199 #include "poppack.h"
1201 #define THUNKLET_TYPE_LS 1
1202 #define THUNKLET_TYPE_SL 2
1204 static HANDLE ThunkletHeap
= 0;
1205 static THUNKLET
*ThunkletAnchor
= NULL
;
1207 static FARPROC ThunkletSysthunkGlueLS
= 0;
1208 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1210 static FARPROC ThunkletCallbackGlueLS
= 0;
1211 static SEGPTR ThunkletCallbackGlueSL
= 0;
1213 /***********************************************************************
1214 * THUNK_ThunkletInit
1216 static BOOL
THUNK_ThunkletInit( void )
1220 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
1221 if (!ThunkletHeap
) return FALSE
;
1223 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1224 if (!thunk
) return FALSE
;
1226 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1227 *thunk
++ = 0x58; /* popl eax */
1228 *thunk
++ = 0xC3; /* ret */
1230 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1231 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1232 *thunk
++ = 0xCB; /* lret */
1237 /***********************************************************************
1238 * SetThunkletCallbackGlue (KERNEL.560)
1240 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1242 ThunkletCallbackGlueLS
= glueLS
;
1243 ThunkletCallbackGlueSL
= glueSL
;
1247 /***********************************************************************
1248 * THUNK_FindThunklet
1250 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1251 DWORD glue
, BYTE type
)
1255 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1256 if ( thunk
->type
== type
1257 && thunk
->target
== target
1258 && thunk
->relay
== relay
1259 && ( type
== THUNKLET_TYPE_LS
?
1260 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
1261 : ( thunk
->glue
== glue
) ) )
1267 /***********************************************************************
1268 * THUNK_AllocLSThunklet
1270 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1271 FARPROC glue
, HTASK16 owner
)
1273 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1277 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1279 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1282 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1283 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1284 thunk
->jmp_glue
= 0xE9;
1286 thunk
->target
= (DWORD
)target
;
1287 thunk
->relay
= (DWORD
)relay
;
1288 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1290 thunk
->type
= THUNKLET_TYPE_LS
;
1291 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1293 thunk
->next
= ThunkletAnchor
;
1294 ThunkletAnchor
= thunk
;
1297 return (FARPROC
)thunk
;
1300 /***********************************************************************
1301 * THUNK_AllocSLThunklet
1303 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1304 SEGPTR glue
, HTASK16 owner
)
1306 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1310 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1312 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1315 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1316 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1317 thunk
->jmp_glue
= 0xEA;
1319 thunk
->target
= (DWORD
)target
;
1320 thunk
->relay
= (DWORD
)relay
;
1321 thunk
->glue
= (DWORD
)glue
;
1323 thunk
->type
= THUNKLET_TYPE_SL
;
1324 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1326 thunk
->next
= ThunkletAnchor
;
1327 ThunkletAnchor
= thunk
;
1330 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1333 /**********************************************************************
1336 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
1338 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1339 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1340 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1343 /**********************************************************************
1344 * IsSLThunklet (KERNEL.612)
1346 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1348 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1349 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1350 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1355 /***********************************************************************
1356 * AllocLSThunkletSysthunk (KERNEL.607)
1358 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1359 FARPROC relay
, DWORD dummy
)
1361 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1362 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1365 /***********************************************************************
1366 * AllocSLThunkletSysthunk (KERNEL.608)
1368 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1369 SEGPTR relay
, DWORD dummy
)
1371 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1372 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1376 /***********************************************************************
1377 * AllocLSThunkletCallbackEx (KERNEL.567)
1379 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1380 DWORD relay
, HTASK16 task
)
1382 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1383 if ( !thunk
) return NULL
;
1385 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1386 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1387 return (FARPROC
)thunk
->target
;
1389 return THUNK_AllocLSThunklet( target
, relay
,
1390 ThunkletCallbackGlueLS
, task
);
1393 /***********************************************************************
1394 * AllocSLThunkletCallbackEx (KERNEL.568)
1396 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1397 DWORD relay
, HTASK16 task
)
1399 THUNKLET
*thunk
= (THUNKLET
*)target
;
1400 if ( !thunk
) return 0;
1402 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1403 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1404 return (SEGPTR
)thunk
->target
;
1406 return THUNK_AllocSLThunklet( target
, relay
,
1407 ThunkletCallbackGlueSL
, task
);
1410 /***********************************************************************
1411 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1413 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1415 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1418 /***********************************************************************
1419 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1421 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1423 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1426 /***********************************************************************
1427 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1429 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1431 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1432 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1433 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1434 return (FARPROC
)thunk
->target
;
1436 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1437 (DWORD
)ThunkletCallbackGlueLS
,
1439 return (FARPROC
)thunk
;
1442 /***********************************************************************
1443 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1445 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1447 THUNKLET
*thunk
= (THUNKLET
*)target
;
1448 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1449 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1450 return (SEGPTR
)thunk
->target
;
1452 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1453 (DWORD
)ThunkletCallbackGlueSL
,
1455 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1459 /***********************************************************************
1460 * FreeThunklet16 (KERNEL.611)
1462 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
1467 /***********************************************************************
1468 * Callback Client API
1471 #define N_CBC_FIXED 20
1472 #define N_CBC_VARIABLE 10
1473 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1475 static SEGPTR
*CBClientRelay16
[ N_CBC_TOTAL
];
1476 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1478 /***********************************************************************
1479 * RegisterCBClient (KERNEL.619)
1481 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1482 SEGPTR
*relay16
, FARPROC
*relay32
)
1484 /* Search for free Callback ID */
1486 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1487 if ( !CBClientRelay16
[ wCBCId
] )
1490 /* Register Callback ID */
1491 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1493 CBClientRelay16
[ wCBCId
] = relay16
;
1494 CBClientRelay32
[ wCBCId
] = relay32
;
1502 /***********************************************************************
1503 * UnRegisterCBClient (KERNEL.622)
1505 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1506 SEGPTR
*relay16
, FARPROC
*relay32
)
1508 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1509 && CBClientRelay16
[ wCBCId
] == relay16
1510 && CBClientRelay32
[ wCBCId
] == relay32
)
1512 CBClientRelay16
[ wCBCId
] = 0;
1513 CBClientRelay32
[ wCBCId
] = 0;
1522 /***********************************************************************
1523 * InitCBClient (KERNEL.623)
1525 void WINAPI
InitCBClient16( FARPROC glueLS
)
1527 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1528 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
1530 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1533 /***********************************************************************
1534 * CBClientGlueSL (KERNEL.604)
1536 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1538 /* Create stack frame */
1539 SEGPTR stackSeg
= STACK16_PUSH( NtCurrentTeb(), 12 );
1540 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1543 stackLin
[3] = BP_reg( context
);
1544 stackLin
[2] = SI_reg( context
);
1545 stackLin
[1] = DI_reg( context
);
1546 stackLin
[0] = DS_reg( context
);
1548 BP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1549 SP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1550 GS_reg( context
) = 0;
1552 /* Jump to 16-bit relay code */
1553 glue
= CBClientRelay16
[ stackLin
[5] ][ stackLin
[4] ];
1554 CS_reg ( context
) = SELECTOROF( glue
);
1555 EIP_reg( context
) = OFFSETOF ( glue
);
1558 /***********************************************************************
1559 * CBClientThunkSL (KERNEL.620)
1561 extern DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1562 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1564 /* Call 32-bit relay code */
1566 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1567 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1569 EAX_reg(context
) = CALL32_CBClient( proc
, args
, &ESI_reg( context
) );
1572 /***********************************************************************
1573 * CBClientThunkSLEx (KERNEL.621)
1575 extern DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
, LPWORD args
,
1576 DWORD
*esi
, INT
*nArgs
);
1577 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1579 /* Call 32-bit relay code */
1581 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1582 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1586 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &ESI_reg( context
), &nArgs
);
1588 /* Restore registers saved by CBClientGlueSL */
1589 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1590 BP_reg( context
) = stackLin
[3];
1591 SI_reg( context
) = stackLin
[2];
1592 DI_reg( context
) = stackLin
[1];
1593 DS_reg( context
) = stackLin
[0];
1594 SP_reg( context
) += 16+nArgs
;
1596 /* Return to caller of CBClient thunklet */
1597 CS_reg ( context
) = stackLin
[9];
1598 EIP_reg( context
) = stackLin
[8];