Updated to new MM header organization.
[wine/dcerpc.git] / if1632 / thunk.c
blob5b1f906d3099dd41b3eb3198511a12393893e1a1
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 /* List of the 16-bit callback functions. This list is used */
31 /* by the build program to generate the file if1632/callto16.S */
33 /* ### start build ### */
34 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
35 extern LONG CALLBACK THUNK_CallTo16_long_ (FARPROC16);
36 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
37 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
38 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
39 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
40 extern WORD CALLBACK THUNK_CallTo16_word_wl (FARPROC16,WORD,LONG);
41 extern WORD CALLBACK THUNK_CallTo16_word_ll (FARPROC16,LONG,LONG);
42 extern LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
43 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
44 extern WORD CALLBACK THUNK_CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
45 extern WORD CALLBACK THUNK_CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
46 extern LONG CALLBACK THUNK_CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
47 extern WORD CALLBACK THUNK_CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
48 extern WORD CALLBACK THUNK_CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
49 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
50 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
51 extern WORD CALLBACK THUNK_CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
52 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
53 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
54 extern WORD CALLBACK THUNK_CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
55 extern WORD CALLBACK THUNK_CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
56 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
57 extern LONG CALLBACK THUNK_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
58 extern WORD CALLBACK THUNK_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
59 extern WORD CALLBACK THUNK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
60 extern WORD CALLBACK THUNK_CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
61 extern LONG CALLBACK THUNK_CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
62 extern WORD CALLBACK THUNK_CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,LONG,LONG);
63 extern LONG CALLBACK THUNK_CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
64 LONG,LONG,LONG);
65 extern LONG CALLBACK THUNK_CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
66 LONG,WORD,LONG,LONG,LONG,LONG,
67 LONG,WORD);
68 extern LONG CALLBACK THUNK_CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
69 WORD,WORD,LONG,WORD,WORD,
70 WORD,WORD,LONG,LONG,LONG,
71 LONG);
72 /* ### stop build ### */
75 typedef void (*RELAY)();
77 #include "pshpack1.h"
79 typedef struct tagTHUNK
81 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
82 BYTE pushl_func; /* 0x68 pushl $proc */
83 FARPROC proc WINE_PACKED;
84 BYTE pushl_eax; /* 0x50 pushl %eax */
85 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
86 RELAY relay WINE_PACKED;
87 struct tagTHUNK *next WINE_PACKED;
88 } THUNK;
90 #include "poppack.h"
92 #define DECL_THUNK(aname,aproc,arelay) \
93 THUNK aname; \
94 aname.popl_eax = 0x58; \
95 aname.pushl_func = 0x68; \
96 aname.proc = (FARPROC) (aproc); \
97 aname.pushl_eax = 0x50; \
98 aname.jmp = 0xe9; \
99 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
100 aname.next = NULL;
102 static THUNK *firstThunk = NULL;
104 static BOOL THUNK_ThunkletInit( void );
106 /* Callbacks function table for the emulator */
107 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
109 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
110 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
111 (void *)THUNK_CallTo16_long_lwwll, /* CallDriverProc */
112 (void *)THUNK_CallTo16_word_wwlll, /* CallDriverCallback */
113 (void *)THUNK_CallTo16_word_wwlll, /* CallTimeFuncProc */
114 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
115 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
116 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
117 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
118 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
119 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
120 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
121 (void *)THUNK_CallTo16_long_l, /* CallASPIPostProc */
122 (void *)THUNK_CallTo16_word_lwll, /* CallDrvControlProc */
123 (void *)THUNK_CallTo16_word_lwlll, /* CallDrvEnableProc */
124 (void *)THUNK_CallTo16_word_llll, /* CallDrvEnumDFontsProc */
125 (void *)THUNK_CallTo16_word_lwll, /* CallDrvEnumObjProc */
126 (void *)THUNK_CallTo16_word_lwwlllll, /* CallDrvOutputProc */
127 (void *)THUNK_CallTo16_long_lwlll, /* CallDrvRealizeProc */
128 (void *)THUNK_CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
129 (void *)THUNK_CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
130 (void *)THUNK_CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
131 (void *)THUNK_CallTo16_word_ww /* CallDrvAbortProc */
134 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
136 CALLOUT_TABLE Callout = { 0 };
139 /***********************************************************************
140 * THUNK_Init
142 BOOL THUNK_Init(void)
144 /* Initialize Thunklets */
145 return THUNK_ThunkletInit();
148 /***********************************************************************
149 * THUNK_Alloc
151 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
153 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
154 if (thunk)
156 thunk->popl_eax = 0x58;
157 thunk->pushl_func = 0x68;
158 thunk->proc = func;
159 thunk->pushl_eax = 0x50;
160 thunk->jmp = 0xe9;
161 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
162 thunk->next = firstThunk;
163 firstThunk = thunk;
165 return thunk;
169 /***********************************************************************
170 * THUNK_Find
172 static THUNK *THUNK_Find( FARPROC func )
174 THUNK *thunk = firstThunk;
175 while (thunk && (thunk->proc != func)) thunk = thunk->next;
176 return thunk;
180 /***********************************************************************
181 * THUNK_Free
183 static void THUNK_Free( THUNK *thunk )
185 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
187 THUNK **prev = &firstThunk;
188 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
189 if (*prev)
191 *prev = thunk->next;
192 HeapFree( GetProcessHeap(), 0, thunk );
193 return;
196 ERR_(thunk)("invalid thunk addr %p\n", thunk );
200 /***********************************************************************
201 * THUNK_EnumObjects16 (GDI.71)
203 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
204 GOBJENUMPROC16 func, LPARAM lParam )
206 DECL_THUNK( thunk, func, THUNK_CallTo16_word_ll );
207 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
211 /*************************************************************************
212 * THUNK_EnumFonts16 (GDI.70)
214 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
215 FONTENUMPROC16 func, LPARAM lParam )
217 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
218 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
221 /******************************************************************
222 * THUNK_EnumMetaFile16 (GDI.175)
224 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
225 MFENUMPROC16 func, LPARAM lParam )
227 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wllwl );
228 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
232 /*************************************************************************
233 * THUNK_EnumFontFamilies16 (GDI.330)
235 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
236 FONTENUMPROC16 func, LPARAM lParam )
238 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
239 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
243 /*************************************************************************
244 * THUNK_EnumFontFamiliesEx16 (GDI.613)
246 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
247 FONTENUMPROCEX16 func, LPARAM lParam,
248 DWORD reserved )
250 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
251 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
252 lParam, reserved );
256 /**********************************************************************
257 * THUNK_LineDDA16 (GDI.100)
259 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
260 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
262 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wwl );
263 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
267 /*******************************************************************
268 * THUNK_EnumWindows16 (USER.54)
270 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
272 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
273 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
277 /**********************************************************************
278 * THUNK_EnumChildWindows16 (USER.55)
280 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
281 LPARAM lParam )
283 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
284 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
288 /**********************************************************************
289 * THUNK_EnumTaskWindows16 (USER.225)
291 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
292 LPARAM lParam )
294 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
295 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
299 /***********************************************************************
300 * THUNK_EnumProps16 (USER.27)
302 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
304 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
305 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
309 /***********************************************************************
310 * THUNK_GrayString16 (USER.185)
312 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
313 GRAYSTRINGPROC16 func, LPARAM lParam,
314 INT16 cch, INT16 x, INT16 y,
315 INT16 cx, INT16 cy )
317 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
318 if (!func)
319 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
320 else
321 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
322 x, y, cx, cy );
326 /***********************************************************************
327 * THUNK_SetWindowsHook16 (USER.121)
329 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
331 HINSTANCE16 hInst = FarGetOwner16( HIWORD(proc) );
332 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
333 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
334 if (!thunk) return 0;
335 return (FARPROC16)SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask);
339 /***********************************************************************
340 * THUNK_UnhookWindowsHook16 (USER.234)
342 BOOL16 WINAPI THUNK_UnhookWindowsHook16( INT16 id, HOOKPROC16 proc )
344 BOOL16 ret;
345 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
346 if (!thunk) return FALSE;
347 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
348 THUNK_Free( thunk );
349 return ret;
353 /***********************************************************************
354 * THUNK_SetWindowsHookEx16 (USER.291)
356 HHOOK WINAPI THUNK_SetWindowsHookEx16( INT16 id, HOOKPROC16 proc,
357 HINSTANCE16 hInst, HTASK16 hTask )
359 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
360 if (!thunk) return 0;
361 return SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask );
365 /***********************************************************************
366 * THUNK_UnhookWindowHookEx16 (USER.292)
368 BOOL16 WINAPI THUNK_UnhookWindowsHookEx16( HHOOK hhook )
370 THUNK *thunk = (THUNK *)HOOK_GetProc16( hhook );
371 BOOL16 ret = UnhookWindowsHookEx16( hhook );
372 if (thunk) THUNK_Free( thunk );
373 return ret;
378 static FARPROC16 defDCHookProc = NULL;
380 /***********************************************************************
381 * THUNK_SetDCHook (GDI.190)
383 BOOL16 WINAPI THUNK_SetDCHook( HDC16 hdc, FARPROC16 proc, DWORD dwHookData )
385 THUNK *thunk, *oldThunk;
387 if (!defDCHookProc) /* Get DCHook Win16 entry point */
389 HMODULE16 hModule = GetModuleHandle16( "USER" );
390 NE_MODULE *pModule = NE_GetPtr( hModule );
392 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) )
393 defDCHookProc = NE_GetEntryPoint( hModule, 362 );
394 else
395 defDCHookProc = (FARPROC16)-1;
398 if (!proc)
399 thunk = NULL;
400 else if (proc != defDCHookProc)
402 thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwll );
403 if (!thunk) return FALSE;
405 else thunk = (THUNK *)DCHook16;
407 /* Free the previous thunk */
408 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
409 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
411 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
415 /***********************************************************************
416 * THUNK_GetDCHook (GDI.191)
418 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
420 THUNK *thunk = NULL;
421 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
422 if (thunk)
424 if (thunk == (THUNK *)DCHook16)
426 /* Note: we can only get here when running built-in USER */
428 if (!defDCHookProc) /* Get DCHook Win16 entry point */
429 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
431 *phookProc = defDCHookProc;
433 else *phookProc = thunk->proc;
435 return ret;
439 /***********************************************************************
440 * THUNK_SetTaskSignalProc (KERNEL.38)
442 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
444 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwwww );
445 if ( !thunk ) return NULL;
447 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
448 if ( !thunk ) return NULL;
450 proc = thunk->proc;
451 THUNK_Free( thunk );
452 return proc;
455 /***********************************************************************
456 * THUNK_CreateThread16 (KERNEL.441)
458 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
460 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
461 DWORD param = ((DWORD *)threadArgs)[1];
462 HeapFree( GetProcessHeap(), 0, threadArgs );
464 return THUNK_CallTo16_long_l( start, param );
466 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
467 FARPROC16 start, SEGPTR param,
468 DWORD flags, LPDWORD id )
470 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
471 if (!threadArgs) return INVALID_HANDLE_VALUE;
472 threadArgs[0] = (DWORD)start;
473 threadArgs[1] = (DWORD)param;
475 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
478 /***********************************************************************
479 * THUNK_MOUSE_Enable (MOUSE.2)
481 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
482 DWORD dwFlags, DWORD dx, DWORD dy,
483 DWORD cButtons, DWORD dwExtraInfo )
485 CONTEXT86 context;
487 memset( &context, 0, sizeof(context) );
488 CS_reg(&context) = SELECTOROF( proc );
489 EIP_reg(&context) = OFFSETOF( proc );
490 AX_reg(&context) = (WORD)dwFlags;
491 BX_reg(&context) = (WORD)dx;
492 CX_reg(&context) = (WORD)dy;
493 DX_reg(&context) = (WORD)cButtons;
494 SI_reg(&context) = LOWORD( dwExtraInfo );
495 DI_reg(&context) = HIWORD( dwExtraInfo );
497 CallTo16RegisterShort( &context, 0 );
499 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
501 static THUNK *lastThunk = NULL;
502 static FARPROC16 lastProc = NULL;
504 if ( lastProc != proc )
506 if ( lastThunk )
507 THUNK_Free( lastThunk );
509 if ( !proc )
510 lastThunk = NULL;
511 else
512 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
514 lastProc = proc;
517 MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
520 /***********************************************************************
521 * GetMouseEventProc (USER.337)
523 FARPROC16 WINAPI GetMouseEventProc16(void)
525 HMODULE16 hmodule = GetModuleHandle16("USER");
526 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
530 /***********************************************************************
531 * WIN16_mouse_event (USER.299)
533 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
535 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
536 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
540 /***********************************************************************
541 * THUNK_KEYBD_Enable (KEYBOARD.2)
543 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
544 BYTE bVk, BYTE bScan,
545 DWORD dwFlags, DWORD dwExtraInfo )
547 CONTEXT86 context;
549 memset( &context, 0, sizeof(context) );
550 CS_reg(&context) = SELECTOROF( proc );
551 EIP_reg(&context) = OFFSETOF( proc );
552 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
553 AL_reg(&context) = bVk;
554 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
555 BL_reg(&context) = bScan;
556 SI_reg(&context) = LOWORD( dwExtraInfo );
557 DI_reg(&context) = HIWORD( dwExtraInfo );
559 CallTo16RegisterShort( &context, 0 );
561 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
563 static THUNK *lastThunk = NULL;
564 static FARPROC16 lastProc = NULL;
566 if ( lastProc != proc )
568 if ( lastThunk )
569 THUNK_Free( lastThunk );
571 if ( !proc )
572 lastThunk = NULL;
573 else
574 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
576 lastProc = proc;
579 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
582 /***********************************************************************
583 * WIN16_keybd_event (USER.289)
585 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
587 DWORD dwFlags = 0;
589 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
590 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
592 keybd_event( AL_reg(context), BL_reg(context),
593 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
597 /***********************************************************************
598 * WIN16_CreateSystemTimer (SYSTEM.2)
600 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
602 CONTEXT86 context;
603 memset( &context, '\0', sizeof(context) );
605 CS_reg( &context ) = SELECTOROF( proc );
606 EIP_reg( &context ) = OFFSETOF( proc );
607 EBP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
608 + (WORD)&((STACK16FRAME*)0)->bp;
610 AX_reg( &context ) = timer;
612 if ( _ConfirmWin16Lock() )
614 FIXME_(system)("Skipping timer %d callback because timer signal "
615 "arrived while we own the Win16Lock!\n", timer );
616 return;
619 CallTo16RegisterShort( &context, 0 );
621 /* FIXME: This does not work if the signal occurs while this thread
622 is currently in 16-bit code. With the current structure
623 of the Wine thunking code, this seems to be hard to fix ... */
625 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
627 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
628 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
629 if (!timer) THUNK_Free( thunk );
630 return timer;
633 /***********************************************************************
634 * THUNK_GetCalloutThunk
636 * Retrieve API entry point with given name from given module.
637 * If module is builtin, return the 32-bit entry point, otherwise
638 * create a 32->16 thunk to the 16-bit entry point, using the
639 * given relay code.
642 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
644 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
645 if ( !proc ) return 0;
647 if ( pModule->flags & NE_FFLAGS_BUILTIN )
648 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
649 else
650 return (FARPROC)THUNK_Alloc( proc, relay );
653 /***********************************************************************
654 * THUNK_InitCallout
656 void THUNK_InitCallout(void)
658 HMODULE hModule;
659 NE_MODULE *pModule;
661 hModule = GetModuleHandleA( "USER32" );
662 if ( hModule )
664 #define GETADDR( var, name ) \
665 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
667 GETADDR( PeekMessageA, "PeekMessageA" );
668 GETADDR( PeekMessageW, "PeekMessageW" );
669 GETADDR( GetMessageA, "GetMessageA" );
670 GETADDR( GetMessageW, "GetMessageW" );
671 GETADDR( SendMessageA, "SendMessageA" );
672 GETADDR( SendMessageW, "SendMessageW" );
673 GETADDR( PostMessageA, "PostMessageA" );
674 GETADDR( PostMessageW, "PostMessageW" );
675 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
676 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
677 GETADDR( TranslateMessage, "TranslateMessage" );
678 GETADDR( DispatchMessageW, "DispatchMessageW" );
679 GETADDR( DispatchMessageA, "DispatchMessageA" );
680 GETADDR( RedrawWindow, "RedrawWindow" );
681 GETADDR( UserSignalProc, "UserSignalProc" );
683 #undef GETADDR
686 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
687 if ( pModule )
689 #define GETADDR( var, name, thk ) \
690 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
691 (RELAY)THUNK_CallTo16_##thk )
693 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
694 GETADDR( GetMessage16, "GetMessage", word_lwww );
695 GETADDR( SendMessage16, "SendMessage", long_wwwl );
696 GETADDR( PostMessage16, "PostMessage", word_wwwl );
697 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
698 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
699 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
700 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
701 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
702 GETADDR( InitApp16, "InitApp", word_w );
703 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
704 GETADDR( UserYield16, "UserYield", word_ );
705 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
707 #undef GETADDR
711 /***********************************************************************
712 * 16->32 Flat Thunk routines:
715 /***********************************************************************
716 * ThunkConnect16 (KERNEL.651)
717 * Connects a 32bit and a 16bit thunkbuffer.
719 UINT WINAPI ThunkConnect16(
720 LPSTR module16, /* [in] name of win16 dll */
721 LPSTR module32, /* [in] name of win32 dll */
722 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
723 DWORD dwReason, /* [in] initialisation argument */
724 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
725 LPSTR thunkfun32, /* [in] win32 thunkfunction */
726 WORD cs /* [in] CS of win16 dll */
728 BOOL directionSL;
730 if (!strncmp(TD->magic, "SL01", 4))
732 directionSL = TRUE;
734 TRACE_(thunk)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
735 module16, (DWORD)TD, module32, thunkfun32, dwReason);
737 else if (!strncmp(TD->magic, "LS01", 4))
739 directionSL = FALSE;
741 TRACE_(thunk)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
742 module16, (DWORD)TD, module32, thunkfun32, dwReason);
744 else
746 ERR_(thunk)("Invalid magic %c%c%c%c\n",
747 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
748 return 0;
751 switch (dwReason)
753 case DLL_PROCESS_ATTACH:
754 if (directionSL)
756 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
757 struct ThunkDataSL *SL = SL16->fpData;
759 if (SL == NULL)
761 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
763 SL->common = SL16->common;
764 SL->flags1 = SL16->flags1;
765 SL->flags2 = SL16->flags2;
767 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
768 SL->targetDB = NULL;
770 lstrcpynA(SL->pszDll16, module16, 255);
771 lstrcpynA(SL->pszDll32, module32, 255);
773 /* We should create a SEGPTR to the ThunkDataSL,
774 but since the contents are not in the original format,
775 any access to this by 16-bit code would crash anyway. */
776 SL16->spData = 0;
777 SL16->fpData = SL;
781 if (SL->flags2 & 0x80000000)
783 TRACE_(thunk)("Preloading 32-bit library\n");
784 LoadLibraryA(module32);
787 else
789 /* nothing to do */
791 break;
793 case DLL_PROCESS_DETACH:
794 /* FIXME: cleanup */
795 break;
798 return 1;
802 /***********************************************************************
803 * C16ThkSL (KERNEL.630)
806 void WINAPI C16ThkSL(CONTEXT86 *context)
808 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
809 WORD cs, ds;
810 GET_CS(cs);
811 GET_DS(ds);
813 /* We produce the following code:
815 * mov ax, __FLATDS
816 * mov es, ax
817 * movzx ecx, cx
818 * mov edx, es:[ecx + $EDX]
819 * push bp
820 * push edx
821 * push dx
822 * push edx
823 * call __FLATCS:CallFrom16Thunk
826 *x++ = 0xB8; *((WORD *)x)++ = ds;
827 *x++ = 0x8E; *x++ = 0xC0;
828 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
829 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
830 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
832 *x++ = 0x55;
833 *x++ = 0x66; *x++ = 0x52;
834 *x++ = 0x52;
835 *x++ = 0x66; *x++ = 0x52;
836 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
837 *((WORD *)x)++ = cs;
839 /* Jump to the stub code just created */
840 EIP_reg(context) = LOWORD(EAX_reg(context));
841 CS_reg(context) = HIWORD(EAX_reg(context));
843 /* Since C16ThkSL got called by a jmp, we need to leave the
844 original return address on the stack */
845 ESP_reg(context) -= 4;
848 /***********************************************************************
849 * C16ThkSL01 (KERNEL.631)
852 void WINAPI C16ThkSL01(CONTEXT86 *context)
854 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
856 if (stub)
858 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
859 struct ThunkDataSL *td = SL16->fpData;
861 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
862 WORD cs;
863 GET_CS(cs);
865 if (!td)
867 ERR_(thunk)("ThunkConnect16 was not called!\n");
868 return;
871 TRACE_(thunk)("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
874 /* We produce the following code:
876 * xor eax, eax
877 * mov edx, $td
878 * call C16ThkSL01
879 * push bp
880 * push edx
881 * push dx
882 * push edx
883 * call __FLATCS:CallFrom16Thunk
886 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
887 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
888 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
890 *x++ = 0x55;
891 *x++ = 0x66; *x++ = 0x52;
892 *x++ = 0x52;
893 *x++ = 0x66; *x++ = 0x52;
894 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
895 *((WORD *)x)++ = cs;
897 /* Jump to the stub code just created */
898 EIP_reg(context) = LOWORD(EAX_reg(context));
899 CS_reg(context) = HIWORD(EAX_reg(context));
901 /* Since C16ThkSL01 got called by a jmp, we need to leave the
902 orginal return address on the stack */
903 ESP_reg(context) -= 4;
905 else
907 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
908 DWORD targetNr = CX_reg(context) / 4;
909 struct SLTargetDB *tdb;
911 TRACE_(thunk)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
912 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
914 for (tdb = td->targetDB; tdb; tdb = tdb->next)
915 if (tdb->process == PROCESS_Current())
916 break;
918 if (!tdb)
920 TRACE_(thunk)("Loading 32-bit library %s\n", td->pszDll32);
921 LoadLibraryA(td->pszDll32);
923 for (tdb = td->targetDB; tdb; tdb = tdb->next)
924 if (tdb->process == PROCESS_Current())
925 break;
928 if (tdb)
930 EDX_reg(context) = tdb->targetTable[targetNr];
932 TRACE_(thunk)("Call target is %08lx\n", EDX_reg(context));
934 else
936 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context)));
937 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
938 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
939 EIP_reg(context) = stack[2];
940 CS_reg(context) = stack[3];
941 ESP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
943 ERR_(thunk)("Process %08lx did not ThunkConnect32 %s to %s\n",
944 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
951 /***********************************************************************
952 * 16<->32 Thunklet/Callback API:
955 #include "pshpack1.h"
956 typedef struct _THUNKLET
958 BYTE prefix_target;
959 BYTE pushl_target;
960 DWORD target;
962 BYTE prefix_relay;
963 BYTE pushl_relay;
964 DWORD relay;
966 BYTE jmp_glue;
967 DWORD glue;
969 BYTE type;
970 HINSTANCE16 owner;
971 struct _THUNKLET *next;
972 } THUNKLET;
973 #include "poppack.h"
975 #define THUNKLET_TYPE_LS 1
976 #define THUNKLET_TYPE_SL 2
978 static HANDLE ThunkletHeap = 0;
979 static THUNKLET *ThunkletAnchor = NULL;
981 static FARPROC ThunkletSysthunkGlueLS = 0;
982 static SEGPTR ThunkletSysthunkGlueSL = 0;
984 static FARPROC ThunkletCallbackGlueLS = 0;
985 static SEGPTR ThunkletCallbackGlueSL = 0;
987 /***********************************************************************
988 * THUNK_ThunkletInit
990 static BOOL THUNK_ThunkletInit( void )
992 LPBYTE thunk;
994 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
995 if (!ThunkletHeap) return FALSE;
997 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
998 if (!thunk) return FALSE;
1000 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1001 *thunk++ = 0x58; /* popl eax */
1002 *thunk++ = 0xC3; /* ret */
1004 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1005 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1006 *thunk++ = 0xCB; /* lret */
1008 return TRUE;
1011 /***********************************************************************
1012 * SetThunkletCallbackGlue (KERNEL.560)
1014 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1016 ThunkletCallbackGlueLS = glueLS;
1017 ThunkletCallbackGlueSL = glueSL;
1021 /***********************************************************************
1022 * THUNK_FindThunklet
1024 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1025 DWORD glue, BYTE type )
1027 THUNKLET *thunk;
1029 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1030 if ( thunk->type == type
1031 && thunk->target == target
1032 && thunk->relay == relay
1033 && ( type == THUNKLET_TYPE_LS ?
1034 ( thunk->glue == glue - (DWORD)&thunk->type )
1035 : ( thunk->glue == glue ) ) )
1036 return thunk;
1038 return NULL;
1041 /***********************************************************************
1042 * THUNK_AllocLSThunklet
1044 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1045 FARPROC glue, HTASK16 owner )
1047 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1048 THUNKLET_TYPE_LS );
1049 if (!thunk)
1051 TDB *pTask = (TDB*)GlobalLock16( owner );
1053 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1054 return 0;
1056 thunk->prefix_target = thunk->prefix_relay = 0x90;
1057 thunk->pushl_target = thunk->pushl_relay = 0x68;
1058 thunk->jmp_glue = 0xE9;
1060 thunk->target = (DWORD)target;
1061 thunk->relay = (DWORD)relay;
1062 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1064 thunk->type = THUNKLET_TYPE_LS;
1065 thunk->owner = pTask? pTask->hInstance : 0;
1067 thunk->next = ThunkletAnchor;
1068 ThunkletAnchor = thunk;
1071 return (FARPROC)thunk;
1074 /***********************************************************************
1075 * THUNK_AllocSLThunklet
1077 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1078 SEGPTR glue, HTASK16 owner )
1080 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1081 THUNKLET_TYPE_SL );
1082 if (!thunk)
1084 TDB *pTask = (TDB*)GlobalLock16( owner );
1086 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1087 return 0;
1089 thunk->prefix_target = thunk->prefix_relay = 0x66;
1090 thunk->pushl_target = thunk->pushl_relay = 0x68;
1091 thunk->jmp_glue = 0xEA;
1093 thunk->target = (DWORD)target;
1094 thunk->relay = (DWORD)relay;
1095 thunk->glue = (DWORD)glue;
1097 thunk->type = THUNKLET_TYPE_SL;
1098 thunk->owner = pTask? pTask->hInstance : 0;
1100 thunk->next = ThunkletAnchor;
1101 ThunkletAnchor = thunk;
1104 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1107 /**********************************************************************
1108 * IsLSThunklet
1110 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1112 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1113 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1114 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1117 /**********************************************************************
1118 * IsSLThunklet (KERNEL.612)
1120 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1122 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1123 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1124 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1129 /***********************************************************************
1130 * AllocLSThunkletSysthunk (KERNEL.607)
1132 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1133 FARPROC relay, DWORD dummy )
1135 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1136 ThunkletSysthunkGlueLS, GetCurrentTask() );
1139 /***********************************************************************
1140 * AllocSLThunkletSysthunk (KERNEL.608)
1142 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1143 SEGPTR relay, DWORD dummy )
1145 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1146 ThunkletSysthunkGlueSL, GetCurrentTask() );
1150 /***********************************************************************
1151 * AllocLSThunkletCallbackEx (KERNEL.567)
1153 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1154 DWORD relay, HTASK16 task )
1156 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1157 if ( !thunk ) return NULL;
1159 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1160 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1161 return (FARPROC)thunk->target;
1163 return THUNK_AllocLSThunklet( target, relay,
1164 ThunkletCallbackGlueLS, task );
1167 /***********************************************************************
1168 * AllocSLThunkletCallbackEx (KERNEL.568)
1170 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1171 DWORD relay, HTASK16 task )
1173 THUNKLET *thunk = (THUNKLET *)target;
1174 if ( !thunk ) return 0;
1176 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1177 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1178 return (SEGPTR)thunk->target;
1180 return THUNK_AllocSLThunklet( target, relay,
1181 ThunkletCallbackGlueSL, task );
1184 /***********************************************************************
1185 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1187 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1189 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1192 /***********************************************************************
1193 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1195 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1197 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1200 /***********************************************************************
1201 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1203 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1205 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1206 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1207 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1208 return (FARPROC)thunk->target;
1210 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1211 (DWORD)ThunkletCallbackGlueLS,
1212 THUNKLET_TYPE_LS );
1213 return (FARPROC)thunk;
1216 /***********************************************************************
1217 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1219 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1221 THUNKLET *thunk = (THUNKLET *)target;
1222 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1223 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1224 return (SEGPTR)thunk->target;
1226 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1227 (DWORD)ThunkletCallbackGlueSL,
1228 THUNKLET_TYPE_SL );
1229 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1233 /***********************************************************************
1234 * FreeThunklet16 (KERNEL.611)
1236 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1238 return FALSE;
1241 /***********************************************************************
1242 * Callback Client API
1245 #define N_CBC_FIXED 20
1246 #define N_CBC_VARIABLE 10
1247 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1249 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1250 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1252 /***********************************************************************
1253 * RegisterCBClient (KERNEL.619)
1255 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1256 SEGPTR *relay16, FARPROC *relay32 )
1258 /* Search for free Callback ID */
1259 if ( wCBCId == -1 )
1260 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1261 if ( !CBClientRelay16[ wCBCId ] )
1262 break;
1264 /* Register Callback ID */
1265 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1267 CBClientRelay16[ wCBCId ] = relay16;
1268 CBClientRelay32[ wCBCId ] = relay32;
1270 else
1271 wCBCId = 0;
1273 return wCBCId;
1276 /***********************************************************************
1277 * UnRegisterCBClient (KERNEL.622)
1279 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1280 SEGPTR *relay16, FARPROC *relay32 )
1282 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1283 && CBClientRelay16[ wCBCId ] == relay16
1284 && CBClientRelay32[ wCBCId ] == relay32 )
1286 CBClientRelay16[ wCBCId ] = 0;
1287 CBClientRelay32[ wCBCId ] = 0;
1289 else
1290 wCBCId = 0;
1292 return wCBCId;
1296 /***********************************************************************
1297 * InitCBClient (KERNEL.623)
1299 void WINAPI InitCBClient16( FARPROC glueLS )
1301 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1302 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1304 SetThunkletCallbackGlue16( glueLS, glueSL );
1307 /***********************************************************************
1308 * CBClientGlueSL (KERNEL.604)
1310 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1312 /* Create stack frame */
1313 SEGPTR stackSeg = stack16_push( 12 );
1314 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1315 SEGPTR glue;
1317 stackLin[3] = BP_reg( context );
1318 stackLin[2] = SI_reg( context );
1319 stackLin[1] = DI_reg( context );
1320 stackLin[0] = DS_reg( context );
1322 EBP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1323 ESP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1324 GS_reg( context ) = 0;
1326 /* Jump to 16-bit relay code */
1327 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1328 CS_reg ( context ) = SELECTOROF( glue );
1329 EIP_reg( context ) = OFFSETOF ( glue );
1332 /***********************************************************************
1333 * CBClientThunkSL (KERNEL.620)
1335 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1336 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1338 /* Call 32-bit relay code */
1340 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1341 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1343 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1346 /***********************************************************************
1347 * CBClientThunkSLEx (KERNEL.621)
1349 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args,
1350 DWORD *esi, INT *nArgs );
1351 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1353 /* Call 32-bit relay code */
1355 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1356 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1357 INT nArgs;
1358 LPWORD stackLin;
1360 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1362 /* Restore registers saved by CBClientGlueSL */
1363 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1364 BP_reg( context ) = stackLin[3];
1365 SI_reg( context ) = stackLin[2];
1366 DI_reg( context ) = stackLin[1];
1367 DS_reg( context ) = stackLin[0];
1368 ESP_reg( context ) += 16+nArgs;
1370 /* Return to caller of CBClient thunklet */
1371 CS_reg ( context ) = stackLin[9];
1372 EIP_reg( context ) = stackLin[8];