Implemented SHCreateShellFolderViewEx.
[wine/hacks.git] / if1632 / thunk.c
blobc7eadc21e5ecaf78495a02fc6852a03e073ac28c
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 */
179 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
181 CALLOUT_TABLE Callout = { 0 };
184 /***********************************************************************
185 * THUNK_Init
187 BOOL THUNK_Init(void)
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 CONTEXT86 context;
255 LRESULT ret;
256 WORD *args;
257 WND *wndPtr = WIN_FindWndPtr( hwnd );
258 DWORD offset = 0;
259 TEB *teb = NtCurrentTeb();
260 int iWndsLocks;
262 /* Window procedures want ax = hInstance, ds = es = ss */
264 memset(&context, '\0', sizeof(context));
265 DS_reg(&context) = SELECTOROF(teb->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(teb->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( teb, offset );
297 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
301 iWndsLocks = WIN_SuspendWndsLock();
303 args = (WORD *)THREAD_STACK16(teb) - 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 = CallTo16RegisterShort( &context, 5 * sizeof(WORD) );
311 if (offset) STACK16_POP( teb, 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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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, THUNK_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)THUNK_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)THUNK_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)THUNK_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)THUNK_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 THUNK_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 = THUNK_CallTo16_long_(proc);break;
625 case 1: ret = THUNK_CallTo16_long_l(proc,args[0]);break;
626 case 2: ret = THUNK_CallTo16_long_ll(proc,args[0],args[1]);break;
627 case 3: ret = THUNK_CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
628 case 4: ret = THUNK_CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
629 break;
630 case 5: ret = THUNK_CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
631 args[4]
633 break;
634 case 6: ret = THUNK_CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
635 args[4],args[5]
637 break;
638 case 7: ret = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 = THUNK_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 CONTEXT86 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 CallTo16RegisterShort( &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( CONTEXT86 *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 CONTEXT86 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 CallTo16RegisterShort( &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( CONTEXT86 *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 CONTEXT86 context;
821 memset( &context, '\0', sizeof(context) );
823 CS_reg( &context ) = SELECTOROF( proc );
824 IP_reg( &context ) = OFFSETOF( proc );
825 BP_reg( &context ) = OFFSETOF( NtCurrentTeb()->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 CallTo16RegisterShort( &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_GetCalloutThunk
854 * Retrieve API entry point with given name from given module.
855 * If module is builtin, return the 32-bit entry point, otherwise
856 * create a 32->16 thunk to the 16-bit entry point, using the
857 * given relay code.
860 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
862 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
863 if ( !proc ) return 0;
865 if ( pModule->flags & NE_FFLAGS_BUILTIN )
866 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
867 else
868 return (FARPROC)THUNK_Alloc( proc, relay );
871 /***********************************************************************
872 * THUNK_InitCallout
874 void THUNK_InitCallout(void)
876 HMODULE hModule;
877 NE_MODULE *pModule;
879 hModule = GetModuleHandleA( "USER32" );
880 if ( hModule )
882 #define GETADDR( var, name ) \
883 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
885 GETADDR( PeekMessageA, "PeekMessageA" );
886 GETADDR( PeekMessageW, "PeekMessageW" );
887 GETADDR( GetMessageA, "GetMessageA" );
888 GETADDR( GetMessageW, "GetMessageW" );
889 GETADDR( SendMessageA, "SendMessageA" );
890 GETADDR( SendMessageW, "SendMessageW" );
891 GETADDR( PostMessageA, "PostMessageA" );
892 GETADDR( PostMessageW, "PostMessageW" );
893 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
894 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
895 GETADDR( TranslateMessage, "TranslateMessage" );
896 GETADDR( DispatchMessageW, "DispatchMessageW" );
897 GETADDR( DispatchMessageA, "DispatchMessageA" );
898 GETADDR( RedrawWindow, "RedrawWindow" );
899 GETADDR( UserSignalProc, "UserSignalProc" );
901 #undef GETADDR
904 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
905 if ( pModule )
907 #define GETADDR( var, name, thk ) \
908 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
909 (RELAY)THUNK_CallTo16_##thk )
911 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
912 GETADDR( GetMessage16, "GetMessage", word_lwww );
913 GETADDR( SendMessage16, "SendMessage", long_wwwl );
914 GETADDR( PostMessage16, "PostMessage", word_wwwl );
915 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
916 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
917 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
918 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
919 GETADDR( InitApp16, "InitApp", word_w );
920 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
921 GETADDR( UserYield16, "UserYield", word_ );
922 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
924 #undef GETADDR
928 /***********************************************************************
929 * 16->32 Flat Thunk routines:
932 /***********************************************************************
933 * ThunkConnect16 (KERNEL.651)
934 * Connects a 32bit and a 16bit thunkbuffer.
936 UINT WINAPI ThunkConnect16(
937 LPSTR module16, /* [in] name of win16 dll */
938 LPSTR module32, /* [in] name of win32 dll */
939 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
940 DWORD dwReason, /* [in] initialisation argument */
941 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
942 LPSTR thunkfun32, /* [in] win32 thunkfunction */
943 WORD cs /* [in] CS of win16 dll */
945 BOOL directionSL;
947 if (!lstrncmpA(TD->magic, "SL01", 4))
949 directionSL = TRUE;
951 TRACE_(thunk)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
952 module16, (DWORD)TD, module32, thunkfun32, dwReason);
954 else if (!lstrncmpA(TD->magic, "LS01", 4))
956 directionSL = FALSE;
958 TRACE_(thunk)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
959 module16, (DWORD)TD, module32, thunkfun32, dwReason);
961 else
963 ERR_(thunk)("Invalid magic %c%c%c%c\n",
964 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
965 return 0;
968 switch (dwReason)
970 case DLL_PROCESS_ATTACH:
971 if (directionSL)
973 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
974 struct ThunkDataSL *SL = SL16->fpData;
976 if (SL == NULL)
978 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
980 SL->common = SL16->common;
981 SL->flags1 = SL16->flags1;
982 SL->flags2 = SL16->flags2;
984 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
985 SL->targetDB = NULL;
987 lstrcpynA(SL->pszDll16, module16, 255);
988 lstrcpynA(SL->pszDll32, module32, 255);
990 /* We should create a SEGPTR to the ThunkDataSL,
991 but since the contents are not in the original format,
992 any access to this by 16-bit code would crash anyway. */
993 SL16->spData = 0;
994 SL16->fpData = SL;
998 if (SL->flags2 & 0x80000000)
1000 TRACE_(thunk)("Preloading 32-bit library\n");
1001 LoadLibraryA(module32);
1004 else
1006 /* nothing to do */
1008 break;
1010 case DLL_PROCESS_DETACH:
1011 /* FIXME: cleanup */
1012 break;
1015 return 1;
1019 /***********************************************************************
1020 * C16ThkSL (KERNEL.630)
1023 void WINAPI C16ThkSL(CONTEXT86 *context)
1025 extern void CallFrom16Thunk(void);
1026 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1027 WORD cs, ds;
1028 GET_CS(cs);
1029 GET_DS(ds);
1031 /* We produce the following code:
1033 * mov ax, __FLATDS
1034 * mov es, ax
1035 * movzx ecx, cx
1036 * mov edx, es:[ecx + $EDX]
1037 * push bp
1038 * push edx
1039 * call __FLATCS:CallFrom16_t_long_
1042 *x++ = 0xB8; *((WORD *)x)++ = ds;
1043 *x++ = 0x8E; *x++ = 0xC0;
1044 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1045 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1046 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
1048 *x++ = 0x55;
1049 *x++ = 0x66; *x++ = 0x52;
1050 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1051 *((WORD *)x)++ = cs;
1053 /* Jump to the stub code just created */
1054 IP_reg(context) = LOWORD(EAX_reg(context));
1055 CS_reg(context) = HIWORD(EAX_reg(context));
1057 /* Since C16ThkSL got called by a jmp, we need to leave the
1058 orginal return address on the stack */
1059 SP_reg(context) -= 4;
1062 /***********************************************************************
1063 * C16ThkSL01 (KERNEL.631)
1066 void WINAPI C16ThkSL01(CONTEXT86 *context)
1068 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1070 if (stub)
1072 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
1073 struct ThunkDataSL *td = SL16->fpData;
1075 extern void CallFrom16Thunk(void);
1076 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1077 WORD cs;
1078 GET_CS(cs);
1080 if (!td)
1082 ERR_(thunk)("ThunkConnect16 was not called!\n");
1083 return;
1086 TRACE_(thunk)("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1089 /* We produce the following code:
1091 * xor eax, eax
1092 * mov edx, $td
1093 * call C16ThkSL01
1094 * push bp
1095 * push edx
1096 * call __FLATCS:CallFrom16_t_long_
1099 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1100 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1101 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1103 *x++ = 0x55;
1104 *x++ = 0x66; *x++ = 0x52;
1105 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1106 *((WORD *)x)++ = cs;
1108 /* Jump to the stub code just created */
1109 IP_reg(context) = LOWORD(EAX_reg(context));
1110 CS_reg(context) = HIWORD(EAX_reg(context));
1112 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1113 orginal return address on the stack */
1114 SP_reg(context) -= 4;
1116 else
1118 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1119 DWORD targetNr = CX_reg(context) / 4;
1120 struct SLTargetDB *tdb;
1122 TRACE_(thunk)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1123 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1125 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1126 if (tdb->process == PROCESS_Current())
1127 break;
1129 if (!tdb)
1131 TRACE_(thunk)("Loading 32-bit library %s\n", td->pszDll32);
1132 LoadLibraryA(td->pszDll32);
1134 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1135 if (tdb->process == PROCESS_Current())
1136 break;
1139 if (tdb)
1141 EDX_reg(context) = tdb->targetTable[targetNr];
1143 TRACE_(thunk)("Call target is %08lx\n", EDX_reg(context));
1145 else
1147 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1148 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1149 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1150 IP_reg(context) = stack[2];
1151 CS_reg(context) = stack[3];
1152 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1154 ERR_(thunk)("Process %08lx did not ThunkConnect32 %s to %s\n",
1155 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1160 DWORD WINAPI
1161 WOW16Call(WORD x,WORD y,WORD z) {
1162 int i;
1163 DWORD calladdr;
1164 FIXME_(thunk)("(0x%04x,0x%04x,%d),calling (",x,y,z);
1166 for (i=0;i<x/2;i++) {
1167 WORD a = STACK16_POP(NtCurrentTeb(),2);
1168 DPRINTF("%04x ",a);
1170 calladdr = STACK16_POP(NtCurrentTeb(),4);
1171 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1172 return 0;
1176 /***********************************************************************
1177 * 16<->32 Thunklet/Callback API:
1180 #include "pshpack1.h"
1181 typedef struct _THUNKLET
1183 BYTE prefix_target;
1184 BYTE pushl_target;
1185 DWORD target;
1187 BYTE prefix_relay;
1188 BYTE pushl_relay;
1189 DWORD relay;
1191 BYTE jmp_glue;
1192 DWORD glue;
1194 BYTE type;
1195 HINSTANCE16 owner;
1196 struct _THUNKLET *next;
1197 } THUNKLET;
1198 #include "poppack.h"
1200 #define THUNKLET_TYPE_LS 1
1201 #define THUNKLET_TYPE_SL 2
1203 static HANDLE ThunkletHeap = 0;
1204 static THUNKLET *ThunkletAnchor = NULL;
1206 static FARPROC ThunkletSysthunkGlueLS = 0;
1207 static SEGPTR ThunkletSysthunkGlueSL = 0;
1209 static FARPROC ThunkletCallbackGlueLS = 0;
1210 static SEGPTR ThunkletCallbackGlueSL = 0;
1212 /***********************************************************************
1213 * THUNK_ThunkletInit
1215 static BOOL THUNK_ThunkletInit( void )
1217 LPBYTE thunk;
1219 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1220 if (!ThunkletHeap) return FALSE;
1222 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1223 if (!thunk) return FALSE;
1225 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1226 *thunk++ = 0x58; /* popl eax */
1227 *thunk++ = 0xC3; /* ret */
1229 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1230 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1231 *thunk++ = 0xCB; /* lret */
1233 return TRUE;
1236 /***********************************************************************
1237 * SetThunkletCallbackGlue (KERNEL.560)
1239 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1241 ThunkletCallbackGlueLS = glueLS;
1242 ThunkletCallbackGlueSL = glueSL;
1246 /***********************************************************************
1247 * THUNK_FindThunklet
1249 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1250 DWORD glue, BYTE type )
1252 THUNKLET *thunk;
1254 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1255 if ( thunk->type == type
1256 && thunk->target == target
1257 && thunk->relay == relay
1258 && ( type == THUNKLET_TYPE_LS ?
1259 ( thunk->glue == glue - (DWORD)&thunk->type )
1260 : ( thunk->glue == glue ) ) )
1261 return thunk;
1263 return NULL;
1266 /***********************************************************************
1267 * THUNK_AllocLSThunklet
1269 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1270 FARPROC glue, HTASK16 owner )
1272 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1273 THUNKLET_TYPE_LS );
1274 if (!thunk)
1276 TDB *pTask = (TDB*)GlobalLock16( owner );
1278 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1279 return 0;
1281 thunk->prefix_target = thunk->prefix_relay = 0x90;
1282 thunk->pushl_target = thunk->pushl_relay = 0x68;
1283 thunk->jmp_glue = 0xE9;
1285 thunk->target = (DWORD)target;
1286 thunk->relay = (DWORD)relay;
1287 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1289 thunk->type = THUNKLET_TYPE_LS;
1290 thunk->owner = pTask? pTask->hInstance : 0;
1292 thunk->next = ThunkletAnchor;
1293 ThunkletAnchor = thunk;
1296 return (FARPROC)thunk;
1299 /***********************************************************************
1300 * THUNK_AllocSLThunklet
1302 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1303 SEGPTR glue, HTASK16 owner )
1305 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1306 THUNKLET_TYPE_SL );
1307 if (!thunk)
1309 TDB *pTask = (TDB*)GlobalLock16( owner );
1311 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1312 return 0;
1314 thunk->prefix_target = thunk->prefix_relay = 0x66;
1315 thunk->pushl_target = thunk->pushl_relay = 0x68;
1316 thunk->jmp_glue = 0xEA;
1318 thunk->target = (DWORD)target;
1319 thunk->relay = (DWORD)relay;
1320 thunk->glue = (DWORD)glue;
1322 thunk->type = THUNKLET_TYPE_SL;
1323 thunk->owner = pTask? pTask->hInstance : 0;
1325 thunk->next = ThunkletAnchor;
1326 ThunkletAnchor = thunk;
1329 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1332 /**********************************************************************
1333 * IsLSThunklet
1335 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1337 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1338 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1339 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1342 /**********************************************************************
1343 * IsSLThunklet (KERNEL.612)
1345 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1347 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1348 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1349 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1354 /***********************************************************************
1355 * AllocLSThunkletSysthunk (KERNEL.607)
1357 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1358 FARPROC relay, DWORD dummy )
1360 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1361 ThunkletSysthunkGlueLS, GetCurrentTask() );
1364 /***********************************************************************
1365 * AllocSLThunkletSysthunk (KERNEL.608)
1367 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1368 SEGPTR relay, DWORD dummy )
1370 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1371 ThunkletSysthunkGlueSL, GetCurrentTask() );
1375 /***********************************************************************
1376 * AllocLSThunkletCallbackEx (KERNEL.567)
1378 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1379 DWORD relay, HTASK16 task )
1381 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1382 if ( !thunk ) return NULL;
1384 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1385 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1386 return (FARPROC)thunk->target;
1388 return THUNK_AllocLSThunklet( target, relay,
1389 ThunkletCallbackGlueLS, task );
1392 /***********************************************************************
1393 * AllocSLThunkletCallbackEx (KERNEL.568)
1395 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1396 DWORD relay, HTASK16 task )
1398 THUNKLET *thunk = (THUNKLET *)target;
1399 if ( !thunk ) return 0;
1401 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1402 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1403 return (SEGPTR)thunk->target;
1405 return THUNK_AllocSLThunklet( target, relay,
1406 ThunkletCallbackGlueSL, task );
1409 /***********************************************************************
1410 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1412 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1414 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1417 /***********************************************************************
1418 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1420 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1422 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1425 /***********************************************************************
1426 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1428 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1430 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1431 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1432 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1433 return (FARPROC)thunk->target;
1435 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1436 (DWORD)ThunkletCallbackGlueLS,
1437 THUNKLET_TYPE_LS );
1438 return (FARPROC)thunk;
1441 /***********************************************************************
1442 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1444 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1446 THUNKLET *thunk = (THUNKLET *)target;
1447 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1448 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1449 return (SEGPTR)thunk->target;
1451 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1452 (DWORD)ThunkletCallbackGlueSL,
1453 THUNKLET_TYPE_SL );
1454 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1458 /***********************************************************************
1459 * FreeThunklet16 (KERNEL.611)
1461 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1463 return FALSE;
1466 /***********************************************************************
1467 * Callback Client API
1470 #define N_CBC_FIXED 20
1471 #define N_CBC_VARIABLE 10
1472 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1474 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1475 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1477 /***********************************************************************
1478 * RegisterCBClient (KERNEL.619)
1480 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1481 SEGPTR *relay16, FARPROC *relay32 )
1483 /* Search for free Callback ID */
1484 if ( wCBCId == -1 )
1485 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1486 if ( !CBClientRelay16[ wCBCId ] )
1487 break;
1489 /* Register Callback ID */
1490 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1492 CBClientRelay16[ wCBCId ] = relay16;
1493 CBClientRelay32[ wCBCId ] = relay32;
1495 else
1496 wCBCId = 0;
1498 return wCBCId;
1501 /***********************************************************************
1502 * UnRegisterCBClient (KERNEL.622)
1504 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1505 SEGPTR *relay16, FARPROC *relay32 )
1507 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1508 && CBClientRelay16[ wCBCId ] == relay16
1509 && CBClientRelay32[ wCBCId ] == relay32 )
1511 CBClientRelay16[ wCBCId ] = 0;
1512 CBClientRelay32[ wCBCId ] = 0;
1514 else
1515 wCBCId = 0;
1517 return wCBCId;
1521 /***********************************************************************
1522 * InitCBClient (KERNEL.623)
1524 void WINAPI InitCBClient16( FARPROC glueLS )
1526 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1527 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1529 SetThunkletCallbackGlue16( glueLS, glueSL );
1532 /***********************************************************************
1533 * CBClientGlueSL (KERNEL.604)
1535 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1537 /* Create stack frame */
1538 SEGPTR stackSeg = STACK16_PUSH( NtCurrentTeb(), 12 );
1539 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1540 SEGPTR glue;
1542 stackLin[3] = BP_reg( context );
1543 stackLin[2] = SI_reg( context );
1544 stackLin[1] = DI_reg( context );
1545 stackLin[0] = DS_reg( context );
1547 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1548 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1549 GS_reg( context ) = 0;
1551 /* Jump to 16-bit relay code */
1552 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1553 CS_reg ( context ) = SELECTOROF( glue );
1554 EIP_reg( context ) = OFFSETOF ( glue );
1557 /***********************************************************************
1558 * CBClientThunkSL (KERNEL.620)
1560 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1562 /* Call 32-bit relay code */
1563 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1565 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1566 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1568 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1571 /***********************************************************************
1572 * CBClientThunkSLEx (KERNEL.621)
1574 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1576 /* Call 32-bit relay code */
1577 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args,
1578 DWORD *esi, INT *nArgs );
1580 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1581 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1582 INT nArgs;
1583 LPWORD stackLin;
1585 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1587 /* Restore registers saved by CBClientGlueSL */
1588 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1589 BP_reg( context ) = stackLin[3];
1590 SI_reg( context ) = stackLin[2];
1591 DI_reg( context ) = stackLin[1];
1592 DS_reg( context ) = stackLin[0];
1593 SP_reg( context ) += 16+nArgs;
1595 /* Return to caller of CBClient thunklet */
1596 CS_reg ( context ) = stackLin[9];
1597 EIP_reg( context ) = stackLin[8];