2 * Win32s Universal Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
10 #include "selectors.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(thunk);
43 typedef struct _UTINFO
54 typedef DWORD (CALLBACK *UTGLUEPROC)( LPVOID lpBuff, DWORD dwUserDefined );
56 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
57 LPSTR lpszInitName, LPSTR lpszProcName,
58 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
61 VOID WINAPI UTUnRegister( HMODULE hModule );
63 /* ### start build ### */
64 extern LONG CALLBACK UTTHUNK_CallTo16_long_ll(FARPROC16,LONG,LONG);
65 /* ### stop build ### */
67 /****************************************************************************
68 * UTGlue16 (KERNEL Wine-specific export)
70 DWORD WINAPI UTGlue16( LPVOID lpBuff, DWORD dwUserDefined, SEGPTR *translationList,
75 /* Convert arguments to flat pointers */
77 if ( translationList )
78 for ( i = 0; translationList[i]; i++ )
80 LPVOID flatPtr = PTR_SEG_TO_LIN( translationList[i] );
81 *(LPVOID *)flatPtr = PTR_SEG_TO_LIN( *(SEGPTR *)flatPtr );
84 /* Call 32-bit routine */
86 return target( lpBuff, dwUserDefined );
89 /****************************************************************************
92 static DWORD WINAPI UTGlue32( FARPROC16 target, LPVOID lpBuff, DWORD dwUserDefined,
93 LPVOID translationList[] )
95 SEGPTR segBuff, *segptrList = NULL;
99 /* Convert arguments to SEGPTRs */
101 if ( translationList )
102 for ( nList = 0; translationList[nList]; nList++ )
107 segptrList = HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR)*nList );
110 FIXME("Unable to allocate segptrList!" );
114 for ( i = 0; i < nList; i++ )
115 segptrList[i] = *(SEGPTR *)translationList[i]
116 = MapLS( *(LPVOID *)translationList[i] );
119 segBuff = MapLS( lpBuff );
121 /* Call 16-bit routine */
123 retv = UTTHUNK_CallTo16_long_ll( target, segBuff, dwUserDefined );
125 /* Free temporary selectors */
131 for ( i = 0; i < nList; i++ )
132 UnMapLS( segptrList[i] );
134 HeapFree( GetProcessHeap(), 0, segptrList );
140 /****************************************************************************
143 static UTINFO *UTAlloc( HMODULE hModule, HMODULE16 hModule16,
144 FARPROC16 target16, FARPROC target32 )
146 static FARPROC16 UTGlue16_Segptr = NULL;
149 if ( !UTGlue16_Segptr )
151 HMODULE16 hModule = GetModuleHandle16( "KERNEL" );
152 UTGlue16_Segptr = GetProcAddress16( hModule, "UTGlue16" );
153 if ( !UTGlue16_Segptr ) return NULL;
156 ut = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_WINE_SEGPTR, sizeof(UTINFO) );
157 if ( !ut ) return NULL;
159 ut->hModule = hModule;
160 ut->hModule16 = hModule16;
162 ut->ut16.popl_eax = 0x58;
163 ut->ut16.pushl = 0x68;
164 ut->ut16.target = (DWORD)target32;
165 ut->ut16.pushl_eax = 0x50;
166 ut->ut16.ljmp = 0xea;
167 ut->ut16.utglue16 = (DWORD)UTGlue16_Segptr;
169 ut->ut32.popl_eax = 0x58;
170 ut->ut32.pushl = 0x68;
171 ut->ut32.target = (DWORD)target16;
172 ut->ut32.pushl_eax = 0x50;
174 ut->ut32.utglue32 = (DWORD)UTGlue32 - ((DWORD)&ut->ut32.utglue32 + sizeof(DWORD));
176 ut->next = PROCESS_Current()->UTState;
177 PROCESS_Current()->UTState = ut;
182 /****************************************************************************
185 static void UTFree( UTINFO *ut )
189 for ( ptr = &PROCESS_Current()->UTState; *ptr; ptr = &(*ptr)->next )
196 HeapFree( GetProcessHeap(), HEAP_WINE_SEGPTR, ut );
199 /****************************************************************************
202 static UTINFO *UTFind( HMODULE hModule )
206 for ( ut = PROCESS_Current()->UTState; ut; ut =ut->next )
207 if ( ut->hModule == hModule )
214 /****************************************************************************
215 * UTRegister (KERNEL32.697)
217 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
218 LPSTR lpszInitName, LPSTR lpszProcName,
219 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
224 FARPROC16 target16, init16;
226 /* Load 16-bit DLL and get UTProc16 entry point */
228 if ( (hModule16 = LoadLibrary16( lpsz16BITDLL )) <= 32
229 || (target16 = GetProcAddress16( hModule16, lpszProcName )) == 0 )
232 /* Allocate UTINFO struct */
234 EnterCriticalSection( &PROCESS_Current()->crit_section );
235 if ( (ut = UTFind( hModule )) != NULL )
238 ut = UTAlloc( hModule, hModule16, target16, pfnUT32CallBack );
239 LeaveCriticalSection( &PROCESS_Current()->crit_section );
243 FreeLibrary16( hModule16 );
247 /* Call UTInit16 if present */
250 && (init16 = GetProcAddress16( hModule16, lpszInitName )) != 0 )
252 SEGPTR callback = SEGPTR_GET( &ut->ut16 );
253 SEGPTR segBuff = MapLS( lpBuff );
255 if ( !UTTHUNK_CallTo16_long_ll( init16, callback, segBuff ) )
258 UTUnRegister( hModule );
264 /* Return 32-bit thunk */
266 *ppfn32Thunk = (FARPROC) &ut->ut32;
271 /****************************************************************************
272 * UTUnRegister (KERNEL32.698)
274 VOID WINAPI UTUnRegister( HMODULE hModule )
277 HMODULE16 hModule16 = 0;
279 EnterCriticalSection( &PROCESS_Current()->crit_section );
280 ut = UTFind( hModule );
283 hModule16 = ut->hModule16;
286 LeaveCriticalSection( &PROCESS_Current()->crit_section );
289 FreeLibrary16( hModule16 );
292 /****************************************************************************
293 * UTInit16 (KERNEL.494)
295 WORD WINAPI UTInit16( DWORD x1, DWORD x2, DWORD x3, DWORD x4 )
297 FIXME("(%08lx, %08lx, %08lx, %08lx): stub\n", x1, x2, x3, x4 );