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 */
180 /***********************************************************************
183 BOOL
THUNK_Init(void)
185 /* Set the window proc calling functions */
186 Callbacks
= &CALLBACK_EmulatorTable
;
187 /* Initialize Thunklets */
188 return THUNK_ThunkletInit();
191 /***********************************************************************
194 static THUNK
*THUNK_Alloc( FARPROC func
, RELAY relay
)
196 THUNK
*thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
199 thunk
->popl_eax
= 0x58;
200 thunk
->pushl_func
= 0x68;
202 thunk
->pushl_eax
= 0x50;
204 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
205 thunk
->next
= firstThunk
;
212 /***********************************************************************
215 static THUNK
*THUNK_Find( FARPROC func
)
217 THUNK
*thunk
= firstThunk
;
218 while (thunk
&& (thunk
->proc
!= func
)) thunk
= thunk
->next
;
223 /***********************************************************************
226 static void THUNK_Free( THUNK
*thunk
)
228 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk
))
230 THUNK
**prev
= &firstThunk
;
231 while (*prev
&& (*prev
!= thunk
)) prev
= &(*prev
)->next
;
235 HeapFree( GetProcessHeap(), 0, thunk
);
239 ERR_(thunk
)("invalid thunk addr %p\n", thunk
);
243 /***********************************************************************
244 * THUNK_CallWndProc16
246 * Call a 16-bit window procedure
248 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
249 UINT16 msg
, WPARAM16 wParam
,
255 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
257 TEB
*teb
= NtCurrentTeb();
260 /* Window procedures want ax = hInstance, ds = es = ss */
262 memset(&context
, '\0', sizeof(context
));
263 DS_reg(&context
) = SELECTOROF(teb
->cur_stack
);
264 ES_reg(&context
) = DS_reg(&context
);
265 EAX_reg(&context
) = wndPtr
? wndPtr
->hInstance
: DS_reg(&context
);
266 CS_reg(&context
) = SELECTOROF(proc
);
267 EIP_reg(&context
) = OFFSETOF(proc
);
268 EBP_reg(&context
) = OFFSETOF(teb
->cur_stack
)
269 + (WORD
)&((STACK16FRAME
*)0)->bp
;
271 WIN_ReleaseWndPtr(wndPtr
);
275 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
276 work if structures passed in lParam are placed in the stack/data
277 segment. Programmers easily make the mistake of converting lParam
278 to a near rather than a far pointer, since Windows apparently
279 allows this. We copy the structures to the 16 bit stack; this is
280 ugly but makes these programs work. */
285 offset
= sizeof(CREATESTRUCT16
); break;
287 offset
= sizeof(DRAWITEMSTRUCT16
); break;
289 offset
= sizeof(COMPAREITEMSTRUCT16
); break;
293 void *s
= PTR_SEG_TO_LIN(lParam
);
294 lParam
= STACK16_PUSH( teb
, offset
);
295 memcpy( PTR_SEG_TO_LIN(lParam
), s
, offset
);
299 iWndsLocks
= WIN_SuspendWndsLock();
301 args
= (WORD
*)THREAD_STACK16(teb
) - 5;
302 args
[0] = LOWORD(lParam
);
303 args
[1] = HIWORD(lParam
);
308 ret
= CallTo16RegisterShort( &context
, 5 * sizeof(WORD
) );
309 if (offset
) STACK16_POP( teb
, offset
);
311 WIN_RestoreWndsLock(iWndsLocks
);
317 /***********************************************************************
318 * THUNK_EnumObjects16 (GDI.71)
320 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
321 GOBJENUMPROC16 func
, LPARAM lParam
)
323 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_ll
);
324 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
328 /*************************************************************************
329 * THUNK_EnumFonts16 (GDI.70)
331 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
332 FONTENUMPROC16 func
, LPARAM lParam
)
334 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
335 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
338 /******************************************************************
339 * THUNK_EnumMetaFile16 (GDI.175)
341 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
342 MFENUMPROC16 func
, LPARAM lParam
)
344 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wllwl
);
345 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
349 /*************************************************************************
350 * THUNK_EnumFontFamilies16 (GDI.330)
352 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
353 FONTENUMPROC16 func
, LPARAM lParam
)
355 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
356 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
360 /*************************************************************************
361 * THUNK_EnumFontFamiliesEx16 (GDI.613)
363 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
364 FONTENUMPROCEX16 func
, LPARAM lParam
,
367 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
368 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
373 /**********************************************************************
374 * THUNK_LineDDA16 (GDI.100)
376 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
377 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
379 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wwl
);
380 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
384 /*******************************************************************
385 * THUNK_EnumWindows16 (USER.54)
387 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
389 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
390 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
394 /**********************************************************************
395 * THUNK_EnumChildWindows16 (USER.55)
397 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
400 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
401 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
405 /**********************************************************************
406 * THUNK_EnumTaskWindows16 (USER.225)
408 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
411 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
412 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
416 /***********************************************************************
417 * THUNK_EnumProps16 (USER.27)
419 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
421 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
422 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
426 /***********************************************************************
427 * THUNK_GrayString16 (USER.185)
429 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
430 GRAYSTRINGPROC16 func
, LPARAM lParam
,
431 INT16 cch
, INT16 x
, INT16 y
,
434 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
436 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
438 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
443 /***********************************************************************
444 * THUNK_SetWindowsHook16 (USER.121)
446 FARPROC16 WINAPI
THUNK_SetWindowsHook16( INT16 id
, HOOKPROC16 proc
)
448 HINSTANCE16 hInst
= FarGetOwner16( HIWORD(proc
) );
449 HTASK16 hTask
= (id
== WH_MSGFILTER
) ? GetCurrentTask() : 0;
450 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
451 if (!thunk
) return 0;
452 return (FARPROC16
)SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
456 /***********************************************************************
457 * THUNK_UnhookWindowsHook16 (USER.234)
459 BOOL16 WINAPI
THUNK_UnhookWindowsHook16( INT16 id
, HOOKPROC16 proc
)
462 THUNK
*thunk
= THUNK_Find( (FARPROC16
)proc
);
463 if (!thunk
) return FALSE
;
464 ret
= UnhookWindowsHook16( id
, (HOOKPROC16
)thunk
);
470 /***********************************************************************
471 * THUNK_SetWindowsHookEx16 (USER.291)
473 HHOOK WINAPI
THUNK_SetWindowsHookEx16( INT16 id
, HOOKPROC16 proc
,
474 HINSTANCE16 hInst
, HTASK16 hTask
)
476 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)THUNK_CallTo16_long_wwl
);
477 if (!thunk
) return 0;
478 return SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
482 /***********************************************************************
483 * THUNK_UnhookWindowHookEx16 (USER.292)
485 BOOL16 WINAPI
THUNK_UnhookWindowsHookEx16( HHOOK hhook
)
487 THUNK
*thunk
= (THUNK
*)HOOK_GetProc16( hhook
);
488 BOOL16 ret
= UnhookWindowsHookEx16( hhook
);
489 if (thunk
) THUNK_Free( thunk
);
495 static FARPROC16 defDCHookProc
= NULL
;
497 /***********************************************************************
498 * THUNK_SetDCHook (GDI.190)
500 BOOL16 WINAPI
THUNK_SetDCHook( HDC16 hdc
, FARPROC16 proc
, DWORD dwHookData
)
502 THUNK
*thunk
, *oldThunk
;
504 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
506 HMODULE16 hModule
= GetModuleHandle16( "USER" );
507 NE_MODULE
*pModule
= NE_GetPtr( hModule
);
509 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
) )
510 defDCHookProc
= NE_GetEntryPoint( hModule
, 362 );
512 defDCHookProc
= (FARPROC16
)-1;
517 else if (proc
!= defDCHookProc
)
519 thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwll
);
520 if (!thunk
) return FALSE
;
522 else thunk
= (THUNK
*)DCHook16
;
524 /* Free the previous thunk */
525 GetDCHook( hdc
, (FARPROC16
*)&oldThunk
);
526 if (oldThunk
&& (oldThunk
!= (THUNK
*)DCHook16
)) THUNK_Free( oldThunk
);
528 return SetDCHook( hdc
, (FARPROC16
)thunk
, dwHookData
);
532 /***********************************************************************
533 * THUNK_GetDCHook (GDI.191)
535 DWORD WINAPI
THUNK_GetDCHook( HDC16 hdc
, FARPROC16
*phookProc
)
538 DWORD ret
= GetDCHook( hdc
, (FARPROC16
*)&thunk
);
541 if (thunk
== (THUNK
*)DCHook16
)
543 /* Note: we can only get here when running built-in USER */
545 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
546 defDCHookProc
= NE_GetEntryPoint(GetModuleHandle16("USER"),362);
548 *phookProc
= defDCHookProc
;
550 else *phookProc
= thunk
->proc
;
556 /***********************************************************************
557 * THUNK_SetTaskSignalProc (KERNEL.38)
559 FARPROC16 WINAPI
THUNK_SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
561 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallTo16_word_wwwww
);
562 if ( !thunk
) return NULL
;
564 thunk
= (THUNK
*)SetTaskSignalProc( hTask
, (FARPROC16
)thunk
);
565 if ( !thunk
) return NULL
;
572 /***********************************************************************
573 * THUNK_CreateThread16 (KERNEL.441)
575 static DWORD CALLBACK
THUNK_StartThread16( LPVOID threadArgs
)
577 FARPROC16 start
= ((FARPROC16
*)threadArgs
)[0];
578 DWORD param
= ((DWORD
*)threadArgs
)[1];
579 HeapFree( GetProcessHeap(), 0, threadArgs
);
581 return THUNK_CallTo16_long_l( start
, param
);
583 HANDLE WINAPI
THUNK_CreateThread16( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
584 FARPROC16 start
, SEGPTR param
,
585 DWORD flags
, LPDWORD id
)
587 DWORD
*threadArgs
= HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD
) );
588 if (!threadArgs
) return INVALID_HANDLE_VALUE
;
589 threadArgs
[0] = (DWORD
)start
;
590 threadArgs
[1] = (DWORD
)param
;
592 return CreateThread( sa
, stack
, THUNK_StartThread16
, threadArgs
, flags
, id
);
595 /***********************************************************************
596 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
597 * Generic thunking routine to call 16 bit functions from 32bit code.
600 * TRUE if the call was done
602 static BOOL WINAPI
THUNK_WOWCallback16Ex(
603 FARPROC16 proc
, /* [in] 16bit function to call */
604 DWORD dwFlags
, /* [in] flags (WCB_*) */
605 DWORD cbArgs
, /* [in] number of arguments */
606 LPVOID xargs
, /* [in/out] arguments */
607 LPDWORD pdwret
/* [out] return value of the 16bit call */
609 LPDWORD args
= (LPDWORD
)xargs
;
612 TRACE_(relay
)("(%p,0x%08lx,%ld,%p,%p)\n",
613 proc
,dwFlags
,cbArgs
,xargs
,pdwret
615 if (dwFlags
== WCB16_CDECL
) {
616 /* swap the arguments */
617 args
= HeapAlloc(GetProcessHeap(),0,cbArgs
*sizeof(DWORD
));
618 for (i
=0;i
<cbArgs
;i
++)
619 args
[i
] = ((DWORD
*)xargs
)[cbArgs
-i
-1];
622 case 0: ret
= THUNK_CallTo16_long_(proc
);break;
623 case 1: ret
= THUNK_CallTo16_long_l(proc
,args
[0]);break;
624 case 2: ret
= THUNK_CallTo16_long_ll(proc
,args
[0],args
[1]);break;
625 case 3: ret
= THUNK_CallTo16_long_lll(proc
,args
[0],args
[1],args
[2]);break;
626 case 4: ret
= THUNK_CallTo16_long_llll(proc
,args
[0],args
[1],args
[2],args
[3]);
628 case 5: ret
= THUNK_CallTo16_long_lllll(proc
,args
[0],args
[1],args
[2],args
[3],
632 case 6: ret
= THUNK_CallTo16_long_llllll(proc
,args
[0],args
[1],args
[2],args
[3],
636 case 7: ret
= THUNK_CallTo16_long_lllllll(proc
,args
[0],args
[1],args
[2],args
[3],
637 args
[4],args
[5],args
[6]
640 case 8: ret
= THUNK_CallTo16_long_llllllll(proc
,args
[0],args
[1],args
[2],args
[3],
641 args
[4],args
[5],args
[6],args
[7]
644 case 9: ret
= THUNK_CallTo16_long_lllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
645 args
[4],args
[5],args
[6],args
[7],args
[8]
648 case 10:ret
= THUNK_CallTo16_long_llllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
649 args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]
652 case 11:ret
= THUNK_CallTo16_long_lllllllllll(proc
,args
[0],args
[1],args
[2],
653 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]
656 case 12:ret
= THUNK_CallTo16_long_llllllllllll(proc
,args
[0],args
[1],args
[2],
657 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
661 case 13:ret
= THUNK_CallTo16_long_lllllllllllll(proc
,args
[0],args
[1],args
[2],
662 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
663 args
[10],args
[11],args
[12]
666 case 14:ret
= THUNK_CallTo16_long_llllllllllllll(proc
,args
[0],args
[1],args
[2],
667 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
668 args
[10],args
[11],args
[12],args
[13]
671 case 15:ret
= THUNK_CallTo16_long_lllllllllllllll(proc
,args
[0],args
[1],args
[2],
672 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
673 args
[10],args
[11],args
[12],args
[13],args
[14]
676 case 16:ret
= THUNK_CallTo16_long_llllllllllllllll(proc
,args
[0],args
[1],args
[2],
677 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
678 args
[10],args
[11],args
[12],args
[13],args
[14],args
[15]
682 ERR_(thunk
)("%ld arguments not supported.\n",cbArgs
);
683 if (dwFlags
== WCB16_CDECL
)
684 HeapFree(GetProcessHeap(),0,args
);
687 if (dwFlags
== WCB16_CDECL
)
688 HeapFree(GetProcessHeap(),0,args
);
694 /***********************************************************************
695 * THUNK_MOUSE_Enable (MOUSE.2)
697 static VOID WINAPI
THUNK_CallMouseEventProc( FARPROC16 proc
,
698 DWORD dwFlags
, DWORD dx
, DWORD dy
,
699 DWORD cButtons
, DWORD dwExtraInfo
)
703 memset( &context
, 0, sizeof(context
) );
704 CS_reg(&context
) = SELECTOROF( proc
);
705 EIP_reg(&context
) = OFFSETOF( proc
);
706 AX_reg(&context
) = (WORD
)dwFlags
;
707 BX_reg(&context
) = (WORD
)dx
;
708 CX_reg(&context
) = (WORD
)dy
;
709 DX_reg(&context
) = (WORD
)cButtons
;
710 SI_reg(&context
) = LOWORD( dwExtraInfo
);
711 DI_reg(&context
) = HIWORD( dwExtraInfo
);
713 CallTo16RegisterShort( &context
, 0 );
715 VOID WINAPI
THUNK_MOUSE_Enable( FARPROC16 proc
)
717 static THUNK
*lastThunk
= NULL
;
718 static FARPROC16 lastProc
= NULL
;
720 if ( lastProc
!= proc
)
723 THUNK_Free( lastThunk
);
728 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallMouseEventProc
);
733 MOUSE_Enable( (LPMOUSE_EVENT_PROC
)lastThunk
);
736 /***********************************************************************
737 * GetMouseEventProc (USER.337)
739 FARPROC16 WINAPI
GetMouseEventProc16(void)
741 HMODULE16 hmodule
= GetModuleHandle16("USER");
742 return NE_GetEntryPoint( hmodule
, NE_GetOrdinal( hmodule
, "mouse_event" ));
746 /***********************************************************************
747 * WIN16_mouse_event (USER.299)
749 void WINAPI
WIN16_mouse_event( CONTEXT86
*context
)
751 mouse_event( AX_reg(context
), BX_reg(context
), CX_reg(context
),
752 DX_reg(context
), MAKELONG(SI_reg(context
), DI_reg(context
)) );
756 /***********************************************************************
757 * THUNK_KEYBD_Enable (KEYBOARD.2)
759 static VOID WINAPI
THUNK_CallKeybdEventProc( FARPROC16 proc
,
760 BYTE bVk
, BYTE bScan
,
761 DWORD dwFlags
, DWORD dwExtraInfo
)
765 memset( &context
, 0, sizeof(context
) );
766 CS_reg(&context
) = SELECTOROF( proc
);
767 EIP_reg(&context
) = OFFSETOF( proc
);
768 AH_reg(&context
) = (dwFlags
& KEYEVENTF_KEYUP
)? 0x80 : 0;
769 AL_reg(&context
) = bVk
;
770 BH_reg(&context
) = (dwFlags
& KEYEVENTF_EXTENDEDKEY
)? 1 : 0;
771 BL_reg(&context
) = bScan
;
772 SI_reg(&context
) = LOWORD( dwExtraInfo
);
773 DI_reg(&context
) = HIWORD( dwExtraInfo
);
775 CallTo16RegisterShort( &context
, 0 );
777 VOID WINAPI
THUNK_KEYBOARD_Enable( FARPROC16 proc
, LPBYTE lpKeyState
)
779 static THUNK
*lastThunk
= NULL
;
780 static FARPROC16 lastProc
= NULL
;
782 if ( lastProc
!= proc
)
785 THUNK_Free( lastThunk
);
790 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallKeybdEventProc
);
795 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC
)lastThunk
, lpKeyState
);
798 /***********************************************************************
799 * WIN16_keybd_event (USER.289)
801 void WINAPI
WIN16_keybd_event( CONTEXT86
*context
)
805 if (AH_reg(context
) & 0x80) dwFlags
|= KEYEVENTF_KEYUP
;
806 if (BH_reg(context
) & 1 ) dwFlags
|= KEYEVENTF_EXTENDEDKEY
;
808 keybd_event( AL_reg(context
), BL_reg(context
),
809 dwFlags
, MAKELONG(SI_reg(context
), DI_reg(context
)) );
813 /***********************************************************************
814 * WIN16_CreateSystemTimer (SYSTEM.2)
816 static void THUNK_CallSystemTimerProc( FARPROC16 proc
, WORD timer
)
819 memset( &context
, '\0', sizeof(context
) );
821 CS_reg( &context
) = SELECTOROF( proc
);
822 IP_reg( &context
) = OFFSETOF( proc
);
823 BP_reg( &context
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
824 + (WORD
)&((STACK16FRAME
*)0)->bp
;
826 AX_reg( &context
) = timer
;
828 if ( _ConfirmWin16Lock() )
830 FIXME_(system
)("Skipping timer %d callback because timer signal "
831 "arrived while we own the Win16Lock!\n", timer
);
835 CallTo16RegisterShort( &context
, 0 );
837 /* FIXME: This does not work if the signal occurs while this thread
838 is currently in 16-bit code. With the current structure
839 of the Wine thunking code, this seems to be hard to fix ... */
841 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
843 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallSystemTimerProc
);
844 WORD timer
= CreateSystemTimer( rate
, (SYSTEMTIMERPROC
)thunk
);
845 if (!timer
) THUNK_Free( thunk
);
849 /***********************************************************************
850 * THUNK_GetCalloutThunk
852 * Retrieve API entry point with given name from given module.
853 * If module is builtin, return the 32-bit entry point, otherwise
854 * create a 32->16 thunk to the 16-bit entry point, using the
858 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
860 FARPROC16 proc
= WIN32_GetProcAddress16( pModule
->self
, name
);
861 if ( !proc
) return 0;
863 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
864 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
866 return (FARPROC
)THUNK_Alloc( proc
, relay
);
869 /***********************************************************************
872 void THUNK_InitCallout(void)
877 hModule
= GetModuleHandleA( "USER32" );
880 #define GETADDR( var, name ) \
881 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
883 GETADDR( PeekMessageA
, "PeekMessageA" );
884 GETADDR( PeekMessageW
, "PeekMessageW" );
885 GETADDR( GetMessageA
, "GetMessageA" );
886 GETADDR( GetMessageW
, "GetMessageW" );
887 GETADDR( SendMessageA
, "SendMessageA" );
888 GETADDR( SendMessageW
, "SendMessageW" );
889 GETADDR( PostMessageA
, "PostMessageA" );
890 GETADDR( PostMessageW
, "PostMessageW" );
891 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
892 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
893 GETADDR( TranslateMessage
, "TranslateMessage" );
894 GETADDR( DispatchMessageW
, "DispatchMessageW" );
895 GETADDR( DispatchMessageA
, "DispatchMessageA" );
896 GETADDR( RedrawWindow
, "RedrawWindow" );
897 GETADDR( UserSignalProc
, "UserSignalProc" );
902 pModule
= NE_GetPtr( GetModuleHandle16( "USER" ) );
905 #define GETADDR( var, name, thk ) \
906 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
907 (RELAY)THUNK_CallTo16_##thk )
909 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
910 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
911 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
912 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
913 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
914 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
915 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
916 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
917 GETADDR( InitApp16
, "InitApp", word_w
);
918 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
919 GETADDR( UserYield16
, "UserYield", word_
);
920 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
926 /***********************************************************************
927 * 16->32 Flat Thunk routines:
930 /***********************************************************************
931 * ThunkConnect16 (KERNEL.651)
932 * Connects a 32bit and a 16bit thunkbuffer.
934 UINT WINAPI
ThunkConnect16(
935 LPSTR module16
, /* [in] name of win16 dll */
936 LPSTR module32
, /* [in] name of win32 dll */
937 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
938 DWORD dwReason
, /* [in] initialisation argument */
939 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
940 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
941 WORD cs
/* [in] CS of win16 dll */
945 if (!lstrncmpA(TD
->magic
, "SL01", 4))
949 TRACE_(thunk
)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
950 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
952 else if (!lstrncmpA(TD
->magic
, "LS01", 4))
956 TRACE_(thunk
)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
957 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
961 ERR_(thunk
)("Invalid magic %c%c%c%c\n",
962 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
968 case DLL_PROCESS_ATTACH
:
971 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
972 struct ThunkDataSL
*SL
= SL16
->fpData
;
976 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
978 SL
->common
= SL16
->common
;
979 SL
->flags1
= SL16
->flags1
;
980 SL
->flags2
= SL16
->flags2
;
982 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
985 lstrcpynA(SL
->pszDll16
, module16
, 255);
986 lstrcpynA(SL
->pszDll32
, module32
, 255);
988 /* We should create a SEGPTR to the ThunkDataSL,
989 but since the contents are not in the original format,
990 any access to this by 16-bit code would crash anyway. */
996 if (SL
->flags2
& 0x80000000)
998 TRACE_(thunk
)("Preloading 32-bit library\n");
999 LoadLibraryA(module32
);
1008 case DLL_PROCESS_DETACH
:
1009 /* FIXME: cleanup */
1017 /***********************************************************************
1018 * C16ThkSL (KERNEL.630)
1021 void WINAPI
C16ThkSL(CONTEXT86
*context
)
1023 extern void CallFrom16Thunk(void);
1024 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1029 /* We produce the following code:
1034 * mov edx, es:[ecx + $EDX]
1037 * call __FLATCS:CallFrom16_t_long_
1040 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
1041 *x
++ = 0x8E; *x
++ = 0xC0;
1042 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1043 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1044 *x
++ = 0x91; *((DWORD
*)x
)++ = EDX_reg(context
);
1047 *x
++ = 0x66; *x
++ = 0x52;
1048 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1049 *((WORD
*)x
)++ = cs
;
1051 /* Jump to the stub code just created */
1052 IP_reg(context
) = LOWORD(EAX_reg(context
));
1053 CS_reg(context
) = HIWORD(EAX_reg(context
));
1055 /* Since C16ThkSL got called by a jmp, we need to leave the
1056 orginal return address on the stack */
1057 SP_reg(context
) -= 4;
1060 /***********************************************************************
1061 * C16ThkSL01 (KERNEL.631)
1064 void WINAPI
C16ThkSL01(CONTEXT86
*context
)
1066 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1070 struct ThunkDataSL16
*SL16
= PTR_SEG_TO_LIN(EDX_reg(context
));
1071 struct ThunkDataSL
*td
= SL16
->fpData
;
1073 extern void CallFrom16Thunk(void);
1074 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1080 ERR_(thunk
)("ThunkConnect16 was not called!\n");
1084 TRACE_(thunk
)("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
1087 /* We produce the following code:
1094 * call __FLATCS:CallFrom16_t_long_
1097 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1098 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
1099 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
1102 *x
++ = 0x66; *x
++ = 0x52;
1103 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
1104 *((WORD
*)x
)++ = cs
;
1106 /* Jump to the stub code just created */
1107 IP_reg(context
) = LOWORD(EAX_reg(context
));
1108 CS_reg(context
) = HIWORD(EAX_reg(context
));
1110 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1111 orginal return address on the stack */
1112 SP_reg(context
) -= 4;
1116 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
1117 DWORD targetNr
= CX_reg(context
) / 4;
1118 struct SLTargetDB
*tdb
;
1120 TRACE_(thunk
)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1121 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
1123 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1124 if (tdb
->process
== PROCESS_Current())
1129 TRACE_(thunk
)("Loading 32-bit library %s\n", td
->pszDll32
);
1130 LoadLibraryA(td
->pszDll32
);
1132 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1133 if (tdb
->process
== PROCESS_Current())
1139 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
1141 TRACE_(thunk
)("Call target is %08lx\n", EDX_reg(context
));
1145 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), SP_reg(context
));
1146 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1147 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1148 IP_reg(context
) = stack
[2];
1149 CS_reg(context
) = stack
[3];
1150 SP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1152 ERR_(thunk
)("Process %08lx did not ThunkConnect32 %s to %s\n",
1153 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
1159 WOW16Call(WORD x
,WORD y
,WORD z
) {
1162 FIXME_(thunk
)("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
1164 for (i
=0;i
<x
/2;i
++) {
1165 WORD a
= STACK16_POP(NtCurrentTeb(),2);
1168 calladdr
= STACK16_POP(NtCurrentTeb(),4);
1169 DPRINTF(") calling address was 0x%08lx\n",calladdr
);
1174 /***********************************************************************
1175 * 16<->32 Thunklet/Callback API:
1178 #include "pshpack1.h"
1179 typedef struct _THUNKLET
1194 struct _THUNKLET
*next
;
1196 #include "poppack.h"
1198 #define THUNKLET_TYPE_LS 1
1199 #define THUNKLET_TYPE_SL 2
1201 static HANDLE ThunkletHeap
= 0;
1202 static THUNKLET
*ThunkletAnchor
= NULL
;
1204 static FARPROC ThunkletSysthunkGlueLS
= 0;
1205 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1207 static FARPROC ThunkletCallbackGlueLS
= 0;
1208 static SEGPTR ThunkletCallbackGlueSL
= 0;
1210 /***********************************************************************
1211 * THUNK_ThunkletInit
1213 static BOOL
THUNK_ThunkletInit( void )
1217 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
1218 if (!ThunkletHeap
) return FALSE
;
1220 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1221 if (!thunk
) return FALSE
;
1223 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1224 *thunk
++ = 0x58; /* popl eax */
1225 *thunk
++ = 0xC3; /* ret */
1227 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1228 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1229 *thunk
++ = 0xCB; /* lret */
1234 /***********************************************************************
1235 * SetThunkletCallbackGlue (KERNEL.560)
1237 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1239 ThunkletCallbackGlueLS
= glueLS
;
1240 ThunkletCallbackGlueSL
= glueSL
;
1244 /***********************************************************************
1245 * THUNK_FindThunklet
1247 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1248 DWORD glue
, BYTE type
)
1252 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1253 if ( thunk
->type
== type
1254 && thunk
->target
== target
1255 && thunk
->relay
== relay
1256 && ( type
== THUNKLET_TYPE_LS
?
1257 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
1258 : ( thunk
->glue
== glue
) ) )
1264 /***********************************************************************
1265 * THUNK_AllocLSThunklet
1267 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1268 FARPROC glue
, HTASK16 owner
)
1270 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1274 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1276 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1279 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1280 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1281 thunk
->jmp_glue
= 0xE9;
1283 thunk
->target
= (DWORD
)target
;
1284 thunk
->relay
= (DWORD
)relay
;
1285 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1287 thunk
->type
= THUNKLET_TYPE_LS
;
1288 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1290 thunk
->next
= ThunkletAnchor
;
1291 ThunkletAnchor
= thunk
;
1294 return (FARPROC
)thunk
;
1297 /***********************************************************************
1298 * THUNK_AllocSLThunklet
1300 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1301 SEGPTR glue
, HTASK16 owner
)
1303 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1307 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1309 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1312 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1313 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1314 thunk
->jmp_glue
= 0xEA;
1316 thunk
->target
= (DWORD
)target
;
1317 thunk
->relay
= (DWORD
)relay
;
1318 thunk
->glue
= (DWORD
)glue
;
1320 thunk
->type
= THUNKLET_TYPE_SL
;
1321 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1323 thunk
->next
= ThunkletAnchor
;
1324 ThunkletAnchor
= thunk
;
1327 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1330 /**********************************************************************
1333 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
1335 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1336 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1337 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1340 /**********************************************************************
1341 * IsSLThunklet (KERNEL.612)
1343 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1345 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1346 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1347 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1352 /***********************************************************************
1353 * AllocLSThunkletSysthunk (KERNEL.607)
1355 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1356 FARPROC relay
, DWORD dummy
)
1358 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1359 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1362 /***********************************************************************
1363 * AllocSLThunkletSysthunk (KERNEL.608)
1365 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1366 SEGPTR relay
, DWORD dummy
)
1368 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1369 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1373 /***********************************************************************
1374 * AllocLSThunkletCallbackEx (KERNEL.567)
1376 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1377 DWORD relay
, HTASK16 task
)
1379 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1380 if ( !thunk
) return NULL
;
1382 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1383 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1384 return (FARPROC
)thunk
->target
;
1386 return THUNK_AllocLSThunklet( target
, relay
,
1387 ThunkletCallbackGlueLS
, task
);
1390 /***********************************************************************
1391 * AllocSLThunkletCallbackEx (KERNEL.568)
1393 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1394 DWORD relay
, HTASK16 task
)
1396 THUNKLET
*thunk
= (THUNKLET
*)target
;
1397 if ( !thunk
) return 0;
1399 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1400 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1401 return (SEGPTR
)thunk
->target
;
1403 return THUNK_AllocSLThunklet( target
, relay
,
1404 ThunkletCallbackGlueSL
, task
);
1407 /***********************************************************************
1408 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1410 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1412 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1415 /***********************************************************************
1416 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1418 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1420 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1423 /***********************************************************************
1424 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1426 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1428 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1429 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1430 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1431 return (FARPROC
)thunk
->target
;
1433 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1434 (DWORD
)ThunkletCallbackGlueLS
,
1436 return (FARPROC
)thunk
;
1439 /***********************************************************************
1440 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1442 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1444 THUNKLET
*thunk
= (THUNKLET
*)target
;
1445 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1446 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1447 return (SEGPTR
)thunk
->target
;
1449 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1450 (DWORD
)ThunkletCallbackGlueSL
,
1452 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1456 /***********************************************************************
1457 * FreeThunklet16 (KERNEL.611)
1459 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
1464 /***********************************************************************
1465 * Callback Client API
1468 #define N_CBC_FIXED 20
1469 #define N_CBC_VARIABLE 10
1470 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1472 static SEGPTR
*CBClientRelay16
[ N_CBC_TOTAL
];
1473 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1475 /***********************************************************************
1476 * RegisterCBClient (KERNEL.619)
1478 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1479 SEGPTR
*relay16
, FARPROC
*relay32
)
1481 /* Search for free Callback ID */
1483 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1484 if ( !CBClientRelay16
[ wCBCId
] )
1487 /* Register Callback ID */
1488 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1490 CBClientRelay16
[ wCBCId
] = relay16
;
1491 CBClientRelay32
[ wCBCId
] = relay32
;
1499 /***********************************************************************
1500 * UnRegisterCBClient (KERNEL.622)
1502 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1503 SEGPTR
*relay16
, FARPROC
*relay32
)
1505 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1506 && CBClientRelay16
[ wCBCId
] == relay16
1507 && CBClientRelay32
[ wCBCId
] == relay32
)
1509 CBClientRelay16
[ wCBCId
] = 0;
1510 CBClientRelay32
[ wCBCId
] = 0;
1519 /***********************************************************************
1520 * InitCBClient (KERNEL.623)
1522 void WINAPI
InitCBClient16( FARPROC glueLS
)
1524 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1525 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
1527 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1530 /***********************************************************************
1531 * CBClientGlueSL (KERNEL.604)
1533 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1535 /* Create stack frame */
1536 SEGPTR stackSeg
= STACK16_PUSH( NtCurrentTeb(), 12 );
1537 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1540 stackLin
[3] = BP_reg( context
);
1541 stackLin
[2] = SI_reg( context
);
1542 stackLin
[1] = DI_reg( context
);
1543 stackLin
[0] = DS_reg( context
);
1545 BP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1546 SP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1547 GS_reg( context
) = 0;
1549 /* Jump to 16-bit relay code */
1550 glue
= CBClientRelay16
[ stackLin
[5] ][ stackLin
[4] ];
1551 CS_reg ( context
) = SELECTOROF( glue
);
1552 EIP_reg( context
) = OFFSETOF ( glue
);
1555 /***********************************************************************
1556 * CBClientThunkSL (KERNEL.620)
1558 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1560 /* Call 32-bit relay code */
1561 extern DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1563 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1564 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1566 EAX_reg(context
) = CALL32_CBClient( proc
, args
, &ESI_reg( context
) );
1569 /***********************************************************************
1570 * CBClientThunkSLEx (KERNEL.621)
1572 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1574 /* Call 32-bit relay code */
1575 extern DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
, LPWORD args
,
1576 DWORD
*esi
, INT
*nArgs
);
1578 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1579 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1583 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &ESI_reg( context
), &nArgs
);
1585 /* Restore registers saved by CBClientGlueSL */
1586 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1587 BP_reg( context
) = stackLin
[3];
1588 SI_reg( context
) = stackLin
[2];
1589 DI_reg( context
) = stackLin
[1];
1590 DS_reg( context
) = stackLin
[0];
1591 SP_reg( context
) += 16+nArgs
;
1593 /* Return to caller of CBClient thunklet */
1594 CS_reg ( context
) = stackLin
[9];
1595 EIP_reg( context
) = stackLin
[8];