Initialize 'str' in ICONTITLE_GetTitlePos.
[wine.git] / if1632 / thunk.c
blobca0be129aedd264790e7d876b499512ebbb448c5
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 "callback.h"
11 #include "builtin16.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "neexe.h"
15 #include "stackframe.h"
16 #include "selectors.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(thunk);
22 /* List of the 16-bit callback functions. This list is used */
23 /* by the build program to generate the file if1632/callto16.S */
25 /* ### start build ### */
26 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
27 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
28 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
29 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
30 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
31 extern LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
32 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
33 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
34 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
35 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
36 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
37 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
38 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
39 /* ### stop build ### */
41 static THUNK *firstThunk = NULL;
43 /* Callbacks function table for the emulator */
44 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
46 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
47 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
48 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
49 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
50 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
51 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
52 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
53 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
54 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
55 (void *)THUNK_CallTo16_long_l /* CallASPIPostProc */
58 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
60 CALLOUT_TABLE Callout = { 0 };
63 /***********************************************************************
64 * THUNK_Alloc
66 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
68 HANDLE16 hSeg;
69 NE_MODULE *pModule;
70 THUNK *thunk;
72 /* NULL maps to NULL */
73 if ( !func ) return NULL;
75 /*
76 * If we got an 16-bit built-in API entry point, retrieve the Wine
77 * 32-bit handler for that API routine.
79 * NOTE: For efficiency reasons, we only check whether the selector
80 * of 'func' points to the code segment of a built-in module.
81 * It might be theoretically possible that the offset is such
82 * that 'func' does not point, in fact, to an API entry point.
83 * In this case, however, the pointer is corrupt anyway.
85 hSeg = GlobalHandle16( SELECTOROF( func ) );
86 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
88 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
89 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
91 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( func ))->target;
93 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
94 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
95 return proc;
98 /* Otherwise, we need to alloc a thunk */
99 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
100 if (thunk)
102 thunk->popl_eax = 0x58;
103 thunk->pushl_func = 0x68;
104 thunk->proc = func;
105 thunk->pushl_eax = 0x50;
106 thunk->jmp = 0xe9;
107 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
108 thunk->magic = CALLTO16_THUNK_MAGIC;
109 thunk->next = firstThunk;
110 firstThunk = thunk;
113 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
114 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
115 return (FARPROC)thunk;
118 /***********************************************************************
119 * THUNK_Free
121 void THUNK_Free( FARPROC thunk )
123 THUNK *t = (THUNK*)thunk;
124 if ( !t || IsBadReadPtr( t, sizeof(*t) )
125 || t->magic != CALLTO16_THUNK_MAGIC )
126 return;
128 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t ))
130 THUNK **prev = &firstThunk;
131 while (*prev && (*prev != t)) prev = &(*prev)->next;
132 if (*prev)
134 *prev = t->next;
135 HeapFree( GetProcessHeap(), 0, t );
136 return;
139 ERR("invalid thunk addr %p\n", thunk );
140 return;
144 /***********************************************************************
145 * THUNK_GetCalloutThunk
147 * Retrieve API entry point with given name from given module.
148 * If module is builtin, return the 32-bit entry point, otherwise
149 * create a 32->16 thunk to the 16-bit entry point, using the
150 * given relay code.
153 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
155 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
156 if ( !proc ) return 0;
158 if ( pModule->flags & NE_FFLAGS_BUILTIN )
159 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
160 else
161 return (FARPROC)THUNK_Alloc( proc, relay );
164 /***********************************************************************
165 * THUNK_InitCallout
167 void THUNK_InitCallout(void)
169 HMODULE hModule;
170 NE_MODULE *pModule;
172 hModule = LoadLibraryA( "user32.dll" );
173 if ( hModule )
175 #define GETADDR( name ) \
176 *(FARPROC *)&Callout.##name = GetProcAddress( hModule, #name )
178 GETADDR( PeekMessageA );
179 GETADDR( PeekMessageW );
180 GETADDR( GetMessageA );
181 GETADDR( GetMessageW );
182 GETADDR( SendMessageA );
183 GETADDR( SendMessageW );
184 GETADDR( PostMessageA );
185 GETADDR( PostMessageW );
186 GETADDR( PostThreadMessageA );
187 GETADDR( PostThreadMessageW );
188 GETADDR( TranslateMessage );
189 GETADDR( DispatchMessageW );
190 GETADDR( DispatchMessageA );
191 GETADDR( RedrawWindow );
192 GETADDR( WaitForInputIdle );
193 GETADDR( MsgWaitForMultipleObjects );
194 GETADDR( WindowFromDC );
195 GETADDR( GetForegroundWindow );
196 GETADDR( IsChild );
197 GETADDR( MessageBoxA );
198 GETADDR( MessageBoxW );
199 #undef GETADDR
202 pModule = NE_GetPtr( LoadLibrary16( "USER.EXE" ) );
203 if ( pModule )
205 #define GETADDR( var, name, thk ) \
206 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
207 (RELAY)THUNK_CallTo16_##thk )
209 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
210 GETADDR( GetMessage16, "GetMessage", word_lwww );
211 GETADDR( SendMessage16, "SendMessage", long_wwwl );
212 GETADDR( PostMessage16, "PostMessage", word_wwwl );
213 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
214 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
215 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
216 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
217 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
218 GETADDR( InitApp16, "InitApp", word_w );
219 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
220 GETADDR( UserYield16, "UserYield", word_ );
221 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
222 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
224 #undef GETADDR