2 * Win32s Universal Thunk API
4 * Copyright 1999 Ulrich Weigand
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/winbase16.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(thunk
);
58 typedef struct _UTINFO
69 static UTINFO
*UT_head
; /* head of Universal Thunk list */
71 typedef DWORD (CALLBACK
*UTGLUEPROC
)( LPVOID lpBuff
, DWORD dwUserDefined
);
73 BOOL WINAPI
UTRegister( HMODULE hModule
, LPSTR lpsz16BITDLL
,
74 LPSTR lpszInitName
, LPSTR lpszProcName
,
75 FARPROC
*ppfn32Thunk
, FARPROC pfnUT32CallBack
,
78 VOID WINAPI
UTUnRegister( HMODULE hModule
);
81 /****************************************************************************
82 * UTGlue16 (KERNEL.666) (KERNEL Wine-specific export)
84 DWORD WINAPI
UTGlue16( LPVOID lpBuff
, DWORD dwUserDefined
, SEGPTR
*translationList
,
89 /* Convert arguments to flat pointers */
91 if ( translationList
)
92 for ( i
= 0; translationList
[i
]; i
++ )
94 LPVOID flatPtr
= MapSL( translationList
[i
] );
95 *(LPVOID
*)flatPtr
= MapSL( *(SEGPTR
*)flatPtr
);
98 /* Call 32-bit routine */
100 return target( lpBuff
, dwUserDefined
);
103 /****************************************************************************
106 static DWORD WINAPI
UTGlue32( FARPROC16 target
, LPVOID lpBuff
, DWORD dwUserDefined
,
107 LPVOID translationList
[] )
109 SEGPTR segBuff
, *segptrList
= NULL
;
114 /* Convert arguments to SEGPTRs */
116 if ( translationList
)
117 for ( nList
= 0; translationList
[nList
]; nList
++ )
122 segptrList
= HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR
)*nList
);
125 FIXME("Unable to allocate segptrList!\n" );
129 for ( i
= 0; i
< nList
; i
++ )
130 segptrList
[i
] = *(SEGPTR
*)translationList
[i
]
131 = MapLS( *(LPVOID
*)translationList
[i
] );
134 segBuff
= MapLS( lpBuff
);
136 /* Call 16-bit routine */
138 args
[3] = SELECTOROF(segBuff
);
139 args
[2] = OFFSETOF(segBuff
);
140 args
[1] = HIWORD(dwUserDefined
);
141 args
[0] = LOWORD(dwUserDefined
);
142 WOWCallback16Ex( (DWORD
)target
, WCB16_PASCAL
, sizeof(args
), args
, &retv
);
144 /* Free temporary selectors */
150 for ( i
= 0; i
< nList
; i
++ )
151 UnMapLS( segptrList
[i
] );
153 HeapFree( GetProcessHeap(), 0, segptrList
);
159 /****************************************************************************
162 static UTINFO
*UTAlloc( HMODULE hModule
, HMODULE16 hModule16
,
163 FARPROC16 target16
, FARPROC target32
)
165 static FARPROC16 UTGlue16_Segptr
= NULL
;
168 if ( !UTGlue16_Segptr
)
170 HMODULE16 hModule
= GetModuleHandle16( "KERNEL" );
171 UTGlue16_Segptr
= GetProcAddress16( hModule
, "UTGlue16" );
172 if ( !UTGlue16_Segptr
) return NULL
;
175 ut
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(UTINFO
) );
176 if ( !ut
) return NULL
;
178 ut
->hModule
= hModule
;
179 ut
->hModule16
= hModule16
;
181 ut
->ut16
.popl_eax
= 0x58;
182 ut
->ut16
.pushl
= 0x68;
183 ut
->ut16
.target
= (DWORD
)target32
;
184 ut
->ut16
.pushl_eax
= 0x50;
185 ut
->ut16
.ljmp
= 0xea;
186 ut
->ut16
.utglue16
= (DWORD
)UTGlue16_Segptr
;
188 ut
->ut32
.popl_eax
= 0x58;
189 ut
->ut32
.pushl
= 0x68;
190 ut
->ut32
.target
= (DWORD
)target16
;
191 ut
->ut32
.pushl_eax
= 0x50;
193 ut
->ut32
.utglue32
= (DWORD
)UTGlue32
- ((DWORD
)&ut
->ut32
.utglue32
+ sizeof(DWORD
));
201 /****************************************************************************
204 static void UTFree( UTINFO
*ut
)
208 for ( ptr
= &UT_head
; *ptr
; ptr
= &(*ptr
)->next
)
215 HeapFree( GetProcessHeap(), 0, ut
);
218 /****************************************************************************
221 static UTINFO
*UTFind( HMODULE hModule
)
225 for ( ut
= UT_head
; ut
; ut
=ut
->next
)
226 if ( ut
->hModule
== hModule
)
233 /****************************************************************************
234 * UTRegister (KERNEL32.@)
236 BOOL WINAPI
UTRegister( HMODULE hModule
, LPSTR lpsz16BITDLL
,
237 LPSTR lpszInitName
, LPSTR lpszProcName
,
238 FARPROC
*ppfn32Thunk
, FARPROC pfnUT32CallBack
,
243 FARPROC16 target16
, init16
;
245 /* Load 16-bit DLL and get UTProc16 entry point */
247 if ( (hModule16
= LoadLibrary16( lpsz16BITDLL
)) <= 32
248 || (target16
= GetProcAddress16( hModule16
, lpszProcName
)) == 0 )
251 /* Allocate UTINFO struct */
254 if ( (ut
= UTFind( hModule
)) != NULL
)
257 ut
= UTAlloc( hModule
, hModule16
, target16
, pfnUT32CallBack
);
262 FreeLibrary16( hModule16
);
266 /* Call UTInit16 if present */
269 && (init16
= GetProcAddress16( hModule16
, lpszInitName
)) != 0 )
271 SEGPTR callback
= MapLS( &ut
->ut16
);
272 SEGPTR segBuff
= MapLS( lpBuff
);
276 args
[3] = SELECTOROF(callback
);
277 args
[2] = OFFSETOF(callback
);
278 args
[1] = SELECTOROF(segBuff
);
279 args
[0] = OFFSETOF(segBuff
);
280 WOWCallback16Ex( (DWORD
)init16
, WCB16_PASCAL
, sizeof(args
), args
, &ret
);
285 UTUnRegister( hModule
);
290 /* Return 32-bit thunk */
292 *ppfn32Thunk
= (FARPROC
) &ut
->ut32
;
297 /****************************************************************************
298 * UTUnRegister (KERNEL32.@)
300 VOID WINAPI
UTUnRegister( HMODULE hModule
)
303 HMODULE16 hModule16
= 0;
306 ut
= UTFind( hModule
);
309 hModule16
= ut
->hModule16
;
315 FreeLibrary16( hModule16
);
318 /****************************************************************************
319 * UTInit (KERNEL.493)
321 WORD WINAPI
UTInit16( DWORD x1
, DWORD x2
, DWORD x3
, DWORD x4
)
323 FIXME("(%08lx, %08lx, %08lx, %08lx): stub\n", x1
, x2
, x3
, x4
);