Made server launching somewhat cleaner and faster.
[wine/multimedia.git] / relay32 / utthunk.c
blobe8b6c7dba20f1fd1db188a9bda2f4370034eccb2
1 /*
2 * Win32s Universal Thunk API
4 * Copyright 1999 Ulrich Weigand
5 */
7 #include "wine/winbase16.h"
8 #include "windef.h"
9 #include "heap.h"
10 #include "module.h"
11 #include "selectors.h"
12 #include "callback.h"
13 #include "process.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(thunk);
18 #include "pshpack1.h"
20 typedef struct
22 BYTE popl_eax;
23 BYTE pushl;
24 DWORD target;
25 BYTE pushl_eax;
26 BYTE ljmp;
27 DWORD utglue16;
29 } UT16THUNK;
31 typedef struct
33 BYTE popl_eax;
34 BYTE pushl;
35 DWORD target;
36 BYTE pushl_eax;
37 BYTE jmp;
38 DWORD utglue32;
40 } UT32THUNK;
42 #include "poppack.h"
44 typedef struct _UTINFO
46 struct _UTINFO *next;
47 HMODULE hModule;
48 HMODULE16 hModule16;
50 UT16THUNK ut16;
51 UT32THUNK ut32;
53 } UTINFO;
55 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
56 LPSTR lpszInitName, LPSTR lpszProcName,
57 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
58 LPVOID lpBuff );
60 VOID WINAPI UTUnRegister( HMODULE hModule );
63 /****************************************************************************
64 * UTGlue16 (WPROCS.*)
66 DWORD WINAPI UTGlue16( LPVOID lpBuff, DWORD dwUserDefined, SEGPTR translationList[],
67 DWORD (CALLBACK *target)( LPVOID lpBuff, DWORD dwUserDefined ) )
69 INT i;
71 /* Convert arguments to flat pointers */
73 if ( translationList )
74 for ( i = 0; translationList[i]; i++ )
76 LPVOID flatPtr = PTR_SEG_TO_LIN( translationList[i] );
77 *(LPVOID *)flatPtr = PTR_SEG_TO_LIN( *(SEGPTR *)flatPtr );
80 /* Call 32-bit routine */
82 return target( lpBuff, dwUserDefined );
85 /****************************************************************************
86 * UTGlue32
88 static DWORD WINAPI UTGlue32( FARPROC16 target, LPVOID lpBuff, DWORD dwUserDefined,
89 LPVOID translationList[] )
91 SEGPTR segBuff, *segptrList = NULL;
92 INT i, nList = 0;
93 DWORD retv;
95 /* Convert arguments to SEGPTRs */
97 if ( translationList )
98 for ( nList = 0; translationList[nList]; nList++ )
101 if ( nList )
103 segptrList = HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR)*nList );
104 if ( !segptrList )
106 FIXME("Unable to allocate segptrList!" );
107 return 0;
110 for ( i = 0; i < nList; i++ )
111 segptrList[i] = *(SEGPTR *)translationList[i]
112 = MapLS( *(LPVOID *)translationList[i] );
115 segBuff = MapLS( lpBuff );
117 /* Call 16-bit routine */
119 retv = Callbacks->CallUTProc( target, segBuff, dwUserDefined );
121 /* Free temporary selectors */
123 UnMapLS( segBuff );
125 if ( nList )
127 for ( i = 0; i < nList; i++ )
128 UnMapLS( segptrList[i] );
130 HeapFree( GetProcessHeap(), 0, segptrList );
133 return retv;
136 /****************************************************************************
137 * UTAlloc
139 static UTINFO *UTAlloc( HMODULE hModule, HMODULE16 hModule16,
140 FARPROC16 target16, FARPROC target32 )
142 static FARPROC16 UTGlue16_Segptr = NULL;
143 UTINFO *ut;
145 if ( !UTGlue16_Segptr )
147 HMODULE16 hModule = GetModuleHandle16( "WPROCS" );
148 int ordinal = NE_GetOrdinal( hModule, "UTGlue16" );
149 if ( hModule && ordinal )
150 UTGlue16_Segptr = NE_GetEntryPoint( hModule, ordinal );
152 if ( !UTGlue16_Segptr ) return NULL;
155 ut = HeapAlloc( SegptrHeap, HEAP_ZERO_MEMORY, sizeof(UTINFO) );
156 if ( !ut ) return NULL;
158 ut->hModule = hModule;
159 ut->hModule16 = hModule16;
161 ut->ut16.popl_eax = 0x58;
162 ut->ut16.pushl = 0x68;
163 ut->ut16.target = (DWORD)target32;
164 ut->ut16.pushl_eax = 0x50;
165 ut->ut16.ljmp = 0xea;
166 ut->ut16.utglue16 = (DWORD)UTGlue16_Segptr;
168 ut->ut32.popl_eax = 0x58;
169 ut->ut32.pushl = 0x68;
170 ut->ut32.target = (DWORD)target16;
171 ut->ut32.pushl_eax = 0x50;
172 ut->ut32.jmp = 0xe9;
173 ut->ut32.utglue32 = (DWORD)UTGlue32 - ((DWORD)&ut->ut32.utglue32 + sizeof(DWORD));
175 ut->next = PROCESS_Current()->UTState;
176 PROCESS_Current()->UTState = ut;
178 return ut;
181 /****************************************************************************
182 * UTFree
184 static void UTFree( UTINFO *ut )
186 UTINFO **ptr;
188 for ( ptr = &PROCESS_Current()->UTState; *ptr; ptr = &(*ptr)->next )
189 if ( *ptr == ut )
191 *ptr = ut->next;
192 break;
195 HeapFree( SegptrHeap, 0, ut );
198 /****************************************************************************
199 * UTFind
201 static UTINFO *UTFind( HMODULE hModule )
203 UTINFO *ut;
205 for ( ut = PROCESS_Current()->UTState; ut; ut =ut->next )
206 if ( ut->hModule == hModule )
207 break;
209 return ut;
213 /****************************************************************************
214 * UTRegister (KERNEL32.697)
216 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
217 LPSTR lpszInitName, LPSTR lpszProcName,
218 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
219 LPVOID lpBuff )
221 UTINFO *ut;
222 HMODULE16 hModule16;
223 FARPROC16 target16, init16;
225 /* Load 16-bit DLL and get UTProc16 entry point */
227 if ( (hModule16 = LoadLibrary16( lpsz16BITDLL )) <= 32
228 || (target16 = WIN32_GetProcAddress16( hModule16, lpszProcName )) == 0 )
229 return FALSE;
231 /* Allocate UTINFO struct */
233 EnterCriticalSection( &PROCESS_Current()->crit_section );
234 if ( (ut = UTFind( hModule )) != NULL )
235 ut = NULL;
236 else
237 ut = UTAlloc( hModule, hModule16, target16, pfnUT32CallBack );
238 LeaveCriticalSection( &PROCESS_Current()->crit_section );
240 if ( !ut )
242 FreeLibrary16( hModule16 );
243 return FALSE;
246 /* Call UTInit16 if present */
248 if ( lpszInitName
249 && (init16 = WIN32_GetProcAddress16( hModule16, lpszInitName )) != 0 )
251 SEGPTR callback = SEGPTR_GET( &ut->ut16 );
252 SEGPTR segBuff = MapLS( lpBuff );
254 if ( !Callbacks->CallUTProc( init16, callback, segBuff ) )
256 UnMapLS( segBuff );
257 UTUnRegister( hModule );
258 return FALSE;
260 UnMapLS( segBuff );
263 /* Return 32-bit thunk */
265 *ppfn32Thunk = (FARPROC) &ut->ut32;
267 return TRUE;
270 /****************************************************************************
271 * UTUnRegister (KERNEL32.698)
273 VOID WINAPI UTUnRegister( HMODULE hModule )
275 UTINFO *ut;
276 HMODULE16 hModule16 = 0;
278 EnterCriticalSection( &PROCESS_Current()->crit_section );
279 ut = UTFind( hModule );
280 if ( !ut )
282 hModule16 = ut->hModule16;
283 UTFree( ut );
285 LeaveCriticalSection( &PROCESS_Current()->crit_section );
287 if ( hModule16 )
288 FreeLibrary16( hModule16 );
291 /****************************************************************************
292 * UTInit16 (KERNEL.494)
294 WORD WINAPI UTInit16( DWORD x1, DWORD x2, DWORD x3, DWORD x4 )
296 FIXME("(%08lx, %08lx, %08lx, %08lx): stub\n", x1, x2, x3, x4 );
297 return 0;