Always retrieve Callout routines using GetProcAddress().
[wine/testsucceed.git] / if1632 / thunk.c
blob64e15859dc3303b344966830ec9c50d1adcaaad0
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 "builtin16.h"
14 #include "user.h"
15 #include "heap.h"
16 #include "neexe.h"
17 #include "process.h"
18 #include "stackframe.h"
19 #include "win.h"
20 #include "flatthunk.h"
21 #include "mouse.h"
22 #include "keyboard.h"
23 #include "debugtools.h"
25 DECLARE_DEBUG_CHANNEL(relay)
26 DECLARE_DEBUG_CHANNEL(system)
27 DECLARE_DEBUG_CHANNEL(thunk)
30 extern LONG CALLBACK CallTo16RegisterShort(const CONTEXT86 *context, INT offset);
31 extern LONG CALLBACK CallTo16RegisterLong(const CONTEXT86 *context, INT offset);
33 /* List of the 16-bit callback functions. This list is used */
34 /* by the build program to generate the file if1632/callto16.S */
36 /* ### start build ### */
37 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
38 extern LONG CALLBACK THUNK_CallTo16_long_ (FARPROC16);
39 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
40 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
41 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
42 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
43 extern WORD CALLBACK THUNK_CallTo16_word_wl (FARPROC16,WORD,LONG);
44 extern WORD CALLBACK THUNK_CallTo16_word_ll (FARPROC16,LONG,LONG);
45 extern LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
46 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
47 extern WORD CALLBACK THUNK_CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
48 extern WORD CALLBACK THUNK_CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
49 extern LONG CALLBACK THUNK_CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
50 extern LONG CALLBACK THUNK_CallTo16_long_lll (FARPROC16,LONG,LONG,LONG);
51 extern WORD CALLBACK THUNK_CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
52 extern WORD CALLBACK THUNK_CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
53 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
54 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
55 extern WORD CALLBACK THUNK_CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
56 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
57 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
58 extern WORD CALLBACK THUNK_CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
59 extern LONG CALLBACK THUNK_CallTo16_long_llll (FARPROC16,LONG,LONG,LONG,LONG);
60 extern WORD CALLBACK THUNK_CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
61 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
62 extern LONG CALLBACK THUNK_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
63 extern WORD CALLBACK THUNK_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
64 extern WORD CALLBACK THUNK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
65 extern WORD CALLBACK THUNK_CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
66 extern LONG CALLBACK THUNK_CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
67 extern LONG CALLBACK THUNK_CallTo16_long_lllll(FARPROC16,LONG,LONG,LONG,LONG,LONG);
68 extern LONG CALLBACK THUNK_CallTo16_long_llllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,LONG);
69 extern LONG CALLBACK THUNK_CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,LONG,LONG);
70 extern WORD CALLBACK THUNK_CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,LONG,LONG);
71 extern LONG CALLBACK THUNK_CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
72 LONG,LONG,LONG);
73 extern LONG CALLBACK THUNK_CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
74 LONG,LONG,LONG);
75 extern LONG CALLBACK THUNK_CallTo16_long_lllllllll(FARPROC16,LONG,LONG,LONG,LONG,
76 LONG,LONG,LONG,LONG,LONG);
77 extern LONG CALLBACK THUNK_CallTo16_long_llllllllll(FARPROC16,LONG,LONG,LONG,LONG,
78 LONG,LONG,LONG,LONG,LONG,LONG);
79 extern LONG CALLBACK THUNK_CallTo16_long_lllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
80 LONG,LONG,LONG,LONG,LONG,LONG,
81 LONG);
82 extern LONG CALLBACK THUNK_CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
83 LONG,LONG,LONG,LONG,LONG,LONG,
84 LONG,LONG);
85 extern LONG CALLBACK THUNK_CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
86 LONG,WORD,LONG,LONG,LONG,LONG,
87 LONG,WORD);
88 extern LONG CALLBACK THUNK_CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
89 LONG,LONG,LONG,LONG,LONG,LONG,
90 LONG,LONG,LONG);
91 extern LONG CALLBACK THUNK_CallTo16_long_llllllllllllll(FARPROC16,LONG,LONG,LONG,
92 LONG,LONG,LONG,LONG,LONG,
93 LONG,LONG,LONG,LONG,LONG,
94 LONG);
95 extern LONG CALLBACK THUNK_CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
96 WORD,WORD,LONG,WORD,WORD,
97 WORD,WORD,LONG,LONG,LONG,
98 LONG);
99 extern LONG CALLBACK THUNK_CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
100 LONG,LONG,LONG,LONG,LONG,
101 LONG,LONG,LONG,LONG,LONG,
102 LONG,LONG);
103 extern LONG CALLBACK THUNK_CallTo16_long_llllllllllllllll(FARPROC16,LONG,LONG,LONG,
104 LONG,LONG,LONG,LONG,LONG,
105 LONG,LONG,LONG,LONG,LONG,
106 LONG,LONG,LONG);
107 extern void CALLBACK THUNK_CallFrom16_p_long_wwwll();
108 /* ### stop build ### */
111 typedef void (*RELAY)();
113 #include "pshpack1.h"
115 typedef struct tagTHUNK
117 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
118 BYTE pushl_func; /* 0x68 pushl $proc */
119 FARPROC proc WINE_PACKED;
120 BYTE pushl_eax; /* 0x50 pushl %eax */
121 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
122 RELAY relay WINE_PACKED;
123 struct tagTHUNK *next WINE_PACKED;
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 /* Callbacks function table for the emulator */
148 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
150 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
151 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
152 (void*)THUNK_CallFrom16_p_long_wwwll, /* CallFrom16WndProc */
153 THUNK_CallWndProc16, /* CallWndProc */
154 (void *)THUNK_CallTo16_long_lwwll, /* CallDriverProc */
155 (void *)THUNK_CallTo16_word_wwlll, /* CallDriverCallback */
156 (void *)THUNK_CallTo16_word_wwlll, /* CallTimeFuncProc */
157 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
158 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
159 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
160 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
161 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
162 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
163 (void *)THUNK_CallTo16_long_l, /* CallWOWCallbackProc */
164 THUNK_WOWCallback16Ex, /* CallWOWCallback16Ex */
165 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
166 (void *)THUNK_CallTo16_long_l, /* CallASPIPostProc */
167 (void *)THUNK_CallTo16_word_lwll, /* CallDrvControlProc */
168 (void *)THUNK_CallTo16_word_lwlll, /* CallDrvEnableProc */
169 (void *)THUNK_CallTo16_word_llll, /* CallDrvEnumDFontsProc */
170 (void *)THUNK_CallTo16_word_lwll, /* CallDrvEnumObjProc */
171 (void *)THUNK_CallTo16_word_lwwlllll, /* CallDrvOutputProc */
172 (void *)THUNK_CallTo16_long_lwlll, /* CallDrvRealizeProc */
173 (void *)THUNK_CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
174 (void *)THUNK_CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
175 (void *)THUNK_CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
176 (void *)THUNK_CallTo16_word_ww /* CallDrvAbortProc */
180 /***********************************************************************
181 * THUNK_Init
183 BOOL THUNK_Init(void)
185 /* Set the window proc calling functions */
186 Callbacks = &CALLBACK_EmulatorTable;
187 /* Initialize Thunklets */
188 return THUNK_ThunkletInit();
191 /***********************************************************************
192 * THUNK_Alloc
194 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
196 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
197 if (thunk)
199 thunk->popl_eax = 0x58;
200 thunk->pushl_func = 0x68;
201 thunk->proc = func;
202 thunk->pushl_eax = 0x50;
203 thunk->jmp = 0xe9;
204 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
205 thunk->next = firstThunk;
206 firstThunk = thunk;
208 return thunk;
212 /***********************************************************************
213 * THUNK_Find
215 static THUNK *THUNK_Find( FARPROC func )
217 THUNK *thunk = firstThunk;
218 while (thunk && (thunk->proc != func)) thunk = thunk->next;
219 return thunk;
223 /***********************************************************************
224 * THUNK_Free
226 static void THUNK_Free( THUNK *thunk )
228 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
230 THUNK **prev = &firstThunk;
231 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
232 if (*prev)
234 *prev = thunk->next;
235 HeapFree( GetProcessHeap(), 0, thunk );
236 return;
239 ERR_(thunk)("invalid thunk addr %p\n", thunk );
243 /***********************************************************************
244 * THUNK_CallWndProc16
246 * Call a 16-bit window procedure
248 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
249 UINT16 msg, WPARAM16 wParam,
250 LPARAM lParam )
252 CONTEXT86 context;
253 LRESULT ret;
254 WORD *args;
255 WND *wndPtr = WIN_FindWndPtr( hwnd );
256 DWORD offset = 0;
257 TEB *teb = NtCurrentTeb();
258 int iWndsLocks;
260 /* Window procedures want ax = hInstance, ds = es = ss */
262 memset(&context, '\0', sizeof(context));
263 DS_reg(&context) = SELECTOROF(teb->cur_stack);
264 ES_reg(&context) = DS_reg(&context);
265 EAX_reg(&context) = wndPtr ? wndPtr->hInstance : DS_reg(&context);
266 CS_reg(&context) = SELECTOROF(proc);
267 EIP_reg(&context) = OFFSETOF(proc);
268 EBP_reg(&context) = OFFSETOF(teb->cur_stack)
269 + (WORD)&((STACK16FRAME*)0)->bp;
271 WIN_ReleaseWndPtr(wndPtr);
273 if (lParam)
275 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
276 work if structures passed in lParam are placed in the stack/data
277 segment. Programmers easily make the mistake of converting lParam
278 to a near rather than a far pointer, since Windows apparently
279 allows this. We copy the structures to the 16 bit stack; this is
280 ugly but makes these programs work. */
281 switch (msg)
283 case WM_CREATE:
284 case WM_NCCREATE:
285 offset = sizeof(CREATESTRUCT16); break;
286 case WM_DRAWITEM:
287 offset = sizeof(DRAWITEMSTRUCT16); break;
288 case WM_COMPAREITEM:
289 offset = sizeof(COMPAREITEMSTRUCT16); break;
291 if (offset)
293 void *s = PTR_SEG_TO_LIN(lParam);
294 lParam = STACK16_PUSH( teb, offset );
295 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
299 iWndsLocks = WIN_SuspendWndsLock();
301 args = (WORD *)THREAD_STACK16(teb) - 5;
302 args[0] = LOWORD(lParam);
303 args[1] = HIWORD(lParam);
304 args[2] = wParam;
305 args[3] = msg;
306 args[4] = hwnd;
308 ret = CallTo16RegisterShort( &context, 5 * sizeof(WORD) );
309 if (offset) STACK16_POP( teb, offset );
311 WIN_RestoreWndsLock(iWndsLocks);
313 return ret;
317 /***********************************************************************
318 * THUNK_EnumObjects16 (GDI.71)
320 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
321 GOBJENUMPROC16 func, LPARAM lParam )
323 DECL_THUNK( thunk, func, THUNK_CallTo16_word_ll );
324 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
328 /*************************************************************************
329 * THUNK_EnumFonts16 (GDI.70)
331 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
332 FONTENUMPROC16 func, LPARAM lParam )
334 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
335 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
338 /******************************************************************
339 * THUNK_EnumMetaFile16 (GDI.175)
341 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
342 MFENUMPROC16 func, LPARAM lParam )
344 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wllwl );
345 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
349 /*************************************************************************
350 * THUNK_EnumFontFamilies16 (GDI.330)
352 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
353 FONTENUMPROC16 func, LPARAM lParam )
355 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
356 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
360 /*************************************************************************
361 * THUNK_EnumFontFamiliesEx16 (GDI.613)
363 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
364 FONTENUMPROCEX16 func, LPARAM lParam,
365 DWORD reserved )
367 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
368 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
369 lParam, reserved );
373 /**********************************************************************
374 * THUNK_LineDDA16 (GDI.100)
376 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
377 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
379 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wwl );
380 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
384 /*******************************************************************
385 * THUNK_EnumWindows16 (USER.54)
387 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
389 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
390 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
394 /**********************************************************************
395 * THUNK_EnumChildWindows16 (USER.55)
397 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
398 LPARAM lParam )
400 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
401 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
405 /**********************************************************************
406 * THUNK_EnumTaskWindows16 (USER.225)
408 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
409 LPARAM lParam )
411 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
412 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
416 /***********************************************************************
417 * THUNK_EnumProps16 (USER.27)
419 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
421 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
422 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
426 /***********************************************************************
427 * THUNK_GrayString16 (USER.185)
429 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
430 GRAYSTRINGPROC16 func, LPARAM lParam,
431 INT16 cch, INT16 x, INT16 y,
432 INT16 cx, INT16 cy )
434 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
435 if (!func)
436 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
437 else
438 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
439 x, y, cx, cy );
443 /***********************************************************************
444 * THUNK_SetWindowsHook16 (USER.121)
446 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
448 HINSTANCE16 hInst = FarGetOwner16( HIWORD(proc) );
449 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
450 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
451 if (!thunk) return 0;
452 return (FARPROC16)SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask);
456 /***********************************************************************
457 * THUNK_UnhookWindowsHook16 (USER.234)
459 BOOL16 WINAPI THUNK_UnhookWindowsHook16( INT16 id, HOOKPROC16 proc )
461 BOOL16 ret;
462 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
463 if (!thunk) return FALSE;
464 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
465 THUNK_Free( thunk );
466 return ret;
470 /***********************************************************************
471 * THUNK_SetWindowsHookEx16 (USER.291)
473 HHOOK WINAPI THUNK_SetWindowsHookEx16( INT16 id, HOOKPROC16 proc,
474 HINSTANCE16 hInst, HTASK16 hTask )
476 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
477 if (!thunk) return 0;
478 return SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask );
482 /***********************************************************************
483 * THUNK_UnhookWindowHookEx16 (USER.292)
485 BOOL16 WINAPI THUNK_UnhookWindowsHookEx16( HHOOK hhook )
487 THUNK *thunk = (THUNK *)HOOK_GetProc16( hhook );
488 BOOL16 ret = UnhookWindowsHookEx16( hhook );
489 if (thunk) THUNK_Free( thunk );
490 return ret;
495 static FARPROC16 defDCHookProc = NULL;
497 /***********************************************************************
498 * THUNK_SetDCHook (GDI.190)
500 BOOL16 WINAPI THUNK_SetDCHook( HDC16 hdc, FARPROC16 proc, DWORD dwHookData )
502 THUNK *thunk, *oldThunk;
504 if (!defDCHookProc) /* Get DCHook Win16 entry point */
506 HMODULE16 hModule = GetModuleHandle16( "USER" );
507 NE_MODULE *pModule = NE_GetPtr( hModule );
509 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) )
510 defDCHookProc = NE_GetEntryPoint( hModule, 362 );
511 else
512 defDCHookProc = (FARPROC16)-1;
515 if (!proc)
516 thunk = NULL;
517 else if (proc != defDCHookProc)
519 thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwll );
520 if (!thunk) return FALSE;
522 else thunk = (THUNK *)DCHook16;
524 /* Free the previous thunk */
525 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
526 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
528 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
532 /***********************************************************************
533 * THUNK_GetDCHook (GDI.191)
535 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
537 THUNK *thunk = NULL;
538 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
539 if (thunk)
541 if (thunk == (THUNK *)DCHook16)
543 /* Note: we can only get here when running built-in USER */
545 if (!defDCHookProc) /* Get DCHook Win16 entry point */
546 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
548 *phookProc = defDCHookProc;
550 else *phookProc = thunk->proc;
552 return ret;
556 /***********************************************************************
557 * THUNK_SetTaskSignalProc (KERNEL.38)
559 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
561 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwwww );
562 if ( !thunk ) return NULL;
564 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
565 if ( !thunk ) return NULL;
567 proc = thunk->proc;
568 THUNK_Free( thunk );
569 return proc;
572 /***********************************************************************
573 * THUNK_CreateThread16 (KERNEL.441)
575 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
577 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
578 DWORD param = ((DWORD *)threadArgs)[1];
579 HeapFree( GetProcessHeap(), 0, threadArgs );
581 return THUNK_CallTo16_long_l( start, param );
583 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
584 FARPROC16 start, SEGPTR param,
585 DWORD flags, LPDWORD id )
587 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
588 if (!threadArgs) return INVALID_HANDLE_VALUE;
589 threadArgs[0] = (DWORD)start;
590 threadArgs[1] = (DWORD)param;
592 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
595 /***********************************************************************
596 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
597 * Generic thunking routine to call 16 bit functions from 32bit code.
599 * RETURNS
600 * TRUE if the call was done
602 static BOOL WINAPI THUNK_WOWCallback16Ex(
603 FARPROC16 proc, /* [in] 16bit function to call */
604 DWORD dwFlags, /* [in] flags (WCB_*) */
605 DWORD cbArgs, /* [in] number of arguments */
606 LPVOID xargs, /* [in/out] arguments */
607 LPDWORD pdwret /* [out] return value of the 16bit call */
609 LPDWORD args = (LPDWORD)xargs;
610 DWORD ret,i;
612 TRACE_(relay)("(%p,0x%08lx,%ld,%p,%p)\n",
613 proc,dwFlags,cbArgs,xargs,pdwret
615 if (dwFlags == WCB16_CDECL) {
616 /* swap the arguments */
617 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
618 for (i=0;i<cbArgs;i++)
619 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
621 switch (cbArgs) {
622 case 0: ret = THUNK_CallTo16_long_(proc);break;
623 case 1: ret = THUNK_CallTo16_long_l(proc,args[0]);break;
624 case 2: ret = THUNK_CallTo16_long_ll(proc,args[0],args[1]);break;
625 case 3: ret = THUNK_CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
626 case 4: ret = THUNK_CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
627 break;
628 case 5: ret = THUNK_CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
629 args[4]
631 break;
632 case 6: ret = THUNK_CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
633 args[4],args[5]
635 break;
636 case 7: ret = THUNK_CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
637 args[4],args[5],args[6]
639 break;
640 case 8: ret = THUNK_CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
641 args[4],args[5],args[6],args[7]
643 break;
644 case 9: ret = THUNK_CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
645 args[4],args[5],args[6],args[7],args[8]
647 break;
648 case 10:ret = THUNK_CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
649 args[4],args[5],args[6],args[7],args[8],args[9]
651 break;
652 case 11:ret = THUNK_CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
653 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
655 break;
656 case 12:ret = THUNK_CallTo16_long_llllllllllll(proc,args[0],args[1],args[2],
657 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
658 args[10],args[11]
660 break;
661 case 13:ret = THUNK_CallTo16_long_lllllllllllll(proc,args[0],args[1],args[2],
662 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
663 args[10],args[11],args[12]
665 break;
666 case 14:ret = THUNK_CallTo16_long_llllllllllllll(proc,args[0],args[1],args[2],
667 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
668 args[10],args[11],args[12],args[13]
670 break;
671 case 15:ret = THUNK_CallTo16_long_lllllllllllllll(proc,args[0],args[1],args[2],
672 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
673 args[10],args[11],args[12],args[13],args[14]
675 break;
676 case 16:ret = THUNK_CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
677 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
678 args[10],args[11],args[12],args[13],args[14],args[15]
680 break;
681 default:
682 ERR_(thunk)("%ld arguments not supported.\n",cbArgs);
683 if (dwFlags == WCB16_CDECL)
684 HeapFree(GetProcessHeap(),0,args);
685 return FALSE;
687 if (dwFlags == WCB16_CDECL)
688 HeapFree(GetProcessHeap(),0,args);
689 if (pdwret)
690 *pdwret = ret;
691 return TRUE;
694 /***********************************************************************
695 * THUNK_MOUSE_Enable (MOUSE.2)
697 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
698 DWORD dwFlags, DWORD dx, DWORD dy,
699 DWORD cButtons, DWORD dwExtraInfo )
701 CONTEXT86 context;
703 memset( &context, 0, sizeof(context) );
704 CS_reg(&context) = SELECTOROF( proc );
705 EIP_reg(&context) = OFFSETOF( proc );
706 AX_reg(&context) = (WORD)dwFlags;
707 BX_reg(&context) = (WORD)dx;
708 CX_reg(&context) = (WORD)dy;
709 DX_reg(&context) = (WORD)cButtons;
710 SI_reg(&context) = LOWORD( dwExtraInfo );
711 DI_reg(&context) = HIWORD( dwExtraInfo );
713 CallTo16RegisterShort( &context, 0 );
715 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
717 static THUNK *lastThunk = NULL;
718 static FARPROC16 lastProc = NULL;
720 if ( lastProc != proc )
722 if ( lastThunk )
723 THUNK_Free( lastThunk );
725 if ( !proc )
726 lastThunk = NULL;
727 else
728 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
730 lastProc = proc;
733 MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
736 /***********************************************************************
737 * GetMouseEventProc (USER.337)
739 FARPROC16 WINAPI GetMouseEventProc16(void)
741 HMODULE16 hmodule = GetModuleHandle16("USER");
742 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
746 /***********************************************************************
747 * WIN16_mouse_event (USER.299)
749 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
751 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
752 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
756 /***********************************************************************
757 * THUNK_KEYBD_Enable (KEYBOARD.2)
759 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
760 BYTE bVk, BYTE bScan,
761 DWORD dwFlags, DWORD dwExtraInfo )
763 CONTEXT86 context;
765 memset( &context, 0, sizeof(context) );
766 CS_reg(&context) = SELECTOROF( proc );
767 EIP_reg(&context) = OFFSETOF( proc );
768 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
769 AL_reg(&context) = bVk;
770 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
771 BL_reg(&context) = bScan;
772 SI_reg(&context) = LOWORD( dwExtraInfo );
773 DI_reg(&context) = HIWORD( dwExtraInfo );
775 CallTo16RegisterShort( &context, 0 );
777 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
779 static THUNK *lastThunk = NULL;
780 static FARPROC16 lastProc = NULL;
782 if ( lastProc != proc )
784 if ( lastThunk )
785 THUNK_Free( lastThunk );
787 if ( !proc )
788 lastThunk = NULL;
789 else
790 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
792 lastProc = proc;
795 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
798 /***********************************************************************
799 * WIN16_keybd_event (USER.289)
801 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
803 DWORD dwFlags = 0;
805 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
806 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
808 keybd_event( AL_reg(context), BL_reg(context),
809 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
813 /***********************************************************************
814 * WIN16_CreateSystemTimer (SYSTEM.2)
816 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
818 CONTEXT86 context;
819 memset( &context, '\0', sizeof(context) );
821 CS_reg( &context ) = SELECTOROF( proc );
822 IP_reg( &context ) = OFFSETOF( proc );
823 BP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
824 + (WORD)&((STACK16FRAME*)0)->bp;
826 AX_reg( &context ) = timer;
828 if ( _ConfirmWin16Lock() )
830 FIXME_(system)("Skipping timer %d callback because timer signal "
831 "arrived while we own the Win16Lock!\n", timer );
832 return;
835 CallTo16RegisterShort( &context, 0 );
837 /* FIXME: This does not work if the signal occurs while this thread
838 is currently in 16-bit code. With the current structure
839 of the Wine thunking code, this seems to be hard to fix ... */
841 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
843 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
844 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
845 if (!timer) THUNK_Free( thunk );
846 return timer;
849 /***********************************************************************
850 * THUNK_GetCalloutThunk
852 * Retrieve API entry point with given name from given module.
853 * If module is builtin, return the 32-bit entry point, otherwise
854 * create a 32->16 thunk to the 16-bit entry point, using the
855 * given relay code.
858 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
860 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
861 if ( !proc ) return 0;
863 if ( pModule->flags & NE_FFLAGS_BUILTIN )
864 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
865 else
866 return (FARPROC)THUNK_Alloc( proc, relay );
869 /***********************************************************************
870 * THUNK_InitCallout
872 void THUNK_InitCallout(void)
874 HMODULE hModule;
875 NE_MODULE *pModule;
877 hModule = GetModuleHandleA( "USER32" );
878 if ( hModule )
880 #define GETADDR( var, name ) \
881 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
883 GETADDR( PeekMessageA, "PeekMessageA" );
884 GETADDR( PeekMessageW, "PeekMessageW" );
885 GETADDR( GetMessageA, "GetMessageA" );
886 GETADDR( GetMessageW, "GetMessageW" );
887 GETADDR( SendMessageA, "SendMessageA" );
888 GETADDR( SendMessageW, "SendMessageW" );
889 GETADDR( PostMessageA, "PostMessageA" );
890 GETADDR( PostMessageW, "PostMessageW" );
891 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
892 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
893 GETADDR( TranslateMessage, "TranslateMessage" );
894 GETADDR( DispatchMessageW, "DispatchMessageW" );
895 GETADDR( DispatchMessageA, "DispatchMessageA" );
896 GETADDR( RedrawWindow, "RedrawWindow" );
897 GETADDR( UserSignalProc, "UserSignalProc" );
899 #undef GETADDR
902 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
903 if ( pModule )
905 #define GETADDR( var, name, thk ) \
906 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
907 (RELAY)THUNK_CallTo16_##thk )
909 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
910 GETADDR( GetMessage16, "GetMessage", word_lwww );
911 GETADDR( SendMessage16, "SendMessage", long_wwwl );
912 GETADDR( PostMessage16, "PostMessage", word_wwwl );
913 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
914 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
915 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
916 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
917 GETADDR( InitApp16, "InitApp", word_w );
918 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
919 GETADDR( UserYield16, "UserYield", word_ );
920 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
922 #undef GETADDR
926 /***********************************************************************
927 * 16->32 Flat Thunk routines:
930 /***********************************************************************
931 * ThunkConnect16 (KERNEL.651)
932 * Connects a 32bit and a 16bit thunkbuffer.
934 UINT WINAPI ThunkConnect16(
935 LPSTR module16, /* [in] name of win16 dll */
936 LPSTR module32, /* [in] name of win32 dll */
937 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
938 DWORD dwReason, /* [in] initialisation argument */
939 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
940 LPSTR thunkfun32, /* [in] win32 thunkfunction */
941 WORD cs /* [in] CS of win16 dll */
943 BOOL directionSL;
945 if (!lstrncmpA(TD->magic, "SL01", 4))
947 directionSL = TRUE;
949 TRACE_(thunk)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
950 module16, (DWORD)TD, module32, thunkfun32, dwReason);
952 else if (!lstrncmpA(TD->magic, "LS01", 4))
954 directionSL = FALSE;
956 TRACE_(thunk)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
957 module16, (DWORD)TD, module32, thunkfun32, dwReason);
959 else
961 ERR_(thunk)("Invalid magic %c%c%c%c\n",
962 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
963 return 0;
966 switch (dwReason)
968 case DLL_PROCESS_ATTACH:
969 if (directionSL)
971 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
972 struct ThunkDataSL *SL = SL16->fpData;
974 if (SL == NULL)
976 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
978 SL->common = SL16->common;
979 SL->flags1 = SL16->flags1;
980 SL->flags2 = SL16->flags2;
982 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
983 SL->targetDB = NULL;
985 lstrcpynA(SL->pszDll16, module16, 255);
986 lstrcpynA(SL->pszDll32, module32, 255);
988 /* We should create a SEGPTR to the ThunkDataSL,
989 but since the contents are not in the original format,
990 any access to this by 16-bit code would crash anyway. */
991 SL16->spData = 0;
992 SL16->fpData = SL;
996 if (SL->flags2 & 0x80000000)
998 TRACE_(thunk)("Preloading 32-bit library\n");
999 LoadLibraryA(module32);
1002 else
1004 /* nothing to do */
1006 break;
1008 case DLL_PROCESS_DETACH:
1009 /* FIXME: cleanup */
1010 break;
1013 return 1;
1017 /***********************************************************************
1018 * C16ThkSL (KERNEL.630)
1021 void WINAPI C16ThkSL(CONTEXT86 *context)
1023 extern void CallFrom16Thunk(void);
1024 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1025 WORD cs, ds;
1026 GET_CS(cs);
1027 GET_DS(ds);
1029 /* We produce the following code:
1031 * mov ax, __FLATDS
1032 * mov es, ax
1033 * movzx ecx, cx
1034 * mov edx, es:[ecx + $EDX]
1035 * push bp
1036 * push edx
1037 * call __FLATCS:CallFrom16_t_long_
1040 *x++ = 0xB8; *((WORD *)x)++ = ds;
1041 *x++ = 0x8E; *x++ = 0xC0;
1042 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1043 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1044 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
1046 *x++ = 0x55;
1047 *x++ = 0x66; *x++ = 0x52;
1048 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1049 *((WORD *)x)++ = cs;
1051 /* Jump to the stub code just created */
1052 IP_reg(context) = LOWORD(EAX_reg(context));
1053 CS_reg(context) = HIWORD(EAX_reg(context));
1055 /* Since C16ThkSL got called by a jmp, we need to leave the
1056 orginal return address on the stack */
1057 SP_reg(context) -= 4;
1060 /***********************************************************************
1061 * C16ThkSL01 (KERNEL.631)
1064 void WINAPI C16ThkSL01(CONTEXT86 *context)
1066 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1068 if (stub)
1070 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
1071 struct ThunkDataSL *td = SL16->fpData;
1073 extern void CallFrom16Thunk(void);
1074 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1075 WORD cs;
1076 GET_CS(cs);
1078 if (!td)
1080 ERR_(thunk)("ThunkConnect16 was not called!\n");
1081 return;
1084 TRACE_(thunk)("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1087 /* We produce the following code:
1089 * xor eax, eax
1090 * mov edx, $td
1091 * call C16ThkSL01
1092 * push bp
1093 * push edx
1094 * call __FLATCS:CallFrom16_t_long_
1097 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1098 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1099 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1101 *x++ = 0x55;
1102 *x++ = 0x66; *x++ = 0x52;
1103 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1104 *((WORD *)x)++ = cs;
1106 /* Jump to the stub code just created */
1107 IP_reg(context) = LOWORD(EAX_reg(context));
1108 CS_reg(context) = HIWORD(EAX_reg(context));
1110 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1111 orginal return address on the stack */
1112 SP_reg(context) -= 4;
1114 else
1116 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1117 DWORD targetNr = CX_reg(context) / 4;
1118 struct SLTargetDB *tdb;
1120 TRACE_(thunk)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1121 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1123 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1124 if (tdb->process == PROCESS_Current())
1125 break;
1127 if (!tdb)
1129 TRACE_(thunk)("Loading 32-bit library %s\n", td->pszDll32);
1130 LoadLibraryA(td->pszDll32);
1132 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1133 if (tdb->process == PROCESS_Current())
1134 break;
1137 if (tdb)
1139 EDX_reg(context) = tdb->targetTable[targetNr];
1141 TRACE_(thunk)("Call target is %08lx\n", EDX_reg(context));
1143 else
1145 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1146 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1147 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1148 IP_reg(context) = stack[2];
1149 CS_reg(context) = stack[3];
1150 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1152 ERR_(thunk)("Process %08lx did not ThunkConnect32 %s to %s\n",
1153 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1158 DWORD WINAPI
1159 WOW16Call(WORD x,WORD y,WORD z) {
1160 int i;
1161 DWORD calladdr;
1162 FIXME_(thunk)("(0x%04x,0x%04x,%d),calling (",x,y,z);
1164 for (i=0;i<x/2;i++) {
1165 WORD a = STACK16_POP(NtCurrentTeb(),2);
1166 DPRINTF("%04x ",a);
1168 calladdr = STACK16_POP(NtCurrentTeb(),4);
1169 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1170 return 0;
1174 /***********************************************************************
1175 * 16<->32 Thunklet/Callback API:
1178 #include "pshpack1.h"
1179 typedef struct _THUNKLET
1181 BYTE prefix_target;
1182 BYTE pushl_target;
1183 DWORD target;
1185 BYTE prefix_relay;
1186 BYTE pushl_relay;
1187 DWORD relay;
1189 BYTE jmp_glue;
1190 DWORD glue;
1192 BYTE type;
1193 HINSTANCE16 owner;
1194 struct _THUNKLET *next;
1195 } THUNKLET;
1196 #include "poppack.h"
1198 #define THUNKLET_TYPE_LS 1
1199 #define THUNKLET_TYPE_SL 2
1201 static HANDLE ThunkletHeap = 0;
1202 static THUNKLET *ThunkletAnchor = NULL;
1204 static FARPROC ThunkletSysthunkGlueLS = 0;
1205 static SEGPTR ThunkletSysthunkGlueSL = 0;
1207 static FARPROC ThunkletCallbackGlueLS = 0;
1208 static SEGPTR ThunkletCallbackGlueSL = 0;
1210 /***********************************************************************
1211 * THUNK_ThunkletInit
1213 static BOOL THUNK_ThunkletInit( void )
1215 LPBYTE thunk;
1217 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1218 if (!ThunkletHeap) return FALSE;
1220 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1221 if (!thunk) return FALSE;
1223 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1224 *thunk++ = 0x58; /* popl eax */
1225 *thunk++ = 0xC3; /* ret */
1227 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1228 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1229 *thunk++ = 0xCB; /* lret */
1231 return TRUE;
1234 /***********************************************************************
1235 * SetThunkletCallbackGlue (KERNEL.560)
1237 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1239 ThunkletCallbackGlueLS = glueLS;
1240 ThunkletCallbackGlueSL = glueSL;
1244 /***********************************************************************
1245 * THUNK_FindThunklet
1247 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1248 DWORD glue, BYTE type )
1250 THUNKLET *thunk;
1252 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1253 if ( thunk->type == type
1254 && thunk->target == target
1255 && thunk->relay == relay
1256 && ( type == THUNKLET_TYPE_LS ?
1257 ( thunk->glue == glue - (DWORD)&thunk->type )
1258 : ( thunk->glue == glue ) ) )
1259 return thunk;
1261 return NULL;
1264 /***********************************************************************
1265 * THUNK_AllocLSThunklet
1267 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1268 FARPROC glue, HTASK16 owner )
1270 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1271 THUNKLET_TYPE_LS );
1272 if (!thunk)
1274 TDB *pTask = (TDB*)GlobalLock16( owner );
1276 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1277 return 0;
1279 thunk->prefix_target = thunk->prefix_relay = 0x90;
1280 thunk->pushl_target = thunk->pushl_relay = 0x68;
1281 thunk->jmp_glue = 0xE9;
1283 thunk->target = (DWORD)target;
1284 thunk->relay = (DWORD)relay;
1285 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1287 thunk->type = THUNKLET_TYPE_LS;
1288 thunk->owner = pTask? pTask->hInstance : 0;
1290 thunk->next = ThunkletAnchor;
1291 ThunkletAnchor = thunk;
1294 return (FARPROC)thunk;
1297 /***********************************************************************
1298 * THUNK_AllocSLThunklet
1300 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1301 SEGPTR glue, HTASK16 owner )
1303 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1304 THUNKLET_TYPE_SL );
1305 if (!thunk)
1307 TDB *pTask = (TDB*)GlobalLock16( owner );
1309 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1310 return 0;
1312 thunk->prefix_target = thunk->prefix_relay = 0x66;
1313 thunk->pushl_target = thunk->pushl_relay = 0x68;
1314 thunk->jmp_glue = 0xEA;
1316 thunk->target = (DWORD)target;
1317 thunk->relay = (DWORD)relay;
1318 thunk->glue = (DWORD)glue;
1320 thunk->type = THUNKLET_TYPE_SL;
1321 thunk->owner = pTask? pTask->hInstance : 0;
1323 thunk->next = ThunkletAnchor;
1324 ThunkletAnchor = thunk;
1327 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1330 /**********************************************************************
1331 * IsLSThunklet
1333 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1335 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1336 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1337 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1340 /**********************************************************************
1341 * IsSLThunklet (KERNEL.612)
1343 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1345 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1346 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1347 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1352 /***********************************************************************
1353 * AllocLSThunkletSysthunk (KERNEL.607)
1355 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1356 FARPROC relay, DWORD dummy )
1358 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1359 ThunkletSysthunkGlueLS, GetCurrentTask() );
1362 /***********************************************************************
1363 * AllocSLThunkletSysthunk (KERNEL.608)
1365 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1366 SEGPTR relay, DWORD dummy )
1368 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1369 ThunkletSysthunkGlueSL, GetCurrentTask() );
1373 /***********************************************************************
1374 * AllocLSThunkletCallbackEx (KERNEL.567)
1376 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1377 DWORD relay, HTASK16 task )
1379 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1380 if ( !thunk ) return NULL;
1382 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1383 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1384 return (FARPROC)thunk->target;
1386 return THUNK_AllocLSThunklet( target, relay,
1387 ThunkletCallbackGlueLS, task );
1390 /***********************************************************************
1391 * AllocSLThunkletCallbackEx (KERNEL.568)
1393 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1394 DWORD relay, HTASK16 task )
1396 THUNKLET *thunk = (THUNKLET *)target;
1397 if ( !thunk ) return 0;
1399 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1400 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1401 return (SEGPTR)thunk->target;
1403 return THUNK_AllocSLThunklet( target, relay,
1404 ThunkletCallbackGlueSL, task );
1407 /***********************************************************************
1408 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1410 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1412 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1415 /***********************************************************************
1416 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1418 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1420 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1423 /***********************************************************************
1424 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1426 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1428 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1429 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1430 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1431 return (FARPROC)thunk->target;
1433 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1434 (DWORD)ThunkletCallbackGlueLS,
1435 THUNKLET_TYPE_LS );
1436 return (FARPROC)thunk;
1439 /***********************************************************************
1440 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1442 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1444 THUNKLET *thunk = (THUNKLET *)target;
1445 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1446 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1447 return (SEGPTR)thunk->target;
1449 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1450 (DWORD)ThunkletCallbackGlueSL,
1451 THUNKLET_TYPE_SL );
1452 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1456 /***********************************************************************
1457 * FreeThunklet16 (KERNEL.611)
1459 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1461 return FALSE;
1464 /***********************************************************************
1465 * Callback Client API
1468 #define N_CBC_FIXED 20
1469 #define N_CBC_VARIABLE 10
1470 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1472 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1473 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1475 /***********************************************************************
1476 * RegisterCBClient (KERNEL.619)
1478 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1479 SEGPTR *relay16, FARPROC *relay32 )
1481 /* Search for free Callback ID */
1482 if ( wCBCId == -1 )
1483 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1484 if ( !CBClientRelay16[ wCBCId ] )
1485 break;
1487 /* Register Callback ID */
1488 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1490 CBClientRelay16[ wCBCId ] = relay16;
1491 CBClientRelay32[ wCBCId ] = relay32;
1493 else
1494 wCBCId = 0;
1496 return wCBCId;
1499 /***********************************************************************
1500 * UnRegisterCBClient (KERNEL.622)
1502 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1503 SEGPTR *relay16, FARPROC *relay32 )
1505 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1506 && CBClientRelay16[ wCBCId ] == relay16
1507 && CBClientRelay32[ wCBCId ] == relay32 )
1509 CBClientRelay16[ wCBCId ] = 0;
1510 CBClientRelay32[ wCBCId ] = 0;
1512 else
1513 wCBCId = 0;
1515 return wCBCId;
1519 /***********************************************************************
1520 * InitCBClient (KERNEL.623)
1522 void WINAPI InitCBClient16( FARPROC glueLS )
1524 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1525 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1527 SetThunkletCallbackGlue16( glueLS, glueSL );
1530 /***********************************************************************
1531 * CBClientGlueSL (KERNEL.604)
1533 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1535 /* Create stack frame */
1536 SEGPTR stackSeg = STACK16_PUSH( NtCurrentTeb(), 12 );
1537 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1538 SEGPTR glue;
1540 stackLin[3] = BP_reg( context );
1541 stackLin[2] = SI_reg( context );
1542 stackLin[1] = DI_reg( context );
1543 stackLin[0] = DS_reg( context );
1545 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1546 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1547 GS_reg( context ) = 0;
1549 /* Jump to 16-bit relay code */
1550 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1551 CS_reg ( context ) = SELECTOROF( glue );
1552 EIP_reg( context ) = OFFSETOF ( glue );
1555 /***********************************************************************
1556 * CBClientThunkSL (KERNEL.620)
1558 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1560 /* Call 32-bit relay code */
1561 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1563 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1564 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1566 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1569 /***********************************************************************
1570 * CBClientThunkSLEx (KERNEL.621)
1572 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1574 /* Call 32-bit relay code */
1575 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args,
1576 DWORD *esi, INT *nArgs );
1578 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1579 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1580 INT nArgs;
1581 LPWORD stackLin;
1583 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1585 /* Restore registers saved by CBClientGlueSL */
1586 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1587 BP_reg( context ) = stackLin[3];
1588 SI_reg( context ) = stackLin[2];
1589 DI_reg( context ) = stackLin[1];
1590 DS_reg( context ) = stackLin[0];
1591 SP_reg( context ) += 16+nArgs;
1593 /* Return to caller of CBClient thunklet */
1594 CS_reg ( context ) = stackLin[9];
1595 EIP_reg( context ) = stackLin[8];