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 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
23 /**********************************************************************
24 * WOWGetDescriptor (WOW32.1) (KERNEL32.70)
26 BOOL WINAPI
WOWGetDescriptor( SEGPTR segptr
, LPLDT_ENTRY ldtent
)
28 return GetThreadSelectorEntry( GetCurrentThread(),
29 segptr
>> 16, ldtent
);
32 /**********************************************************************
33 * WOWGetVDMPointer (WOW32.5) (KERNEL32.56)
35 LPVOID WINAPI
WOWGetVDMPointer( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
37 /* FIXME: add size check too */
40 return PTR_SEG_TO_LIN( vp
);
42 return DOSMEM_MapRealToLinear( vp
);
45 /**********************************************************************
46 * WOWGetVDMPointerFix (WOW32.6) (KERNEL32.68)
48 LPVOID WINAPI
WOWGetVDMPointerFix( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
51 * Hmmm. According to the docu, we should call:
53 * GlobalFix16( SELECTOROF(vp) );
55 * But this is unnecessary under Wine, as we never move global
56 * memory segments in linear memory anyway.
58 * (I'm not so sure what we are *supposed* to do if
59 * fProtectedMode is TRUE, anyway ...)
62 return WOWGetVDMPointer( vp
, dwBytes
, fProtectedMode
);
65 /**********************************************************************
66 * WOWGetVDMPointerUnFix (WOW32.7) (KERNEL32.69)
68 VOID WINAPI
WOWGetVDMPointerUnfix( DWORD vp
)
71 * See above why we don't call:
73 * GlobalUnfix16( SELECTOROF(vp) );
78 /**********************************************************************
79 * WOWGlobalAlloc16 (WOW32.8) (KERNEL32.59)
81 WORD WINAPI
WOWGlobalAlloc16( WORD wFlags
, DWORD cb
)
83 return (WORD
)GlobalAlloc16( wFlags
, cb
);
86 /**********************************************************************
87 * WOWGlobalFree16 (WOW32.10) (KERNEL32.62)
89 WORD WINAPI
WOWGlobalFree16( WORD hMem
)
91 return (WORD
)GlobalFree16( (HGLOBAL16
)hMem
);
94 /**********************************************************************
95 * WOWGlobalLock16 (WOW32.11) (KERNEL32.60)
97 DWORD WINAPI
WOWGlobalLock16( WORD hMem
)
99 return (DWORD
)WIN16_GlobalLock16( (HGLOBAL16
)hMem
);
102 /**********************************************************************
103 * WOWGlobalUnlock16 (WOW32.13) (KERNEL32.61)
105 BOOL WINAPI
WOWGlobalUnlock16( WORD hMem
)
107 return (BOOL
)GlobalUnlock16( (HGLOBAL16
)hMem
);
110 /**********************************************************************
111 * WOWGlobalAllocLock16 (WOW32.9) (KERNEL32.63)
113 DWORD WINAPI
WOWGlobalAllocLock16( WORD wFlags
, DWORD cb
, WORD
*phMem
)
115 WORD hMem
= WOWGlobalAlloc16( wFlags
, cb
);
116 if (phMem
) *phMem
= hMem
;
118 return WOWGlobalLock16( hMem
);
121 /**********************************************************************
122 * WOWGlobalLockSize16 (WOW32.12) (KERNEL32.65)
124 DWORD WINAPI
WOWGlobalLockSize16( WORD hMem
, PDWORD pcb
)
127 *pcb
= GlobalSize16( (HGLOBAL16
)hMem
);
129 return WOWGlobalLock16( hMem
);
132 /**********************************************************************
133 * WOWGlobalUnlockFree16 (WOW32.14) (KERNEL32.64)
135 WORD WINAPI
WOWGlobalUnlockFree16( DWORD vpMem
)
137 if ( !WOWGlobalUnlock16( HIWORD(vpMem
) ) )
140 return WOWGlobalFree16( HIWORD(vpMem
) );
144 /**********************************************************************
145 * WOWYield16 (WOW32.17) (KERNEL32.66)
147 VOID WINAPI
WOWYield16( void )
150 * This does the right thing for both Win16 and Win32 tasks.
151 * More or less, at least :-/
156 /**********************************************************************
157 * WOWDirectedYield16 (WOW32.4) (KERNEL32.67)
159 VOID WINAPI
WOWDirectedYield16( WORD htask16
)
162 * Argh. Our scheduler doesn't like DirectedYield by Win32
163 * tasks at all. So we do hope that this routine is indeed
164 * only ever called by Win16 tasks that have thunked up ...
166 DirectedYield16( (HTASK16
)htask16
);
170 /***********************************************************************
171 * WOWHandle32 (WOW32.16) (KERNEL32.57)
173 HANDLE WINAPI
WOWHandle32( WORD handle
, WOW_HANDLE_TYPE type
)
183 case WOW_TYPE_HMETAFILE
:
185 case WOW_TYPE_HBITMAP
:
186 case WOW_TYPE_HBRUSH
:
187 case WOW_TYPE_HPALETTE
:
189 case WOW_TYPE_HACCEL
:
191 case WOW_TYPE_FULLHWND
:
192 return (HANDLE
)handle
;
195 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
196 return (HANDLE
)handle
;
200 /***********************************************************************
201 * WOWHandle16 (WOW32.15) (KERNEL32.58)
203 WORD WINAPI
WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
205 if ( HIWORD(handle
) )
206 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle
, type
);
216 case WOW_TYPE_HMETAFILE
:
218 case WOW_TYPE_HBITMAP
:
219 case WOW_TYPE_HBRUSH
:
220 case WOW_TYPE_HPALETTE
:
222 case WOW_TYPE_HACCEL
:
224 case WOW_TYPE_FULLHWND
:
225 return LOWORD(handle
);
228 ERR( "handle 0x%08x of unknown type %d\n", handle
, type
);
229 return LOWORD(handle
);
233 /**********************************************************************
234 * WOWCallback16 (WOW32.2) (KERNEL32.54)
236 DWORD WINAPI
WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
240 if ( !WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
241 sizeof(DWORD
), &dwParam
, &ret
) )
247 /**********************************************************************
248 * WOWCallback16Ex (WOW32.3) (KERNEL32.55)
250 BOOL WINAPI
WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
251 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
256 * Arguments must be prepared in the correct order by the caller
257 * (both for PASCAL and CDECL calling convention), so we simply
258 * copy them to the 16-bit stack ...
260 memcpy( (LPBYTE
)CURRENT_STACK16
- cbArgs
, (LPBYTE
)pArgs
, cbArgs
);
264 * Actually, we should take care whether the called routine cleans up
265 * its stack or not. Fortunately, our CallTo16 core doesn't rely on
266 * the callee to do so; after the routine has returned, the 16-bit
267 * stack pointer is always reset to the position it had before.
270 ret
= CallTo16Long( (FARPROC16
)vpfn16
, cbArgs
);
275 return TRUE
; /* success */
281 * 16-bit WOW routines (in KERNEL)
284 /**********************************************************************
285 * GetVDMPointer32W16 (KERNEL.516)
287 DWORD WINAPI
GetVDMPointer32W16( SEGPTR vp
, UINT16 fMode
)
289 return (DWORD
)WOWGetVDMPointer( vp
, 0, (DWORD
)fMode
);
292 /***********************************************************************
293 * LoadLibraryEx32W16 (KERNEL.513)
295 DWORD WINAPI
LoadLibraryEx32W16( LPCSTR lpszLibFile
, DWORD hFile
, DWORD dwFlags
)
299 SYSLEVEL_ReleaseWin16Lock();
300 hModule
= LoadLibraryExA( lpszLibFile
, (HANDLE
)hFile
, dwFlags
);
301 SYSLEVEL_RestoreWin16Lock();
303 return (DWORD
)hModule
;
306 /***********************************************************************
307 * GetProcAddress32W16 (KERNEL.515)
309 DWORD WINAPI
GetProcAddress32W16( DWORD hModule
, LPCSTR lpszProc
)
311 return (DWORD
)GetProcAddress( (HMODULE
)hModule
, lpszProc
);
314 /***********************************************************************
315 * FreeLibrary32W16 (KERNEL.514)
317 DWORD WINAPI
FreeLibrary32W16( DWORD hLibModule
)
321 SYSLEVEL_ReleaseWin16Lock();
322 retv
= FreeLibrary( (HMODULE
)hLibModule
);
323 SYSLEVEL_RestoreWin16Lock();
329 /**********************************************************************
332 static DWORD
WOW_CallProc32W16( BOOL Ex
)
334 DWORD nrofargs
, argconvmask
;
341 SYSLEVEL_ReleaseWin16Lock();
343 VA_START16( valist
);
344 nrofargs
= VA_ARG16( valist
, DWORD
);
345 argconvmask
= VA_ARG16( valist
, DWORD
);
346 proc32
= VA_ARG16( valist
, FARPROC
);
347 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
348 args
= (DWORD
*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD
)*nrofargs
);
349 if(args
== NULL
) proc32
= NULL
; /* maybe we should WARN here? */
350 /* CallProcEx doesn't need its args reversed */
351 for (i
=0;i
<nrofargs
;i
++) {
355 aix
= nrofargs
- i
- 1;
357 if (argconvmask
& (1<<i
))
359 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
360 if (args
) args
[aix
] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
361 if (TRACE_ON(thunk
)) DPRINTF("%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
365 DWORD arg
= VA_ARG16( valist
, DWORD
);
366 if (args
) args
[aix
] = arg
;
367 if (TRACE_ON(thunk
)) DPRINTF("%ld,", arg
);
370 if (TRACE_ON(thunk
)) DPRINTF("])\n");
374 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
375 * 32-bit CDECL routine ...
378 if (!proc32
) ret
= 0;
379 else switch (nrofargs
)
381 case 0: ret
= proc32();
383 case 1: ret
= proc32(args
[0]);
385 case 2: ret
= proc32(args
[0],args
[1]);
387 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
389 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
391 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
393 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
395 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
397 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
399 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
401 case 10:ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
403 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]);
406 /* FIXME: should go up to 32 arguments */
407 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
412 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
413 if (!Ex
) stack16_pop( (3 + nrofargs
) * sizeof(DWORD
) );
415 TRACE("returns %08lx\n",ret
);
416 HeapFree( GetProcessHeap(), 0, args
);
418 SYSLEVEL_RestoreWin16Lock();
423 /**********************************************************************
424 * CallProc32W16 (KERNEL.517)
426 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
427 * DWORD fAddressConvert, DWORD cParams );
429 DWORD WINAPI
CallProc32W16( void )
431 return WOW_CallProc32W16( FALSE
);
434 /**********************************************************************
435 * CallProcEx32W16 (KERNEL.518)
437 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
438 * DWORD lpProcAddress, DWORD p1, ... );
440 DWORD WINAPI
CallProcEx32W16( void )
442 return WOW_CallProc32W16( TRUE
);
446 /**********************************************************************
447 * WOW16Call (KERNEL.501)
452 DWORD WINAPI
WOW16Call(WORD x
,WORD y
,WORD z
)
457 FIXME("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
460 for (i
=0;i
<x
/2;i
++) {
461 WORD a
= VA_ARG16(args
,WORD
);
464 calladdr
= VA_ARG16(args
,DWORD
);
466 stack16_pop( x
+ sizeof(DWORD
) );
467 DPRINTF(") calling address was 0x%08lx\n",calladdr
);