2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
12 #include "stackframe.h"
13 #include "builtin16.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(thunk);
19 * These are the 16-bit side WOW routines. They reside in wownt16.h
20 * in the SDK; since we don't support Win16 source code anyway, I've
21 * placed them here for compilation with Wine ...
24 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
26 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
27 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
28 DWORD WINAPI FreeLibrary32W16(DWORD);
30 #define CPEX_DEST_STDCALL 0x00000000L
31 #define CPEX_DEST_CDECL 0x80000000L
33 DWORD WINAPI CallProcExW16(VOID);
34 DWORD WINAPI CallProcEx32W16(VOID);
37 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
40 /**********************************************************************
41 * K32WOWGetDescriptor (KERNEL32.70)
43 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
45 return GetThreadSelectorEntry( GetCurrentThread(),
46 segptr >> 16, ldtent );
49 /**********************************************************************
50 * K32WOWGetVDMPointer (KERNEL32.56)
52 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
54 /* FIXME: add size check too */
59 return DOSMEM_MapRealToLinear( vp );
62 /**********************************************************************
63 * K32WOWGetVDMPointerFix (KERNEL32.68)
65 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
68 * Hmmm. According to the docu, we should call:
70 * GlobalFix16( SELECTOROF(vp) );
72 * But this is unnecessary under Wine, as we never move global
73 * memory segments in linear memory anyway.
75 * (I'm not so sure what we are *supposed* to do if
76 * fProtectedMode is TRUE, anyway ...)
79 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
82 /**********************************************************************
83 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
85 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
88 * See above why we don't call:
90 * GlobalUnfix16( SELECTOROF(vp) );
95 /**********************************************************************
96 * K32WOWGlobalAlloc16 (KERNEL32.59)
98 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
100 return (WORD)GlobalAlloc16( wFlags, cb );
103 /**********************************************************************
104 * K32WOWGlobalFree16 (KERNEL32.62)
106 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
108 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
111 /**********************************************************************
112 * K32WOWGlobalUnlock16 (KERNEL32.61)
114 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
116 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
119 /**********************************************************************
120 * K32WOWGlobalAllocLock16 (KERNEL32.63)
122 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
124 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
125 if (phMem) *phMem = hMem;
127 return K32WOWGlobalLock16( hMem );
130 /**********************************************************************
131 * K32WOWGlobalLockSize16 (KERNEL32.65)
133 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
136 *pcb = GlobalSize16( (HGLOBAL16)hMem );
138 return K32WOWGlobalLock16( hMem );
141 /**********************************************************************
142 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
144 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
146 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
149 return K32WOWGlobalFree16( HIWORD(vpMem) );
153 /**********************************************************************
154 * K32WOWYield16 (KERNEL32.66)
156 VOID WINAPI K32WOWYield16( void )
159 * This does the right thing for both Win16 and Win32 tasks.
160 * More or less, at least :-/
165 /**********************************************************************
166 * K32WOWDirectedYield16 (KERNEL32.67)
168 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
171 * Argh. Our scheduler doesn't like DirectedYield by Win32
172 * tasks at all. So we do hope that this routine is indeed
173 * only ever called by Win16 tasks that have thunked up ...
175 DirectedYield16( (HTASK16)htask16 );
179 /***********************************************************************
180 * K32WOWHandle32 (KERNEL32.57)
182 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
192 case WOW_TYPE_HMETAFILE:
194 case WOW_TYPE_HBITMAP:
195 case WOW_TYPE_HBRUSH:
196 case WOW_TYPE_HPALETTE:
198 case WOW_TYPE_HACCEL:
200 case WOW_TYPE_FULLHWND:
201 return (HANDLE)handle;
204 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
205 return (HANDLE)handle;
209 /***********************************************************************
210 * K32WOWHandle16 (KERNEL32.58)
212 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
214 if ( HIWORD(handle ) )
215 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
225 case WOW_TYPE_HMETAFILE:
227 case WOW_TYPE_HBITMAP:
228 case WOW_TYPE_HBRUSH:
229 case WOW_TYPE_HPALETTE:
231 case WOW_TYPE_HACCEL:
233 case WOW_TYPE_FULLHWND:
234 return LOWORD(handle);
237 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
238 return LOWORD(handle);
242 /**********************************************************************
243 * K32WOWCallback16Ex (KERNEL32.55)
245 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
246 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
251 * Arguments must be prepared in the correct order by the caller
252 * (both for PASCAL and CDECL calling convention), so we simply
253 * copy them to the 16-bit stack ...
255 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
259 * Actually, we should take care whether the called routine cleans up
260 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
261 * the callee to do so; after the routine has returned, the 16-bit
262 * stack pointer is always reset to the position it had before.
265 ret = wine_call_to_16_long( (FARPROC16)vpfn16, cbArgs );
270 return TRUE; /* success */
273 /**********************************************************************
274 * K32WOWCallback16 (KERNEL32.54)
276 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
280 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
281 sizeof(DWORD), &dwParam, &ret ) )
289 * 16-bit WOW routines (in KERNEL)
292 /**********************************************************************
293 * GetVDMPointer32W (KERNEL.516)
295 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
297 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
300 /***********************************************************************
301 * LoadLibraryEx32W (KERNEL.513)
303 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
306 DOS_FULL_NAME full_name;
309 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
310 a buildin module. This case is handled in MODULE_LoadLibraryExA */
312 if ( ! DIR_SearchPath ( NULL, lpszLibFile, ".DLL", &full_name, FALSE ) ) {
313 strcpy ( full_name.short_name, lpszLibFile );
316 ReleaseThunkLock( &mutex_count );
317 hModule = LoadLibraryExA( full_name.short_name, (HANDLE)hFile, dwFlags );
318 RestoreThunkLock( mutex_count );
319 return (DWORD)hModule;
322 /***********************************************************************
323 * GetProcAddress32W (KERNEL.515)
325 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
327 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
330 /***********************************************************************
331 * FreeLibrary32W (KERNEL.514)
333 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
338 ReleaseThunkLock( &mutex_count );
339 retv = FreeLibrary( (HMODULE)hLibModule );
340 RestoreThunkLock( mutex_count );
345 /**********************************************************************
348 static DWORD WOW_CallProc32W16( BOOL Ex )
350 DWORD nrofargs, argconvmask;
358 ReleaseThunkLock( &mutex_count );
360 VA_START16( valist );
361 nrofargs = VA_ARG16( valist, DWORD );
362 argconvmask = VA_ARG16( valist, DWORD );
363 proc32 = VA_ARG16( valist, FARPROC );
364 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
365 args = (DWORD*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD)*nrofargs );
366 if(args == NULL) proc32 = NULL; /* maybe we should WARN here? */
367 /* CallProcEx doesn't need its args reversed */
368 for (i=0;i<nrofargs;i++) {
372 aix = nrofargs - i - 1;
374 if (argconvmask & (1<<i))
376 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
377 if (args) args[aix] = (DWORD)MapSL(ptr);
378 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,MapSL(ptr));
382 DWORD arg = VA_ARG16( valist, DWORD );
383 if (args) args[aix] = arg;
384 if (TRACE_ON(thunk)) DPRINTF("%ld,", arg);
387 if (TRACE_ON(thunk)) DPRINTF("])\n");
391 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
392 * 32-bit CDECL routine ...
395 if (!proc32) ret = 0;
396 else switch (nrofargs)
398 case 0: ret = proc32();
400 case 1: ret = proc32(args[0]);
402 case 2: ret = proc32(args[0],args[1]);
404 case 3: ret = proc32(args[0],args[1],args[2]);
406 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
408 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
410 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
412 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
414 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
416 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
418 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
420 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]);
422 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]);
424 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]);
427 /* FIXME: should go up to 32 arguments */
428 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
433 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
434 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
436 TRACE("returns %08lx\n",ret);
437 HeapFree( GetProcessHeap(), 0, args );
439 RestoreThunkLock( mutex_count );
443 /**********************************************************************
444 * CallProc32W (KERNEL.517)
446 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
447 * DWORD fAddressConvert, DWORD cParams );
449 DWORD WINAPI CallProc32W16( void )
451 return WOW_CallProc32W16( FALSE );
454 /**********************************************************************
455 * _CallProcEx32W (KERNEL.518)
457 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
458 * DWORD lpProcAddress, DWORD p1, ... );
460 DWORD WINAPI CallProcEx32W16( void )
462 return WOW_CallProc32W16( TRUE );
466 /**********************************************************************
467 * WOW16Call (KERNEL.500)
472 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
477 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
480 for (i=0;i<x/2;i++) {
481 WORD a = VA_ARG16(args,WORD);
484 calladdr = VA_ARG16(args,DWORD);
486 stack16_pop( x + sizeof(DWORD) );
487 DPRINTF(") calling address was 0x%08lx\n",calladdr);