2 * Win32 WOW Generic 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/winbase16.h"
26 #include "stackframe.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
32 * These are the 16-bit side WOW routines. They reside in wownt16.h
33 * in the SDK; since we don't support Win16 source code anyway, I've
34 * placed them here for compilation with Wine ...
37 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
39 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
40 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
41 DWORD WINAPI FreeLibrary32W16(DWORD);
43 #define CPEX_DEST_STDCALL 0x00000000L
44 #define CPEX_DEST_CDECL 0x80000000L
46 DWORD WINAPI CallProcExW16(VOID);
47 DWORD WINAPI CallProcEx32W16(VOID);
50 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
53 /**********************************************************************
54 * K32WOWGetDescriptor (KERNEL32.70)
56 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
58 return GetThreadSelectorEntry( GetCurrentThread(),
59 segptr >> 16, ldtent );
62 /**********************************************************************
63 * K32WOWGetVDMPointer (KERNEL32.56)
65 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
67 /* FIXME: add size check too */
72 return DOSMEM_MapRealToLinear( vp );
75 /**********************************************************************
76 * K32WOWGetVDMPointerFix (KERNEL32.68)
78 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
81 * Hmmm. According to the docu, we should call:
83 * GlobalFix16( SELECTOROF(vp) );
85 * But this is unnecessary under Wine, as we never move global
86 * memory segments in linear memory anyway.
88 * (I'm not so sure what we are *supposed* to do if
89 * fProtectedMode is TRUE, anyway ...)
92 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
95 /**********************************************************************
96 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
98 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
101 * See above why we don't call:
103 * GlobalUnfix16( SELECTOROF(vp) );
108 /**********************************************************************
109 * K32WOWGlobalAlloc16 (KERNEL32.59)
111 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
113 return (WORD)GlobalAlloc16( wFlags, cb );
116 /**********************************************************************
117 * K32WOWGlobalFree16 (KERNEL32.62)
119 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
121 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
124 /**********************************************************************
125 * K32WOWGlobalUnlock16 (KERNEL32.61)
127 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
129 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
132 /**********************************************************************
133 * K32WOWGlobalAllocLock16 (KERNEL32.63)
135 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
137 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
138 if (phMem) *phMem = hMem;
140 return K32WOWGlobalLock16( hMem );
143 /**********************************************************************
144 * K32WOWGlobalLockSize16 (KERNEL32.65)
146 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
149 *pcb = GlobalSize16( (HGLOBAL16)hMem );
151 return K32WOWGlobalLock16( hMem );
154 /**********************************************************************
155 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
157 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
159 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
162 return K32WOWGlobalFree16( HIWORD(vpMem) );
166 /**********************************************************************
167 * K32WOWYield16 (KERNEL32.66)
169 VOID WINAPI K32WOWYield16( void )
172 * This does the right thing for both Win16 and Win32 tasks.
173 * More or less, at least :-/
178 /**********************************************************************
179 * K32WOWDirectedYield16 (KERNEL32.67)
181 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
184 * Argh. Our scheduler doesn't like DirectedYield by Win32
185 * tasks at all. So we do hope that this routine is indeed
186 * only ever called by Win16 tasks that have thunked up ...
188 DirectedYield16( (HTASK16)htask16 );
192 /***********************************************************************
193 * K32WOWHandle32 (KERNEL32.57)
195 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
205 case WOW_TYPE_HMETAFILE:
207 case WOW_TYPE_HBITMAP:
208 case WOW_TYPE_HBRUSH:
209 case WOW_TYPE_HPALETTE:
211 case WOW_TYPE_HACCEL:
213 case WOW_TYPE_FULLHWND:
214 return (HANDLE)(ULONG_PTR)handle;
217 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
218 return (HANDLE)(ULONG_PTR)handle;
222 /***********************************************************************
223 * K32WOWHandle16 (KERNEL32.58)
225 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
227 if ( HIWORD(handle ) )
228 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
238 case WOW_TYPE_HMETAFILE:
240 case WOW_TYPE_HBITMAP:
241 case WOW_TYPE_HBRUSH:
242 case WOW_TYPE_HPALETTE:
244 case WOW_TYPE_HACCEL:
246 case WOW_TYPE_FULLHWND:
247 return LOWORD(handle);
250 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
251 return LOWORD(handle);
255 /**********************************************************************
256 * K32WOWCallback16Ex (KERNEL32.55)
258 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
259 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
264 * Arguments must be prepared in the correct order by the caller
265 * (both for PASCAL and CDECL calling convention), so we simply
266 * copy them to the 16-bit stack ...
268 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
272 * Actually, we should take care whether the called routine cleans up
273 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
274 * the callee to do so; after the routine has returned, the 16-bit
275 * stack pointer is always reset to the position it had before.
278 ret = wine_call_to_16_long( (FARPROC16)vpfn16, cbArgs );
283 return TRUE; /* success */
286 /**********************************************************************
287 * K32WOWCallback16 (KERNEL32.54)
289 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
293 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
294 sizeof(DWORD), &dwParam, &ret ) )
302 * 16-bit WOW routines (in KERNEL)
305 /**********************************************************************
306 * GetVDMPointer32W (KERNEL.516)
308 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
310 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
311 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
314 /***********************************************************************
315 * LoadLibraryEx32W (KERNEL.513)
317 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
320 DOS_FULL_NAME full_name;
323 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
324 a buildin module. This case is handled in MODULE_LoadLibraryExA */
326 if ( ! DIR_SearchPath ( NULL, lpszLibFile, ".DLL", &full_name, FALSE ) ) {
327 strcpy ( full_name.short_name, lpszLibFile );
330 ReleaseThunkLock( &mutex_count );
331 hModule = LoadLibraryExA( full_name.short_name, (HANDLE)hFile, dwFlags );
332 RestoreThunkLock( mutex_count );
333 return (DWORD)hModule;
336 /***********************************************************************
337 * GetProcAddress32W (KERNEL.515)
339 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
341 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
344 /***********************************************************************
345 * FreeLibrary32W (KERNEL.514)
347 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
352 ReleaseThunkLock( &mutex_count );
353 retv = FreeLibrary( (HMODULE)hLibModule );
354 RestoreThunkLock( mutex_count );
359 /**********************************************************************
362 static DWORD WOW_CallProc32W16( BOOL Ex )
364 DWORD nrofargs, argconvmask;
372 ReleaseThunkLock( &mutex_count );
374 VA_START16( valist );
375 nrofargs = VA_ARG16( valist, DWORD );
376 argconvmask = VA_ARG16( valist, DWORD );
377 proc32 = VA_ARG16( valist, FARPROC );
378 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
379 args = (DWORD*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*nrofargs );
380 if(args == NULL) proc32 = NULL; /* maybe we should WARN here? */
381 /* CallProcEx doesn't need its args reversed */
382 for (i=0;i<nrofargs;i++) {
386 aix = nrofargs - i - 1;
388 if (argconvmask & (1<<i))
390 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
391 if (args) args[aix] = (DWORD)MapSL(ptr);
392 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,MapSL(ptr));
396 DWORD arg = VA_ARG16( valist, DWORD );
397 if (args) args[aix] = arg;
398 if (TRACE_ON(thunk)) DPRINTF("%ld,", arg);
401 if (TRACE_ON(thunk)) DPRINTF("])\n");
405 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
406 * 32-bit CDECL routine ...
409 if (!proc32) ret = 0;
410 else switch (nrofargs)
412 case 0: ret = proc32();
414 case 1: ret = proc32(args[0]);
416 case 2: ret = proc32(args[0],args[1]);
418 case 3: ret = proc32(args[0],args[1],args[2]);
420 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
422 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
424 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
426 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
428 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
430 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
432 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
434 case 11:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
436 case 12:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
438 case 13:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
441 /* FIXME: should go up to 32 arguments */
442 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
447 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
448 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
450 TRACE("returns %08lx\n",ret);
451 HeapFree( GetProcessHeap(), 0, args );
453 RestoreThunkLock( mutex_count );
457 /**********************************************************************
458 * CallProc32W (KERNEL.517)
460 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
461 * DWORD fAddressConvert, DWORD cParams );
463 DWORD WINAPI CallProc32W16( void )
465 return WOW_CallProc32W16( FALSE );
468 /**********************************************************************
469 * _CallProcEx32W (KERNEL.518)
471 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
472 * DWORD lpProcAddress, DWORD p1, ... );
474 DWORD WINAPI CallProcEx32W16( void )
476 return WOW_CallProc32W16( TRUE );
480 /**********************************************************************
481 * WOW16Call (KERNEL.500)
486 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
491 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
494 for (i=0;i<x/2;i++) {
495 WORD a = VA_ARG16(args,WORD);
498 calladdr = VA_ARG16(args,DWORD);
500 stack16_pop( x + sizeof(DWORD) );
501 DPRINTF(") calling address was 0x%08lx\n",calladdr);