dplayx: Handling for CreatePlayer messages
[wine/gsoc_dplay.git] / dlls / kernel32 / utthunk.c
blobd3333a2225c08e5ed47fb735e557cd6a0867b9e7
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 #ifdef __i386__
23 #include <stdarg.h>
25 #include "wine/winbase16.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winternl.h"
29 #include "wownt32.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
34 #include "pshpack1.h"
36 typedef struct
38 BYTE popl_eax;
39 BYTE pushl;
40 DWORD target;
41 BYTE pushl_eax;
42 BYTE ljmp;
43 DWORD utglue16;
45 } UT16THUNK;
47 typedef struct
49 BYTE popl_eax;
50 BYTE pushl;
51 DWORD target;
52 BYTE pushl_eax;
53 BYTE jmp;
54 DWORD utglue32;
56 } UT32THUNK;
58 #include "poppack.h"
60 typedef struct _UTINFO
62 struct _UTINFO *next;
63 HMODULE hModule;
64 HMODULE16 hModule16;
66 UT16THUNK ut16;
67 UT32THUNK ut32;
69 } UTINFO;
71 static UTINFO *UT_head; /* head of Universal Thunk list */
73 typedef DWORD (CALLBACK *UTGLUEPROC)( LPVOID lpBuff, DWORD dwUserDefined );
75 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
76 LPSTR lpszInitName, LPSTR lpszProcName,
77 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
78 LPVOID lpBuff );
80 VOID WINAPI UTUnRegister( HMODULE hModule );
83 /****************************************************************************
84 * UTGlue16 (KERNEL.666) (KERNEL Wine-specific export)
86 DWORD WINAPI UTGlue16( LPVOID lpBuff, DWORD dwUserDefined, SEGPTR *translationList,
87 UTGLUEPROC target )
89 INT i;
91 /* Convert arguments to flat pointers */
93 if ( translationList )
94 for ( i = 0; translationList[i]; i++ )
96 LPVOID flatPtr = MapSL( translationList[i] );
97 *(LPVOID *)flatPtr = MapSL( *(SEGPTR *)flatPtr );
100 /* Call 32-bit routine */
102 return target( lpBuff, dwUserDefined );
105 /****************************************************************************
106 * UTGlue32
108 static DWORD WINAPI UTGlue32( FARPROC16 target, LPVOID lpBuff, DWORD dwUserDefined,
109 LPVOID translationList[] )
111 SEGPTR segBuff, *segptrList = NULL;
112 INT i, nList = 0;
113 DWORD retv;
114 WORD args[4];
116 /* Convert arguments to SEGPTRs */
118 if ( translationList )
119 for ( nList = 0; translationList[nList]; nList++ )
122 if ( nList )
124 segptrList = HeapAlloc( GetProcessHeap(), 0, sizeof(SEGPTR)*nList );
125 if ( !segptrList )
127 FIXME("Unable to allocate segptrList!\n" );
128 return 0;
131 for ( i = 0; i < nList; i++ )
132 segptrList[i] = *(SEGPTR *)translationList[i]
133 = MapLS( *(LPVOID *)translationList[i] );
136 segBuff = MapLS( lpBuff );
138 /* Call 16-bit routine */
140 args[3] = SELECTOROF(segBuff);
141 args[2] = OFFSETOF(segBuff);
142 args[1] = HIWORD(dwUserDefined);
143 args[0] = LOWORD(dwUserDefined);
144 WOWCallback16Ex( (DWORD)target, WCB16_PASCAL, sizeof(args), args, &retv );
146 /* Free temporary selectors */
148 UnMapLS( segBuff );
150 if ( nList )
152 for ( i = 0; i < nList; i++ )
153 UnMapLS( segptrList[i] );
155 HeapFree( GetProcessHeap(), 0, segptrList );
158 return retv;
161 /****************************************************************************
162 * UTAlloc
164 static UTINFO *UTAlloc( HMODULE hModule, HMODULE16 hModule16,
165 FARPROC16 target16, FARPROC target32 )
167 static FARPROC16 UTGlue16_Segptr = NULL;
168 UTINFO *ut;
170 if ( !UTGlue16_Segptr )
172 HMODULE16 hMod = GetModuleHandle16( "KERNEL" );
173 UTGlue16_Segptr = GetProcAddress16( hMod, "UTGlue16" );
174 if ( !UTGlue16_Segptr ) return NULL;
177 ut = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(UTINFO) );
178 if ( !ut ) return NULL;
180 ut->hModule = hModule;
181 ut->hModule16 = hModule16;
183 ut->ut16.popl_eax = 0x58;
184 ut->ut16.pushl = 0x68;
185 ut->ut16.target = (DWORD)target32;
186 ut->ut16.pushl_eax = 0x50;
187 ut->ut16.ljmp = 0xea;
188 ut->ut16.utglue16 = (DWORD)UTGlue16_Segptr;
190 ut->ut32.popl_eax = 0x58;
191 ut->ut32.pushl = 0x68;
192 ut->ut32.target = (DWORD)target16;
193 ut->ut32.pushl_eax = 0x50;
194 ut->ut32.jmp = 0xe9;
195 ut->ut32.utglue32 = (DWORD)UTGlue32 - ((DWORD)&ut->ut32.utglue32 + sizeof(DWORD));
197 ut->next = UT_head;
198 UT_head = ut;
200 return ut;
203 /****************************************************************************
204 * UTFree
206 static void UTFree( UTINFO *ut )
208 UTINFO **ptr;
210 for ( ptr = &UT_head; *ptr; ptr = &(*ptr)->next )
211 if ( *ptr == ut )
213 *ptr = ut->next;
214 break;
217 HeapFree( GetProcessHeap(), 0, ut );
220 /****************************************************************************
221 * UTFind
223 static UTINFO *UTFind( HMODULE hModule )
225 UTINFO *ut;
227 for ( ut = UT_head; ut; ut =ut->next )
228 if ( ut->hModule == hModule )
229 break;
231 return ut;
235 /****************************************************************************
236 * UTRegister (KERNEL32.@)
238 BOOL WINAPI UTRegister( HMODULE hModule, LPSTR lpsz16BITDLL,
239 LPSTR lpszInitName, LPSTR lpszProcName,
240 FARPROC *ppfn32Thunk, FARPROC pfnUT32CallBack,
241 LPVOID lpBuff )
243 UTINFO *ut;
244 HMODULE16 hModule16;
245 FARPROC16 target16, init16;
247 /* Load 16-bit DLL and get UTProc16 entry point */
249 if ( (hModule16 = LoadLibrary16( lpsz16BITDLL )) <= 32
250 || (target16 = GetProcAddress16( hModule16, lpszProcName )) == 0 )
251 return FALSE;
253 /* Allocate UTINFO struct */
255 RtlAcquirePebLock();
256 if ( (ut = UTFind( hModule )) != NULL )
257 ut = NULL;
258 else
259 ut = UTAlloc( hModule, hModule16, target16, pfnUT32CallBack );
260 RtlReleasePebLock();
262 if ( !ut )
264 FreeLibrary16( hModule16 );
265 return FALSE;
268 /* Call UTInit16 if present */
270 if ( lpszInitName
271 && (init16 = GetProcAddress16( hModule16, lpszInitName )) != 0 )
273 SEGPTR callback = MapLS( &ut->ut16 );
274 SEGPTR segBuff = MapLS( lpBuff );
275 WORD args[4];
276 DWORD ret;
278 args[3] = SELECTOROF(callback);
279 args[2] = OFFSETOF(callback);
280 args[1] = SELECTOROF(segBuff);
281 args[0] = OFFSETOF(segBuff);
282 WOWCallback16Ex( (DWORD)init16, WCB16_PASCAL, sizeof(args), args, &ret );
283 UnMapLS( segBuff );
284 UnMapLS( callback );
285 if (!ret)
287 UTUnRegister( hModule );
288 return FALSE;
292 /* Return 32-bit thunk */
294 *ppfn32Thunk = (FARPROC) &ut->ut32;
296 return TRUE;
299 /****************************************************************************
300 * UTUnRegister (KERNEL32.@)
302 VOID WINAPI UTUnRegister( HMODULE hModule )
304 UTINFO *ut;
305 HMODULE16 hModule16 = 0;
307 RtlAcquirePebLock();
308 ut = UTFind( hModule );
309 if ( ut )
311 hModule16 = ut->hModule16;
312 UTFree( ut );
314 RtlReleasePebLock();
316 if ( hModule16 )
317 FreeLibrary16( hModule16 );
320 /****************************************************************************
321 * UTInit (KERNEL.493)
323 WORD WINAPI UTInit16( DWORD x1, DWORD x2, DWORD x3, DWORD x4 )
325 FIXME("(%08x, %08x, %08x, %08x): stub\n", x1, x2, x3, x4 );
326 return 0;
329 #endif /* __i386__ */