2 * Win32s Universal Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
11 #include "selectors.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(thunk);
44 typedef struct _UTINFO
55 typedef DWORD (CALLBACK *UTGLUEPROC)( LPVOID lpBuff, DWORD dwUserDefined );
57 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
58 LPSTR lpszInitName, LPSTR lpszProcName,
59 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
62 VOID WINAPI UTUnRegister( HMODULE hModule );
65 /****************************************************************************
68 DWORD WINAPI UTGlue16( LPVOID lpBuff, DWORD dwUserDefined, SEGPTR *translationList,
73 /* Convert arguments to flat pointers */
75 if ( translationList )
76 for ( i = 0; translationList[i]; i++ )
78 LPVOID flatPtr = PTR_SEG_TO_LIN( translationList[i] );
79 *(LPVOID *)flatPtr = PTR_SEG_TO_LIN( *(SEGPTR *)flatPtr );
82 /* Call 32-bit routine */
84 return target( lpBuff, dwUserDefined );
87 /****************************************************************************
90 static DWORD WINAPI UTGlue32( FARPROC16 target, LPVOID lpBuff, DWORD dwUserDefined,
91 LPVOID translationList[] )
93 SEGPTR segBuff, *segptrList = NULL;
97 /* Convert arguments to SEGPTRs */
99 if ( translationList )
100 for ( nList = 0; translationList[nList]; nList++ )
105 segptrList = HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR)*nList );
108 FIXME("Unable to allocate segptrList!" );
112 for ( i = 0; i < nList; i++ )
113 segptrList[i] = *(SEGPTR *)translationList[i]
114 = MapLS( *(LPVOID *)translationList[i] );
117 segBuff = MapLS( lpBuff );
119 /* Call 16-bit routine */
121 retv = Callbacks->CallUTProc( target, segBuff, dwUserDefined );
123 /* Free temporary selectors */
129 for ( i = 0; i < nList; i++ )
130 UnMapLS( segptrList[i] );
132 HeapFree( GetProcessHeap(), 0, segptrList );
138 /****************************************************************************
141 static UTINFO *UTAlloc( HMODULE hModule, HMODULE16 hModule16,
142 FARPROC16 target16, FARPROC target32 )
144 static FARPROC16 UTGlue16_Segptr = NULL;
147 if ( !UTGlue16_Segptr )
149 HMODULE16 hModule = GetModuleHandle16( "WPROCS" );
150 int ordinal = NE_GetOrdinal( hModule, "UTGlue16" );
151 if ( hModule && ordinal )
152 UTGlue16_Segptr = NE_GetEntryPoint( hModule, ordinal );
154 if ( !UTGlue16_Segptr ) return NULL;
157 ut = HeapAlloc( SegptrHeap, HEAP_ZERO_MEMORY, sizeof(UTINFO) );
158 if ( !ut ) return NULL;
160 ut->hModule = hModule;
161 ut->hModule16 = hModule16;
163 ut->ut16.popl_eax = 0x58;
164 ut->ut16.pushl = 0x68;
165 ut->ut16.target = (DWORD)target32;
166 ut->ut16.pushl_eax = 0x50;
167 ut->ut16.ljmp = 0xea;
168 ut->ut16.utglue16 = (DWORD)UTGlue16_Segptr;
170 ut->ut32.popl_eax = 0x58;
171 ut->ut32.pushl = 0x68;
172 ut->ut32.target = (DWORD)target16;
173 ut->ut32.pushl_eax = 0x50;
175 ut->ut32.utglue32 = (DWORD)UTGlue32 - ((DWORD)&ut->ut32.utglue32 + sizeof(DWORD));
177 ut->next = PROCESS_Current()->UTState;
178 PROCESS_Current()->UTState = ut;
183 /****************************************************************************
186 static void UTFree( UTINFO *ut )
190 for ( ptr = &PROCESS_Current()->UTState; *ptr; ptr = &(*ptr)->next )
197 HeapFree( SegptrHeap, 0, ut );
200 /****************************************************************************
203 static UTINFO *UTFind( HMODULE hModule )
207 for ( ut = PROCESS_Current()->UTState; ut; ut =ut->next )
208 if ( ut->hModule == hModule )
215 /****************************************************************************
216 * UTRegister (KERNEL32.697)
218 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
219 LPSTR lpszInitName, LPSTR lpszProcName,
220 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
225 FARPROC16 target16, init16;
227 /* Load 16-bit DLL and get UTProc16 entry point */
229 if ( (hModule16 = LoadLibrary16( lpsz16BITDLL )) <= 32
230 || (target16 = WIN32_GetProcAddress16( hModule16, lpszProcName )) == 0 )
233 /* Allocate UTINFO struct */
235 EnterCriticalSection( &PROCESS_Current()->crit_section );
236 if ( (ut = UTFind( hModule )) != NULL )
239 ut = UTAlloc( hModule, hModule16, target16, pfnUT32CallBack );
240 LeaveCriticalSection( &PROCESS_Current()->crit_section );
244 FreeLibrary16( hModule16 );
248 /* Call UTInit16 if present */
251 && (init16 = WIN32_GetProcAddress16( hModule16, lpszInitName )) != 0 )
253 SEGPTR callback = SEGPTR_GET( &ut->ut16 );
254 SEGPTR segBuff = MapLS( lpBuff );
256 if ( !Callbacks->CallUTProc( init16, callback, segBuff ) )
259 UTUnRegister( hModule );
265 /* Return 32-bit thunk */
267 *ppfn32Thunk = (FARPROC) &ut->ut32;
272 /****************************************************************************
273 * UTUnRegister (KERNEL32.698)
275 VOID WINAPI UTUnRegister( HMODULE hModule )
278 HMODULE16 hModule16 = 0;
280 EnterCriticalSection( &PROCESS_Current()->crit_section );
281 ut = UTFind( hModule );
284 hModule16 = ut->hModule16;
287 LeaveCriticalSection( &PROCESS_Current()->crit_section );
290 FreeLibrary16( hModule16 );
293 /****************************************************************************
294 * UTInit16 (KERNEL.494)
296 WORD WINAPI UTInit16( DWORD x1, DWORD x2, DWORD x3, DWORD x4 )
298 FIXME("(%08lx, %08lx, %08lx, %08lx): stub\n", x1, x2, x3, x4 );