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
)
212 case WOW_TYPE_HBITMAP
:
213 case WOW_TYPE_HBRUSH
:
214 case WOW_TYPE_HPALETTE
:
216 case WOW_TYPE_HACCEL
:
217 return (HANDLE
)(ULONG_PTR
)handle
;
219 case WOW_TYPE_HMETAFILE
:
220 FIXME( "conversion of metafile handles not supported yet\n" );
221 return (HANDLE
)(ULONG_PTR
)handle
;
224 return (HANDLE
)TASK_GetPtr(handle
)->teb
->tid
;
226 case WOW_TYPE_FULLHWND
:
227 FIXME( "conversion of full window handles not supported yet\n" );
228 return (HANDLE
)(ULONG_PTR
)handle
;
231 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
232 return (HANDLE
)(ULONG_PTR
)handle
;
236 /***********************************************************************
237 * K32WOWHandle16 (KERNEL32.58)
239 WORD WINAPI
K32WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
241 if ( HIWORD(handle
) )
242 ERR( "handle %p of type %d has non-zero HIWORD\n", handle
, type
);
253 case WOW_TYPE_HBITMAP
:
254 case WOW_TYPE_HBRUSH
:
255 case WOW_TYPE_HPALETTE
:
257 case WOW_TYPE_HACCEL
:
258 case WOW_TYPE_FULLHWND
:
259 return LOWORD(handle
);
261 case WOW_TYPE_HMETAFILE
:
262 FIXME( "conversion of metafile handles not supported yet\n" );
263 return LOWORD(handle
);
266 return THREAD_IdToTEB((DWORD
)handle
)->htask16
;
269 ERR( "handle %p of unknown type %d\n", handle
, type
);
270 return LOWORD(handle
);
274 /**********************************************************************
275 * K32WOWCallback16Ex (KERNEL32.55)
277 BOOL WINAPI
K32WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
278 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
283 * Arguments must be prepared in the correct order by the caller
284 * (both for PASCAL and CDECL calling convention), so we simply
285 * copy them to the 16-bit stack ...
287 memcpy( (LPBYTE
)CURRENT_STACK16
- cbArgs
, (LPBYTE
)pArgs
, cbArgs
);
291 * Actually, we should take care whether the called routine cleans up
292 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
293 * the callee to do so; after the routine has returned, the 16-bit
294 * stack pointer is always reset to the position it had before.
297 ret
= wine_call_to_16( (FARPROC16
)vpfn16
, cbArgs
);
302 return TRUE
; /* success */
305 /**********************************************************************
306 * K32WOWCallback16 (KERNEL32.54)
308 DWORD WINAPI
K32WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
312 if ( !K32WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
313 sizeof(DWORD
), &dwParam
, &ret
) )
321 * 16-bit WOW routines (in KERNEL)
324 /**********************************************************************
325 * GetVDMPointer32W (KERNEL.516)
327 DWORD WINAPI
GetVDMPointer32W16( SEGPTR vp
, UINT16 fMode
)
329 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp
)));
330 return (DWORD
)K32WOWGetVDMPointer( vp
, 0, (DWORD
)fMode
);
333 /***********************************************************************
334 * LoadLibraryEx32W (KERNEL.513)
336 DWORD WINAPI
LoadLibraryEx32W16( LPCSTR lpszLibFile
, DWORD hFile
, DWORD dwFlags
)
339 DOS_FULL_NAME full_name
;
341 UNICODE_STRING libfileW
;
343 static const WCHAR dllW
[] = {'.','D','L','L',0};
347 SetLastError(ERROR_INVALID_PARAMETER
);
351 if (!RtlCreateUnicodeStringFromAsciiz(&libfileW
, lpszLibFile
))
353 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
357 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
358 a buildin module. This case is handled in MODULE_LoadLibraryExA */
360 filenameW
= libfileW
.Buffer
;
361 if ( DIR_SearchPath( NULL
, filenameW
, dllW
, &full_name
, FALSE
) )
362 filenameW
= full_name
.short_name
;
364 ReleaseThunkLock( &mutex_count
);
365 hModule
= LoadLibraryExW( filenameW
, (HANDLE
)hFile
, dwFlags
);
366 RestoreThunkLock( mutex_count
);
368 RtlFreeUnicodeString(&libfileW
);
370 return (DWORD
)hModule
;
373 /***********************************************************************
374 * GetProcAddress32W (KERNEL.515)
376 DWORD WINAPI
GetProcAddress32W16( DWORD hModule
, LPCSTR lpszProc
)
378 return (DWORD
)GetProcAddress( (HMODULE
)hModule
, lpszProc
);
381 /***********************************************************************
382 * FreeLibrary32W (KERNEL.514)
384 DWORD WINAPI
FreeLibrary32W16( DWORD hLibModule
)
389 ReleaseThunkLock( &mutex_count
);
390 retv
= FreeLibrary( (HMODULE
)hLibModule
);
391 RestoreThunkLock( mutex_count
);
396 /**********************************************************************
399 static DWORD
WOW_CallProc32W16( BOOL Ex
)
401 DWORD nrofargs
, argconvmask
;
409 ReleaseThunkLock( &mutex_count
);
411 VA_START16( valist
);
412 nrofargs
= VA_ARG16( valist
, DWORD
);
413 argconvmask
= VA_ARG16( valist
, DWORD
);
414 proc32
= VA_ARG16( valist
, FARPROC
);
415 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
416 args
= (DWORD
*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD
)*nrofargs
);
417 if(args
== NULL
) proc32
= NULL
; /* maybe we should WARN here? */
418 /* CallProcEx doesn't need its args reversed */
419 for (i
=0;i
<nrofargs
;i
++) {
423 aix
= nrofargs
- i
- 1;
425 if (argconvmask
& (1<<i
))
427 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
428 if (args
) args
[aix
] = (DWORD
)MapSL(ptr
);
429 if (TRACE_ON(thunk
)) DPRINTF("%08lx(%p),",ptr
,MapSL(ptr
));
433 DWORD arg
= VA_ARG16( valist
, DWORD
);
434 if (args
) args
[aix
] = arg
;
435 if (TRACE_ON(thunk
)) DPRINTF("%ld,", arg
);
438 if (TRACE_ON(thunk
)) DPRINTF("])\n");
442 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
443 * 32-bit CDECL routine ...
446 if (!proc32
) ret
= 0;
447 else switch (nrofargs
)
449 case 0: ret
= proc32();
451 case 1: ret
= proc32(args
[0]);
453 case 2: ret
= proc32(args
[0],args
[1]);
455 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
457 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
459 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
461 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
463 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
465 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
467 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
469 case 10:ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
471 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]);
473 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]);
475 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]);
478 /* FIXME: should go up to 32 arguments */
479 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
484 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
485 if (!Ex
) stack16_pop( (3 + nrofargs
) * sizeof(DWORD
) );
487 TRACE("returns %08lx\n",ret
);
488 HeapFree( GetProcessHeap(), 0, args
);
490 RestoreThunkLock( mutex_count
);
494 /**********************************************************************
495 * CallProc32W (KERNEL.517)
497 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
498 * DWORD fAddressConvert, DWORD cParams );
500 DWORD WINAPI
CallProc32W16( void )
502 return WOW_CallProc32W16( FALSE
);
505 /**********************************************************************
506 * _CallProcEx32W (KERNEL.518)
508 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
509 * DWORD lpProcAddress, DWORD p1, ... );
511 DWORD WINAPI
CallProcEx32W16( void )
513 return WOW_CallProc32W16( TRUE
);
517 /**********************************************************************
518 * WOW16Call (KERNEL.500)
523 DWORD WINAPI
WOW16Call(WORD x
,WORD y
,WORD z
)
528 FIXME("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
531 for (i
=0;i
<x
/2;i
++) {
532 WORD a
= VA_ARG16(args
,WORD
);
535 calladdr
= VA_ARG16(args
,DWORD
);
537 stack16_pop( x
+ sizeof(DWORD
) );
538 DPRINTF(") calling address was 0x%08lx\n",calladdr
);