2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
12 #include "stackframe.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(thunk);
18 * These are the 16-bit side WOW routines. They reside in wownt16.h
19 * in the SDK; since we don't support Win16 source code anyway, I've
20 * placed them here for compilation with Wine ...
23 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
25 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
26 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
27 DWORD WINAPI FreeLibrary32W16(DWORD);
29 #define CPEX_DEST_STDCALL 0x00000000L
30 #define CPEX_DEST_CDECL 0x80000000L
32 DWORD WINAPI CallProcExW16(VOID);
33 DWORD WINAPI CallProcEx32W16(VOID);
36 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
39 /**********************************************************************
40 * K32WOWGetDescriptor (KERNEL32.70)
42 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
44 return GetThreadSelectorEntry( GetCurrentThread(),
45 segptr >> 16, ldtent );
48 /**********************************************************************
49 * K32WOWGetVDMPointer (KERNEL32.56)
51 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
53 /* FIXME: add size check too */
58 return DOSMEM_MapRealToLinear( vp );
61 /**********************************************************************
62 * K32WOWGetVDMPointerFix (KERNEL32.68)
64 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
67 * Hmmm. According to the docu, we should call:
69 * GlobalFix16( SELECTOROF(vp) );
71 * But this is unnecessary under Wine, as we never move global
72 * memory segments in linear memory anyway.
74 * (I'm not so sure what we are *supposed* to do if
75 * fProtectedMode is TRUE, anyway ...)
78 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
81 /**********************************************************************
82 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
84 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
87 * See above why we don't call:
89 * GlobalUnfix16( SELECTOROF(vp) );
94 /**********************************************************************
95 * K32WOWGlobalAlloc16 (KERNEL32.59)
97 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
99 return (WORD)GlobalAlloc16( wFlags, cb );
102 /**********************************************************************
103 * K32WOWGlobalFree16 (KERNEL32.62)
105 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
107 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
110 /**********************************************************************
111 * K32WOWGlobalUnlock16 (KERNEL32.61)
113 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
115 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
118 /**********************************************************************
119 * K32WOWGlobalAllocLock16 (KERNEL32.63)
121 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
123 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
124 if (phMem) *phMem = hMem;
126 return K32WOWGlobalLock16( hMem );
129 /**********************************************************************
130 * K32WOWGlobalLockSize16 (KERNEL32.65)
132 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
135 *pcb = GlobalSize16( (HGLOBAL16)hMem );
137 return K32WOWGlobalLock16( hMem );
140 /**********************************************************************
141 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
143 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
145 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
148 return K32WOWGlobalFree16( HIWORD(vpMem) );
152 /**********************************************************************
153 * K32WOWYield16 (KERNEL32.66)
155 VOID WINAPI K32WOWYield16( void )
158 * This does the right thing for both Win16 and Win32 tasks.
159 * More or less, at least :-/
164 /**********************************************************************
165 * K32WOWDirectedYield16 (KERNEL32.67)
167 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
170 * Argh. Our scheduler doesn't like DirectedYield by Win32
171 * tasks at all. So we do hope that this routine is indeed
172 * only ever called by Win16 tasks that have thunked up ...
174 DirectedYield16( (HTASK16)htask16 );
178 /***********************************************************************
179 * K32WOWHandle32 (KERNEL32.57)
181 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
191 case WOW_TYPE_HMETAFILE:
193 case WOW_TYPE_HBITMAP:
194 case WOW_TYPE_HBRUSH:
195 case WOW_TYPE_HPALETTE:
197 case WOW_TYPE_HACCEL:
199 case WOW_TYPE_FULLHWND:
200 return (HANDLE)handle;
203 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
204 return (HANDLE)handle;
208 /***********************************************************************
209 * K32WOWHandle16 (KERNEL32.58)
211 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
213 if ( HIWORD(handle ) )
214 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );
224 case WOW_TYPE_HMETAFILE:
226 case WOW_TYPE_HBITMAP:
227 case WOW_TYPE_HBRUSH:
228 case WOW_TYPE_HPALETTE:
230 case WOW_TYPE_HACCEL:
232 case WOW_TYPE_FULLHWND:
233 return LOWORD(handle);
236 ERR( "handle 0x%08x of unknown type %d\n", handle, type );
237 return LOWORD(handle);
241 /**********************************************************************
242 * K32WOWCallback16Ex (KERNEL32.55)
244 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
245 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
250 * Arguments must be prepared in the correct order by the caller
251 * (both for PASCAL and CDECL calling convention), so we simply
252 * copy them to the 16-bit stack ...
254 memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );
258 * Actually, we should take care whether the called routine cleans up
259 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
260 * the callee to do so; after the routine has returned, the 16-bit
261 * stack pointer is always reset to the position it had before.
264 ret = wine_call_to_16_long( (FARPROC16)vpfn16, cbArgs );
269 return TRUE; /* success */
272 /**********************************************************************
273 * K32WOWCallback16 (KERNEL32.54)
275 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
279 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
280 sizeof(DWORD), &dwParam, &ret ) )
288 * 16-bit WOW routines (in KERNEL)
291 /**********************************************************************
292 * GetVDMPointer32W (KERNEL.516)
294 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
296 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
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);