2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
13 #include "stackframe.h"
14 #include "builtin16.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(thunk);
20 * These are the 16-bit side WOW routines. They reside in wownt16.h
21 * in the SDK; since we don't support Win16 source code anyway, I've
22 * placed them here for compilation with Wine ...
25 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
27 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
28 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
29 DWORD WINAPI FreeLibrary32W16(DWORD);
31 #define CPEX_DEST_STDCALL 0x00000000L
32 #define CPEX_DEST_CDECL 0x80000000L
34 DWORD WINAPI CallProcExW16(VOID);
35 DWORD WINAPI CallProcEx32W16(VOID);
38 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
41 /**********************************************************************
42 * K32WOWGetDescriptor (KERNEL32.70)
44 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
46 return GetThreadSelectorEntry( GetCurrentThread(),
47 segptr >> 16, ldtent );
50 /**********************************************************************
51 * K32WOWGetVDMPointer (KERNEL32.56)
53 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
55 /* FIXME: add size check too */
60 return DOSMEM_MapRealToLinear( vp );
63 /**********************************************************************
64 * K32WOWGetVDMPointerFix (KERNEL32.68)
66 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
69 * Hmmm. According to the docu, we should call:
71 * GlobalFix16( SELECTOROF(vp) );
73 * But this is unnecessary under Wine, as we never move global
74 * memory segments in linear memory anyway.
76 * (I'm not so sure what we are *supposed* to do if
77 * fProtectedMode is TRUE, anyway ...)
80 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
83 /**********************************************************************
84 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
86 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
89 * See above why we don't call:
91 * GlobalUnfix16( SELECTOROF(vp) );
96 /**********************************************************************
97 * K32WOWGlobalAlloc16 (KERNEL32.59)
99 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
101 return (WORD)GlobalAlloc16( wFlags, cb );
104 /**********************************************************************
105 * K32WOWGlobalFree16 (KERNEL32.62)
107 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
109 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
112 /**********************************************************************
113 * K32WOWGlobalUnlock16 (KERNEL32.61)
115 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
117 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
120 /**********************************************************************
121 * K32WOWGlobalAllocLock16 (KERNEL32.63)
123 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
125 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
126 if (phMem) *phMem = hMem;
128 return K32WOWGlobalLock16( hMem );
131 /**********************************************************************
132 * K32WOWGlobalLockSize16 (KERNEL32.65)
134 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
137 *pcb = GlobalSize16( (HGLOBAL16)hMem );
139 return K32WOWGlobalLock16( hMem );
142 /**********************************************************************
143 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
145 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
147 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
150 return K32WOWGlobalFree16( HIWORD(vpMem) );
154 /**********************************************************************
155 * K32WOWYield16 (KERNEL32.66)
157 VOID WINAPI K32WOWYield16( void )
160 * This does the right thing for both Win16 and Win32 tasks.
161 * More or less, at least :-/
166 /**********************************************************************
167 * K32WOWDirectedYield16 (KERNEL32.67)
169 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
172 * Argh. Our scheduler doesn't like DirectedYield by Win32
173 * tasks at all. So we do hope that this routine is indeed
174 * only ever called by Win16 tasks that have thunked up ...
176 DirectedYield16( (HTASK16)htask16 );
180 /***********************************************************************
181 * K32WOWHandle32 (KERNEL32.57)
183 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
193 case WOW_TYPE_HMETAFILE:
195 case WOW_TYPE_HBITMAP:
196 case WOW_TYPE_HBRUSH:
197 case WOW_TYPE_HPALETTE:
199 case WOW_TYPE_HACCEL:
201 case WOW_TYPE_FULLHWND:
202 return (HANDLE)handle;
205 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
206 return (HANDLE)handle;
210 /***********************************************************************
211 * K32WOWHandle16 (KERNEL32.58)
213 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
215 if ( HIWORD(handle ) )
216 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
226 case WOW_TYPE_HMETAFILE:
228 case WOW_TYPE_HBITMAP:
229 case WOW_TYPE_HBRUSH:
230 case WOW_TYPE_HPALETTE:
232 case WOW_TYPE_HACCEL:
234 case WOW_TYPE_FULLHWND:
235 return LOWORD(handle);
238 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
239 return LOWORD(handle);
243 /**********************************************************************
244 * K32WOWCallback16Ex (KERNEL32.55)
246 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
247 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
252 * Arguments must be prepared in the correct order by the caller
253 * (both for PASCAL and CDECL calling convention), so we simply
254 * copy them to the 16-bit stack ...
256 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
260 * Actually, we should take care whether the called routine cleans up
261 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
262 * the callee to do so; after the routine has returned, the 16-bit
263 * stack pointer is always reset to the position it had before.
266 ret = wine_call_to_16_long( (FARPROC16)vpfn16, cbArgs );
271 return TRUE; /* success */
274 /**********************************************************************
275 * K32WOWCallback16 (KERNEL32.54)
277 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
281 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
282 sizeof(DWORD), &dwParam, &ret ) )
290 * 16-bit WOW routines (in KERNEL)
293 /**********************************************************************
294 * GetVDMPointer32W (KERNEL.516)
296 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
298 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
301 /***********************************************************************
302 * LoadLibraryEx32W (KERNEL.513)
304 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
307 DOS_FULL_NAME full_name;
310 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
311 a buildin module. This case is handled in MODULE_LoadLibraryExA */
313 if ( ! DIR_SearchPath ( NULL, lpszLibFile, ".DLL", &full_name, FALSE ) ) {
314 strcpy ( full_name.short_name, lpszLibFile );
317 ReleaseThunkLock( &mutex_count );
318 hModule = LoadLibraryExA( full_name.short_name, (HANDLE)hFile, dwFlags );
319 RestoreThunkLock( mutex_count );
320 return (DWORD)hModule;
323 /***********************************************************************
324 * GetProcAddress32W (KERNEL.515)
326 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
328 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
331 /***********************************************************************
332 * FreeLibrary32W (KERNEL.514)
334 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
339 ReleaseThunkLock( &mutex_count );
340 retv = FreeLibrary( (HMODULE)hLibModule );
341 RestoreThunkLock( mutex_count );
346 /**********************************************************************
349 static DWORD WOW_CallProc32W16( BOOL Ex )
351 DWORD nrofargs, argconvmask;
359 ReleaseThunkLock( &mutex_count );
361 VA_START16( valist );
362 nrofargs = VA_ARG16( valist, DWORD );
363 argconvmask = VA_ARG16( valist, DWORD );
364 proc32 = VA_ARG16( valist, FARPROC );
365 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
366 args = (DWORD*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*nrofargs );
367 if(args == NULL) proc32 = NULL; /* maybe we should WARN here? */
368 /* CallProcEx doesn't need its args reversed */
369 for (i=0;i<nrofargs;i++) {
373 aix = nrofargs - i - 1;
375 if (argconvmask & (1<<i))
377 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
378 if (args) args[aix] = (DWORD)MapSL(ptr);
379 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,MapSL(ptr));
383 DWORD arg = VA_ARG16( valist, DWORD );
384 if (args) args[aix] = arg;
385 if (TRACE_ON(thunk)) DPRINTF("%ld,", arg);
388 if (TRACE_ON(thunk)) DPRINTF("])\n");
392 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
393 * 32-bit CDECL routine ...
396 if (!proc32) ret = 0;
397 else switch (nrofargs)
399 case 0: ret = proc32();
401 case 1: ret = proc32(args[0]);
403 case 2: ret = proc32(args[0],args[1]);
405 case 3: ret = proc32(args[0],args[1],args[2]);
407 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
409 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
411 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
413 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
415 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
417 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
419 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
421 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]);
423 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]);
425 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]);
428 /* FIXME: should go up to 32 arguments */
429 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
434 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
435 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
437 TRACE("returns %08lx\n",ret);
438 HeapFree( GetProcessHeap(), 0, args );
440 RestoreThunkLock( mutex_count );
444 /**********************************************************************
445 * CallProc32W (KERNEL.517)
447 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
448 * DWORD fAddressConvert, DWORD cParams );
450 DWORD WINAPI CallProc32W16( void )
452 return WOW_CallProc32W16( FALSE );
455 /**********************************************************************
456 * _CallProcEx32W (KERNEL.518)
458 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
459 * DWORD lpProcAddress, DWORD p1, ... );
461 DWORD WINAPI CallProcEx32W16( void )
463 return WOW_CallProc32W16( TRUE );
467 /**********************************************************************
468 * WOW16Call (KERNEL.500)
473 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
478 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
481 for (i=0;i<x/2;i++) {
482 WORD a = VA_ARG16(args,WORD);
485 calladdr = VA_ARG16(args,DWORD);
487 stack16_pop( x + sizeof(DWORD) );
488 DPRINTF(") calling address was 0x%08lx\n",calladdr);