Rearrange winver detection code and cache the winver value we
[wine.git] / if1632 / thunk.c
blob61ef072ee85d52f7abeb588df8618f7d5f630b4c
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 "task.h"
11 #include "hook.h"
12 #include "callback.h"
13 #include "user.h"
14 #include "heap.h"
15 #include "neexe.h"
16 #include "process.h"
17 #include "stackframe.h"
18 #include "win.h"
19 #include "flatthunk.h"
20 #include "mouse.h"
21 #include "keyboard.h"
22 #include "debugtools.h"
24 DECLARE_DEBUG_CHANNEL(relay)
25 DECLARE_DEBUG_CHANNEL(system)
26 DECLARE_DEBUG_CHANNEL(thunk)
29 /* List of the 16-bit callback functions. This list is used */
30 /* by the build program to generate the file if1632/callto16.S */
32 /* ### start build ### */
33 extern LONG CALLBACK CallTo16_sreg_(const CONTEXT *context, INT offset);
34 extern LONG CALLBACK CallTo16_lreg_(const CONTEXT *context, INT offset);
35 extern WORD CALLBACK CallTo16_word_ (FARPROC16);
36 extern LONG CALLBACK CallTo16_long_ (FARPROC16);
37 extern WORD CALLBACK CallTo16_word_w (FARPROC16,WORD);
38 extern WORD CALLBACK CallTo16_word_l (FARPROC16,LONG);
39 extern LONG CALLBACK CallTo16_long_l (FARPROC16,LONG);
40 extern WORD CALLBACK CallTo16_word_ww (FARPROC16,WORD,WORD);
41 extern WORD CALLBACK CallTo16_word_wl (FARPROC16,WORD,LONG);
42 extern WORD CALLBACK CallTo16_word_ll (FARPROC16,LONG,LONG);
43 extern LONG CALLBACK CallTo16_long_ll (FARPROC16,LONG,LONG);
44 extern WORD CALLBACK CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
45 extern WORD CALLBACK CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
46 extern WORD CALLBACK CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
47 extern LONG CALLBACK CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
48 extern LONG CALLBACK CallTo16_long_lll (FARPROC16,LONG,LONG,LONG);
49 extern WORD CALLBACK CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
50 extern WORD CALLBACK CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
51 extern WORD CALLBACK CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
52 extern WORD CALLBACK CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
53 extern WORD CALLBACK CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
54 extern WORD CALLBACK CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
55 extern LONG CALLBACK CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
56 extern WORD CALLBACK CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
57 extern LONG CALLBACK CallTo16_long_llll (FARPROC16,LONG,LONG,LONG,LONG);
58 extern WORD CALLBACK CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
59 extern WORD CALLBACK CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
60 extern LONG CALLBACK CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
61 extern WORD CALLBACK CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
62 extern WORD CALLBACK CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
63 extern WORD CALLBACK CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
64 extern LONG CALLBACK CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
65 extern LONG CALLBACK CallTo16_long_lllll(FARPROC16,LONG,LONG,LONG,LONG,LONG);
66 extern LONG CALLBACK CallTo16_long_llllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
67 LONG);
68 extern LONG CALLBACK CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
69 LONG,LONG);
70 extern WORD CALLBACK CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,
71 LONG,LONG);
72 extern LONG CALLBACK CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
73 LONG,LONG,LONG);
74 extern LONG CALLBACK CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
75 LONG,LONG,LONG);
76 extern LONG CALLBACK CallTo16_long_lllllllll(FARPROC16,LONG,LONG,LONG,LONG,
77 LONG,LONG,LONG,LONG,LONG);
78 extern LONG CALLBACK CallTo16_long_llllllllll(FARPROC16,LONG,LONG,LONG,LONG,
79 LONG,LONG,LONG,LONG,LONG,LONG);
80 extern LONG CALLBACK CallTo16_long_lllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
81 LONG,LONG,LONG,LONG,LONG,LONG,
82 LONG);
83 extern LONG CALLBACK CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
84 LONG,LONG,LONG,LONG,LONG,LONG,
85 LONG,LONG);
86 extern LONG CALLBACK CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
87 LONG,WORD,LONG,LONG,LONG,LONG,
88 LONG,WORD);
89 extern LONG CALLBACK CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
90 LONG,LONG,LONG,LONG,LONG,LONG,
91 LONG,LONG,LONG);
92 extern LONG CALLBACK CallTo16_long_llllllllllllll(FARPROC16,LONG,LONG,LONG,
93 LONG,LONG,LONG,LONG,LONG,
94 LONG,LONG,LONG,LONG,LONG,
95 LONG);
96 extern LONG CALLBACK CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
97 WORD,WORD,LONG,WORD,WORD,
98 WORD,WORD,LONG,LONG,LONG,
99 LONG);
100 extern LONG CALLBACK CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
101 LONG,LONG,LONG,LONG,LONG,
102 LONG,LONG,LONG,LONG,LONG,
103 LONG,LONG);
104 extern LONG CALLBACK CallTo16_long_llllllllllllllll(FARPROC16,LONG,LONG,LONG,
105 LONG,LONG,LONG,LONG,LONG,
106 LONG,LONG,LONG,LONG,LONG,
107 LONG,LONG,LONG);
108 /* ### stop build ### */
111 typedef void (*RELAY)();
113 #include "pshpack1.h"
115 typedef struct tagTHUNK
117 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
118 BYTE pushl_func; /* 0x68 pushl $proc */
119 FARPROC proc WINE_PACKED;
120 BYTE pushl_eax; /* 0x50 pushl %eax */
121 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
122 RELAY relay WINE_PACKED;
123 struct tagTHUNK *next WINE_PACKED;
124 } THUNK;
126 #include "poppack.h"
128 #define DECL_THUNK(aname,aproc,arelay) \
129 THUNK aname; \
130 aname.popl_eax = 0x58; \
131 aname.pushl_func = 0x68; \
132 aname.proc = (FARPROC) (aproc); \
133 aname.pushl_eax = 0x50; \
134 aname.jmp = 0xe9; \
135 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
136 aname.next = NULL;
138 static THUNK *firstThunk = NULL;
140 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
141 UINT16 msg, WPARAM16 wParam,
142 LPARAM lParam );
143 static BOOL WINAPI THUNK_WOWCallback16Ex( FARPROC16,DWORD,DWORD,
144 LPVOID,LPDWORD );
145 static BOOL THUNK_ThunkletInit( void );
147 extern void CallFrom16_p_long_wwwll(void);
149 /* Callbacks function table for the emulator */
150 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
152 (void *)CallTo16_sreg_, /* CallRegisterShortProc */
153 (void *)CallTo16_lreg_, /* CallRegisterLongProc */
154 (void*)CallFrom16_p_long_wwwll, /* CallFrom16WndProc */
155 THUNK_CallWndProc16, /* CallWndProc */
156 (void *)CallTo16_long_lwwll, /* CallDriverProc */
157 (void *)CallTo16_word_wwlll, /* CallDriverCallback */
158 (void *)CallTo16_word_wwlll, /* CallTimeFuncProc */
159 (void *)CallTo16_word_w, /* CallWindowsExitProc */
160 (void *)CallTo16_word_lwww, /* CallWordBreakProc */
161 (void *)CallTo16_word_ww, /* CallBootAppProc */
162 (void *)CallTo16_word_www, /* CallLoadAppSegProc */
163 (void *)CallTo16_word_www, /* CallLocalNotifyFunc */
164 (void *)CallTo16_word_www, /* CallResourceHandlerProc */
165 (void *)CallTo16_long_l, /* CallWOWCallbackProc */
166 THUNK_WOWCallback16Ex, /* CallWOWCallback16Ex */
167 (void *)CallTo16_long_ll, /* CallUTProc */
168 (void *)CallTo16_long_l, /* CallASPIPostProc */
169 (void *)CallTo16_word_lwll, /* CallDrvControlProc */
170 (void *)CallTo16_word_lwlll, /* CallDrvEnableProc */
171 (void *)CallTo16_word_llll, /* CallDrvEnumDFontsProc */
172 (void *)CallTo16_word_lwll, /* CallDrvEnumObjProc */
173 (void *)CallTo16_word_lwwlllll, /* CallDrvOutputProc */
174 (void *)CallTo16_long_lwlll, /* CallDrvRealizeProc */
175 (void *)CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
176 (void *)CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
177 (void *)CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
178 (void *)CallTo16_word_ww /* CallDrvAbortProc */
182 /***********************************************************************
183 * THUNK_Init
185 BOOL THUNK_Init(void)
187 /* Set the window proc calling functions */
188 Callbacks = &CALLBACK_EmulatorTable;
189 /* Initialize Thunklets */
190 return THUNK_ThunkletInit();
193 /***********************************************************************
194 * THUNK_Alloc
196 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
198 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
199 if (thunk)
201 thunk->popl_eax = 0x58;
202 thunk->pushl_func = 0x68;
203 thunk->proc = func;
204 thunk->pushl_eax = 0x50;
205 thunk->jmp = 0xe9;
206 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
207 thunk->next = firstThunk;
208 firstThunk = thunk;
210 return thunk;
214 /***********************************************************************
215 * THUNK_Find
217 static THUNK *THUNK_Find( FARPROC func )
219 THUNK *thunk = firstThunk;
220 while (thunk && (thunk->proc != func)) thunk = thunk->next;
221 return thunk;
225 /***********************************************************************
226 * THUNK_Free
228 static void THUNK_Free( THUNK *thunk )
230 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
232 THUNK **prev = &firstThunk;
233 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
234 if (*prev)
236 *prev = thunk->next;
237 HeapFree( GetProcessHeap(), 0, thunk );
238 return;
241 ERR_(thunk)("invalid thunk addr %p\n", thunk );
245 /***********************************************************************
246 * THUNK_CallWndProc16
248 * Call a 16-bit window procedure
250 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
251 UINT16 msg, WPARAM16 wParam,
252 LPARAM lParam )
254 CONTEXT context;
255 LRESULT ret;
256 WORD *args;
257 WND *wndPtr = WIN_FindWndPtr( hwnd );
258 DWORD offset = 0;
259 THDB *thdb = THREAD_Current();
260 int iWndsLocks;
262 /* Window procedures want ax = hInstance, ds = es = ss */
264 memset(&context, '\0', sizeof(context));
265 DS_reg(&context) = SELECTOROF(thdb->cur_stack);
266 ES_reg(&context) = DS_reg(&context);
267 EAX_reg(&context) = wndPtr ? wndPtr->hInstance : DS_reg(&context);
268 CS_reg(&context) = SELECTOROF(proc);
269 EIP_reg(&context) = OFFSETOF(proc);
270 EBP_reg(&context) = OFFSETOF(thdb->cur_stack)
271 + (WORD)&((STACK16FRAME*)0)->bp;
273 WIN_ReleaseWndPtr(wndPtr);
275 if (lParam)
277 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
278 work if structures passed in lParam are placed in the stack/data
279 segment. Programmers easily make the mistake of converting lParam
280 to a near rather than a far pointer, since Windows apparently
281 allows this. We copy the structures to the 16 bit stack; this is
282 ugly but makes these programs work. */
283 switch (msg)
285 case WM_CREATE:
286 case WM_NCCREATE:
287 offset = sizeof(CREATESTRUCT16); break;
288 case WM_DRAWITEM:
289 offset = sizeof(DRAWITEMSTRUCT16); break;
290 case WM_COMPAREITEM:
291 offset = sizeof(COMPAREITEMSTRUCT16); break;
293 if (offset)
295 void *s = PTR_SEG_TO_LIN(lParam);
296 lParam = STACK16_PUSH( thdb, offset );
297 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
301 iWndsLocks = WIN_SuspendWndsLock();
303 args = (WORD *)THREAD_STACK16(thdb) - 5;
304 args[0] = LOWORD(lParam);
305 args[1] = HIWORD(lParam);
306 args[2] = wParam;
307 args[3] = msg;
308 args[4] = hwnd;
310 ret = CallTo16_sreg_( &context, 5 * sizeof(WORD) );
311 if (offset) STACK16_POP( thdb, offset );
313 WIN_RestoreWndsLock(iWndsLocks);
315 return ret;
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 = FarGetOwner16( 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 */
508 HMODULE16 hModule = GetModuleHandle16( "USER" );
509 NE_MODULE *pModule = NE_GetPtr( hModule );
511 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) )
512 defDCHookProc = NE_GetEntryPoint( hModule, 362 );
513 else
514 defDCHookProc = (FARPROC16)-1;
517 if (!proc)
518 thunk = NULL;
519 else if (proc != defDCHookProc)
521 thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwll );
522 if (!thunk) return FALSE;
524 else thunk = (THUNK *)DCHook16;
526 /* Free the previous thunk */
527 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
528 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
530 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
534 /***********************************************************************
535 * THUNK_GetDCHook (GDI.191)
537 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
539 THUNK *thunk = NULL;
540 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
541 if (thunk)
543 if (thunk == (THUNK *)DCHook16)
545 /* Note: we can only get here when running built-in USER */
547 if (!defDCHookProc) /* Get DCHook Win16 entry point */
548 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
550 *phookProc = defDCHookProc;
552 else *phookProc = thunk->proc;
554 return ret;
558 /***********************************************************************
559 * THUNK_SetTaskSignalProc (KERNEL.38)
561 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
563 THUNK *thunk = THUNK_Alloc( proc, (RELAY)CallTo16_word_wwwww );
564 if ( !thunk ) return NULL;
566 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
567 if ( !thunk ) return NULL;
569 proc = thunk->proc;
570 THUNK_Free( thunk );
571 return proc;
574 /***********************************************************************
575 * THUNK_CreateThread16 (KERNEL.441)
577 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
579 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
580 DWORD param = ((DWORD *)threadArgs)[1];
581 HeapFree( GetProcessHeap(), 0, threadArgs );
583 return CallTo16_long_l( start, param );
585 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
586 FARPROC16 start, SEGPTR param,
587 DWORD flags, LPDWORD id )
589 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
590 if (!threadArgs) return INVALID_HANDLE_VALUE;
591 threadArgs[0] = (DWORD)start;
592 threadArgs[1] = (DWORD)param;
594 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
597 /***********************************************************************
598 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
599 * Generic thunking routine to call 16 bit functions from 32bit code.
601 * RETURNS
602 * TRUE if the call was done
604 static BOOL WINAPI THUNK_WOWCallback16Ex(
605 FARPROC16 proc, /* [in] 16bit function to call */
606 DWORD dwFlags, /* [in] flags (WCB_*) */
607 DWORD cbArgs, /* [in] number of arguments */
608 LPVOID xargs, /* [in/out] arguments */
609 LPDWORD pdwret /* [out] return value of the 16bit call */
611 LPDWORD args = (LPDWORD)xargs;
612 DWORD ret,i;
614 TRACE_(relay)("(%p,0x%08lx,%ld,%p,%p)\n",
615 proc,dwFlags,cbArgs,xargs,pdwret
617 if (dwFlags == WCB16_CDECL) {
618 /* swap the arguments */
619 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
620 for (i=0;i<cbArgs;i++)
621 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
623 switch (cbArgs) {
624 case 0: ret = CallTo16_long_(proc);break;
625 case 1: ret = CallTo16_long_l(proc,args[0]);break;
626 case 2: ret = CallTo16_long_ll(proc,args[0],args[1]);break;
627 case 3: ret = CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
628 case 4: ret = CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
629 break;
630 case 5: ret = CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
631 args[4]
633 break;
634 case 6: ret = CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
635 args[4],args[5]
637 break;
638 case 7: ret = CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
639 args[4],args[5],args[6]
641 break;
642 case 8: ret = CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
643 args[4],args[5],args[6],args[7]
645 break;
646 case 9: ret = CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
647 args[4],args[5],args[6],args[7],args[8]
649 break;
650 case 10:ret = CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
651 args[4],args[5],args[6],args[7],args[8],args[9]
653 break;
654 case 11:ret = CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
655 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
657 break;
658 case 12:ret = CallTo16_long_llllllllllll(proc,args[0],args[1],args[2],
659 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
660 args[10],args[11]
662 break;
663 case 13:ret = CallTo16_long_lllllllllllll(proc,args[0],args[1],args[2],
664 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
665 args[10],args[11],args[12]
667 break;
668 case 14:ret = CallTo16_long_llllllllllllll(proc,args[0],args[1],args[2],
669 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
670 args[10],args[11],args[12],args[13]
672 break;
673 case 15:ret = CallTo16_long_lllllllllllllll(proc,args[0],args[1],args[2],
674 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
675 args[10],args[11],args[12],args[13],args[14]
677 break;
678 case 16:ret = CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
679 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
680 args[10],args[11],args[12],args[13],args[14],args[15]
682 break;
683 default:
684 ERR_(thunk)("%ld arguments not supported.\n",cbArgs);
685 if (dwFlags == WCB16_CDECL)
686 HeapFree(GetProcessHeap(),0,args);
687 return FALSE;
689 if (dwFlags == WCB16_CDECL)
690 HeapFree(GetProcessHeap(),0,args);
691 if (pdwret)
692 *pdwret = ret;
693 return TRUE;
696 /***********************************************************************
697 * THUNK_MOUSE_Enable (MOUSE.2)
699 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
700 DWORD dwFlags, DWORD dx, DWORD dy,
701 DWORD cButtons, DWORD dwExtraInfo )
703 CONTEXT context;
705 memset( &context, 0, sizeof(context) );
706 CS_reg(&context) = SELECTOROF( proc );
707 EIP_reg(&context) = OFFSETOF( proc );
708 AX_reg(&context) = (WORD)dwFlags;
709 BX_reg(&context) = (WORD)dx;
710 CX_reg(&context) = (WORD)dy;
711 DX_reg(&context) = (WORD)cButtons;
712 SI_reg(&context) = LOWORD( dwExtraInfo );
713 DI_reg(&context) = HIWORD( dwExtraInfo );
715 CallTo16_sreg_( &context, 0 );
717 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
719 static THUNK *lastThunk = NULL;
720 static FARPROC16 lastProc = NULL;
722 if ( lastProc != proc )
724 if ( lastThunk )
725 THUNK_Free( lastThunk );
727 if ( !proc )
728 lastThunk = NULL;
729 else
730 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
732 lastProc = proc;
735 MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
738 /***********************************************************************
739 * GetMouseEventProc (USER.337)
741 FARPROC16 WINAPI GetMouseEventProc16(void)
743 HMODULE16 hmodule = GetModuleHandle16("USER");
744 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
748 /***********************************************************************
749 * WIN16_mouse_event (USER.299)
751 void WINAPI WIN16_mouse_event( CONTEXT *context )
753 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
754 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
758 /***********************************************************************
759 * THUNK_KEYBD_Enable (KEYBOARD.2)
761 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
762 BYTE bVk, BYTE bScan,
763 DWORD dwFlags, DWORD dwExtraInfo )
765 CONTEXT context;
767 memset( &context, 0, sizeof(context) );
768 CS_reg(&context) = SELECTOROF( proc );
769 EIP_reg(&context) = OFFSETOF( proc );
770 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
771 AL_reg(&context) = bVk;
772 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
773 BL_reg(&context) = bScan;
774 SI_reg(&context) = LOWORD( dwExtraInfo );
775 DI_reg(&context) = HIWORD( dwExtraInfo );
777 CallTo16_sreg_( &context, 0 );
779 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
781 static THUNK *lastThunk = NULL;
782 static FARPROC16 lastProc = NULL;
784 if ( lastProc != proc )
786 if ( lastThunk )
787 THUNK_Free( lastThunk );
789 if ( !proc )
790 lastThunk = NULL;
791 else
792 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
794 lastProc = proc;
797 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
800 /***********************************************************************
801 * WIN16_keybd_event (USER.289)
803 void WINAPI WIN16_keybd_event( CONTEXT *context )
805 DWORD dwFlags = 0;
807 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
808 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
810 keybd_event( AL_reg(context), BL_reg(context),
811 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
815 /***********************************************************************
816 * WIN16_CreateSystemTimer (SYSTEM.2)
818 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
820 CONTEXT context;
821 memset( &context, '\0', sizeof(context) );
823 CS_reg( &context ) = SELECTOROF( proc );
824 IP_reg( &context ) = OFFSETOF( proc );
825 BP_reg( &context ) = OFFSETOF( THREAD_Current()->cur_stack )
826 + (WORD)&((STACK16FRAME*)0)->bp;
828 AX_reg( &context ) = timer;
830 if ( _ConfirmWin16Lock() )
832 FIXME_(system)("Skipping timer %d callback because timer signal "
833 "arrived while we own the Win16Lock!\n", timer );
834 return;
837 CallTo16_sreg_( &context, 0 );
839 /* FIXME: This does not work if the signal occurs while this thread
840 is currently in 16-bit code. With the current structure
841 of the Wine thunking code, this seems to be hard to fix ... */
843 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
845 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
846 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
847 if (!timer) THUNK_Free( thunk );
848 return timer;
851 /***********************************************************************
852 * THUNK_InitCallout
854 void THUNK_InitCallout(void)
856 HMODULE hModule;
857 WINE_MODREF *wm;
858 NE_MODULE *pModule;
860 hModule = GetModuleHandleA( "USER32" );
861 wm = MODULE32_LookupHMODULE( hModule );
862 if ( wm && !(wm->flags & WINE_MODREF_INTERNAL) )
864 #define GETADDR( var, name ) \
865 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
867 GETADDR( PeekMessageA, "PeekMessageA" );
868 GETADDR( PeekMessageW, "PeekMessageW" );
869 GETADDR( GetMessageA, "GetMessageA" );
870 GETADDR( GetMessageW, "GetMessageW" );
871 GETADDR( SendMessageA, "SendMessageA" );
872 GETADDR( SendMessageW, "SendMessageW" );
873 GETADDR( PostMessageA, "PostMessageA" );
874 GETADDR( PostMessageW, "PostMessageW" );
875 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
876 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
877 GETADDR( TranslateMessage, "TranslateMessage" );
878 GETADDR( DispatchMessageW, "DispatchMessageW" );
879 GETADDR( DispatchMessageA, "DispatchMessageA" );
880 GETADDR( RedrawWindow, "RedrawWindow" );
881 GETADDR( UserSignalProc, "UserSignalProc" );
883 #undef GETADDR
886 hModule = GetModuleHandle16( "USER" );
887 pModule = NE_GetPtr( hModule );
888 if ( pModule && !(pModule->flags & NE_FFLAGS_BUILTIN) )
890 #define GETADDR( var, name, thk ) \
891 *(FARPROC *)&Callout.##var = (FARPROC) \
892 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
893 (RELAY)CallTo16_##thk )
895 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
896 GETADDR( GetMessage16, "GetMessage", word_lwww );
897 GETADDR( SendMessage16, "SendMessage", long_wwwl );
898 GETADDR( PostMessage16, "PostMessage", word_wwwl );
899 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
900 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
901 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
902 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
903 GETADDR( InitApp16, "InitApp", word_w );
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 #include "pshpack1.h"
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 #include "poppack.h"
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 && ( type == THUNKLET_TYPE_LS ?
1243 ( thunk->glue == glue - (DWORD)&thunk->type )
1244 : ( thunk->glue == glue ) ) )
1245 return thunk;
1247 return NULL;
1250 /***********************************************************************
1251 * THUNK_AllocLSThunklet
1253 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1254 FARPROC glue, HTASK16 owner )
1256 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1257 THUNKLET_TYPE_LS );
1258 if (!thunk)
1260 TDB *pTask = (TDB*)GlobalLock16( owner );
1262 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1263 return 0;
1265 thunk->prefix_target = thunk->prefix_relay = 0x90;
1266 thunk->pushl_target = thunk->pushl_relay = 0x68;
1267 thunk->jmp_glue = 0xE9;
1269 thunk->target = (DWORD)target;
1270 thunk->relay = (DWORD)relay;
1271 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1273 thunk->type = THUNKLET_TYPE_LS;
1274 thunk->owner = pTask? pTask->hInstance : 0;
1276 thunk->next = ThunkletAnchor;
1277 ThunkletAnchor = thunk;
1280 return (FARPROC)thunk;
1283 /***********************************************************************
1284 * THUNK_AllocSLThunklet
1286 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1287 SEGPTR glue, HTASK16 owner )
1289 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1290 THUNKLET_TYPE_SL );
1291 if (!thunk)
1293 TDB *pTask = (TDB*)GlobalLock16( owner );
1295 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1296 return 0;
1298 thunk->prefix_target = thunk->prefix_relay = 0x66;
1299 thunk->pushl_target = thunk->pushl_relay = 0x68;
1300 thunk->jmp_glue = 0xEA;
1302 thunk->target = (DWORD)target;
1303 thunk->relay = (DWORD)relay;
1304 thunk->glue = (DWORD)glue;
1306 thunk->type = THUNKLET_TYPE_SL;
1307 thunk->owner = pTask? pTask->hInstance : 0;
1309 thunk->next = ThunkletAnchor;
1310 ThunkletAnchor = thunk;
1313 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1316 /**********************************************************************
1317 * IsLSThunklet
1319 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1321 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1322 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1323 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1326 /**********************************************************************
1327 * IsSLThunklet (KERNEL.612)
1329 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1331 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1332 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1333 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1338 /***********************************************************************
1339 * AllocLSThunkletSysthunk (KERNEL.607)
1341 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1342 FARPROC relay, DWORD dummy )
1344 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1345 ThunkletSysthunkGlueLS, GetCurrentTask() );
1348 /***********************************************************************
1349 * AllocSLThunkletSysthunk (KERNEL.608)
1351 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1352 SEGPTR relay, DWORD dummy )
1354 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1355 ThunkletSysthunkGlueSL, GetCurrentTask() );
1359 /***********************************************************************
1360 * AllocLSThunkletCallbackEx (KERNEL.567)
1362 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1363 DWORD relay, HTASK16 task )
1365 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1366 if ( !thunk ) return NULL;
1368 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1369 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1370 return (FARPROC)thunk->target;
1372 return THUNK_AllocLSThunklet( target, relay,
1373 ThunkletCallbackGlueLS, task );
1376 /***********************************************************************
1377 * AllocSLThunkletCallbackEx (KERNEL.568)
1379 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1380 DWORD relay, HTASK16 task )
1382 THUNKLET *thunk = (THUNKLET *)target;
1383 if ( !thunk ) return 0;
1385 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1386 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1387 return (SEGPTR)thunk->target;
1389 return THUNK_AllocSLThunklet( target, relay,
1390 ThunkletCallbackGlueSL, task );
1393 /***********************************************************************
1394 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1396 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1398 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1401 /***********************************************************************
1402 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1404 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1406 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1409 /***********************************************************************
1410 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1412 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1414 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1415 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1416 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1417 return (FARPROC)thunk->target;
1419 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1420 (DWORD)ThunkletCallbackGlueLS,
1421 THUNKLET_TYPE_LS );
1422 return (FARPROC)thunk;
1425 /***********************************************************************
1426 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1428 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1430 THUNKLET *thunk = (THUNKLET *)target;
1431 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1432 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1433 return (SEGPTR)thunk->target;
1435 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1436 (DWORD)ThunkletCallbackGlueSL,
1437 THUNKLET_TYPE_SL );
1438 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1442 /***********************************************************************
1443 * Callback Client API
1446 #define N_CBC_FIXED 20
1447 #define N_CBC_VARIABLE 10
1448 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1450 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1451 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1453 /***********************************************************************
1454 * RegisterCBClient (KERNEL.619)
1456 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1457 SEGPTR *relay16, FARPROC *relay32 )
1459 /* Search for free Callback ID */
1460 if ( wCBCId == -1 )
1461 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1462 if ( !CBClientRelay16[ wCBCId ] )
1463 break;
1465 /* Register Callback ID */
1466 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1468 CBClientRelay16[ wCBCId ] = relay16;
1469 CBClientRelay32[ wCBCId ] = relay32;
1471 else
1472 wCBCId = 0;
1474 return wCBCId;
1477 /***********************************************************************
1478 * UnRegisterCBClient (KERNEL.622)
1480 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1481 SEGPTR *relay16, FARPROC *relay32 )
1483 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1484 && CBClientRelay16[ wCBCId ] == relay16
1485 && CBClientRelay32[ wCBCId ] == relay32 )
1487 CBClientRelay16[ wCBCId ] = 0;
1488 CBClientRelay32[ wCBCId ] = 0;
1490 else
1491 wCBCId = 0;
1493 return wCBCId;
1497 /***********************************************************************
1498 * InitCBClient (KERNEL.623)
1500 void WINAPI InitCBClient16( FARPROC glueLS )
1502 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1503 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1505 SetThunkletCallbackGlue16( glueLS, glueSL );
1508 /***********************************************************************
1509 * CBClientGlueSL (KERNEL.604)
1511 void WINAPI CBClientGlueSL( CONTEXT *context )
1513 /* Create stack frame */
1514 SEGPTR stackSeg = STACK16_PUSH( THREAD_Current(), 12 );
1515 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1516 SEGPTR glue;
1518 stackLin[3] = BP_reg( context );
1519 stackLin[2] = SI_reg( context );
1520 stackLin[1] = DI_reg( context );
1521 stackLin[0] = DS_reg( context );
1523 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1524 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1525 GS_reg( context ) = 0;
1527 /* Jump to 16-bit relay code */
1528 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1529 CS_reg ( context ) = SELECTOROF( glue );
1530 EIP_reg( context ) = OFFSETOF ( glue );
1533 /***********************************************************************
1534 * CBClientThunkSL (KERNEL.620)
1536 void WINAPI CBClientThunkSL( CONTEXT *context )
1538 /* Call 32-bit relay code */
1539 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1541 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1542 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1544 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1547 /***********************************************************************
1548 * CBClientThunkSLEx (KERNEL.621)
1550 void WINAPI CBClientThunkSLEx( CONTEXT *context )
1552 /* Call 32-bit relay code */
1553 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args,
1554 DWORD *esi, INT *nArgs );
1556 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1557 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1558 INT nArgs;
1559 LPWORD stackLin;
1561 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1563 /* Restore registers saved by CBClientGlueSL */
1564 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1565 BP_reg( context ) = stackLin[3];
1566 SI_reg( context ) = stackLin[2];
1567 DI_reg( context ) = stackLin[1];
1568 DS_reg( context ) = stackLin[0];
1569 SP_reg( context ) += 16+nArgs;
1571 /* Return to caller of CBClient thunklet */
1572 CS_reg ( context ) = stackLin[9];
1573 EIP_reg( context ) = stackLin[8];