Handle null wndPtr for mdi window.
[wine/multimedia.git] / if1632 / thunk.c
blobec782913fc9a1fde3d3416a8b5db62ecbb9ae79c
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
11 #include "hook.h"
12 #include "callback.h"
13 #include "task.h"
14 #include "user.h"
15 #include "heap.h"
16 #include "module.h"
17 #include "process.h"
18 #include "stackframe.h"
19 #include "selectors.h"
20 #include "syslevel.h"
21 #include "task.h"
22 #include "except.h"
23 #include "win.h"
24 #include "flatthunk.h"
25 #include "mouse.h"
26 #include "keyboard.h"
27 #include "debug.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,
68 LONG);
69 extern LONG CALLBACK CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
70 LONG,LONG);
71 extern WORD CALLBACK CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,
72 LONG,LONG);
73 extern LONG CALLBACK CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
74 LONG,LONG,LONG);
75 extern LONG CALLBACK CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
76 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,
83 LONG);
84 extern LONG CALLBACK CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
85 LONG,LONG,LONG,LONG,LONG,LONG,
86 LONG,LONG);
87 extern LONG CALLBACK CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
88 LONG,WORD,LONG,LONG,LONG,LONG,
89 LONG,WORD);
90 extern LONG CALLBACK CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
91 LONG,LONG,LONG,LONG,LONG,LONG,
92 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,
96 LONG);
97 extern LONG CALLBACK CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
98 WORD,WORD,LONG,WORD,WORD,
99 WORD,WORD,LONG,LONG,LONG,
100 LONG);
101 extern LONG CALLBACK CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
102 LONG,LONG,LONG,LONG,LONG,
103 LONG,LONG,LONG,LONG,LONG,
104 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,
108 LONG,LONG,LONG);
109 /* ### stop build ### */
112 typedef void (*RELAY)();
114 #pragma pack(1)
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;
125 } THUNK;
127 #pragma pack(4)
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,
138 LPARAM lParam );
139 static BOOL WINAPI THUNK_CallTaskReschedule(void);
140 static BOOL WINAPI THUNK_WOWCallback16Ex( FARPROC16,DWORD,DWORD,
141 LPVOID,LPDWORD );
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 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 /***********************************************************************
185 * THUNK_Init
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 /***********************************************************************
198 * THUNK_Alloc
200 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
202 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
203 if (thunk)
205 thunk->popl_eax = 0x58;
206 thunk->pushl_func = 0x68;
207 thunk->proc = func;
208 thunk->pushl_eax = 0x50;
209 thunk->jmp = 0xe9;
210 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
211 thunk->next = firstThunk;
212 firstThunk = thunk;
214 return thunk;
218 /***********************************************************************
219 * THUNK_Find
221 static THUNK *THUNK_Find( FARPROC func )
223 THUNK *thunk = firstThunk;
224 while (thunk && (thunk->proc != func)) thunk = thunk->next;
225 return thunk;
229 /***********************************************************************
230 * THUNK_Free
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;
238 if (*prev)
240 *prev = thunk->next;
241 HeapFree( GetProcessHeap(), 0, thunk );
242 return;
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,
256 LPARAM lParam )
258 CONTEXT context;
259 LRESULT ret;
260 WORD *args;
261 WND *wndPtr = WIN_FindWndPtr( hwnd );
262 DWORD offset = 0;
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 if (lParam)
278 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
279 work if structures passed in lParam are placed in the stack/data
280 segment. Programmers easily make the mistake of converting lParam
281 to a near rather than a far pointer, since Windows apparently
282 allows this. We copy the structures to the 16 bit stack; this is
283 ugly but makes these programs work. */
284 switch (msg)
286 case WM_CREATE:
287 case WM_NCCREATE:
288 offset = sizeof(CREATESTRUCT16); break;
289 case WM_DRAWITEM:
290 offset = sizeof(DRAWITEMSTRUCT16); break;
291 case WM_COMPAREITEM:
292 offset = sizeof(COMPAREITEMSTRUCT16); break;
294 if (offset)
296 void *s = PTR_SEG_TO_LIN(lParam);
297 lParam = STACK16_PUSH( thdb, offset );
298 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
302 args = (WORD *)THREAD_STACK16(thdb) - 5;
303 args[0] = LOWORD(lParam);
304 args[1] = HIWORD(lParam);
305 args[2] = wParam;
306 args[3] = msg;
307 args[4] = hwnd;
309 ret = CallTo16_sreg_( &context, 5 * sizeof(WORD) );
310 if (offset) STACK16_POP( thdb, offset );
311 return ret;
315 /***********************************************************************
316 * THUNK_CallTaskReschedule
318 static BOOL WINAPI THUNK_CallTaskReschedule(void)
320 return CallTo16_word_(TASK_RescheduleProc);
324 /***********************************************************************
325 * THUNK_EnumObjects16 (GDI.71)
327 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
328 GOBJENUMPROC16 func, LPARAM lParam )
330 DECL_THUNK( thunk, func, CallTo16_word_ll );
331 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
335 /*************************************************************************
336 * THUNK_EnumFonts16 (GDI.70)
338 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
339 FONTENUMPROC16 func, LPARAM lParam )
341 DECL_THUNK( thunk, func, CallTo16_word_llwl );
342 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
345 /******************************************************************
346 * THUNK_EnumMetaFile16 (GDI.175)
348 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
349 MFENUMPROC16 func, LPARAM lParam )
351 DECL_THUNK( thunk, func, CallTo16_word_wllwl );
352 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
356 /*************************************************************************
357 * THUNK_EnumFontFamilies16 (GDI.330)
359 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
360 FONTENUMPROC16 func, LPARAM lParam )
362 DECL_THUNK( thunk, func, CallTo16_word_llwl );
363 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
367 /*************************************************************************
368 * THUNK_EnumFontFamiliesEx16 (GDI.613)
370 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
371 FONTENUMPROCEX16 func, LPARAM lParam,
372 DWORD reserved )
374 DECL_THUNK( thunk, func, CallTo16_word_llwl );
375 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
376 lParam, reserved );
380 /**********************************************************************
381 * THUNK_LineDDA16 (GDI.100)
383 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
384 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
386 DECL_THUNK( thunk, func, CallTo16_word_wwl );
387 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
391 /*******************************************************************
392 * THUNK_EnumWindows16 (USER.54)
394 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
396 DECL_THUNK( thunk, func, CallTo16_word_wl );
397 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
401 /**********************************************************************
402 * THUNK_EnumChildWindows16 (USER.55)
404 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
405 LPARAM lParam )
407 DECL_THUNK( thunk, func, CallTo16_word_wl );
408 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
412 /**********************************************************************
413 * THUNK_EnumTaskWindows16 (USER.225)
415 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
416 LPARAM lParam )
418 DECL_THUNK( thunk, func, CallTo16_word_wl );
419 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
423 /***********************************************************************
424 * THUNK_EnumProps16 (USER.27)
426 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
428 DECL_THUNK( thunk, func, CallTo16_word_wlw );
429 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
433 /***********************************************************************
434 * THUNK_GrayString16 (USER.185)
436 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
437 GRAYSTRINGPROC16 func, LPARAM lParam,
438 INT16 cch, INT16 x, INT16 y,
439 INT16 cx, INT16 cy )
441 DECL_THUNK( thunk, func, CallTo16_word_wlw );
442 if (!func)
443 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
444 else
445 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
446 x, y, cx, cy );
450 /***********************************************************************
451 * THUNK_SetWindowsHook16 (USER.121)
453 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
455 HINSTANCE16 hInst = FarGetOwner16( HIWORD(proc) );
456 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
457 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)CallTo16_long_wwl );
458 if (!thunk) return 0;
459 return (FARPROC16)SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask);
463 /***********************************************************************
464 * THUNK_UnhookWindowsHook16 (USER.234)
466 BOOL16 WINAPI THUNK_UnhookWindowsHook16( INT16 id, HOOKPROC16 proc )
468 BOOL16 ret;
469 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
470 if (!thunk) return FALSE;
471 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
472 THUNK_Free( thunk );
473 return ret;
477 /***********************************************************************
478 * THUNK_SetWindowsHookEx16 (USER.291)
480 HHOOK WINAPI THUNK_SetWindowsHookEx16( INT16 id, HOOKPROC16 proc,
481 HINSTANCE16 hInst, HTASK16 hTask )
483 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)CallTo16_long_wwl );
484 if (!thunk) return 0;
485 return SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask );
489 /***********************************************************************
490 * THUNK_UnhookWindowHookEx16 (USER.292)
492 BOOL16 WINAPI THUNK_UnhookWindowsHookEx16( HHOOK hhook )
494 THUNK *thunk = (THUNK *)HOOK_GetProc16( hhook );
495 BOOL16 ret = UnhookWindowsHookEx16( hhook );
496 if (thunk) THUNK_Free( thunk );
497 return ret;
502 static FARPROC16 defDCHookProc = NULL;
504 /***********************************************************************
505 * THUNK_SetDCHook (GDI.190)
507 BOOL16 WINAPI THUNK_SetDCHook( HDC16 hdc, FARPROC16 proc, DWORD dwHookData )
509 THUNK *thunk, *oldThunk;
511 if (!defDCHookProc) /* Get DCHook Win16 entry point */
512 defDCHookProc = NE_GetEntryPoint( GetModuleHandle16("USER"), 362 );
514 if (proc != defDCHookProc)
516 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwll );
517 if (!thunk) return FALSE;
519 else thunk = (THUNK *)DCHook16;
521 /* Free the previous thunk */
522 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
523 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
525 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
529 /***********************************************************************
530 * THUNK_GetDCHook (GDI.191)
532 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
534 THUNK *thunk = NULL;
535 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
536 if (thunk)
538 if (thunk == (THUNK *)DCHook16)
540 if (!defDCHookProc) /* Get DCHook Win16 entry point */
541 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
542 *phookProc = defDCHookProc;
544 else *phookProc = thunk->proc;
546 return ret;
550 /***********************************************************************
551 * THUNK_SetTaskSignalProc (KERNEL.38)
553 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
555 static FARPROC16 defSignalProc16 = NULL;
557 THUNK *thunk = NULL;
559 if( !defSignalProc16 )
560 defSignalProc16 = NE_GetEntryPoint(GetModuleHandle16("USER"), 314 );
562 if( proc == defSignalProc16 )
563 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)&USER_SignalProc );
564 else
566 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwwww );
567 if( !thunk ) return FALSE;
568 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
571 if( thunk != (THUNK*)USER_SignalProc )
573 if( !thunk ) return NULL;
575 proc = thunk->proc;
576 THUNK_Free( thunk );
577 return proc;
579 return defSignalProc16;
582 /***********************************************************************
583 * THUNK_CreateThread16 (KERNEL.441)
585 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
587 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
588 DWORD param = ((DWORD *)threadArgs)[1];
589 HeapFree( GetProcessHeap(), 0, threadArgs );
591 return CallTo16_long_l( start, param );
593 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
594 FARPROC16 start, SEGPTR param,
595 DWORD flags, LPDWORD id )
597 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
598 if (!threadArgs) return INVALID_HANDLE_VALUE;
599 threadArgs[0] = (DWORD)start;
600 threadArgs[1] = (DWORD)param;
602 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
605 /***********************************************************************
606 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
607 * Generic thunking routine to call 16 bit functions from 32bit code.
609 * RETURNS
610 * TRUE if the call was done
612 static BOOL WINAPI THUNK_WOWCallback16Ex(
613 FARPROC16 proc, /* [in] 16bit function to call */
614 DWORD dwFlags, /* [in] flags (WCB_*) */
615 DWORD cbArgs, /* [in] number of arguments */
616 LPVOID xargs, /* [in/out] arguments */
617 LPDWORD pdwret /* [out] return value of the 16bit call */
619 LPDWORD args = (LPDWORD)xargs;
620 DWORD ret,i;
622 TRACE(relay,"(%p,0x%08lx,%ld,%p,%p)\n",
623 proc,dwFlags,cbArgs,xargs,pdwret
625 if (dwFlags == WCB16_CDECL) {
626 /* swap the arguments */
627 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
628 for (i=0;i<cbArgs;i++)
629 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
631 switch (cbArgs) {
632 case 0: ret = CallTo16_long_(proc);break;
633 case 1: ret = CallTo16_long_l(proc,args[0]);break;
634 case 2: ret = CallTo16_long_ll(proc,args[0],args[1]);break;
635 case 3: ret = CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
636 case 4: ret = CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
637 break;
638 case 5: ret = CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
639 args[4]
641 break;
642 case 6: ret = CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
643 args[4],args[5]
645 break;
646 case 7: ret = CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
647 args[4],args[5],args[6]
649 break;
650 case 8: ret = CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
651 args[4],args[5],args[6],args[7]
653 break;
654 case 9: ret = CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
655 args[4],args[5],args[6],args[7],args[8]
657 break;
658 case 10:ret = CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
659 args[4],args[5],args[6],args[7],args[8],args[9]
661 break;
662 case 11:ret = CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
663 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
665 break;
666 case 12:ret = CallTo16_long_llllllllllll(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]
670 break;
671 case 13:ret = CallTo16_long_lllllllllllll(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]
675 break;
676 case 14:ret = CallTo16_long_llllllllllllll(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]
680 break;
681 case 15:ret = CallTo16_long_lllllllllllllll(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]
685 break;
686 case 16:ret = CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
687 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
688 args[10],args[11],args[12],args[13],args[14],args[15]
690 break;
691 default:
692 ERR(thunk,"%ld arguments not supported.\n",cbArgs);
693 if (dwFlags == WCB16_CDECL)
694 HeapFree(GetProcessHeap(),0,args);
695 return FALSE;
697 if (dwFlags == WCB16_CDECL)
698 HeapFree(GetProcessHeap(),0,args);
699 if (pdwret)
700 *pdwret = ret;
701 return TRUE;
704 /***********************************************************************
705 * THUNK_MOUSE_Enable (MOUSE.2)
707 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
708 DWORD dwFlags, DWORD dx, DWORD dy,
709 DWORD cButtons, DWORD dwExtraInfo )
711 CONTEXT context;
713 memset( &context, 0, sizeof(context) );
714 CS_reg(&context) = SELECTOROF( proc );
715 EIP_reg(&context) = OFFSETOF( proc );
716 AX_reg(&context) = (WORD)dwFlags;
717 BX_reg(&context) = (WORD)dx;
718 CX_reg(&context) = (WORD)dy;
719 DX_reg(&context) = (WORD)cButtons;
720 SI_reg(&context) = LOWORD( dwExtraInfo );
721 DI_reg(&context) = HIWORD( dwExtraInfo );
723 CallTo16_sreg_( &context, 0 );
725 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
727 static THUNK *lastThunk = NULL;
728 static FARPROC16 lastProc = NULL;
730 if ( lastProc != proc )
732 if ( lastThunk )
733 THUNK_Free( lastThunk );
735 if ( !proc )
736 lastThunk = NULL;
737 else
738 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
740 lastProc = proc;
743 return MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
746 /***********************************************************************
747 * GetMouseEventProc (USER.337)
749 FARPROC16 WINAPI GetMouseEventProc16(void)
751 HMODULE16 hmodule = GetModuleHandle16("USER");
752 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
756 /***********************************************************************
757 * WIN16_mouse_event (USER.299)
759 void WINAPI WIN16_mouse_event( CONTEXT *context )
761 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
762 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
766 /***********************************************************************
767 * THUNK_KEYBD_Enable (KEYBOARD.2)
769 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
770 BYTE bVk, BYTE bScan,
771 DWORD dwFlags, DWORD dwExtraInfo )
773 CONTEXT context;
775 memset( &context, 0, sizeof(context) );
776 CS_reg(&context) = SELECTOROF( proc );
777 EIP_reg(&context) = OFFSETOF( proc );
778 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
779 AL_reg(&context) = bVk;
780 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
781 BL_reg(&context) = bScan;
782 SI_reg(&context) = LOWORD( dwExtraInfo );
783 DI_reg(&context) = HIWORD( dwExtraInfo );
785 CallTo16_sreg_( &context, 0 );
787 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
789 static THUNK *lastThunk = NULL;
790 static FARPROC16 lastProc = NULL;
792 if ( lastProc != proc )
794 if ( lastThunk )
795 THUNK_Free( lastThunk );
797 if ( !proc )
798 lastThunk = NULL;
799 else
800 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
802 lastProc = proc;
805 return KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
808 /***********************************************************************
809 * WIN16_keybd_event (USER.289)
811 void WINAPI WIN16_keybd_event( CONTEXT *context )
813 DWORD dwFlags = 0;
815 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
816 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
818 keybd_event( AL_reg(context), BL_reg(context),
819 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
823 /***********************************************************************
824 * WIN16_CreateSystemTimer (SYSTEM.2)
826 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
828 CONTEXT context;
829 memset( &context, '\0', sizeof(context) );
831 CS_reg( &context ) = SELECTOROF( proc );
832 IP_reg( &context ) = OFFSETOF( proc );
833 BP_reg( &context ) = OFFSETOF( THREAD_Current()->cur_stack )
834 + (WORD)&((STACK16FRAME*)0)->bp;
836 AX_reg( &context ) = timer;
838 if ( _ConfirmWin16Lock() )
840 FIXME( system, "Skipping timer %d callback because timer signal "
841 "arrived while we own the Win16Lock!\n", timer );
842 return;
845 CallTo16_sreg_( &context, 0 );
847 /* FIXME: This does not work if the signal occurs while this thread
848 is currently in 16-bit code. With the current structure
849 of the Wine thunking code, this seems to be hard to fix ... */
851 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
853 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
854 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
855 if (!timer) THUNK_Free( thunk );
856 return timer;
859 /***********************************************************************
860 * THUNK_InitCallout
862 void THUNK_InitCallout(void)
864 HMODULE hModule = GetModuleHandleA( "USER32" );
865 if ( hModule )
867 #define GETADDR( var, name ) \
868 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
870 GETADDR( PeekMessageA, "PeekMessageA" );
871 GETADDR( PeekMessageW, "PeekMessageW" );
872 GETADDR( GetMessageA, "GetMessageA" );
873 GETADDR( GetMessageW, "GetMessageW" );
874 GETADDR( SendMessageA, "SendMessageA" );
875 GETADDR( SendMessageW, "SendMessageW" );
876 GETADDR( PostMessageA, "PostMessageA" );
877 GETADDR( PostMessageW, "PostMessageW" );
878 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
879 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
880 GETADDR( TranslateMessage, "TranslateMessage" );
881 GETADDR( DispatchMessageW, "DispatchMessageW" );
882 GETADDR( DispatchMessageA, "DispatchMessageA" );
883 GETADDR( RedrawWindow, "RedrawWindow" );
885 #undef GETADDR
888 hModule = GetModuleHandle16( "USER" );
889 if ( hModule )
891 #define GETADDR( var, name, thk ) \
892 *(FARPROC *)&Callout.##var = (FARPROC) \
893 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
894 (RELAY)CallTo16_##thk )
896 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
897 GETADDR( GetMessage16, "GetMessage", word_lwww );
898 GETADDR( SendMessage16, "SendMessage", long_wwwl );
899 GETADDR( PostMessage16, "PostMessage", word_wwwl );
900 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
901 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
902 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
903 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
904 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
905 GETADDR( UserYield16, "UserYield", word_ );
906 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
908 #undef GETADDR
912 /***********************************************************************
913 * 16->32 Flat Thunk routines:
916 /***********************************************************************
917 * ThunkConnect16 (KERNEL.651)
918 * Connects a 32bit and a 16bit thunkbuffer.
920 UINT WINAPI ThunkConnect16(
921 LPSTR module16, /* [in] name of win16 dll */
922 LPSTR module32, /* [in] name of win32 dll */
923 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
924 DWORD dwReason, /* [in] initialisation argument */
925 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
926 LPSTR thunkfun32, /* [in] win32 thunkfunction */
927 WORD cs /* [in] CS of win16 dll */
929 BOOL directionSL;
931 if (!lstrncmpA(TD->magic, "SL01", 4))
933 directionSL = TRUE;
935 TRACE(thunk, "SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
936 module16, (DWORD)TD, module32, thunkfun32, dwReason);
938 else if (!lstrncmpA(TD->magic, "LS01", 4))
940 directionSL = FALSE;
942 TRACE(thunk, "LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
943 module16, (DWORD)TD, module32, thunkfun32, dwReason);
945 else
947 ERR(thunk, "Invalid magic %c%c%c%c\n",
948 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
949 return 0;
952 switch (dwReason)
954 case DLL_PROCESS_ATTACH:
955 if (directionSL)
957 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
958 struct ThunkDataSL *SL = SL16->fpData;
960 if (SL == NULL)
962 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
964 SL->common = SL16->common;
965 SL->flags1 = SL16->flags1;
966 SL->flags2 = SL16->flags2;
968 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
969 SL->targetDB = NULL;
971 lstrcpynA(SL->pszDll16, module16, 255);
972 lstrcpynA(SL->pszDll32, module32, 255);
974 /* We should create a SEGPTR to the ThunkDataSL,
975 but since the contents are not in the original format,
976 any access to this by 16-bit code would crash anyway. */
977 SL16->spData = 0;
978 SL16->fpData = SL;
982 if (SL->flags2 & 0x80000000)
984 TRACE(thunk, "Preloading 32-bit library\n");
985 LoadLibraryA(module32);
988 else
990 /* nothing to do */
992 break;
994 case DLL_PROCESS_DETACH:
995 /* FIXME: cleanup */
996 break;
999 return 1;
1003 /***********************************************************************
1004 * C16ThkSL (KERNEL.630)
1007 void WINAPI C16ThkSL(CONTEXT *context)
1009 extern void CallFrom16_t_long_(void);
1010 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1011 WORD cs, ds;
1012 GET_CS(cs);
1013 GET_DS(ds);
1015 /* We produce the following code:
1017 * mov ax, __FLATDS
1018 * mov es, ax
1019 * movzx ecx, cx
1020 * mov edx, es:[ecx + $EDX]
1021 * push bp
1022 * push edx
1023 * call __FLATCS:CallFrom16_t_long_
1026 *x++ = 0xB8; *((WORD *)x)++ = ds;
1027 *x++ = 0x8E; *x++ = 0xC0;
1028 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1029 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1030 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
1032 *x++ = 0x55;
1033 *x++ = 0x66; *x++ = 0x52;
1034 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
1035 *((WORD *)x)++ = cs;
1037 /* Jump to the stub code just created */
1038 IP_reg(context) = LOWORD(EAX_reg(context));
1039 CS_reg(context) = HIWORD(EAX_reg(context));
1041 /* Since C16ThkSL got called by a jmp, we need to leave the
1042 orginal return address on the stack */
1043 SP_reg(context) -= 4;
1046 /***********************************************************************
1047 * C16ThkSL01 (KERNEL.631)
1050 void WINAPI C16ThkSL01(CONTEXT *context)
1052 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1054 if (stub)
1056 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
1057 struct ThunkDataSL *td = SL16->fpData;
1059 extern void CallFrom16_t_long_(void);
1060 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1061 WORD cs;
1062 GET_CS(cs);
1064 if (!td)
1066 ERR(thunk, "ThunkConnect16 was not called!\n");
1067 return;
1070 TRACE(thunk, "Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1073 /* We produce the following code:
1075 * xor eax, eax
1076 * mov edx, $td
1077 * call C16ThkSL01
1078 * push bp
1079 * push edx
1080 * call __FLATCS:CallFrom16_t_long_
1083 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1084 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1085 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1087 *x++ = 0x55;
1088 *x++ = 0x66; *x++ = 0x52;
1089 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16_t_long_;
1090 *((WORD *)x)++ = cs;
1092 /* Jump to the stub code just created */
1093 IP_reg(context) = LOWORD(EAX_reg(context));
1094 CS_reg(context) = HIWORD(EAX_reg(context));
1096 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1097 orginal return address on the stack */
1098 SP_reg(context) -= 4;
1100 else
1102 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1103 DWORD targetNr = CX_reg(context) / 4;
1104 struct SLTargetDB *tdb;
1106 TRACE(thunk, "Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1107 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1109 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1110 if (tdb->process == PROCESS_Current())
1111 break;
1113 if (!tdb)
1115 TRACE(thunk, "Loading 32-bit library %s\n", td->pszDll32);
1116 LoadLibraryA(td->pszDll32);
1118 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1119 if (tdb->process == PROCESS_Current())
1120 break;
1123 if (tdb)
1125 EDX_reg(context) = tdb->targetTable[targetNr];
1127 TRACE(thunk, "Call target is %08lx\n", EDX_reg(context));
1129 else
1131 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1132 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1133 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1134 IP_reg(context) = stack[2];
1135 CS_reg(context) = stack[3];
1136 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1138 ERR(thunk, "Process %08lx did not ThunkConnect32 %s to %s\n",
1139 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1144 DWORD WINAPI
1145 WOW16Call(WORD x,WORD y,WORD z) {
1146 int i;
1147 DWORD calladdr;
1148 FIXME(thunk,"(0x%04x,0x%04x,%d),calling (",x,y,z);
1150 for (i=0;i<x/2;i++) {
1151 WORD a = STACK16_POP(THREAD_Current(),2);
1152 DPRINTF("%04x ",a);
1154 calladdr = STACK16_POP(THREAD_Current(),4);
1155 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1156 return 0;
1160 /***********************************************************************
1161 * 16<->32 Thunklet/Callback API:
1164 #pragma pack(1)
1165 typedef struct _THUNKLET
1167 BYTE prefix_target;
1168 BYTE pushl_target;
1169 DWORD target;
1171 BYTE prefix_relay;
1172 BYTE pushl_relay;
1173 DWORD relay;
1175 BYTE jmp_glue;
1176 DWORD glue;
1178 BYTE type;
1179 HINSTANCE16 owner;
1180 struct _THUNKLET *next;
1181 } THUNKLET;
1182 #pragma pack(4)
1184 #define THUNKLET_TYPE_LS 1
1185 #define THUNKLET_TYPE_SL 2
1187 static HANDLE ThunkletHeap = 0;
1188 static THUNKLET *ThunkletAnchor = NULL;
1190 static FARPROC ThunkletSysthunkGlueLS = 0;
1191 static SEGPTR ThunkletSysthunkGlueSL = 0;
1193 static FARPROC ThunkletCallbackGlueLS = 0;
1194 static SEGPTR ThunkletCallbackGlueSL = 0;
1196 /***********************************************************************
1197 * THUNK_ThunkletInit
1199 static BOOL THUNK_ThunkletInit( void )
1201 LPBYTE thunk;
1203 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1204 if (!ThunkletHeap) return FALSE;
1206 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1207 if (!thunk) return FALSE;
1209 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1210 *thunk++ = 0x58; /* popl eax */
1211 *thunk++ = 0xC3; /* ret */
1213 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1214 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1215 *thunk++ = 0xCB; /* lret */
1217 return TRUE;
1220 /***********************************************************************
1221 * SetThunkletCallbackGlue (KERNEL.560)
1223 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1225 ThunkletCallbackGlueLS = glueLS;
1226 ThunkletCallbackGlueSL = glueSL;
1230 /***********************************************************************
1231 * THUNK_FindThunklet
1233 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1234 DWORD glue, BYTE type )
1236 THUNKLET *thunk;
1238 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1239 if ( thunk->type == type
1240 && thunk->target == target
1241 && thunk->relay == relay
1242 && thunk->glue == glue )
1243 return thunk;
1245 return NULL;
1248 /***********************************************************************
1249 * THUNK_AllocLSThunklet
1251 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1252 FARPROC glue, HTASK16 owner )
1254 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1255 THUNKLET_TYPE_LS );
1256 if (!thunk)
1258 TDB *pTask = (TDB*)GlobalLock16( owner );
1260 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1261 return 0;
1263 thunk->prefix_target = thunk->prefix_relay = 0x90;
1264 thunk->pushl_target = thunk->pushl_relay = 0x68;
1265 thunk->jmp_glue = 0xE9;
1267 thunk->target = (DWORD)target;
1268 thunk->relay = (DWORD)relay;
1269 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1271 thunk->type = THUNKLET_TYPE_LS;
1272 thunk->owner = pTask? pTask->hInstance : 0;
1274 thunk->next = ThunkletAnchor;
1275 ThunkletAnchor = thunk;
1278 return (FARPROC)thunk;
1281 /***********************************************************************
1282 * THUNK_AllocSLThunklet
1284 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1285 SEGPTR glue, HTASK16 owner )
1287 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1288 THUNKLET_TYPE_SL );
1289 if (!thunk)
1291 TDB *pTask = (TDB*)GlobalLock16( owner );
1293 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1294 return 0;
1296 thunk->prefix_target = thunk->prefix_relay = 0x66;
1297 thunk->pushl_target = thunk->pushl_relay = 0x68;
1298 thunk->jmp_glue = 0xEA;
1300 thunk->target = (DWORD)target;
1301 thunk->relay = (DWORD)relay;
1302 thunk->glue = (DWORD)glue;
1304 thunk->type = THUNKLET_TYPE_SL;
1305 thunk->owner = pTask? pTask->hInstance : 0;
1307 thunk->next = ThunkletAnchor;
1308 ThunkletAnchor = thunk;
1311 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1314 /**********************************************************************
1315 * IsLSThunklet
1317 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1319 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1320 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1321 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1324 /**********************************************************************
1325 * IsSLThunklet (KERNEL.612)
1327 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1329 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1330 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1331 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1336 /***********************************************************************
1337 * AllocLSThunkletSysthunk (KERNEL.607)
1339 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1340 FARPROC relay, DWORD dummy )
1342 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1343 ThunkletSysthunkGlueLS, GetCurrentTask() );
1346 /***********************************************************************
1347 * AllocSLThunkletSysthunk (KERNEL.608)
1349 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1350 SEGPTR relay, DWORD dummy )
1352 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1353 ThunkletSysthunkGlueSL, GetCurrentTask() );
1357 /***********************************************************************
1358 * AllocLSThunkletCallbackEx (KERNEL.567)
1360 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1361 DWORD relay, HTASK16 task )
1363 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1364 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1365 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1366 return (FARPROC)thunk->target;
1368 return THUNK_AllocLSThunklet( target, relay,
1369 ThunkletCallbackGlueLS, task );
1372 /***********************************************************************
1373 * AllocSLThunkletCallbackEx (KERNEL.568)
1375 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1376 DWORD relay, HTASK16 task )
1378 THUNKLET *thunk = (THUNKLET *)target;
1379 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1380 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1381 return (SEGPTR)thunk->target;
1383 return THUNK_AllocSLThunklet( target, relay,
1384 ThunkletCallbackGlueSL, task );
1387 /***********************************************************************
1388 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1390 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1392 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1395 /***********************************************************************
1396 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1398 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1400 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1403 /***********************************************************************
1404 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1406 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1408 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1409 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1410 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1411 return (FARPROC)thunk->target;
1413 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1414 (DWORD)ThunkletCallbackGlueLS,
1415 THUNKLET_TYPE_LS );
1416 return (FARPROC)thunk;
1419 /***********************************************************************
1420 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1422 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1424 THUNKLET *thunk = (THUNKLET *)target;
1425 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1426 && thunk->glue == (DWORD)ThunkletCallbackGlueLS )
1427 return (SEGPTR)thunk->target;
1429 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1430 (DWORD)ThunkletCallbackGlueSL,
1431 THUNKLET_TYPE_SL );
1432 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1436 /***********************************************************************
1437 * Callback Client API
1440 #define N_CBC_FIXED 20
1441 #define N_CBC_VARIABLE 10
1442 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1444 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1445 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1447 /***********************************************************************
1448 * RegisterCBClient (KERNEL.619)
1450 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1451 SEGPTR *relay16, FARPROC *relay32 )
1453 /* Search for free Callback ID */
1454 if ( wCBCId == -1 )
1455 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1456 if ( !CBClientRelay16[ wCBCId ] )
1457 break;
1459 /* Register Callback ID */
1460 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1462 CBClientRelay16[ wCBCId ] = relay16;
1463 CBClientRelay32[ wCBCId ] = relay32;
1465 else
1466 wCBCId = 0;
1468 return wCBCId;
1471 /***********************************************************************
1472 * UnRegisterCBClient (KERNEL.622)
1474 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1475 SEGPTR *relay16, FARPROC *relay32 )
1477 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1478 && CBClientRelay16[ wCBCId ] == relay16
1479 && CBClientRelay32[ wCBCId ] == relay32 )
1481 CBClientRelay16[ wCBCId ] = 0;
1482 CBClientRelay32[ wCBCId ] = 0;
1484 else
1485 wCBCId = 0;
1487 return wCBCId;
1491 /***********************************************************************
1492 * InitCBClient (KERNEL.623)
1494 void WINAPI InitCBClient16( FARPROC glueLS )
1496 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1497 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1499 SetThunkletCallbackGlue16( glueLS, glueSL );
1502 /***********************************************************************
1503 * CBClientGlueSL (KERNEL.604)
1505 void WINAPI CBClientGlueSL( CONTEXT *context )
1507 /* Create stack frame */
1508 SEGPTR stackSeg = STACK16_PUSH( THREAD_Current(), 12 );
1509 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1510 SEGPTR glue;
1512 stackLin[3] = BP_reg( context );
1513 stackLin[2] = SI_reg( context );
1514 stackLin[1] = DI_reg( context );
1515 stackLin[0] = DS_reg( context );
1517 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1518 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1519 GS_reg( context ) = 0;
1521 /* Jump to 16-bit relay code */
1522 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1523 CS_reg ( context ) = SELECTOROF( glue );
1524 EIP_reg( context ) = OFFSETOF ( glue );
1527 /***********************************************************************
1528 * CBClientThunkSL (KERNEL.620)
1530 void WINAPI CBClientThunkSL( CONTEXT *context )
1532 /* Call 32-bit relay code */
1533 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args );
1535 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1536 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1538 EAX_reg(context) = CALL32_CBClient( proc, args );
1541 /***********************************************************************
1542 * CBClientThunkSLEx (KERNEL.621)
1544 void WINAPI CBClientThunkSLEx( CONTEXT *context )
1546 /* Call 32-bit relay code */
1547 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc,
1548 LPWORD args, INT *nArgs );
1550 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1551 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1552 INT nArgs;
1553 LPWORD stackLin;
1555 EAX_reg(context) = CALL32_CBClientEx( proc, args, &nArgs );
1557 /* Restore registers saved by CBClientGlueSL */
1558 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1559 BP_reg( context ) = stackLin[3];
1560 SI_reg( context ) = stackLin[2];
1561 DI_reg( context ) = stackLin[1];
1562 DS_reg( context ) = stackLin[0];
1563 SP_reg( context ) += 16+nArgs;
1565 /* Return to caller of CBClient thunklet */
1566 CS_reg ( context ) = stackLin[9];
1567 EIP_reg( context ) = stackLin[8];