Changed the "this_is_a_syntax_error" error message to a more specific
[wine/multimedia.git] / if1632 / thunk.c
blob5501e7d12d6edc4563db466ab09db81d533fa0d9
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 DEFAULT_DEBUG_CHANNEL(thunk)
28 /* List of the 16-bit callback functions. This list is used */
29 /* by the build program to generate the file if1632/callto16.S */
31 /* ### start build ### */
32 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
33 extern LONG CALLBACK THUNK_CallTo16_long_ (FARPROC16);
34 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
35 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
36 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
37 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
38 extern WORD CALLBACK THUNK_CallTo16_word_wl (FARPROC16,WORD,LONG);
39 extern WORD CALLBACK THUNK_CallTo16_word_ll (FARPROC16,LONG,LONG);
40 extern LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
41 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
42 extern WORD CALLBACK THUNK_CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
43 extern WORD CALLBACK THUNK_CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
44 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
45 extern WORD CALLBACK THUNK_CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
46 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
47 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
48 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
49 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
50 extern WORD CALLBACK THUNK_CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
51 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
52 extern LONG CALLBACK THUNK_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
53 extern WORD CALLBACK THUNK_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
54 /* ### stop build ### */
58 #include "pshpack1.h"
60 typedef struct tagTHUNK
62 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
63 BYTE pushl_func; /* 0x68 pushl $proc */
64 FARPROC16 proc WINE_PACKED;
65 BYTE pushl_eax; /* 0x50 pushl %eax */
66 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
67 RELAY relay WINE_PACKED;
68 struct tagTHUNK *next WINE_PACKED;
69 DWORD magic;
70 } THUNK;
72 #define CALLTO16_THUNK_MAGIC 0x54484e4b /* "THNK" */
74 #include "poppack.h"
76 #define DECL_THUNK(aname,aproc,arelay) \
77 THUNK aname; \
78 aname.popl_eax = 0x58; \
79 aname.pushl_func = 0x68; \
80 aname.proc = (FARPROC) (aproc); \
81 aname.pushl_eax = 0x50; \
82 aname.jmp = 0xe9; \
83 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
84 aname.next = NULL; \
85 aname.magic = CALLTO16_THUNK_MAGIC;
87 static THUNK *firstThunk = NULL;
89 static BOOL THUNK_ThunkletInit( void );
91 /* Callbacks function table for the emulator */
92 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
94 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
95 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
96 (void *)THUNK_CallTo16_long_lwwll, /* CallDriverProc */
97 (void *)THUNK_CallTo16_word_wwlll, /* CallDriverCallback */
98 (void *)THUNK_CallTo16_word_wwlll, /* CallTimeFuncProc */
99 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
100 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
101 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
102 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
103 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
104 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
105 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
106 (void *)THUNK_CallTo16_long_l /* CallASPIPostProc */
109 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
111 CALLOUT_TABLE Callout = { 0 };
114 /***********************************************************************
115 * THUNK_Init
117 BOOL THUNK_Init(void)
119 /* Initialize Thunklets */
120 return THUNK_ThunkletInit();
123 /***********************************************************************
124 * THUNK_Alloc
126 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
128 HANDLE16 hSeg;
129 NE_MODULE *pModule;
130 THUNK *thunk;
132 /* NULL maps to NULL */
133 if ( !func ) return NULL;
136 * If we got an 16-bit built-in API entry point, retrieve the Wine
137 * 32-bit handler for that API routine.
139 * NOTE: For efficiency reasons, we only check whether the selector
140 * of 'func' points to the code segment of a built-in module.
141 * It might be theoretically possible that the offset is such
142 * that 'func' does not point, in fact, to an API entry point.
143 * In this case, however, the pointer is corrupt anyway.
145 hSeg = GlobalHandle16( SELECTOROF( func ) );
146 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
148 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
149 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
151 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( func ))->target;
153 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
154 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
155 return proc;
158 /* Otherwise, we need to alloc a thunk */
159 thunk = HeapAlloc( SystemHeap, 0, sizeof(*thunk) );
160 if (thunk)
162 thunk->popl_eax = 0x58;
163 thunk->pushl_func = 0x68;
164 thunk->proc = func;
165 thunk->pushl_eax = 0x50;
166 thunk->jmp = 0xe9;
167 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
168 thunk->magic = CALLTO16_THUNK_MAGIC;
169 thunk->next = firstThunk;
170 firstThunk = thunk;
173 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
174 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
175 return (FARPROC)thunk;
178 /***********************************************************************
179 * THUNK_Free
181 void THUNK_Free( FARPROC thunk )
183 THUNK *t = (THUNK*)thunk;
184 if ( !t || IsBadReadPtr( t, sizeof(*t) )
185 || t->magic != CALLTO16_THUNK_MAGIC )
186 return;
188 if (HEAP_IsInsideHeap( SystemHeap, 0, t ))
190 THUNK **prev = &firstThunk;
191 while (*prev && (*prev != t)) prev = &(*prev)->next;
192 if (*prev)
194 *prev = t->next;
195 HeapFree( SystemHeap, 0, t );
196 return;
199 ERR("invalid thunk addr %p\n", thunk );
200 return;
204 /***********************************************************************
205 * THUNK_EnumObjects16 (GDI.71)
207 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
208 GOBJENUMPROC16 func, LPARAM lParam )
210 DECL_THUNK( thunk, func, THUNK_CallTo16_word_ll );
211 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
215 /*************************************************************************
216 * THUNK_EnumFonts16 (GDI.70)
218 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
219 FONTENUMPROC16 func, LPARAM lParam )
221 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
222 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
225 /******************************************************************
226 * THUNK_EnumMetaFile16 (GDI.175)
228 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
229 MFENUMPROC16 func, LPARAM lParam )
231 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wllwl );
232 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
236 /*************************************************************************
237 * THUNK_EnumFontFamilies16 (GDI.330)
239 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
240 FONTENUMPROC16 func, LPARAM lParam )
242 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
243 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
247 /*************************************************************************
248 * THUNK_EnumFontFamiliesEx16 (GDI.613)
250 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
251 FONTENUMPROCEX16 func, LPARAM lParam,
252 DWORD reserved )
254 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
255 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
256 lParam, reserved );
260 /**********************************************************************
261 * THUNK_LineDDA16 (GDI.100)
263 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
264 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
266 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wwl );
267 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
271 /*******************************************************************
272 * THUNK_EnumWindows16 (USER.54)
274 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
276 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
277 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
281 /**********************************************************************
282 * THUNK_EnumChildWindows16 (USER.55)
284 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
285 LPARAM lParam )
287 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
288 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
292 /**********************************************************************
293 * THUNK_EnumTaskWindows16 (USER.225)
295 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
296 LPARAM lParam )
298 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
299 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
303 /***********************************************************************
304 * THUNK_EnumProps16 (USER.27)
306 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
308 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
309 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
313 /***********************************************************************
314 * THUNK_GrayString16 (USER.185)
316 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
317 GRAYSTRINGPROC16 func, LPARAM lParam,
318 INT16 cch, INT16 x, INT16 y,
319 INT16 cx, INT16 cy )
321 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
322 if (!func)
323 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
324 else
325 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
326 x, y, cx, cy );
330 /***********************************************************************
331 * THUNK_GetCalloutThunk
333 * Retrieve API entry point with given name from given module.
334 * If module is builtin, return the 32-bit entry point, otherwise
335 * create a 32->16 thunk to the 16-bit entry point, using the
336 * given relay code.
339 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
341 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
342 if ( !proc ) return 0;
344 if ( pModule->flags & NE_FFLAGS_BUILTIN )
345 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
346 else
347 return (FARPROC)THUNK_Alloc( proc, relay );
350 /***********************************************************************
351 * THUNK_InitCallout
353 void THUNK_InitCallout(void)
355 HMODULE hModule;
356 NE_MODULE *pModule;
358 hModule = GetModuleHandleA( "USER32" );
359 if ( hModule )
361 #define GETADDR( var, name ) \
362 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
364 GETADDR( PeekMessageA, "PeekMessageA" );
365 GETADDR( PeekMessageW, "PeekMessageW" );
366 GETADDR( GetMessageA, "GetMessageA" );
367 GETADDR( GetMessageW, "GetMessageW" );
368 GETADDR( SendMessageA, "SendMessageA" );
369 GETADDR( SendMessageW, "SendMessageW" );
370 GETADDR( PostMessageA, "PostMessageA" );
371 GETADDR( PostMessageW, "PostMessageW" );
372 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
373 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
374 GETADDR( TranslateMessage, "TranslateMessage" );
375 GETADDR( DispatchMessageW, "DispatchMessageW" );
376 GETADDR( DispatchMessageA, "DispatchMessageA" );
377 GETADDR( RedrawWindow, "RedrawWindow" );
379 #undef GETADDR
382 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
383 if ( pModule )
385 #define GETADDR( var, name, thk ) \
386 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
387 (RELAY)THUNK_CallTo16_##thk )
389 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
390 GETADDR( GetMessage16, "GetMessage", word_lwww );
391 GETADDR( SendMessage16, "SendMessage", long_wwwl );
392 GETADDR( PostMessage16, "PostMessage", word_wwwl );
393 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
394 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
395 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
396 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
397 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
398 GETADDR( InitApp16, "InitApp", word_w );
399 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
400 GETADDR( UserYield16, "UserYield", word_ );
401 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
402 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
404 #undef GETADDR
408 /***********************************************************************
409 * 16->32 Flat Thunk routines:
412 /***********************************************************************
413 * ThunkConnect16 (KERNEL.651)
414 * Connects a 32bit and a 16bit thunkbuffer.
416 UINT WINAPI ThunkConnect16(
417 LPSTR module16, /* [in] name of win16 dll */
418 LPSTR module32, /* [in] name of win32 dll */
419 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
420 DWORD dwReason, /* [in] initialisation argument */
421 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
422 LPSTR thunkfun32, /* [in] win32 thunkfunction */
423 WORD cs /* [in] CS of win16 dll */
425 BOOL directionSL;
427 if (!strncmp(TD->magic, "SL01", 4))
429 directionSL = TRUE;
431 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
432 module16, (DWORD)TD, module32, thunkfun32, dwReason);
434 else if (!strncmp(TD->magic, "LS01", 4))
436 directionSL = FALSE;
438 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
439 module16, (DWORD)TD, module32, thunkfun32, dwReason);
441 else
443 ERR("Invalid magic %c%c%c%c\n",
444 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
445 return 0;
448 switch (dwReason)
450 case DLL_PROCESS_ATTACH:
451 if (directionSL)
453 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
454 struct ThunkDataSL *SL = SL16->fpData;
456 if (SL == NULL)
458 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
460 SL->common = SL16->common;
461 SL->flags1 = SL16->flags1;
462 SL->flags2 = SL16->flags2;
464 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
465 SL->targetDB = NULL;
467 lstrcpynA(SL->pszDll16, module16, 255);
468 lstrcpynA(SL->pszDll32, module32, 255);
470 /* We should create a SEGPTR to the ThunkDataSL,
471 but since the contents are not in the original format,
472 any access to this by 16-bit code would crash anyway. */
473 SL16->spData = 0;
474 SL16->fpData = SL;
478 if (SL->flags2 & 0x80000000)
480 TRACE("Preloading 32-bit library\n");
481 LoadLibraryA(module32);
484 else
486 /* nothing to do */
488 break;
490 case DLL_PROCESS_DETACH:
491 /* FIXME: cleanup */
492 break;
495 return 1;
499 /***********************************************************************
500 * C16ThkSL (KERNEL.630)
503 void WINAPI C16ThkSL(CONTEXT86 *context)
505 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
506 WORD cs, ds;
507 GET_CS(cs);
508 GET_DS(ds);
510 /* We produce the following code:
512 * mov ax, __FLATDS
513 * mov es, ax
514 * movzx ecx, cx
515 * mov edx, es:[ecx + $EDX]
516 * push bp
517 * push edx
518 * push dx
519 * push edx
520 * call __FLATCS:CallFrom16Thunk
523 *x++ = 0xB8; *((WORD *)x)++ = ds;
524 *x++ = 0x8E; *x++ = 0xC0;
525 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
526 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
527 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
529 *x++ = 0x55;
530 *x++ = 0x66; *x++ = 0x52;
531 *x++ = 0x52;
532 *x++ = 0x66; *x++ = 0x52;
533 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
534 *((WORD *)x)++ = cs;
536 /* Jump to the stub code just created */
537 EIP_reg(context) = LOWORD(EAX_reg(context));
538 CS_reg(context) = HIWORD(EAX_reg(context));
540 /* Since C16ThkSL got called by a jmp, we need to leave the
541 original return address on the stack */
542 ESP_reg(context) -= 4;
545 /***********************************************************************
546 * C16ThkSL01 (KERNEL.631)
549 void WINAPI C16ThkSL01(CONTEXT86 *context)
551 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
553 if (stub)
555 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
556 struct ThunkDataSL *td = SL16->fpData;
558 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
559 WORD cs;
560 GET_CS(cs);
562 if (!td)
564 ERR("ThunkConnect16 was not called!\n");
565 return;
568 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
571 /* We produce the following code:
573 * xor eax, eax
574 * mov edx, $td
575 * call C16ThkSL01
576 * push bp
577 * push edx
578 * push dx
579 * push edx
580 * call __FLATCS:CallFrom16Thunk
583 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
584 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
585 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
587 *x++ = 0x55;
588 *x++ = 0x66; *x++ = 0x52;
589 *x++ = 0x52;
590 *x++ = 0x66; *x++ = 0x52;
591 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
592 *((WORD *)x)++ = cs;
594 /* Jump to the stub code just created */
595 EIP_reg(context) = LOWORD(EAX_reg(context));
596 CS_reg(context) = HIWORD(EAX_reg(context));
598 /* Since C16ThkSL01 got called by a jmp, we need to leave the
599 orginal return address on the stack */
600 ESP_reg(context) -= 4;
602 else
604 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
605 DWORD targetNr = CX_reg(context) / 4;
606 struct SLTargetDB *tdb;
608 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
609 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
611 for (tdb = td->targetDB; tdb; tdb = tdb->next)
612 if (tdb->process == PROCESS_Current())
613 break;
615 if (!tdb)
617 TRACE("Loading 32-bit library %s\n", td->pszDll32);
618 LoadLibraryA(td->pszDll32);
620 for (tdb = td->targetDB; tdb; tdb = tdb->next)
621 if (tdb->process == PROCESS_Current())
622 break;
625 if (tdb)
627 EDX_reg(context) = tdb->targetTable[targetNr];
629 TRACE("Call target is %08lx\n", EDX_reg(context));
631 else
633 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context)));
634 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
635 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
636 EIP_reg(context) = stack[2];
637 CS_reg(context) = stack[3];
638 ESP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
640 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
641 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
648 /***********************************************************************
649 * 16<->32 Thunklet/Callback API:
652 #include "pshpack1.h"
653 typedef struct _THUNKLET
655 BYTE prefix_target;
656 BYTE pushl_target;
657 DWORD target;
659 BYTE prefix_relay;
660 BYTE pushl_relay;
661 DWORD relay;
663 BYTE jmp_glue;
664 DWORD glue;
666 BYTE type;
667 HINSTANCE16 owner;
668 struct _THUNKLET *next;
669 } THUNKLET;
670 #include "poppack.h"
672 #define THUNKLET_TYPE_LS 1
673 #define THUNKLET_TYPE_SL 2
675 static HANDLE ThunkletHeap = 0;
676 static THUNKLET *ThunkletAnchor = NULL;
678 static FARPROC ThunkletSysthunkGlueLS = 0;
679 static SEGPTR ThunkletSysthunkGlueSL = 0;
681 static FARPROC ThunkletCallbackGlueLS = 0;
682 static SEGPTR ThunkletCallbackGlueSL = 0;
684 /***********************************************************************
685 * THUNK_ThunkletInit
687 static BOOL THUNK_ThunkletInit( void )
689 LPBYTE thunk;
691 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
692 if (!ThunkletHeap) return FALSE;
694 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
695 if (!thunk) return FALSE;
697 ThunkletSysthunkGlueLS = (FARPROC)thunk;
698 *thunk++ = 0x58; /* popl eax */
699 *thunk++ = 0xC3; /* ret */
701 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
702 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
703 *thunk++ = 0xCB; /* lret */
705 return TRUE;
708 /***********************************************************************
709 * SetThunkletCallbackGlue (KERNEL.560)
711 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
713 ThunkletCallbackGlueLS = glueLS;
714 ThunkletCallbackGlueSL = glueSL;
718 /***********************************************************************
719 * THUNK_FindThunklet
721 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
722 DWORD glue, BYTE type )
724 THUNKLET *thunk;
726 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
727 if ( thunk->type == type
728 && thunk->target == target
729 && thunk->relay == relay
730 && ( type == THUNKLET_TYPE_LS ?
731 ( thunk->glue == glue - (DWORD)&thunk->type )
732 : ( thunk->glue == glue ) ) )
733 return thunk;
735 return NULL;
738 /***********************************************************************
739 * THUNK_AllocLSThunklet
741 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
742 FARPROC glue, HTASK16 owner )
744 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
745 THUNKLET_TYPE_LS );
746 if (!thunk)
748 TDB *pTask = (TDB*)GlobalLock16( owner );
750 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
751 return 0;
753 thunk->prefix_target = thunk->prefix_relay = 0x90;
754 thunk->pushl_target = thunk->pushl_relay = 0x68;
755 thunk->jmp_glue = 0xE9;
757 thunk->target = (DWORD)target;
758 thunk->relay = (DWORD)relay;
759 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
761 thunk->type = THUNKLET_TYPE_LS;
762 thunk->owner = pTask? pTask->hInstance : 0;
764 thunk->next = ThunkletAnchor;
765 ThunkletAnchor = thunk;
768 return (FARPROC)thunk;
771 /***********************************************************************
772 * THUNK_AllocSLThunklet
774 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
775 SEGPTR glue, HTASK16 owner )
777 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
778 THUNKLET_TYPE_SL );
779 if (!thunk)
781 TDB *pTask = (TDB*)GlobalLock16( owner );
783 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
784 return 0;
786 thunk->prefix_target = thunk->prefix_relay = 0x66;
787 thunk->pushl_target = thunk->pushl_relay = 0x68;
788 thunk->jmp_glue = 0xEA;
790 thunk->target = (DWORD)target;
791 thunk->relay = (DWORD)relay;
792 thunk->glue = (DWORD)glue;
794 thunk->type = THUNKLET_TYPE_SL;
795 thunk->owner = pTask? pTask->hInstance : 0;
797 thunk->next = ThunkletAnchor;
798 ThunkletAnchor = thunk;
801 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
804 /**********************************************************************
805 * IsLSThunklet
807 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
809 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
810 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
811 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
814 /**********************************************************************
815 * IsSLThunklet (KERNEL.612)
817 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
819 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
820 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
821 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
826 /***********************************************************************
827 * AllocLSThunkletSysthunk (KERNEL.607)
829 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
830 FARPROC relay, DWORD dummy )
832 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
833 ThunkletSysthunkGlueLS, GetCurrentTask() );
836 /***********************************************************************
837 * AllocSLThunkletSysthunk (KERNEL.608)
839 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
840 SEGPTR relay, DWORD dummy )
842 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
843 ThunkletSysthunkGlueSL, GetCurrentTask() );
847 /***********************************************************************
848 * AllocLSThunkletCallbackEx (KERNEL.567)
850 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
851 DWORD relay, HTASK16 task )
853 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
854 if ( !thunk ) return NULL;
856 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
857 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
858 return (FARPROC)thunk->target;
860 return THUNK_AllocLSThunklet( target, relay,
861 ThunkletCallbackGlueLS, task );
864 /***********************************************************************
865 * AllocSLThunkletCallbackEx (KERNEL.568)
867 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
868 DWORD relay, HTASK16 task )
870 THUNKLET *thunk = (THUNKLET *)target;
871 if ( !thunk ) return 0;
873 if ( IsLSThunklet( thunk ) && thunk->relay == relay
874 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
875 return (SEGPTR)thunk->target;
877 return THUNK_AllocSLThunklet( target, relay,
878 ThunkletCallbackGlueSL, task );
881 /***********************************************************************
882 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
884 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
886 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
889 /***********************************************************************
890 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
892 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
894 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
897 /***********************************************************************
898 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
900 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
902 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
903 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
904 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
905 return (FARPROC)thunk->target;
907 thunk = THUNK_FindThunklet( (DWORD)target, relay,
908 (DWORD)ThunkletCallbackGlueLS,
909 THUNKLET_TYPE_LS );
910 return (FARPROC)thunk;
913 /***********************************************************************
914 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
916 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
918 THUNKLET *thunk = (THUNKLET *)target;
919 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
920 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
921 return (SEGPTR)thunk->target;
923 thunk = THUNK_FindThunklet( (DWORD)target, relay,
924 (DWORD)ThunkletCallbackGlueSL,
925 THUNKLET_TYPE_SL );
926 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
930 /***********************************************************************
931 * FreeThunklet16 (KERNEL.611)
933 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
935 return FALSE;
938 /***********************************************************************
939 * Callback Client API
942 #define N_CBC_FIXED 20
943 #define N_CBC_VARIABLE 10
944 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
946 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
947 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
949 /***********************************************************************
950 * RegisterCBClient (KERNEL.619)
952 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
953 SEGPTR relay16, FARPROC *relay32 )
955 /* Search for free Callback ID */
956 if ( wCBCId == -1 )
957 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
958 if ( !CBClientRelay16[ wCBCId ] )
959 break;
961 /* Register Callback ID */
962 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
964 CBClientRelay16[ wCBCId ] = relay16;
965 CBClientRelay32[ wCBCId ] = relay32;
967 else
968 wCBCId = 0;
970 return wCBCId;
973 /***********************************************************************
974 * UnRegisterCBClient (KERNEL.622)
976 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
977 SEGPTR relay16, FARPROC *relay32 )
979 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
980 && CBClientRelay16[ wCBCId ] == relay16
981 && CBClientRelay32[ wCBCId ] == relay32 )
983 CBClientRelay16[ wCBCId ] = 0;
984 CBClientRelay32[ wCBCId ] = 0;
986 else
987 wCBCId = 0;
989 return wCBCId;
993 /***********************************************************************
994 * InitCBClient (KERNEL.623)
996 void WINAPI InitCBClient16( FARPROC glueLS )
998 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
999 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1001 SetThunkletCallbackGlue16( glueLS, glueSL );
1004 /***********************************************************************
1005 * CBClientGlueSL (KERNEL.604)
1007 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1009 /* Create stack frame */
1010 SEGPTR stackSeg = stack16_push( 12 );
1011 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1012 SEGPTR glue, *glueTab;
1014 stackLin[3] = BP_reg( context );
1015 stackLin[2] = SI_reg( context );
1016 stackLin[1] = DI_reg( context );
1017 stackLin[0] = DS_reg( context );
1019 EBP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1020 ESP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1021 GS_reg( context ) = 0;
1023 /* Jump to 16-bit relay code */
1024 glueTab = PTR_SEG_TO_LIN( CBClientRelay16[ stackLin[5] ] );
1025 glue = glueTab[ stackLin[4] ];
1026 CS_reg ( context ) = SELECTOROF( glue );
1027 EIP_reg( context ) = OFFSETOF ( glue );
1030 /***********************************************************************
1031 * CBClientThunkSL (KERNEL.620)
1033 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1034 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1036 /* Call 32-bit relay code */
1038 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1039 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1041 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1044 /***********************************************************************
1045 * CBClientThunkSLEx (KERNEL.621)
1047 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
1048 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1050 /* Call 32-bit relay code */
1052 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1053 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1054 INT nArgs;
1055 LPWORD stackLin;
1057 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1059 /* Restore registers saved by CBClientGlueSL */
1060 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1061 BP_reg( context ) = stackLin[3];
1062 SI_reg( context ) = stackLin[2];
1063 DI_reg( context ) = stackLin[1];
1064 DS_reg( context ) = stackLin[0];
1065 ESP_reg( context ) += 16+nArgs;
1067 /* Return to caller of CBClient thunklet */
1068 CS_reg ( context ) = stackLin[9];
1069 EIP_reg( context ) = stackLin[8];