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
22 #include "wine/port.h"
24 #include "wine/winbase16.h"
31 #include "stackframe.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
37 * These are the 16-bit side WOW routines. They reside in wownt16.h
38 * in the SDK; since we don't support Win16 source code anyway, I've
39 * placed them here for compilation with Wine ...
42 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
44 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
45 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
46 DWORD WINAPI FreeLibrary32W16(DWORD);
48 #define CPEX_DEST_STDCALL 0x00000000L
49 #define CPEX_DEST_CDECL 0x80000000L
51 DWORD WINAPI CallProcExW16(VOID);
52 DWORD WINAPI CallProcEx32W16(VOID);
55 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
58 /**********************************************************************
59 * K32WOWGetDescriptor (KERNEL32.70)
61 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
63 return GetThreadSelectorEntry( GetCurrentThread(),
64 segptr >> 16, ldtent );
67 /**********************************************************************
68 * K32WOWGetVDMPointer (KERNEL32.56)
70 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
72 /* FIXME: add size check too */
77 return DOSMEM_MapRealToLinear( vp );
80 /**********************************************************************
81 * K32WOWGetVDMPointerFix (KERNEL32.68)
83 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
86 * Hmmm. According to the docu, we should call:
88 * GlobalFix16( SELECTOROF(vp) );
90 * But this is unnecessary under Wine, as we never move global
91 * memory segments in linear memory anyway.
93 * (I'm not so sure what we are *supposed* to do if
94 * fProtectedMode is TRUE, anyway ...)
97 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
100 /**********************************************************************
101 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
103 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
106 * See above why we don't call:
108 * GlobalUnfix16( SELECTOROF(vp) );
113 /**********************************************************************
114 * K32WOWGlobalAlloc16 (KERNEL32.59)
116 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
118 return (WORD)GlobalAlloc16( wFlags, cb );
121 /**********************************************************************
122 * K32WOWGlobalFree16 (KERNEL32.62)
124 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
126 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
129 /**********************************************************************
130 * K32WOWGlobalUnlock16 (KERNEL32.61)
132 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
134 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
137 /**********************************************************************
138 * K32WOWGlobalAllocLock16 (KERNEL32.63)
140 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
142 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
143 if (phMem) *phMem = hMem;
145 return K32WOWGlobalLock16( hMem );
148 /**********************************************************************
149 * K32WOWGlobalLockSize16 (KERNEL32.65)
151 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
154 *pcb = GlobalSize16( (HGLOBAL16)hMem );
156 return K32WOWGlobalLock16( hMem );
159 /**********************************************************************
160 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
162 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
164 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
167 return K32WOWGlobalFree16( HIWORD(vpMem) );
171 /**********************************************************************
172 * K32WOWYield16 (KERNEL32.66)
174 VOID WINAPI K32WOWYield16( void )
177 * This does the right thing for both Win16 and Win32 tasks.
178 * More or less, at least :-/
183 /**********************************************************************
184 * K32WOWDirectedYield16 (KERNEL32.67)
186 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
189 * Argh. Our scheduler doesn't like DirectedYield by Win32
190 * tasks at all. So we do hope that this routine is indeed
191 * only ever called by Win16 tasks that have thunked up ...
193 DirectedYield16( (HTASK16)htask16 );
197 /***********************************************************************
198 * K32WOWHandle32 (KERNEL32.57)
200 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
210 case WOW_TYPE_HMETAFILE:
212 case WOW_TYPE_HBITMAP:
213 case WOW_TYPE_HBRUSH:
214 case WOW_TYPE_HPALETTE:
216 case WOW_TYPE_HACCEL:
218 case WOW_TYPE_FULLHWND:
219 return (HANDLE)(ULONG_PTR)handle;
222 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
223 return (HANDLE)(ULONG_PTR)handle;
227 /***********************************************************************
228 * K32WOWHandle16 (KERNEL32.58)
230 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
232 if ( HIWORD(handle ) )
233 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
243 case WOW_TYPE_HMETAFILE:
245 case WOW_TYPE_HBITMAP:
246 case WOW_TYPE_HBRUSH:
247 case WOW_TYPE_HPALETTE:
249 case WOW_TYPE_HACCEL:
251 case WOW_TYPE_FULLHWND:
252 return LOWORD(handle);
255 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
256 return LOWORD(handle);
260 /**********************************************************************
261 * K32WOWCallback16Ex (KERNEL32.55)
263 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
264 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
269 * Arguments must be prepared in the correct order by the caller
270 * (both for PASCAL and CDECL calling convention), so we simply
271 * copy them to the 16-bit stack ...
273 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
277 * Actually, we should take care whether the called routine cleans up
278 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
279 * the callee to do so; after the routine has returned, the 16-bit
280 * stack pointer is always reset to the position it had before.
283 ret = wine_call_to_16_long( (FARPROC16)vpfn16, cbArgs );
288 return TRUE; /* success */
291 /**********************************************************************
292 * K32WOWCallback16 (KERNEL32.54)
294 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
298 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
299 sizeof(DWORD), &dwParam, &ret ) )
307 * 16-bit WOW routines (in KERNEL)
310 /**********************************************************************
311 * GetVDMPointer32W (KERNEL.516)
313 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
315 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
316 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
319 /***********************************************************************
320 * LoadLibraryEx32W (KERNEL.513)
322 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
325 DOS_FULL_NAME full_name;
327 UNICODE_STRING libfileW;
329 static const WCHAR dllW[] = {'.','D','L','L',0};
333 SetLastError(ERROR_INVALID_PARAMETER);
337 if (!RtlCreateUnicodeStringFromAsciiz(&libfileW, lpszLibFile))
339 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
343 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
344 a buildin module. This case is handled in MODULE_LoadLibraryExA */
346 filenameW = libfileW.Buffer;
347 if ( DIR_SearchPath( NULL, filenameW, dllW, &full_name, FALSE ) )
348 filenameW = full_name.short_name;
350 ReleaseThunkLock( &mutex_count );
351 hModule = LoadLibraryExW( filenameW, (HANDLE)hFile, dwFlags );
352 RestoreThunkLock( mutex_count );
354 RtlFreeUnicodeString(&libfileW);
356 return (DWORD)hModule;
359 /***********************************************************************
360 * GetProcAddress32W (KERNEL.515)
362 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
364 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
367 /***********************************************************************
368 * FreeLibrary32W (KERNEL.514)
370 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
375 ReleaseThunkLock( &mutex_count );
376 retv = FreeLibrary( (HMODULE)hLibModule );
377 RestoreThunkLock( mutex_count );
382 /**********************************************************************
385 static DWORD WOW_CallProc32W16( BOOL Ex )
387 DWORD nrofargs, argconvmask;
395 ReleaseThunkLock( &mutex_count );
397 VA_START16( valist );
398 nrofargs = VA_ARG16( valist, DWORD );
399 argconvmask = VA_ARG16( valist, DWORD );
400 proc32 = VA_ARG16( valist, FARPROC );
401 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
402 args = (DWORD*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*nrofargs );
403 if(args == NULL) proc32 = NULL; /* maybe we should WARN here? */
404 /* CallProcEx doesn't need its args reversed */
405 for (i=0;i<nrofargs;i++) {
409 aix = nrofargs - i - 1;
411 if (argconvmask & (1<<i))
413 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
414 if (args) args[aix] = (DWORD)MapSL(ptr);
415 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,MapSL(ptr));
419 DWORD arg = VA_ARG16( valist, DWORD );
420 if (args) args[aix] = arg;
421 if (TRACE_ON(thunk)) DPRINTF("%ld,", arg);
424 if (TRACE_ON(thunk)) DPRINTF("])\n");
428 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
429 * 32-bit CDECL routine ...
432 if (!proc32) ret = 0;
433 else switch (nrofargs)
435 case 0: ret = proc32();
437 case 1: ret = proc32(args[0]);
439 case 2: ret = proc32(args[0],args[1]);
441 case 3: ret = proc32(args[0],args[1],args[2]);
443 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
445 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
447 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
449 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
451 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
453 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
455 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
457 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]);
459 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]);
461 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]);
464 /* FIXME: should go up to 32 arguments */
465 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
470 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
471 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
473 TRACE("returns %08lx\n",ret);
474 HeapFree( GetProcessHeap(), 0, args );
476 RestoreThunkLock( mutex_count );
480 /**********************************************************************
481 * CallProc32W (KERNEL.517)
483 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
484 * DWORD fAddressConvert, DWORD cParams );
486 DWORD WINAPI CallProc32W16( void )
488 return WOW_CallProc32W16( FALSE );
491 /**********************************************************************
492 * _CallProcEx32W (KERNEL.518)
494 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
495 * DWORD lpProcAddress, DWORD p1, ... );
497 DWORD WINAPI CallProcEx32W16( void )
499 return WOW_CallProc32W16( TRUE );
503 /**********************************************************************
504 * WOW16Call (KERNEL.500)
509 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
514 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
517 for (i=0;i<x/2;i++) {
518 WORD a = VA_ARG16(args,WORD);
521 calladdr = VA_ARG16(args,DWORD);
523 stack16_pop( x + sizeof(DWORD) );
524 DPRINTF(") calling address was 0x%08lx\n",calladdr);