LoadModule32/WinExec32 should return 16-bit hInstance/hTask.
[wine/multimedia.git] / if1632 / thunk.c
blob48c68e6a8caa71e9266b24dc6d846f95902a86ce
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include "windows.h"
9 #include "callback.h"
10 #include "resource.h"
11 #include "task.h"
12 #include "user.h"
13 #include "heap.h"
14 #include "hook.h"
15 #include "module.h"
16 #include "process.h"
17 #include "stackframe.h"
18 #include "selectors.h"
19 #include "task.h"
20 #include "except.h"
21 #include "win.h"
22 #include "flatthunk.h"
23 #include "mouse.h"
24 #include "keyboard.h"
25 #include "debug.h"
28 /* List of the 16-bit callback functions. This list is used */
29 /* by the build program to generate the file if1632/callto16.S */
31 /* ### start build ### */
32 extern LONG CALLBACK CallTo16_sreg_(const CONTEXT *context, INT32 offset);
33 extern LONG CALLBACK CallTo16_lreg_(const CONTEXT *context, INT32 offset);
34 extern WORD CALLBACK CallTo16_word_ (FARPROC16);
35 extern LONG CALLBACK CallTo16_long_ (FARPROC16);
36 extern WORD CALLBACK CallTo16_word_w (FARPROC16,WORD);
37 extern LONG CALLBACK CallTo16_long_l (FARPROC16,LONG);
38 extern WORD CALLBACK CallTo16_word_ww (FARPROC16,WORD,WORD);
39 extern WORD CALLBACK CallTo16_word_wl (FARPROC16,WORD,LONG);
40 extern WORD CALLBACK CallTo16_word_ll (FARPROC16,LONG,LONG);
41 extern LONG CALLBACK CallTo16_long_ll (FARPROC16,LONG,LONG);
42 extern WORD CALLBACK CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
43 extern WORD CALLBACK CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
44 extern WORD CALLBACK CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
45 extern LONG CALLBACK CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
46 extern LONG CALLBACK CallTo16_long_lll (FARPROC16,LONG,LONG,LONG);
47 extern WORD CALLBACK CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
48 extern WORD CALLBACK CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
49 extern WORD CALLBACK CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
50 extern WORD CALLBACK CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
51 extern WORD CALLBACK CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
52 extern WORD CALLBACK CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
53 extern LONG CALLBACK CallTo16_long_llll (FARPROC16,LONG,LONG,LONG,LONG);
54 extern WORD CALLBACK CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
55 extern LONG CALLBACK CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
56 extern WORD CALLBACK CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
57 extern WORD CALLBACK CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
58 extern WORD CALLBACK CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
59 extern LONG CALLBACK CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
60 extern LONG CALLBACK CallTo16_long_lllll(FARPROC16,LONG,LONG,LONG,LONG,LONG);
61 extern LONG CALLBACK CallTo16_long_llllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
62 LONG);
63 extern LONG CALLBACK CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
64 LONG,LONG);
65 extern WORD CALLBACK CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,
66 LONG,LONG);
67 extern LONG CALLBACK CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
68 LONG,LONG,LONG);
69 extern LONG CALLBACK CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
70 LONG,LONG,LONG);
71 extern LONG CALLBACK CallTo16_long_lllllllll(FARPROC16,LONG,LONG,LONG,LONG,
72 LONG,LONG,LONG,LONG,LONG);
73 extern LONG CALLBACK CallTo16_long_llllllllll(FARPROC16,LONG,LONG,LONG,LONG,
74 LONG,LONG,LONG,LONG,LONG,LONG);
75 extern LONG CALLBACK CallTo16_long_lllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
76 LONG,LONG,LONG,LONG,LONG,LONG,
77 LONG);
78 extern LONG CALLBACK CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
79 LONG,LONG,LONG,LONG,LONG,LONG,
80 LONG,LONG);
81 extern LONG CALLBACK CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
82 LONG,WORD,LONG,LONG,LONG,LONG,
83 LONG,WORD);
84 extern LONG CALLBACK CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
85 LONG,LONG,LONG,LONG,LONG,LONG,
86 LONG,LONG,LONG);
87 extern LONG CALLBACK CallTo16_long_llllllllllllll(FARPROC16,LONG,LONG,LONG,
88 LONG,LONG,LONG,LONG,LONG,
89 LONG,LONG,LONG,LONG,LONG,
90 LONG);
91 extern LONG CALLBACK CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
92 WORD,WORD,LONG,WORD,WORD,
93 WORD,WORD,LONG,LONG,LONG,
94 LONG);
95 extern LONG CALLBACK CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
96 LONG,LONG,LONG,LONG,LONG,
97 LONG,LONG,LONG,LONG,LONG,
98 LONG,LONG);
99 extern LONG CALLBACK CallTo16_long_llllllllllllllll(FARPROC16,LONG,LONG,LONG,
100 LONG,LONG,LONG,LONG,LONG,
101 LONG,LONG,LONG,LONG,LONG,
102 LONG,LONG,LONG);
103 /* ### stop build ### */
106 typedef void (*RELAY)();
108 #pragma pack(1)
110 typedef struct tagTHUNK
112 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
113 BYTE pushl_func; /* 0x68 pushl $proc */
114 FARPROC32 proc WINE_PACKED;
115 BYTE pushl_eax; /* 0x50 pushl %eax */
116 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
117 RELAY relay WINE_PACKED;
118 struct tagTHUNK *next WINE_PACKED;
119 } THUNK;
121 #pragma pack(4)
123 #define DECL_THUNK(name,proc,relay) \
124 THUNK name = { 0x58, 0x68, (FARPROC32)(proc), 0x50, 0xe9, \
125 (RELAY)((char *)(relay) - (char *)(&(name).next)), NULL }
128 static THUNK *firstThunk = NULL;
130 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
131 UINT16 msg, WPARAM16 wParam,
132 LPARAM lParam );
133 static void WINAPI THUNK_CallTaskReschedule(void);
134 static BOOL32 WINAPI THUNK_WOWCallback16Ex( FARPROC16,DWORD,DWORD,
135 LPVOID,LPDWORD );
137 /* TASK_Reschedule() 16-bit entry point */
138 static FARPROC16 TASK_RescheduleProc;
140 static BOOL32 THUNK_ThunkletInit( void );
142 extern void CallFrom16_p_long_wwwll(void);
144 /* Callbacks function table for the emulator */
145 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
147 (void *)CallTo16_sreg_, /* CallRegisterShortProc */
148 (void *)CallTo16_lreg_, /* CallRegisterLongProc */
149 THUNK_CallTaskReschedule, /* CallTaskRescheduleProc */
150 CallFrom16_p_long_wwwll, /* CallFrom16WndProc */
151 THUNK_CallWndProc16, /* CallWndProc */
152 (void *)CallTo16_long_lwwll, /* CallDriverProc */
153 (void *)CallTo16_word_wwlll, /* CallDriverCallback */
154 (void *)CallTo16_word_wwlll, /* CallTimeFuncProc */
155 (void *)CallTo16_word_w, /* CallWindowsExitProc */
156 (void *)CallTo16_word_lwww, /* CallWordBreakProc */
157 (void *)CallTo16_word_ww, /* CallBootAppProc */
158 (void *)CallTo16_word_www, /* CallLoadAppSegProc */
159 (void *)CallTo16_word_www, /* CallLocalNotifyFunc */
160 (void *)CallTo16_word_, /* CallSystemTimerProc */
161 (void *)CallTo16_word_www, /* CallResourceHandlerProc */
162 (void *)CallTo16_word_wwwl, /* CallPostAppMessageProc */
163 (void *)CallTo16_long_l, /* CallWOWCallbackProc */
164 THUNK_WOWCallback16Ex, /* CallWOWCallback16Ex */
165 (void *)CallTo16_long_l, /* CallASPIPostProc */
166 (void *)CallTo16_word_lwll, /* CallDrvControlProc */
167 (void *)CallTo16_word_lwlll, /* CallDrvEnableProc */
168 (void *)CallTo16_word_llll, /* CallDrvEnumDFontsProc */
169 (void *)CallTo16_word_lwll, /* CallDrvEnumObjProc */
170 (void *)CallTo16_word_lwwlllll, /* CallDrvOutputProc */
171 (void *)CallTo16_long_lwlll, /* CallDrvRealizeProc */
172 (void *)CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
173 (void *)CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
174 (void *)CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
175 (void *)CallTo16_word_ww /* CallDrvAbortProc */
179 /***********************************************************************
180 * THUNK_Init
182 BOOL32 THUNK_Init(void)
184 /* Set the window proc calling functions */
185 Callbacks = &CALLBACK_EmulatorTable;
186 /* Get the 16-bit reschedule function pointer */
187 TASK_RescheduleProc = MODULE_GetWndProcEntry16( "TASK_Reschedule" );
188 /* Initialize Thunklets */
189 return THUNK_ThunkletInit();
192 /***********************************************************************
193 * THUNK_Alloc
195 static THUNK *THUNK_Alloc( FARPROC32 func, RELAY relay )
197 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
198 if (thunk)
200 thunk->popl_eax = 0x58;
201 thunk->pushl_func = 0x68;
202 thunk->proc = func;
203 thunk->pushl_eax = 0x50;
204 thunk->jmp = 0xe9;
205 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
206 thunk->next = firstThunk;
207 firstThunk = thunk;
209 return thunk;
213 /***********************************************************************
214 * THUNK_Find
216 static THUNK *THUNK_Find( FARPROC32 func )
218 THUNK *thunk = firstThunk;
219 while (thunk && (thunk->proc != func)) thunk = thunk->next;
220 return thunk;
224 /***********************************************************************
225 * THUNK_Free
227 static void THUNK_Free( THUNK *thunk )
229 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
231 THUNK **prev = &firstThunk;
232 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
233 if (*prev)
235 *prev = thunk->next;
236 HeapFree( GetProcessHeap(), 0, thunk );
237 return;
240 ERR(thunk, "invalid thunk addr %p\n", thunk );
244 /***********************************************************************
245 * THUNK_CallWndProc16
247 * Call a 16-bit window procedure
249 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
250 UINT16 msg, WPARAM16 wParam,
251 LPARAM lParam )
253 CONTEXT context;
254 LRESULT ret;
255 WORD *args;
256 WND *wndPtr = WIN_FindWndPtr( hwnd );
257 DWORD offset = 0;
258 THDB *thdb = THREAD_Current();
260 /* Window procedures want ax = hInstance, ds = es = ss */
262 memset(&context, '\0', sizeof(context));
263 DS_reg(&context) = SELECTOROF(thdb->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(thdb->cur_stack)
269 + (WORD)&((STACK16FRAME*)0)->bp;
271 if (lParam)
273 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
274 work if structures passed in lParam are placed in the stack/data
275 segment. Programmers easily make the mistake of converting lParam
276 to a near rather than a far pointer, since Windows apparently
277 allows this. We copy the structures to the 16 bit stack; this is
278 ugly but makes these programs work. */
279 switch (msg)
281 case WM_CREATE:
282 case WM_NCCREATE:
283 offset = sizeof(CREATESTRUCT16); break;
284 case WM_DRAWITEM:
285 offset = sizeof(DRAWITEMSTRUCT16); break;
286 case WM_COMPAREITEM:
287 offset = sizeof(COMPAREITEMSTRUCT16); break;
289 if (offset)
291 void *s = PTR_SEG_TO_LIN(lParam);
292 lParam = STACK16_PUSH( thdb, offset );
293 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
297 args = (WORD *)THREAD_STACK16(thdb) - 5;
298 args[0] = LOWORD(lParam);
299 args[1] = HIWORD(lParam);
300 args[2] = wParam;
301 args[3] = msg;
302 args[4] = hwnd;
304 ret = CallTo16_sreg_( &context, 5 * sizeof(WORD) );
305 if (offset) STACK16_POP( thdb, offset );
306 return ret;
310 /***********************************************************************
311 * THUNK_CallTaskReschedule
313 static void WINAPI THUNK_CallTaskReschedule(void)
315 CallTo16_word_(TASK_RescheduleProc);
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, 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, 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, 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, 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,
367 DWORD reserved )
369 DECL_THUNK( thunk, func, CallTo16_word_llwl );
370 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
371 lParam, reserved );
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, 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, CallTo16_word_wl );
392 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
396 /**********************************************************************
397 * THUNK_EnumChildWindows16 (USER.55)
399 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
400 LPARAM lParam )
402 DECL_THUNK( thunk, func, 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,
411 LPARAM lParam )
413 DECL_THUNK( thunk, func, 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, 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,
434 INT16 cx, INT16 cy )
436 DECL_THUNK( thunk, func, CallTo16_word_wlw );
437 if (!func)
438 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
439 else
440 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
441 x, y, cx, cy );
445 /***********************************************************************
446 * THUNK_SetWindowsHook16 (USER.121)
448 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
450 HINSTANCE16 hInst = FarGetOwner( HIWORD(proc) );
451 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
452 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)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 )
463 BOOL16 ret;
464 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
465 if (!thunk) return FALSE;
466 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
467 THUNK_Free( thunk );
468 return ret;
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)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 );
492 return ret;
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 */
507 defDCHookProc = NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
509 if (proc != defDCHookProc)
511 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwll );
512 if (!thunk) return FALSE;
514 else thunk = (THUNK *)DCHook;
516 /* Free the previous thunk */
517 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
518 if (oldThunk && (oldThunk != (THUNK *)DCHook)) THUNK_Free( oldThunk );
520 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
524 /***********************************************************************
525 * THUNK_GetDCHook (GDI.191)
527 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
529 THUNK *thunk = NULL;
530 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
531 if (thunk)
533 if (thunk == (THUNK *)DCHook)
535 if (!defDCHookProc) /* Get DCHook Win16 entry point */
536 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
537 *phookProc = defDCHookProc;
539 else *phookProc = thunk->proc;
541 return ret;
545 /***********************************************************************
546 * THUNK_SetTaskSignalProc (KERNEL.38)
548 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
550 static FARPROC16 defSignalProc16 = NULL;
552 THUNK *thunk = NULL;
554 if( !defSignalProc16 )
555 defSignalProc16 = NE_GetEntryPoint(GetModuleHandle16("USER"), 314 );
557 if( proc == defSignalProc16 )
558 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)&USER_SignalProc );
559 else
561 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwwww );
562 if( !thunk ) return FALSE;
563 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
566 if( thunk != (THUNK*)USER_SignalProc )
568 if( !thunk ) return NULL;
570 proc = thunk->proc;
571 THUNK_Free( thunk );
572 return proc;
574 return defSignalProc16;
577 /***********************************************************************
578 * THUNK_CreateThread16 (KERNEL.441)
580 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
582 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
583 DWORD param = ((DWORD *)threadArgs)[1];
584 HeapFree( GetProcessHeap(), 0, threadArgs );
586 return CallTo16_long_l( start, param );
588 HANDLE32 WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
589 FARPROC16 start, SEGPTR param,
590 DWORD flags, LPDWORD id )
592 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
593 if (!threadArgs) return INVALID_HANDLE_VALUE32;
594 threadArgs[0] = (DWORD)start;
595 threadArgs[1] = (DWORD)param;
597 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
600 /***********************************************************************
601 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
602 * Generic thunking routine to call 16 bit functions from 32bit code.
604 * RETURNS
605 * TRUE if the call was done
607 static BOOL32 WINAPI THUNK_WOWCallback16Ex(
608 FARPROC16 proc, /* [in] 16bit function to call */
609 DWORD dwFlags, /* [in] flags (WCB_*) */
610 DWORD cbArgs, /* [in] number of arguments */
611 LPVOID xargs, /* [in/out] arguments */
612 LPDWORD pdwret /* [out] return value of the 16bit call */
614 LPDWORD args = (LPDWORD)xargs;
615 DWORD ret,i;
617 TRACE(relay,"(%p,0x%08lx,%ld,%p,%p)\n",
618 proc,dwFlags,cbArgs,xargs,pdwret
620 if (dwFlags == WCB16_CDECL) {
621 /* swap the arguments */
622 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
623 for (i=0;i<cbArgs;i++)
624 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
626 switch (cbArgs) {
627 case 0: ret = CallTo16_long_(proc);break;
628 case 1: ret = CallTo16_long_l(proc,args[0]);break;
629 case 2: ret = CallTo16_long_ll(proc,args[0],args[1]);break;
630 case 3: ret = CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
631 case 4: ret = CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
632 break;
633 case 5: ret = CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
634 args[4]
636 break;
637 case 6: ret = CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
638 args[4],args[5]
640 break;
641 case 7: ret = CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
642 args[4],args[5],args[6]
644 break;
645 case 8: ret = CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
646 args[4],args[5],args[6],args[7]
648 break;
649 case 9: ret = CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
650 args[4],args[5],args[6],args[7],args[8]
652 break;
653 case 10:ret = CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
654 args[4],args[5],args[6],args[7],args[8],args[9]
656 break;
657 case 11:ret = CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
658 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
660 break;
661 case 12:ret = CallTo16_long_llllllllllll(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]
665 break;
666 case 13:ret = CallTo16_long_lllllllllllll(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]
670 break;
671 case 14:ret = CallTo16_long_llllllllllllll(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]
675 break;
676 case 15:ret = CallTo16_long_lllllllllllllll(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]
680 break;
681 case 16:ret = CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
682 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
683 args[10],args[11],args[12],args[13],args[14],args[15]
685 break;
686 default:
687 ERR(thunk,"%ld arguments not supported.\n",cbArgs);
688 if (dwFlags == WCB16_CDECL)
689 HeapFree(GetProcessHeap(),0,args);
690 return FALSE;
692 if (dwFlags == WCB16_CDECL)
693 HeapFree(GetProcessHeap(),0,args);
694 if (pdwret)
695 *pdwret = ret;
696 return TRUE;
699 /***********************************************************************
700 * THUNK_MOUSE_Enable (MOUSE.2)
702 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
703 DWORD dwFlags, DWORD dx, DWORD dy,
704 DWORD cButtons, DWORD dwExtraInfo )
706 CONTEXT context;
708 memset( &context, 0, sizeof(context) );
709 CS_reg(&context) = SELECTOROF( proc );
710 EIP_reg(&context) = OFFSETOF( proc );
711 AX_reg(&context) = (WORD)dwFlags;
712 BX_reg(&context) = (WORD)dx;
713 CX_reg(&context) = (WORD)dy;
714 DX_reg(&context) = (WORD)cButtons;
715 SI_reg(&context) = LOWORD( dwExtraInfo );
716 DI_reg(&context) = HIWORD( dwExtraInfo );
718 CallTo16_sreg_( &context, 0 );
720 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
722 static THUNK *lastThunk = NULL;
723 static FARPROC16 lastProc = NULL;
725 if ( lastProc != proc )
727 if ( lastThunk )
728 THUNK_Free( lastThunk );
730 if ( !proc )
731 lastThunk = NULL;
732 else
733 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
735 lastProc = proc;
738 return MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
741 /***********************************************************************
742 * GetMouseEventProc (USER.337)
744 FARPROC16 WINAPI GetMouseEventProc(void)
746 HMODULE16 hmodule = GetModuleHandle16("USER");
747 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
751 /***********************************************************************
752 * WIN16_mouse_event (USER.299)
754 void WINAPI WIN16_mouse_event( CONTEXT *context )
756 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
757 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
761 /***********************************************************************
762 * THUNK_KEYBD_Enable (KEYBOARD.2)
764 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
765 BYTE bVk, BYTE bScan,
766 DWORD dwFlags, DWORD dwExtraInfo )
768 CONTEXT context;
770 memset( &context, 0, sizeof(context) );
771 CS_reg(&context) = SELECTOROF( proc );
772 EIP_reg(&context) = OFFSETOF( proc );
773 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
774 AL_reg(&context) = bVk;
775 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
776 BL_reg(&context) = bScan;
777 SI_reg(&context) = LOWORD( dwExtraInfo );
778 DI_reg(&context) = HIWORD( dwExtraInfo );
780 CallTo16_sreg_( &context, 0 );
782 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
784 static THUNK *lastThunk = NULL;
785 static FARPROC16 lastProc = NULL;
787 if ( lastProc != proc )
789 if ( lastThunk )
790 THUNK_Free( lastThunk );
792 if ( !proc )
793 lastThunk = NULL;
794 else
795 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
797 lastProc = proc;
800 return KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
803 /***********************************************************************
804 * WIN16_keybd_event (USER.289)
806 void WINAPI WIN16_keybd_event( CONTEXT *context )
808 DWORD dwFlags = 0;
810 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
811 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
813 keybd_event( AL_reg(context), BL_reg(context),
814 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
819 /***********************************************************************
820 * 16->32 Flat Thunk routines:
823 /***********************************************************************
824 * ThunkConnect16 (KERNEL.651)
825 * Connects a 32bit and a 16bit thunkbuffer.
827 UINT32 WINAPI ThunkConnect16(
828 LPSTR module16, /* [in] name of win16 dll */
829 LPSTR module32, /* [in] name of win32 dll */
830 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
831 DWORD dwReason, /* [in] initialisation argument */
832 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
833 LPSTR thunkfun32, /* [in] win32 thunkfunction */
834 WORD cs /* [in] CS of win16 dll */
836 BOOL32 directionSL;
838 if (!lstrncmp32A(TD->magic, "SL01", 4))
840 directionSL = TRUE;
842 TRACE(thunk, "SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
843 module16, (DWORD)TD, module32, thunkfun32, dwReason);
845 else if (!lstrncmp32A(TD->magic, "LS01", 4))
847 directionSL = FALSE;
849 TRACE(thunk, "LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
850 module16, (DWORD)TD, module32, thunkfun32, dwReason);
852 else
854 ERR(thunk, "Invalid magic %c%c%c%c\n",
855 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
856 return 0;
859 switch (dwReason)
861 case DLL_PROCESS_ATTACH:
862 if (directionSL)
864 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
865 struct ThunkDataSL *SL = SL16->fpData;
867 if (SL == NULL)
869 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
871 SL->common = SL16->common;
872 SL->flags1 = SL16->flags1;
873 SL->flags2 = SL16->flags2;
875 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
876 SL->targetDB = NULL;
878 lstrcpyn32A(SL->pszDll16, module16, 255);
879 lstrcpyn32A(SL->pszDll32, module32, 255);
881 /* We should create a SEGPTR to the ThunkDataSL,
882 but since the contents are not in the original format,
883 any access to this by 16-bit code would crash anyway. */
884 SL16->spData = 0;
885 SL16->fpData = SL;
889 if (SL->flags2 & 0x80000000)
891 TRACE(thunk, "Preloading 32-bit library\n");
892 LoadLibrary32A(module32);
895 else
897 /* nothing to do */
899 break;
901 case DLL_PROCESS_DETACH:
902 /* FIXME: cleanup */
903 break;
906 return 1;
910 /***********************************************************************
911 * C16ThkSL (KERNEL.630)
914 void WINAPI C16ThkSL(CONTEXT *context)
916 extern void CallFrom16_t_long_(void);
917 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
918 WORD cs, ds;
919 GET_CS(cs);
920 GET_DS(ds);
922 /* We produce the following code:
924 * mov ax, __FLATDS
925 * mov es, ax
926 * movzx ecx, cx
927 * mov edx, es:[ecx + $EDX]
928 * push bp
929 * push edx
930 * call __FLATCS:CallFrom16_t_long_
933 *x++ = 0xB8; *((WORD *)x)++ = ds;
934 *x++ = 0x8E; *x++ = 0xC0;
935 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
936 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
937 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
939 *x++ = 0x55;
940 *x++ = 0x66; *x++ = 0x52;
941 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
942 *((WORD *)x)++ = cs;
944 /* Jump to the stub code just created */
945 IP_reg(context) = LOWORD(EAX_reg(context));
946 CS_reg(context) = HIWORD(EAX_reg(context));
948 /* Since C16ThkSL got called by a jmp, we need to leave the
949 orginal return address on the stack */
950 SP_reg(context) -= 4;
953 /***********************************************************************
954 * C16ThkSL01 (KERNEL.631)
957 void WINAPI C16ThkSL01(CONTEXT *context)
959 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
961 if (stub)
963 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
964 struct ThunkDataSL *td = SL16->fpData;
966 extern void CallFrom16_t_long_(void);
967 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
968 WORD cs;
969 GET_CS(cs);
971 if (!td)
973 ERR(thunk, "ThunkConnect16 was not called!\n");
974 return;
977 TRACE(thunk, "Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
980 /* We produce the following code:
982 * xor eax, eax
983 * mov edx, $td
984 * call C16ThkSL01
985 * push bp
986 * push edx
987 * call __FLATCS:CallFrom16_t_long_
990 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
991 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
992 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
994 *x++ = 0x55;
995 *x++ = 0x66; *x++ = 0x52;
996 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
997 *((WORD *)x)++ = cs;
999 /* Jump to the stub code just created */
1000 IP_reg(context) = LOWORD(EAX_reg(context));
1001 CS_reg(context) = HIWORD(EAX_reg(context));
1003 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1004 orginal return address on the stack */
1005 SP_reg(context) -= 4;
1007 else
1009 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1010 DWORD targetNr = CX_reg(context) / 4;
1011 struct SLTargetDB *tdb;
1013 TRACE(thunk, "Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1014 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1016 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1017 if (tdb->process == PROCESS_Current())
1018 break;
1020 if (!tdb)
1022 TRACE(thunk, "Loading 32-bit library %s\n", td->pszDll32);
1023 LoadLibrary32A(td->pszDll32);
1025 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1026 if (tdb->process == PROCESS_Current())
1027 break;
1030 if (tdb)
1032 EDX_reg(context) = tdb->targetTable[targetNr];
1034 TRACE(thunk, "Call target is %08lx\n", EDX_reg(context));
1036 else
1038 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1039 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1040 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1041 IP_reg(context) = stack[2];
1042 CS_reg(context) = stack[3];
1043 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1045 ERR(thunk, "Process %08lx did not ThunkConnect32 %s to %s\n",
1046 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1051 DWORD WINAPI
1052 WOW16Call(WORD x,WORD y,WORD z) {
1053 int i;
1054 DWORD calladdr;
1055 FIXME(thunk,"(0x%04x,0x%04x,%d),calling (",x,y,z);
1057 for (i=0;i<x/2;i++) {
1058 WORD a = STACK16_POP(THREAD_Current(),2);
1059 DPRINTF("%04x ",a);
1061 calladdr = STACK16_POP(THREAD_Current(),4);
1062 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1063 return 0;
1067 /***********************************************************************
1068 * 16<->32 Thunklet/Callback API:
1071 #pragma pack(1)
1072 typedef struct _THUNKLET
1074 BYTE prefix_target;
1075 BYTE pushl_target;
1076 DWORD target;
1078 BYTE prefix_relay;
1079 BYTE pushl_relay;
1080 DWORD relay;
1082 BYTE jmp_glue;
1083 DWORD glue;
1085 BYTE type;
1086 HINSTANCE16 owner;
1087 struct _THUNKLET *next;
1088 } THUNKLET;
1089 #pragma pack(4)
1091 #define THUNKLET_TYPE_LS 1
1092 #define THUNKLET_TYPE_SL 2
1094 static HANDLE32 ThunkletHeap = 0;
1095 static THUNKLET *ThunkletAnchor = NULL;
1097 static FARPROC32 ThunkletSysthunkGlueLS = 0;
1098 static SEGPTR ThunkletSysthunkGlueSL = 0;
1100 static FARPROC32 ThunkletCallbackGlueLS = 0;
1101 static SEGPTR ThunkletCallbackGlueSL = 0;
1103 /***********************************************************************
1104 * THUNK_ThunkletInit
1106 static BOOL32 THUNK_ThunkletInit( void )
1108 LPBYTE thunk;
1110 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1111 if (!ThunkletHeap) return FALSE;
1113 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1114 if (!thunk) return FALSE;
1116 ThunkletSysthunkGlueLS = (FARPROC32)thunk;
1117 *thunk++ = 0x58; /* popl eax */
1118 *thunk++ = 0xC3; /* ret */
1120 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1121 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1122 *thunk++ = 0xCB; /* lret */
1124 return TRUE;
1127 /***********************************************************************
1128 * SetThunkletCallbackGlue (KERNEL.560)
1130 void WINAPI SetThunkletCallbackGlue( FARPROC32 glueLS, SEGPTR glueSL )
1132 ThunkletCallbackGlueLS = glueLS;
1133 ThunkletCallbackGlueSL = glueSL;
1137 /***********************************************************************
1138 * THUNK_FindThunklet
1140 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1141 DWORD glue, BYTE type )
1143 THUNKLET *thunk;
1145 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1146 if ( thunk->type == type
1147 && thunk->target == target
1148 && thunk->relay == relay
1149 && thunk->glue == glue )
1150 return thunk;
1152 return NULL;
1155 /***********************************************************************
1156 * THUNK_AllocLSThunklet
1158 FARPROC32 THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1159 FARPROC32 glue, HTASK16 owner )
1161 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1162 THUNKLET_TYPE_LS );
1163 if (!thunk)
1165 TDB *pTask = (TDB*)GlobalLock16( owner );
1167 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1168 return 0;
1170 thunk->prefix_target = thunk->prefix_relay = 0x90;
1171 thunk->pushl_target = thunk->pushl_relay = 0x68;
1172 thunk->jmp_glue = 0xE9;
1174 thunk->target = (DWORD)target;
1175 thunk->relay = (DWORD)relay;
1176 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1178 thunk->type = THUNKLET_TYPE_LS;
1179 thunk->owner = pTask? pTask->hInstance : 0;
1181 thunk->next = ThunkletAnchor;
1182 ThunkletAnchor = thunk;
1185 return (FARPROC32)thunk;
1188 /***********************************************************************
1189 * THUNK_AllocSLThunklet
1191 SEGPTR THUNK_AllocSLThunklet( FARPROC32 target, DWORD relay,
1192 SEGPTR glue, HTASK16 owner )
1194 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1195 THUNKLET_TYPE_SL );
1196 if (!thunk)
1198 TDB *pTask = (TDB*)GlobalLock16( owner );
1200 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1201 return 0;
1203 thunk->prefix_target = thunk->prefix_relay = 0x66;
1204 thunk->pushl_target = thunk->pushl_relay = 0x68;
1205 thunk->jmp_glue = 0xEA;
1207 thunk->target = (DWORD)target;
1208 thunk->relay = (DWORD)relay;
1209 thunk->glue = (DWORD)glue;
1211 thunk->type = THUNKLET_TYPE_SL;
1212 thunk->owner = pTask? pTask->hInstance : 0;
1214 thunk->next = ThunkletAnchor;
1215 ThunkletAnchor = thunk;
1218 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1221 /**********************************************************************
1222 * IsLSThunklet
1224 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1226 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1227 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1228 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1231 /**********************************************************************
1232 * IsSLThunklet (KERNEL.612)
1234 BOOL16 WINAPI IsSLThunklet( THUNKLET *thunk )
1236 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1237 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1238 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1243 /***********************************************************************
1244 * AllocLSThunkletSysthunk (KERNEL.607)
1246 FARPROC32 WINAPI AllocLSThunkletSysthunk( SEGPTR target,
1247 FARPROC32 relay, DWORD dummy )
1249 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1250 ThunkletSysthunkGlueLS, GetCurrentTask() );
1253 /***********************************************************************
1254 * AllocSLThunkletSysthunk (KERNEL.608)
1256 SEGPTR WINAPI AllocSLThunkletSysthunk( FARPROC32 target,
1257 SEGPTR relay, DWORD dummy )
1259 return THUNK_AllocSLThunklet( (FARPROC32)relay, (DWORD)target,
1260 ThunkletSysthunkGlueSL, GetCurrentTask() );
1264 /***********************************************************************
1265 * AllocLSThunkletCallbackEx (KERNEL.567)
1267 FARPROC32 WINAPI AllocLSThunkletCallbackEx( SEGPTR target,
1268 DWORD relay, HTASK16 task )
1270 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1271 if ( IsSLThunklet( thunk ) && thunk->relay == relay
1272 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1273 return (FARPROC32)thunk->target;
1275 return THUNK_AllocLSThunklet( target, relay,
1276 ThunkletCallbackGlueLS, task );
1279 /***********************************************************************
1280 * AllocSLThunkletCallbackEx (KERNEL.568)
1282 SEGPTR WINAPI AllocSLThunkletCallbackEx( FARPROC32 target,
1283 DWORD relay, HTASK16 task )
1285 THUNKLET *thunk = (THUNKLET *)target;
1286 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1287 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1288 return (SEGPTR)thunk->target;
1290 return THUNK_AllocSLThunklet( target, relay,
1291 ThunkletCallbackGlueSL, task );
1294 /***********************************************************************
1295 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1297 FARPROC32 WINAPI AllocLSThunkletCallback( SEGPTR target, DWORD relay )
1299 return AllocLSThunkletCallbackEx( target, relay, GetCurrentTask() );
1302 /***********************************************************************
1303 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1305 SEGPTR WINAPI AllocSLThunkletCallback( FARPROC32 target, DWORD relay )
1307 return AllocSLThunkletCallbackEx( target, relay, GetCurrentTask() );
1310 /***********************************************************************
1311 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1313 FARPROC32 WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1315 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1316 if ( thunk && IsSLThunklet( thunk ) && thunk->relay == relay
1317 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1318 return (FARPROC32)thunk->target;
1320 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1321 (DWORD)ThunkletCallbackGlueLS,
1322 THUNKLET_TYPE_LS );
1323 return (FARPROC32)thunk;
1326 /***********************************************************************
1327 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1329 SEGPTR WINAPI FindSLThunkletCallback( FARPROC32 target, DWORD relay )
1331 THUNKLET *thunk = (THUNKLET *)target;
1332 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1333 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1334 return (SEGPTR)thunk->target;
1336 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1337 (DWORD)ThunkletCallbackGlueSL,
1338 THUNKLET_TYPE_SL );
1339 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1343 /***********************************************************************
1344 * Callback Client API
1347 #define N_CBC_FIXED 20
1348 #define N_CBC_VARIABLE 10
1349 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1351 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1352 static FARPROC32 *CBClientRelay32[ N_CBC_TOTAL ];
1354 /***********************************************************************
1355 * RegisterCBClient (KERNEL.619)
1357 INT16 WINAPI RegisterCBClient( INT16 wCBCId,
1358 SEGPTR *relay16, FARPROC32 *relay32 )
1360 /* Search for free Callback ID */
1361 if ( wCBCId == -1 )
1362 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1363 if ( !CBClientRelay16[ wCBCId ] )
1364 break;
1366 /* Register Callback ID */
1367 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1369 CBClientRelay16[ wCBCId ] = relay16;
1370 CBClientRelay32[ wCBCId ] = relay32;
1372 else
1373 wCBCId = 0;
1375 return wCBCId;
1378 /***********************************************************************
1379 * UnRegisterCBClient (KERNEL.622)
1381 INT16 WINAPI UnRegisterCBClient( INT16 wCBCId,
1382 SEGPTR *relay16, FARPROC32 *relay32 )
1384 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1385 && CBClientRelay16[ wCBCId ] == relay16
1386 && CBClientRelay32[ wCBCId ] == relay32 )
1388 CBClientRelay16[ wCBCId ] = 0;
1389 CBClientRelay32[ wCBCId ] = 0;
1391 else
1392 wCBCId = 0;
1394 return wCBCId;
1398 /***********************************************************************
1399 * InitCBClient (KERNEL.623)
1401 void WINAPI InitCBClient( FARPROC32 glueLS )
1403 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1404 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1406 SetThunkletCallbackGlue( glueLS, glueSL );
1409 /***********************************************************************
1410 * CBClientGlueSL (KERNEL.604)
1412 void WINAPI CBClientGlueSL( CONTEXT *context )
1414 /* Create stack frame */
1415 SEGPTR stackSeg = STACK16_PUSH( THREAD_Current(), 12 );
1416 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1417 SEGPTR glue;
1419 stackLin[3] = BP_reg( context );
1420 stackLin[2] = SI_reg( context );
1421 stackLin[1] = DI_reg( context );
1422 stackLin[0] = DS_reg( context );
1424 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1425 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1426 GS_reg( context ) = 0;
1428 /* Jump to 16-bit relay code */
1429 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1430 CS_reg ( context ) = SELECTOROF( glue );
1431 EIP_reg( context ) = OFFSETOF ( glue );
1434 /***********************************************************************
1435 * CBClientThunkSL (KERNEL.620)
1437 void WINAPI CBClientThunkSL( CONTEXT *context )
1439 /* Call 32-bit relay code */
1440 extern DWORD WINAPI CALL32_CBClient( FARPROC32 proc, LPWORD args );
1442 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1443 FARPROC32 proc = CBClientRelay32[ args[2] ][ args[1] ];
1445 EAX_reg(context) = CALL32_CBClient( proc, args );