4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
18 #include "stackframe.h"
19 #include "selectors.h"
24 #include "flatthunk.h"
30 /* List of the 16-bit callback functions. This list is used */
31 /* by the build program to generate the file if1632/callto16.S */
33 /* ### start build ### */
34 extern LONG CALLBACK
CallTo16_sreg_(const CONTEXT
*context
, INT offset
);
35 extern LONG CALLBACK
CallTo16_lreg_(const CONTEXT
*context
, INT offset
);
36 extern WORD CALLBACK
CallTo16_word_ (FARPROC16
);
37 extern LONG CALLBACK
CallTo16_long_ (FARPROC16
);
38 extern WORD CALLBACK
CallTo16_word_w (FARPROC16
,WORD
);
39 extern WORD CALLBACK
CallTo16_word_l (FARPROC16
,LONG
);
40 extern LONG CALLBACK
CallTo16_long_l (FARPROC16
,LONG
);
41 extern WORD CALLBACK
CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
42 extern WORD CALLBACK
CallTo16_word_wl (FARPROC16
,WORD
,LONG
);
43 extern WORD CALLBACK
CallTo16_word_ll (FARPROC16
,LONG
,LONG
);
44 extern LONG CALLBACK
CallTo16_long_ll (FARPROC16
,LONG
,LONG
);
45 extern WORD CALLBACK
CallTo16_word_www (FARPROC16
,WORD
,WORD
,WORD
);
46 extern WORD CALLBACK
CallTo16_word_wwl (FARPROC16
,WORD
,WORD
,LONG
);
47 extern WORD CALLBACK
CallTo16_word_wlw (FARPROC16
,WORD
,LONG
,WORD
);
48 extern LONG CALLBACK
CallTo16_long_wwl (FARPROC16
,WORD
,WORD
,LONG
);
49 extern LONG CALLBACK
CallTo16_long_lll (FARPROC16
,LONG
,LONG
,LONG
);
50 extern WORD CALLBACK
CallTo16_word_llwl (FARPROC16
,LONG
,LONG
,WORD
,LONG
);
51 extern WORD CALLBACK
CallTo16_word_lwll (FARPROC16
,LONG
,WORD
,LONG
,LONG
);
52 extern WORD CALLBACK
CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
53 extern WORD CALLBACK
CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
54 extern WORD CALLBACK
CallTo16_word_wwll (FARPROC16
,WORD
,WORD
,LONG
,LONG
);
55 extern WORD CALLBACK
CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
56 extern LONG CALLBACK
CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
57 extern WORD CALLBACK
CallTo16_word_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
58 extern LONG CALLBACK
CallTo16_long_llll (FARPROC16
,LONG
,LONG
,LONG
,LONG
);
59 extern WORD CALLBACK
CallTo16_word_wllwl(FARPROC16
,WORD
,LONG
,LONG
,WORD
,LONG
);
60 extern WORD CALLBACK
CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
61 extern LONG CALLBACK
CallTo16_long_lwwll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
);
62 extern WORD CALLBACK
CallTo16_word_wwlll(FARPROC16
,WORD
,WORD
,LONG
,LONG
,LONG
);
63 extern WORD CALLBACK
CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
64 extern WORD CALLBACK
CallTo16_word_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
65 extern LONG CALLBACK
CallTo16_long_lwlll(FARPROC16
,LONG
,WORD
,LONG
,LONG
,LONG
);
66 extern LONG CALLBACK
CallTo16_long_lllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
);
67 extern LONG CALLBACK
CallTo16_long_llllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,
69 extern LONG CALLBACK
CallTo16_long_lllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,
71 extern WORD CALLBACK
CallTo16_word_llwwlll(FARPROC16
,LONG
,LONG
,WORD
,WORD
,LONG
,
73 extern LONG CALLBACK
CallTo16_word_lwwlllll(FARPROC16
,LONG
,WORD
,WORD
,LONG
,LONG
,
75 extern LONG CALLBACK
CallTo16_long_llllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,LONG
,
77 extern LONG CALLBACK
CallTo16_long_lllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
78 LONG
,LONG
,LONG
,LONG
,LONG
);
79 extern LONG CALLBACK
CallTo16_long_llllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
80 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
);
81 extern LONG CALLBACK
CallTo16_long_lllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
82 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
84 extern LONG CALLBACK
CallTo16_long_llllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
85 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
87 extern LONG CALLBACK
CallTo16_long_lwwllwlllllw(FARPROC16
,LONG
,WORD
,WORD
,LONG
,
88 LONG
,WORD
,LONG
,LONG
,LONG
,LONG
,
90 extern LONG CALLBACK
CallTo16_long_lllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,LONG
,
91 LONG
,LONG
,LONG
,LONG
,LONG
,LONG
,
93 extern LONG CALLBACK
CallTo16_long_llllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
94 LONG
,LONG
,LONG
,LONG
,LONG
,
95 LONG
,LONG
,LONG
,LONG
,LONG
,
97 extern LONG CALLBACK
CallTo16_word_lwwwwlwwwwllll(FARPROC16
,LONG
,WORD
,WORD
,
98 WORD
,WORD
,LONG
,WORD
,WORD
,
99 WORD
,WORD
,LONG
,LONG
,LONG
,
101 extern LONG CALLBACK
CallTo16_long_lllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
102 LONG
,LONG
,LONG
,LONG
,LONG
,
103 LONG
,LONG
,LONG
,LONG
,LONG
,
105 extern LONG CALLBACK
CallTo16_long_llllllllllllllll(FARPROC16
,LONG
,LONG
,LONG
,
106 LONG
,LONG
,LONG
,LONG
,LONG
,
107 LONG
,LONG
,LONG
,LONG
,LONG
,
109 /* ### stop build ### */
112 typedef void (*RELAY
)();
116 typedef struct tagTHUNK
118 BYTE popl_eax
; /* 0x58 popl %eax (return address)*/
119 BYTE pushl_func
; /* 0x68 pushl $proc */
120 FARPROC proc WINE_PACKED
;
121 BYTE pushl_eax
; /* 0x50 pushl %eax */
122 BYTE jmp
; /* 0xe9 jmp relay (relative jump)*/
123 RELAY relay WINE_PACKED
;
124 struct tagTHUNK
*next WINE_PACKED
;
129 #define DECL_THUNK(name,proc,relay) \
130 THUNK name = { 0x58, 0x68, (FARPROC)(proc), 0x50, 0xe9, \
131 (RELAY)((char *)(relay) - (char *)(&(name).next)), NULL }
134 static THUNK
*firstThunk
= NULL
;
136 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
137 UINT16 msg
, WPARAM16 wParam
,
139 static BOOL WINAPI
THUNK_CallTaskReschedule(void);
140 static BOOL WINAPI
THUNK_WOWCallback16Ex( FARPROC16
,DWORD
,DWORD
,
143 /* TASK_Reschedule() 16-bit entry point */
144 static FARPROC16 TASK_RescheduleProc
;
146 static BOOL
THUNK_ThunkletInit( void );
148 extern void CallFrom16_p_long_wwwll(void);
150 /* Callbacks function table for the emulator */
151 static const CALLBACKS_TABLE CALLBACK_EmulatorTable
=
153 (void *)CallTo16_sreg_
, /* CallRegisterShortProc */
154 (void *)CallTo16_lreg_
, /* CallRegisterLongProc */
155 THUNK_CallTaskReschedule
, /* CallTaskRescheduleProc */
156 (void*)CallFrom16_p_long_wwwll
, /* CallFrom16WndProc */
157 THUNK_CallWndProc16
, /* CallWndProc */
158 (void *)CallTo16_long_lwwll
, /* CallDriverProc */
159 (void *)CallTo16_word_wwlll
, /* CallDriverCallback */
160 (void *)CallTo16_word_wwlll
, /* CallTimeFuncProc */
161 (void *)CallTo16_word_w
, /* CallWindowsExitProc */
162 (void *)CallTo16_word_lwww
, /* CallWordBreakProc */
163 (void *)CallTo16_word_ww
, /* CallBootAppProc */
164 (void *)CallTo16_word_www
, /* CallLoadAppSegProc */
165 (void *)CallTo16_word_www
, /* CallLocalNotifyFunc */
166 (void *)CallTo16_word_www
, /* CallResourceHandlerProc */
167 (void *)CallTo16_long_l
, /* CallWOWCallbackProc */
168 THUNK_WOWCallback16Ex
, /* CallWOWCallback16Ex */
169 (void *)CallTo16_long_ll
, /* CallUTProc */
170 (void *)CallTo16_long_l
, /* CallASPIPostProc */
171 (void *)CallTo16_word_lwll
, /* CallDrvControlProc */
172 (void *)CallTo16_word_lwlll
, /* CallDrvEnableProc */
173 (void *)CallTo16_word_llll
, /* CallDrvEnumDFontsProc */
174 (void *)CallTo16_word_lwll
, /* CallDrvEnumObjProc */
175 (void *)CallTo16_word_lwwlllll
, /* CallDrvOutputProc */
176 (void *)CallTo16_long_lwlll
, /* CallDrvRealizeProc */
177 (void *)CallTo16_word_lwwwwlwwwwllll
, /* CallDrvStretchBltProc */
178 (void *)CallTo16_long_lwwllwlllllw
, /* CallDrvExtTextOutProc */
179 (void *)CallTo16_word_llwwlll
, /* CallDrvGetCharWidth */
180 (void *)CallTo16_word_ww
/* CallDrvAbortProc */
184 /***********************************************************************
187 BOOL
THUNK_Init(void)
189 /* Set the window proc calling functions */
190 Callbacks
= &CALLBACK_EmulatorTable
;
191 /* Get the 16-bit reschedule function pointer */
192 TASK_RescheduleProc
= MODULE_GetWndProcEntry16( "TASK_Reschedule" );
193 /* Initialize Thunklets */
194 return THUNK_ThunkletInit();
197 /***********************************************************************
200 static THUNK
*THUNK_Alloc( FARPROC func
, RELAY relay
)
202 THUNK
*thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
205 thunk
->popl_eax
= 0x58;
206 thunk
->pushl_func
= 0x68;
208 thunk
->pushl_eax
= 0x50;
210 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
211 thunk
->next
= firstThunk
;
218 /***********************************************************************
221 static THUNK
*THUNK_Find( FARPROC func
)
223 THUNK
*thunk
= firstThunk
;
224 while (thunk
&& (thunk
->proc
!= func
)) thunk
= thunk
->next
;
229 /***********************************************************************
232 static void THUNK_Free( THUNK
*thunk
)
234 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk
))
236 THUNK
**prev
= &firstThunk
;
237 while (*prev
&& (*prev
!= thunk
)) prev
= &(*prev
)->next
;
241 HeapFree( GetProcessHeap(), 0, thunk
);
245 ERR(thunk
, "invalid thunk addr %p\n", thunk
);
249 /***********************************************************************
250 * THUNK_CallWndProc16
252 * Call a 16-bit window procedure
254 static LRESULT WINAPI
THUNK_CallWndProc16( WNDPROC16 proc
, HWND16 hwnd
,
255 UINT16 msg
, WPARAM16 wParam
,
261 WND
*wndPtr
= WIN_FindWndPtr( hwnd
);
263 THDB
*thdb
= THREAD_Current();
265 /* Window procedures want ax = hInstance, ds = es = ss */
267 memset(&context
, '\0', sizeof(context
));
268 DS_reg(&context
) = SELECTOROF(thdb
->cur_stack
);
269 ES_reg(&context
) = DS_reg(&context
);
270 EAX_reg(&context
) = wndPtr
? wndPtr
->hInstance
: DS_reg(&context
);
271 CS_reg(&context
) = SELECTOROF(proc
);
272 EIP_reg(&context
) = OFFSETOF(proc
);
273 EBP_reg(&context
) = OFFSETOF(thdb
->cur_stack
)
274 + (WORD
)&((STACK16FRAME
*)0)->bp
;
276 WIN_ReleaseWndPtr(wndPtr
);
280 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
281 work if structures passed in lParam are placed in the stack/data
282 segment. Programmers easily make the mistake of converting lParam
283 to a near rather than a far pointer, since Windows apparently
284 allows this. We copy the structures to the 16 bit stack; this is
285 ugly but makes these programs work. */
290 offset
= sizeof(CREATESTRUCT16
); break;
292 offset
= sizeof(DRAWITEMSTRUCT16
); break;
294 offset
= sizeof(COMPAREITEMSTRUCT16
); break;
298 void *s
= PTR_SEG_TO_LIN(lParam
);
299 lParam
= STACK16_PUSH( thdb
, offset
);
300 memcpy( PTR_SEG_TO_LIN(lParam
), s
, offset
);
304 args
= (WORD
*)THREAD_STACK16(thdb
) - 5;
305 args
[0] = LOWORD(lParam
);
306 args
[1] = HIWORD(lParam
);
311 ret
= CallTo16_sreg_( &context
, 5 * sizeof(WORD
) );
312 if (offset
) STACK16_POP( thdb
, offset
);
317 /***********************************************************************
318 * THUNK_CallTaskReschedule
320 static BOOL WINAPI
THUNK_CallTaskReschedule(void)
322 return CallTo16_word_(TASK_RescheduleProc
);
326 /***********************************************************************
327 * THUNK_EnumObjects16 (GDI.71)
329 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
330 GOBJENUMPROC16 func
, LPARAM lParam
)
332 DECL_THUNK( thunk
, func
, CallTo16_word_ll
);
333 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
337 /*************************************************************************
338 * THUNK_EnumFonts16 (GDI.70)
340 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
341 FONTENUMPROC16 func
, LPARAM lParam
)
343 DECL_THUNK( thunk
, func
, CallTo16_word_llwl
);
344 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
347 /******************************************************************
348 * THUNK_EnumMetaFile16 (GDI.175)
350 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
351 MFENUMPROC16 func
, LPARAM lParam
)
353 DECL_THUNK( thunk
, func
, CallTo16_word_wllwl
);
354 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
358 /*************************************************************************
359 * THUNK_EnumFontFamilies16 (GDI.330)
361 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
362 FONTENUMPROC16 func
, LPARAM lParam
)
364 DECL_THUNK( thunk
, func
, CallTo16_word_llwl
);
365 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
369 /*************************************************************************
370 * THUNK_EnumFontFamiliesEx16 (GDI.613)
372 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
373 FONTENUMPROCEX16 func
, LPARAM lParam
,
376 DECL_THUNK( thunk
, func
, CallTo16_word_llwl
);
377 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
382 /**********************************************************************
383 * THUNK_LineDDA16 (GDI.100)
385 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
386 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
388 DECL_THUNK( thunk
, func
, CallTo16_word_wwl
);
389 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
393 /*******************************************************************
394 * THUNK_EnumWindows16 (USER.54)
396 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
398 DECL_THUNK( thunk
, func
, CallTo16_word_wl
);
399 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
403 /**********************************************************************
404 * THUNK_EnumChildWindows16 (USER.55)
406 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
409 DECL_THUNK( thunk
, func
, CallTo16_word_wl
);
410 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
414 /**********************************************************************
415 * THUNK_EnumTaskWindows16 (USER.225)
417 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
420 DECL_THUNK( thunk
, func
, CallTo16_word_wl
);
421 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
425 /***********************************************************************
426 * THUNK_EnumProps16 (USER.27)
428 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
430 DECL_THUNK( thunk
, func
, CallTo16_word_wlw
);
431 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
435 /***********************************************************************
436 * THUNK_GrayString16 (USER.185)
438 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
439 GRAYSTRINGPROC16 func
, LPARAM lParam
,
440 INT16 cch
, INT16 x
, INT16 y
,
443 DECL_THUNK( thunk
, func
, CallTo16_word_wlw
);
445 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
447 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
452 /***********************************************************************
453 * THUNK_SetWindowsHook16 (USER.121)
455 FARPROC16 WINAPI
THUNK_SetWindowsHook16( INT16 id
, HOOKPROC16 proc
)
457 HINSTANCE16 hInst
= FarGetOwner16( HIWORD(proc
) );
458 HTASK16 hTask
= (id
== WH_MSGFILTER
) ? GetCurrentTask() : 0;
459 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)CallTo16_long_wwl
);
460 if (!thunk
) return 0;
461 return (FARPROC16
)SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
465 /***********************************************************************
466 * THUNK_UnhookWindowsHook16 (USER.234)
468 BOOL16 WINAPI
THUNK_UnhookWindowsHook16( INT16 id
, HOOKPROC16 proc
)
471 THUNK
*thunk
= THUNK_Find( (FARPROC16
)proc
);
472 if (!thunk
) return FALSE
;
473 ret
= UnhookWindowsHook16( id
, (HOOKPROC16
)thunk
);
479 /***********************************************************************
480 * THUNK_SetWindowsHookEx16 (USER.291)
482 HHOOK WINAPI
THUNK_SetWindowsHookEx16( INT16 id
, HOOKPROC16 proc
,
483 HINSTANCE16 hInst
, HTASK16 hTask
)
485 THUNK
*thunk
= THUNK_Alloc( (FARPROC16
)proc
, (RELAY
)CallTo16_long_wwl
);
486 if (!thunk
) return 0;
487 return SetWindowsHookEx16( id
, (HOOKPROC16
)thunk
, hInst
, hTask
);
491 /***********************************************************************
492 * THUNK_UnhookWindowHookEx16 (USER.292)
494 BOOL16 WINAPI
THUNK_UnhookWindowsHookEx16( HHOOK hhook
)
496 THUNK
*thunk
= (THUNK
*)HOOK_GetProc16( hhook
);
497 BOOL16 ret
= UnhookWindowsHookEx16( hhook
);
498 if (thunk
) THUNK_Free( thunk
);
504 static FARPROC16 defDCHookProc
= NULL
;
506 /***********************************************************************
507 * THUNK_SetDCHook (GDI.190)
509 BOOL16 WINAPI
THUNK_SetDCHook( HDC16 hdc
, FARPROC16 proc
, DWORD dwHookData
)
511 THUNK
*thunk
, *oldThunk
;
513 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
514 defDCHookProc
= NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
516 if (proc
!= defDCHookProc
)
518 thunk
= THUNK_Alloc( proc
, (RELAY
)CallTo16_word_wwll
);
519 if (!thunk
) return FALSE
;
521 else thunk
= (THUNK
*)DCHook16
;
523 /* Free the previous thunk */
524 GetDCHook( hdc
, (FARPROC16
*)&oldThunk
);
525 if (oldThunk
&& (oldThunk
!= (THUNK
*)DCHook16
)) THUNK_Free( oldThunk
);
527 return SetDCHook( hdc
, (FARPROC16
)thunk
, dwHookData
);
531 /***********************************************************************
532 * THUNK_GetDCHook (GDI.191)
534 DWORD WINAPI
THUNK_GetDCHook( HDC16 hdc
, FARPROC16
*phookProc
)
537 DWORD ret
= GetDCHook( hdc
, (FARPROC16
*)&thunk
);
540 if (thunk
== (THUNK
*)DCHook16
)
542 if (!defDCHookProc
) /* Get DCHook Win16 entry point */
543 defDCHookProc
= NE_GetEntryPoint(GetModuleHandle16("USER"),362);
544 *phookProc
= defDCHookProc
;
546 else *phookProc
= thunk
->proc
;
552 /***********************************************************************
553 * THUNK_SetTaskSignalProc (KERNEL.38)
555 FARPROC16 WINAPI
THUNK_SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
557 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)CallTo16_word_wwwww
);
558 if ( !thunk
) return NULL
;
560 thunk
= (THUNK
*)SetTaskSignalProc( hTask
, (FARPROC16
)thunk
);
561 if ( !thunk
) return NULL
;
568 /***********************************************************************
569 * THUNK_CreateThread16 (KERNEL.441)
571 static DWORD CALLBACK
THUNK_StartThread16( LPVOID threadArgs
)
573 FARPROC16 start
= ((FARPROC16
*)threadArgs
)[0];
574 DWORD param
= ((DWORD
*)threadArgs
)[1];
575 HeapFree( GetProcessHeap(), 0, threadArgs
);
577 return CallTo16_long_l( start
, param
);
579 HANDLE WINAPI
THUNK_CreateThread16( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
580 FARPROC16 start
, SEGPTR param
,
581 DWORD flags
, LPDWORD id
)
583 DWORD
*threadArgs
= HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD
) );
584 if (!threadArgs
) return INVALID_HANDLE_VALUE
;
585 threadArgs
[0] = (DWORD
)start
;
586 threadArgs
[1] = (DWORD
)param
;
588 return CreateThread( sa
, stack
, THUNK_StartThread16
, threadArgs
, flags
, id
);
591 /***********************************************************************
592 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
593 * Generic thunking routine to call 16 bit functions from 32bit code.
596 * TRUE if the call was done
598 static BOOL WINAPI
THUNK_WOWCallback16Ex(
599 FARPROC16 proc
, /* [in] 16bit function to call */
600 DWORD dwFlags
, /* [in] flags (WCB_*) */
601 DWORD cbArgs
, /* [in] number of arguments */
602 LPVOID xargs
, /* [in/out] arguments */
603 LPDWORD pdwret
/* [out] return value of the 16bit call */
605 LPDWORD args
= (LPDWORD
)xargs
;
608 TRACE(relay
,"(%p,0x%08lx,%ld,%p,%p)\n",
609 proc
,dwFlags
,cbArgs
,xargs
,pdwret
611 if (dwFlags
== WCB16_CDECL
) {
612 /* swap the arguments */
613 args
= HeapAlloc(GetProcessHeap(),0,cbArgs
*sizeof(DWORD
));
614 for (i
=0;i
<cbArgs
;i
++)
615 args
[i
] = ((DWORD
*)xargs
)[cbArgs
-i
-1];
618 case 0: ret
= CallTo16_long_(proc
);break;
619 case 1: ret
= CallTo16_long_l(proc
,args
[0]);break;
620 case 2: ret
= CallTo16_long_ll(proc
,args
[0],args
[1]);break;
621 case 3: ret
= CallTo16_long_lll(proc
,args
[0],args
[1],args
[2]);break;
622 case 4: ret
= CallTo16_long_llll(proc
,args
[0],args
[1],args
[2],args
[3]);
624 case 5: ret
= CallTo16_long_lllll(proc
,args
[0],args
[1],args
[2],args
[3],
628 case 6: ret
= CallTo16_long_llllll(proc
,args
[0],args
[1],args
[2],args
[3],
632 case 7: ret
= CallTo16_long_lllllll(proc
,args
[0],args
[1],args
[2],args
[3],
633 args
[4],args
[5],args
[6]
636 case 8: ret
= CallTo16_long_llllllll(proc
,args
[0],args
[1],args
[2],args
[3],
637 args
[4],args
[5],args
[6],args
[7]
640 case 9: ret
= CallTo16_long_lllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
641 args
[4],args
[5],args
[6],args
[7],args
[8]
644 case 10:ret
= CallTo16_long_llllllllll(proc
,args
[0],args
[1],args
[2],args
[3],
645 args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]
648 case 11:ret
= CallTo16_long_lllllllllll(proc
,args
[0],args
[1],args
[2],
649 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]
652 case 12:ret
= CallTo16_long_llllllllllll(proc
,args
[0],args
[1],args
[2],
653 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
657 case 13:ret
= CallTo16_long_lllllllllllll(proc
,args
[0],args
[1],args
[2],
658 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
659 args
[10],args
[11],args
[12]
662 case 14:ret
= CallTo16_long_llllllllllllll(proc
,args
[0],args
[1],args
[2],
663 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
664 args
[10],args
[11],args
[12],args
[13]
667 case 15:ret
= CallTo16_long_lllllllllllllll(proc
,args
[0],args
[1],args
[2],
668 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
669 args
[10],args
[11],args
[12],args
[13],args
[14]
672 case 16:ret
= CallTo16_long_llllllllllllllll(proc
,args
[0],args
[1],args
[2],
673 args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],
674 args
[10],args
[11],args
[12],args
[13],args
[14],args
[15]
678 ERR(thunk
,"%ld arguments not supported.\n",cbArgs
);
679 if (dwFlags
== WCB16_CDECL
)
680 HeapFree(GetProcessHeap(),0,args
);
683 if (dwFlags
== WCB16_CDECL
)
684 HeapFree(GetProcessHeap(),0,args
);
690 /***********************************************************************
691 * THUNK_MOUSE_Enable (MOUSE.2)
693 static VOID WINAPI
THUNK_CallMouseEventProc( FARPROC16 proc
,
694 DWORD dwFlags
, DWORD dx
, DWORD dy
,
695 DWORD cButtons
, DWORD dwExtraInfo
)
699 memset( &context
, 0, sizeof(context
) );
700 CS_reg(&context
) = SELECTOROF( proc
);
701 EIP_reg(&context
) = OFFSETOF( proc
);
702 AX_reg(&context
) = (WORD
)dwFlags
;
703 BX_reg(&context
) = (WORD
)dx
;
704 CX_reg(&context
) = (WORD
)dy
;
705 DX_reg(&context
) = (WORD
)cButtons
;
706 SI_reg(&context
) = LOWORD( dwExtraInfo
);
707 DI_reg(&context
) = HIWORD( dwExtraInfo
);
709 CallTo16_sreg_( &context
, 0 );
711 VOID WINAPI
THUNK_MOUSE_Enable( FARPROC16 proc
)
713 static THUNK
*lastThunk
= NULL
;
714 static FARPROC16 lastProc
= NULL
;
716 if ( lastProc
!= proc
)
719 THUNK_Free( lastThunk
);
724 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallMouseEventProc
);
729 return MOUSE_Enable( (LPMOUSE_EVENT_PROC
)lastThunk
);
732 /***********************************************************************
733 * GetMouseEventProc (USER.337)
735 FARPROC16 WINAPI
GetMouseEventProc16(void)
737 HMODULE16 hmodule
= GetModuleHandle16("USER");
738 return NE_GetEntryPoint( hmodule
, NE_GetOrdinal( hmodule
, "mouse_event" ));
742 /***********************************************************************
743 * WIN16_mouse_event (USER.299)
745 void WINAPI
WIN16_mouse_event( CONTEXT
*context
)
747 mouse_event( AX_reg(context
), BX_reg(context
), CX_reg(context
),
748 DX_reg(context
), MAKELONG(SI_reg(context
), DI_reg(context
)) );
752 /***********************************************************************
753 * THUNK_KEYBD_Enable (KEYBOARD.2)
755 static VOID WINAPI
THUNK_CallKeybdEventProc( FARPROC16 proc
,
756 BYTE bVk
, BYTE bScan
,
757 DWORD dwFlags
, DWORD dwExtraInfo
)
761 memset( &context
, 0, sizeof(context
) );
762 CS_reg(&context
) = SELECTOROF( proc
);
763 EIP_reg(&context
) = OFFSETOF( proc
);
764 AH_reg(&context
) = (dwFlags
& KEYEVENTF_KEYUP
)? 0x80 : 0;
765 AL_reg(&context
) = bVk
;
766 BH_reg(&context
) = (dwFlags
& KEYEVENTF_EXTENDEDKEY
)? 1 : 0;
767 BL_reg(&context
) = bScan
;
768 SI_reg(&context
) = LOWORD( dwExtraInfo
);
769 DI_reg(&context
) = HIWORD( dwExtraInfo
);
771 CallTo16_sreg_( &context
, 0 );
773 VOID WINAPI
THUNK_KEYBOARD_Enable( FARPROC16 proc
, LPBYTE lpKeyState
)
775 static THUNK
*lastThunk
= NULL
;
776 static FARPROC16 lastProc
= NULL
;
778 if ( lastProc
!= proc
)
781 THUNK_Free( lastThunk
);
786 lastThunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallKeybdEventProc
);
791 return KEYBOARD_Enable( (LPKEYBD_EVENT_PROC
)lastThunk
, lpKeyState
);
794 /***********************************************************************
795 * WIN16_keybd_event (USER.289)
797 void WINAPI
WIN16_keybd_event( CONTEXT
*context
)
801 if (AH_reg(context
) & 0x80) dwFlags
|= KEYEVENTF_KEYUP
;
802 if (BH_reg(context
) & 1 ) dwFlags
|= KEYEVENTF_EXTENDEDKEY
;
804 keybd_event( AL_reg(context
), BL_reg(context
),
805 dwFlags
, MAKELONG(SI_reg(context
), DI_reg(context
)) );
809 /***********************************************************************
810 * WIN16_CreateSystemTimer (SYSTEM.2)
812 static void THUNK_CallSystemTimerProc( FARPROC16 proc
, WORD timer
)
815 memset( &context
, '\0', sizeof(context
) );
817 CS_reg( &context
) = SELECTOROF( proc
);
818 IP_reg( &context
) = OFFSETOF( proc
);
819 BP_reg( &context
) = OFFSETOF( THREAD_Current()->cur_stack
)
820 + (WORD
)&((STACK16FRAME
*)0)->bp
;
822 AX_reg( &context
) = timer
;
824 if ( _ConfirmWin16Lock() )
826 FIXME( system
, "Skipping timer %d callback because timer signal "
827 "arrived while we own the Win16Lock!\n", timer
);
831 CallTo16_sreg_( &context
, 0 );
833 /* FIXME: This does not work if the signal occurs while this thread
834 is currently in 16-bit code. With the current structure
835 of the Wine thunking code, this seems to be hard to fix ... */
837 WORD WINAPI
WIN16_CreateSystemTimer( WORD rate
, FARPROC16 proc
)
839 THUNK
*thunk
= THUNK_Alloc( proc
, (RELAY
)THUNK_CallSystemTimerProc
);
840 WORD timer
= CreateSystemTimer( rate
, (SYSTEMTIMERPROC
)thunk
);
841 if (!timer
) THUNK_Free( thunk
);
845 /***********************************************************************
848 void THUNK_InitCallout(void)
850 HMODULE hModule
= GetModuleHandleA( "USER32" );
853 #define GETADDR( var, name ) \
854 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
856 GETADDR( PeekMessageA
, "PeekMessageA" );
857 GETADDR( PeekMessageW
, "PeekMessageW" );
858 GETADDR( GetMessageA
, "GetMessageA" );
859 GETADDR( GetMessageW
, "GetMessageW" );
860 GETADDR( SendMessageA
, "SendMessageA" );
861 GETADDR( SendMessageW
, "SendMessageW" );
862 GETADDR( PostMessageA
, "PostMessageA" );
863 GETADDR( PostMessageW
, "PostMessageW" );
864 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
865 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
866 GETADDR( TranslateMessage
, "TranslateMessage" );
867 GETADDR( DispatchMessageW
, "DispatchMessageW" );
868 GETADDR( DispatchMessageA
, "DispatchMessageA" );
869 GETADDR( RedrawWindow
, "RedrawWindow" );
870 GETADDR( UserSignalProc
, "UserSignalProc" );
875 hModule
= GetModuleHandle16( "USER" );
878 #define GETADDR( var, name, thk ) \
879 *(FARPROC *)&Callout.##var = (FARPROC) \
880 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
881 (RELAY)CallTo16_##thk )
883 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
884 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
885 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
886 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
887 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
888 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
889 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
890 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
891 GETADDR( InitApp16
, "InitApp", word_w
);
892 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
893 GETADDR( UserYield16
, "UserYield", word_
);
894 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
900 /***********************************************************************
901 * 16->32 Flat Thunk routines:
904 /***********************************************************************
905 * ThunkConnect16 (KERNEL.651)
906 * Connects a 32bit and a 16bit thunkbuffer.
908 UINT WINAPI
ThunkConnect16(
909 LPSTR module16
, /* [in] name of win16 dll */
910 LPSTR module32
, /* [in] name of win32 dll */
911 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
912 DWORD dwReason
, /* [in] initialisation argument */
913 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
914 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
915 WORD cs
/* [in] CS of win16 dll */
919 if (!lstrncmpA(TD
->magic
, "SL01", 4))
923 TRACE(thunk
, "SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
924 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
926 else if (!lstrncmpA(TD
->magic
, "LS01", 4))
930 TRACE(thunk
, "LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
931 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
935 ERR(thunk
, "Invalid magic %c%c%c%c\n",
936 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
942 case DLL_PROCESS_ATTACH
:
945 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
946 struct ThunkDataSL
*SL
= SL16
->fpData
;
950 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
952 SL
->common
= SL16
->common
;
953 SL
->flags1
= SL16
->flags1
;
954 SL
->flags2
= SL16
->flags2
;
956 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
959 lstrcpynA(SL
->pszDll16
, module16
, 255);
960 lstrcpynA(SL
->pszDll32
, module32
, 255);
962 /* We should create a SEGPTR to the ThunkDataSL,
963 but since the contents are not in the original format,
964 any access to this by 16-bit code would crash anyway. */
970 if (SL
->flags2
& 0x80000000)
972 TRACE(thunk
, "Preloading 32-bit library\n");
973 LoadLibraryA(module32
);
982 case DLL_PROCESS_DETACH
:
991 /***********************************************************************
992 * C16ThkSL (KERNEL.630)
995 void WINAPI
C16ThkSL(CONTEXT
*context
)
997 extern void CallFrom16_t_long_(void);
998 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1003 /* We produce the following code:
1008 * mov edx, es:[ecx + $EDX]
1011 * call __FLATCS:CallFrom16_t_long_
1014 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
1015 *x
++ = 0x8E; *x
++ = 0xC0;
1016 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1017 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1018 *x
++ = 0x91; *((DWORD
*)x
)++ = EDX_reg(context
);
1021 *x
++ = 0x66; *x
++ = 0x52;
1022 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16_t_long_
;
1023 *((WORD
*)x
)++ = cs
;
1025 /* Jump to the stub code just created */
1026 IP_reg(context
) = LOWORD(EAX_reg(context
));
1027 CS_reg(context
) = HIWORD(EAX_reg(context
));
1029 /* Since C16ThkSL got called by a jmp, we need to leave the
1030 orginal return address on the stack */
1031 SP_reg(context
) -= 4;
1034 /***********************************************************************
1035 * C16ThkSL01 (KERNEL.631)
1038 void WINAPI
C16ThkSL01(CONTEXT
*context
)
1040 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
1044 struct ThunkDataSL16
*SL16
= PTR_SEG_TO_LIN(EDX_reg(context
));
1045 struct ThunkDataSL
*td
= SL16
->fpData
;
1047 extern void CallFrom16_t_long_(void);
1048 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1054 ERR(thunk
, "ThunkConnect16 was not called!\n");
1058 TRACE(thunk
, "Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
1061 /* We produce the following code:
1068 * call __FLATCS:CallFrom16_t_long_
1071 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1072 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
1073 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
1076 *x
++ = 0x66; *x
++ = 0x52;
1077 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16_t_long_
;
1078 *((WORD
*)x
)++ = cs
;
1080 /* Jump to the stub code just created */
1081 IP_reg(context
) = LOWORD(EAX_reg(context
));
1082 CS_reg(context
) = HIWORD(EAX_reg(context
));
1084 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1085 orginal return address on the stack */
1086 SP_reg(context
) -= 4;
1090 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
1091 DWORD targetNr
= CX_reg(context
) / 4;
1092 struct SLTargetDB
*tdb
;
1094 TRACE(thunk
, "Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1095 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
1097 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1098 if (tdb
->process
== PROCESS_Current())
1103 TRACE(thunk
, "Loading 32-bit library %s\n", td
->pszDll32
);
1104 LoadLibraryA(td
->pszDll32
);
1106 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1107 if (tdb
->process
== PROCESS_Current())
1113 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
1115 TRACE(thunk
, "Call target is %08lx\n", EDX_reg(context
));
1119 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), SP_reg(context
));
1120 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1121 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1122 IP_reg(context
) = stack
[2];
1123 CS_reg(context
) = stack
[3];
1124 SP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1126 ERR(thunk
, "Process %08lx did not ThunkConnect32 %s to %s\n",
1127 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
1133 WOW16Call(WORD x
,WORD y
,WORD z
) {
1136 FIXME(thunk
,"(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
1138 for (i
=0;i
<x
/2;i
++) {
1139 WORD a
= STACK16_POP(THREAD_Current(),2);
1142 calladdr
= STACK16_POP(THREAD_Current(),4);
1143 DPRINTF(") calling address was 0x%08lx\n",calladdr
);
1148 /***********************************************************************
1149 * 16<->32 Thunklet/Callback API:
1153 typedef struct _THUNKLET
1168 struct _THUNKLET
*next
;
1172 #define THUNKLET_TYPE_LS 1
1173 #define THUNKLET_TYPE_SL 2
1175 static HANDLE ThunkletHeap
= 0;
1176 static THUNKLET
*ThunkletAnchor
= NULL
;
1178 static FARPROC ThunkletSysthunkGlueLS
= 0;
1179 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1181 static FARPROC ThunkletCallbackGlueLS
= 0;
1182 static SEGPTR ThunkletCallbackGlueSL
= 0;
1184 /***********************************************************************
1185 * THUNK_ThunkletInit
1187 static BOOL
THUNK_ThunkletInit( void )
1191 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
1192 if (!ThunkletHeap
) return FALSE
;
1194 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1195 if (!thunk
) return FALSE
;
1197 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1198 *thunk
++ = 0x58; /* popl eax */
1199 *thunk
++ = 0xC3; /* ret */
1201 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1202 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1203 *thunk
++ = 0xCB; /* lret */
1208 /***********************************************************************
1209 * SetThunkletCallbackGlue (KERNEL.560)
1211 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1213 ThunkletCallbackGlueLS
= glueLS
;
1214 ThunkletCallbackGlueSL
= glueSL
;
1218 /***********************************************************************
1219 * THUNK_FindThunklet
1221 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1222 DWORD glue
, BYTE type
)
1226 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1227 if ( thunk
->type
== type
1228 && thunk
->target
== target
1229 && thunk
->relay
== relay
1230 && thunk
->glue
== glue
)
1236 /***********************************************************************
1237 * THUNK_AllocLSThunklet
1239 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1240 FARPROC glue
, HTASK16 owner
)
1242 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1246 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1248 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1251 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1252 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1253 thunk
->jmp_glue
= 0xE9;
1255 thunk
->target
= (DWORD
)target
;
1256 thunk
->relay
= (DWORD
)relay
;
1257 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1259 thunk
->type
= THUNKLET_TYPE_LS
;
1260 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1262 thunk
->next
= ThunkletAnchor
;
1263 ThunkletAnchor
= thunk
;
1266 return (FARPROC
)thunk
;
1269 /***********************************************************************
1270 * THUNK_AllocSLThunklet
1272 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1273 SEGPTR glue
, HTASK16 owner
)
1275 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1279 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
1281 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1284 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1285 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1286 thunk
->jmp_glue
= 0xEA;
1288 thunk
->target
= (DWORD
)target
;
1289 thunk
->relay
= (DWORD
)relay
;
1290 thunk
->glue
= (DWORD
)glue
;
1292 thunk
->type
= THUNKLET_TYPE_SL
;
1293 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1295 thunk
->next
= ThunkletAnchor
;
1296 ThunkletAnchor
= thunk
;
1299 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1302 /**********************************************************************
1305 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
1307 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1308 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1309 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1312 /**********************************************************************
1313 * IsSLThunklet (KERNEL.612)
1315 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1317 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1318 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1319 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1324 /***********************************************************************
1325 * AllocLSThunkletSysthunk (KERNEL.607)
1327 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1328 FARPROC relay
, DWORD dummy
)
1330 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1331 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1334 /***********************************************************************
1335 * AllocSLThunkletSysthunk (KERNEL.608)
1337 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1338 SEGPTR relay
, DWORD dummy
)
1340 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1341 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1345 /***********************************************************************
1346 * AllocLSThunkletCallbackEx (KERNEL.567)
1348 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1349 DWORD relay
, HTASK16 task
)
1351 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1352 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1353 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1354 return (FARPROC
)thunk
->target
;
1356 return THUNK_AllocLSThunklet( target
, relay
,
1357 ThunkletCallbackGlueLS
, task
);
1360 /***********************************************************************
1361 * AllocSLThunkletCallbackEx (KERNEL.568)
1363 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1364 DWORD relay
, HTASK16 task
)
1366 THUNKLET
*thunk
= (THUNKLET
*)target
;
1367 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1368 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
)
1369 return (SEGPTR
)thunk
->target
;
1371 return THUNK_AllocSLThunklet( target
, relay
,
1372 ThunkletCallbackGlueSL
, task
);
1375 /***********************************************************************
1376 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1378 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1380 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1383 /***********************************************************************
1384 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1386 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1388 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1391 /***********************************************************************
1392 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1394 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1396 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
1397 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1398 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1399 return (FARPROC
)thunk
->target
;
1401 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1402 (DWORD
)ThunkletCallbackGlueLS
,
1404 return (FARPROC
)thunk
;
1407 /***********************************************************************
1408 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1410 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1412 THUNKLET
*thunk
= (THUNKLET
*)target
;
1413 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1414 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
)
1415 return (SEGPTR
)thunk
->target
;
1417 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1418 (DWORD
)ThunkletCallbackGlueSL
,
1420 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1424 /***********************************************************************
1425 * Callback Client API
1428 #define N_CBC_FIXED 20
1429 #define N_CBC_VARIABLE 10
1430 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1432 static SEGPTR
*CBClientRelay16
[ N_CBC_TOTAL
];
1433 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1435 /***********************************************************************
1436 * RegisterCBClient (KERNEL.619)
1438 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1439 SEGPTR
*relay16
, FARPROC
*relay32
)
1441 /* Search for free Callback ID */
1443 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1444 if ( !CBClientRelay16
[ wCBCId
] )
1447 /* Register Callback ID */
1448 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1450 CBClientRelay16
[ wCBCId
] = relay16
;
1451 CBClientRelay32
[ wCBCId
] = relay32
;
1459 /***********************************************************************
1460 * UnRegisterCBClient (KERNEL.622)
1462 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1463 SEGPTR
*relay16
, FARPROC
*relay32
)
1465 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1466 && CBClientRelay16
[ wCBCId
] == relay16
1467 && CBClientRelay32
[ wCBCId
] == relay32
)
1469 CBClientRelay16
[ wCBCId
] = 0;
1470 CBClientRelay32
[ wCBCId
] = 0;
1479 /***********************************************************************
1480 * InitCBClient (KERNEL.623)
1482 void WINAPI
InitCBClient16( FARPROC glueLS
)
1484 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1485 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
1487 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1490 /***********************************************************************
1491 * CBClientGlueSL (KERNEL.604)
1493 void WINAPI
CBClientGlueSL( CONTEXT
*context
)
1495 /* Create stack frame */
1496 SEGPTR stackSeg
= STACK16_PUSH( THREAD_Current(), 12 );
1497 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1500 stackLin
[3] = BP_reg( context
);
1501 stackLin
[2] = SI_reg( context
);
1502 stackLin
[1] = DI_reg( context
);
1503 stackLin
[0] = DS_reg( context
);
1505 BP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1506 SP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1507 GS_reg( context
) = 0;
1509 /* Jump to 16-bit relay code */
1510 glue
= CBClientRelay16
[ stackLin
[5] ][ stackLin
[4] ];
1511 CS_reg ( context
) = SELECTOROF( glue
);
1512 EIP_reg( context
) = OFFSETOF ( glue
);
1515 /***********************************************************************
1516 * CBClientThunkSL (KERNEL.620)
1518 void WINAPI
CBClientThunkSL( CONTEXT
*context
)
1520 /* Call 32-bit relay code */
1521 extern DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
);
1523 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1524 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1526 EAX_reg(context
) = CALL32_CBClient( proc
, args
);
1529 /***********************************************************************
1530 * CBClientThunkSLEx (KERNEL.621)
1532 void WINAPI
CBClientThunkSLEx( CONTEXT
*context
)
1534 /* Call 32-bit relay code */
1535 extern DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
,
1536 LPWORD args
, INT
*nArgs
);
1538 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1539 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1543 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &nArgs
);
1545 /* Restore registers saved by CBClientGlueSL */
1546 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1547 BP_reg( context
) = stackLin
[3];
1548 SI_reg( context
) = stackLin
[2];
1549 DI_reg( context
) = stackLin
[1];
1550 DS_reg( context
) = stackLin
[0];
1551 SP_reg( context
) += 16+nArgs
;
1553 /* Return to caller of CBClient thunklet */
1554 CS_reg ( context
) = stackLin
[9];
1555 EIP_reg( context
) = stackLin
[8];