comctl32/tests: Remove variables cx and cy which are not really used from test_approx...
[wine.git] / dlls / krnl386.exe16 / utthunk.c
bloba1889fa3502e6010124b2ef8f538056eb9caaa17
1 /*
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
21 #include <stdarg.h>
23 #include "wine/winbase16.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winternl.h"
27 #include "wownt32.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
32 #include "pshpack1.h"
34 typedef struct
36 BYTE popl_eax;
37 BYTE pushl;
38 DWORD target;
39 BYTE pushl_eax;
40 BYTE ljmp;
41 DWORD utglue16;
43 } UT16THUNK;
45 typedef struct
47 BYTE popl_eax;
48 BYTE pushl;
49 DWORD target;
50 BYTE pushl_eax;
51 BYTE jmp;
52 DWORD utglue32;
54 } UT32THUNK;
56 #include "poppack.h"
58 typedef struct _UTINFO
60 struct _UTINFO *next;
61 HMODULE hModule;
62 HMODULE16 hModule16;
64 UT16THUNK ut16;
65 UT32THUNK ut32;
67 } 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,
76 LPVOID lpBuff );
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,
85 UTGLUEPROC target )
87 INT i;
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 /****************************************************************************
104 * UTGlue32
106 static DWORD WINAPI UTGlue32( FARPROC16 target, LPVOID lpBuff, DWORD dwUserDefined,
107 LPVOID translationList[] )
109 SEGPTR segBuff, *segptrList = NULL;
110 INT i, nList = 0;
111 DWORD retv;
112 WORD args[4];
114 /* Convert arguments to SEGPTRs */
116 if ( translationList )
117 for ( nList = 0; translationList[nList]; nList++ )
120 if ( nList )
122 segptrList = HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR)*nList );
123 if ( !segptrList )
125 FIXME("Unable to allocate segptrList!\n" );
126 return 0;
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 */
146 UnMapLS( segBuff );
148 if ( nList )
150 for ( i = 0; i < nList; i++ )
151 UnMapLS( segptrList[i] );
153 HeapFree( GetProcessHeap(), 0, segptrList );
156 return retv;
159 /****************************************************************************
160 * UTAlloc
162 static UTINFO *UTAlloc( HMODULE hModule, HMODULE16 hModule16,
163 FARPROC16 target16, FARPROC target32 )
165 static FARPROC16 UTGlue16_Segptr = NULL;
166 UTINFO *ut;
168 if ( !UTGlue16_Segptr )
170 HMODULE16 hMod = GetModuleHandle16( "KERNEL" );
171 UTGlue16_Segptr = GetProcAddress16( hMod, "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;
192 ut->ut32.jmp = 0xe9;
193 ut->ut32.utglue32 = (DWORD)UTGlue32 - ((DWORD)&ut->ut32.utglue32 + sizeof(DWORD));
195 ut->next = UT_head;
196 UT_head = ut;
198 return ut;
201 /****************************************************************************
202 * UTFree
204 static void UTFree( UTINFO *ut )
206 UTINFO **ptr;
208 for ( ptr = &UT_head; *ptr; ptr = &(*ptr)->next )
209 if ( *ptr == ut )
211 *ptr = ut->next;
212 break;
215 HeapFree( GetProcessHeap(), 0, ut );
218 /****************************************************************************
219 * UTFind
221 static UTINFO *UTFind( HMODULE hModule )
223 UTINFO *ut;
225 for ( ut = UT_head; ut; ut =ut->next )
226 if ( ut->hModule == hModule )
227 break;
229 return ut;
233 /****************************************************************************
234 * UTRegister (KERNEL32.@)
236 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
237 LPSTR lpszInitName, LPSTR lpszProcName,
238 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
239 LPVOID lpBuff )
241 UTINFO *ut;
242 HMODULE16 hModule16;
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 )
249 return FALSE;
251 /* Allocate UTINFO struct */
253 RtlAcquirePebLock();
254 if ( (ut = UTFind( hModule )) != NULL )
255 ut = NULL;
256 else
257 ut = UTAlloc( hModule, hModule16, target16, pfnUT32CallBack );
258 RtlReleasePebLock();
260 if ( !ut )
262 FreeLibrary16( hModule16 );
263 return FALSE;
266 /* Call UTInit16 if present */
268 if ( lpszInitName
269 && (init16 = GetProcAddress16( hModule16, lpszInitName )) != 0 )
271 SEGPTR callback = MapLS( &ut->ut16 );
272 SEGPTR segBuff = MapLS( lpBuff );
273 WORD args[4];
274 DWORD ret;
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 );
281 UnMapLS( segBuff );
282 UnMapLS( callback );
283 if (!ret)
285 UTUnRegister( hModule );
286 return FALSE;
290 /* Return 32-bit thunk */
292 *ppfn32Thunk = (FARPROC) &ut->ut32;
294 return TRUE;
297 /****************************************************************************
298 * UTUnRegister (KERNEL32.@)
300 VOID WINAPI UTUnRegister( HMODULE hModule )
302 UTINFO *ut;
303 HMODULE16 hModule16 = 0;
305 RtlAcquirePebLock();
306 ut = UTFind( hModule );
307 if ( ut )
309 hModule16 = ut->hModule16;
310 UTFree( ut );
312 RtlReleasePebLock();
314 if ( hModule16 )
315 FreeLibrary16( hModule16 );
318 /****************************************************************************
319 * UTInit (KERNEL.493)
321 WORD WINAPI UTInit16( DWORD x1, DWORD x2, DWORD x3, DWORD x4 )
323 FIXME("(%08x, %08x, %08x, %08x): stub\n", x1, x2, x3, x4 );
324 return 0;