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"
32 #include "stackframe.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
38 * These are the 16-bit side WOW routines. They reside in wownt16.h
39 * in the SDK; since we don't support Win16 source code anyway, I've
40 * placed them here for compilation with Wine ...
43 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
45 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
46 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
47 DWORD WINAPI FreeLibrary32W16(DWORD);
49 #define CPEX_DEST_STDCALL 0x00000000L
50 #define CPEX_DEST_CDECL 0x80000000L
52 DWORD WINAPI CallProcExW16(VOID);
53 DWORD WINAPI CallProcEx32W16(VOID);
56 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
59 /**********************************************************************
60 * K32WOWGetDescriptor (KERNEL32.70)
62 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
64 return GetThreadSelectorEntry( GetCurrentThread(),
65 segptr >> 16, ldtent );
68 /**********************************************************************
69 * K32WOWGetVDMPointer (KERNEL32.56)
71 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
73 /* FIXME: add size check too */
78 return DOSMEM_MapRealToLinear( vp );
81 /**********************************************************************
82 * K32WOWGetVDMPointerFix (KERNEL32.68)
84 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
87 * Hmmm. According to the docu, we should call:
89 * GlobalFix16( SELECTOROF(vp) );
91 * But this is unnecessary under Wine, as we never move global
92 * memory segments in linear memory anyway.
94 * (I'm not so sure what we are *supposed* to do if
95 * fProtectedMode is TRUE, anyway ...)
98 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
101 /**********************************************************************
102 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
104 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
107 * See above why we don't call:
109 * GlobalUnfix16( SELECTOROF(vp) );
114 /**********************************************************************
115 * K32WOWGlobalAlloc16 (KERNEL32.59)
117 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
119 return (WORD)GlobalAlloc16( wFlags, cb );
122 /**********************************************************************
123 * K32WOWGlobalFree16 (KERNEL32.62)
125 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
127 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
130 /**********************************************************************
131 * K32WOWGlobalUnlock16 (KERNEL32.61)
133 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
135 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
138 /**********************************************************************
139 * K32WOWGlobalAllocLock16 (KERNEL32.63)
141 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
143 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
144 if (phMem) *phMem = hMem;
146 return K32WOWGlobalLock16( hMem );
149 /**********************************************************************
150 * K32WOWGlobalLockSize16 (KERNEL32.65)
152 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
155 *pcb = GlobalSize16( (HGLOBAL16)hMem );
157 return K32WOWGlobalLock16( hMem );
160 /**********************************************************************
161 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
163 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
165 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
168 return K32WOWGlobalFree16( HIWORD(vpMem) );
172 /**********************************************************************
173 * K32WOWYield16 (KERNEL32.66)
175 VOID WINAPI K32WOWYield16( void )
178 * This does the right thing for both Win16 and Win32 tasks.
179 * More or less, at least :-/
184 /**********************************************************************
185 * K32WOWDirectedYield16 (KERNEL32.67)
187 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
190 * Argh. Our scheduler doesn't like DirectedYield by Win32
191 * tasks at all. So we do hope that this routine is indeed
192 * only ever called by Win16 tasks that have thunked up ...
194 DirectedYield16( (HTASK16)htask16 );
198 /***********************************************************************
199 * K32WOWHandle32 (KERNEL32.57)
201 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
211 case WOW_TYPE_HMETAFILE:
213 case WOW_TYPE_HBITMAP:
214 case WOW_TYPE_HBRUSH:
215 case WOW_TYPE_HPALETTE:
217 case WOW_TYPE_HACCEL:
218 case WOW_TYPE_FULLHWND:
219 return (HANDLE)(ULONG_PTR)handle;
222 return (HANDLE)TASK_GetPtr(handle)->teb->tid;
225 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
226 return (HANDLE)(ULONG_PTR)handle;
230 /***********************************************************************
231 * K32WOWHandle16 (KERNEL32.58)
233 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
235 if ( HIWORD(handle ) )
236 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
246 case WOW_TYPE_HMETAFILE:
248 case WOW_TYPE_HBITMAP:
249 case WOW_TYPE_HBRUSH:
250 case WOW_TYPE_HPALETTE:
252 case WOW_TYPE_HACCEL:
253 case WOW_TYPE_FULLHWND:
254 return LOWORD(handle);
257 return THREAD_IdToTEB((DWORD)handle)->htask16;
260 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
261 return LOWORD(handle);
265 /**********************************************************************
266 * K32WOWCallback16Ex (KERNEL32.55)
268 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
269 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
274 * Arguments must be prepared in the correct order by the caller
275 * (both for PASCAL and CDECL calling convention), so we simply
276 * copy them to the 16-bit stack ...
278 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
282 * Actually, we should take care whether the called routine cleans up
283 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
284 * the callee to do so; after the routine has returned, the 16-bit
285 * stack pointer is always reset to the position it had before.
288 ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs );
293 return TRUE; /* success */
296 /**********************************************************************
297 * K32WOWCallback16 (KERNEL32.54)
299 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
303 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
304 sizeof(DWORD), &dwParam, &ret ) )
312 * 16-bit WOW routines (in KERNEL)
315 /**********************************************************************
316 * GetVDMPointer32W (KERNEL.516)
318 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
320 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
321 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
324 /***********************************************************************
325 * LoadLibraryEx32W (KERNEL.513)
327 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
330 DOS_FULL_NAME full_name;
332 UNICODE_STRING libfileW;
334 static const WCHAR dllW[] = {'.','D','L','L',0};
338 SetLastError(ERROR_INVALID_PARAMETER);
342 if (!RtlCreateUnicodeStringFromAsciiz(&libfileW, lpszLibFile))
344 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
348 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
349 a buildin module. This case is handled in MODULE_LoadLibraryExA */
351 filenameW = libfileW.Buffer;
352 if ( DIR_SearchPath( NULL, filenameW, dllW, &full_name, FALSE ) )
353 filenameW = full_name.short_name;
355 ReleaseThunkLock( &mutex_count );
356 hModule = LoadLibraryExW( filenameW, (HANDLE)hFile, dwFlags );
357 RestoreThunkLock( mutex_count );
359 RtlFreeUnicodeString(&libfileW);
361 return (DWORD)hModule;
364 /***********************************************************************
365 * GetProcAddress32W (KERNEL.515)
367 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
369 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
372 /***********************************************************************
373 * FreeLibrary32W (KERNEL.514)
375 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
380 ReleaseThunkLock( &mutex_count );
381 retv = FreeLibrary( (HMODULE)hLibModule );
382 RestoreThunkLock( mutex_count );
387 /**********************************************************************
390 static DWORD WOW_CallProc32W16( BOOL Ex )
392 DWORD nrofargs, argconvmask;
400 ReleaseThunkLock( &mutex_count );
402 VA_START16( valist );
403 nrofargs = VA_ARG16( valist, DWORD );
404 argconvmask = VA_ARG16( valist, DWORD );
405 proc32 = VA_ARG16( valist, FARPROC );
406 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
407 args = (DWORD*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*nrofargs );
408 if(args == NULL) proc32 = NULL; /* maybe we should WARN here? */
409 /* CallProcEx doesn't need its args reversed */
410 for (i=0;i<nrofargs;i++) {
414 aix = nrofargs - i - 1;
416 if (argconvmask & (1<<i))
418 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
419 if (args) args[aix] = (DWORD)MapSL(ptr);
420 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,MapSL(ptr));
424 DWORD arg = VA_ARG16( valist, DWORD );
425 if (args) args[aix] = arg;
426 if (TRACE_ON(thunk)) DPRINTF("%ld,", arg);
429 if (TRACE_ON(thunk)) DPRINTF("])\n");
433 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
434 * 32-bit CDECL routine ...
437 if (!proc32) ret = 0;
438 else switch (nrofargs)
440 case 0: ret = proc32();
442 case 1: ret = proc32(args[0]);
444 case 2: ret = proc32(args[0],args[1]);
446 case 3: ret = proc32(args[0],args[1],args[2]);
448 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
450 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
452 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
454 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
456 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
458 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
460 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
462 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]);
464 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]);
466 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]);
469 /* FIXME: should go up to 32 arguments */
470 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
475 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
476 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
478 TRACE("returns %08lx\n",ret);
479 HeapFree( GetProcessHeap(), 0, args );
481 RestoreThunkLock( mutex_count );
485 /**********************************************************************
486 * CallProc32W (KERNEL.517)
488 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
489 * DWORD fAddressConvert, DWORD cParams );
491 DWORD WINAPI CallProc32W16( void )
493 return WOW_CallProc32W16( FALSE );
496 /**********************************************************************
497 * _CallProcEx32W (KERNEL.518)
499 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
500 * DWORD lpProcAddress, DWORD p1, ... );
502 DWORD WINAPI CallProcEx32W16( void )
504 return WOW_CallProc32W16( TRUE );
508 /**********************************************************************
509 * WOW16Call (KERNEL.500)
514 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
519 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
522 for (i=0;i<x/2;i++) {
523 WORD a = VA_ARG16(args,WORD);
526 calladdr = VA_ARG16(args,DWORD);
528 stack16_pop( x + sizeof(DWORD) );
529 DPRINTF(") calling address was 0x%08lx\n",calladdr);