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
);
55 /* thunk for 16-bit CreateThread */
62 static DWORD CALLBACK
start_thread16( LPVOID threadArgs
)
64 struct thread_args args
= *(struct thread_args
*)threadArgs
;
65 HeapFree( GetProcessHeap(), 0, threadArgs
);
66 return K32WOWCallback16( (DWORD
)args
.proc
, args
.param
);
70 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
73 /**********************************************************************
74 * K32WOWGetDescriptor (KERNEL32.70)
76 BOOL WINAPI
K32WOWGetDescriptor( SEGPTR segptr
, LPLDT_ENTRY ldtent
)
78 return GetThreadSelectorEntry( GetCurrentThread(),
79 segptr
>> 16, ldtent
);
82 /**********************************************************************
83 * K32WOWGetVDMPointer (KERNEL32.56)
85 LPVOID WINAPI
K32WOWGetVDMPointer( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
87 /* FIXME: add size check too */
92 return DOSMEM_MapRealToLinear( vp
);
95 /**********************************************************************
96 * K32WOWGetVDMPointerFix (KERNEL32.68)
98 LPVOID WINAPI
K32WOWGetVDMPointerFix( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
101 * Hmmm. According to the docu, we should call:
103 * GlobalFix16( SELECTOROF(vp) );
105 * But this is unnecessary under Wine, as we never move global
106 * memory segments in linear memory anyway.
108 * (I'm not so sure what we are *supposed* to do if
109 * fProtectedMode is TRUE, anyway ...)
112 return K32WOWGetVDMPointer( vp
, dwBytes
, fProtectedMode
);
115 /**********************************************************************
116 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
118 VOID WINAPI
K32WOWGetVDMPointerUnfix( DWORD vp
)
121 * See above why we don't call:
123 * GlobalUnfix16( SELECTOROF(vp) );
128 /**********************************************************************
129 * K32WOWGlobalAlloc16 (KERNEL32.59)
131 WORD WINAPI
K32WOWGlobalAlloc16( WORD wFlags
, DWORD cb
)
133 return (WORD
)GlobalAlloc16( wFlags
, cb
);
136 /**********************************************************************
137 * K32WOWGlobalFree16 (KERNEL32.62)
139 WORD WINAPI
K32WOWGlobalFree16( WORD hMem
)
141 return (WORD
)GlobalFree16( (HGLOBAL16
)hMem
);
144 /**********************************************************************
145 * K32WOWGlobalUnlock16 (KERNEL32.61)
147 BOOL WINAPI
K32WOWGlobalUnlock16( WORD hMem
)
149 return (BOOL
)GlobalUnlock16( (HGLOBAL16
)hMem
);
152 /**********************************************************************
153 * K32WOWGlobalAllocLock16 (KERNEL32.63)
155 DWORD WINAPI
K32WOWGlobalAllocLock16( WORD wFlags
, DWORD cb
, WORD
*phMem
)
157 WORD hMem
= K32WOWGlobalAlloc16( wFlags
, cb
);
158 if (phMem
) *phMem
= hMem
;
160 return K32WOWGlobalLock16( hMem
);
163 /**********************************************************************
164 * K32WOWGlobalLockSize16 (KERNEL32.65)
166 DWORD WINAPI
K32WOWGlobalLockSize16( WORD hMem
, PDWORD pcb
)
169 *pcb
= GlobalSize16( (HGLOBAL16
)hMem
);
171 return K32WOWGlobalLock16( hMem
);
174 /**********************************************************************
175 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
177 WORD WINAPI
K32WOWGlobalUnlockFree16( DWORD vpMem
)
179 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem
) ) )
182 return K32WOWGlobalFree16( HIWORD(vpMem
) );
186 /**********************************************************************
187 * K32WOWYield16 (KERNEL32.66)
189 VOID WINAPI
K32WOWYield16( void )
192 * This does the right thing for both Win16 and Win32 tasks.
193 * More or less, at least :-/
198 /**********************************************************************
199 * K32WOWDirectedYield16 (KERNEL32.67)
201 VOID WINAPI
K32WOWDirectedYield16( WORD htask16
)
204 * Argh. Our scheduler doesn't like DirectedYield by Win32
205 * tasks at all. So we do hope that this routine is indeed
206 * only ever called by Win16 tasks that have thunked up ...
208 DirectedYield16( (HTASK16
)htask16
);
212 /***********************************************************************
213 * K32WOWHandle32 (KERNEL32.57)
215 HANDLE WINAPI
K32WOWHandle32( WORD handle
, WOW_HANDLE_TYPE type
)
226 case WOW_TYPE_HBITMAP
:
227 case WOW_TYPE_HBRUSH
:
228 case WOW_TYPE_HPALETTE
:
230 case WOW_TYPE_HACCEL
:
231 return (HANDLE
)(ULONG_PTR
)handle
;
233 case WOW_TYPE_HMETAFILE
:
234 FIXME( "conversion of metafile handles not supported yet\n" );
235 return (HANDLE
)(ULONG_PTR
)handle
;
238 return (HANDLE
)TASK_GetPtr(handle
)->teb
->tid
;
240 case WOW_TYPE_FULLHWND
:
241 FIXME( "conversion of full window handles not supported yet\n" );
242 return (HANDLE
)(ULONG_PTR
)handle
;
245 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
246 return (HANDLE
)(ULONG_PTR
)handle
;
250 /***********************************************************************
251 * K32WOWHandle16 (KERNEL32.58)
253 WORD WINAPI
K32WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
264 case WOW_TYPE_HBITMAP
:
265 case WOW_TYPE_HBRUSH
:
266 case WOW_TYPE_HPALETTE
:
268 case WOW_TYPE_HACCEL
:
269 case WOW_TYPE_FULLHWND
:
270 if ( HIWORD(handle
) )
271 ERR( "handle %p of type %d has non-zero HIWORD\n", handle
, type
);
272 return LOWORD(handle
);
274 case WOW_TYPE_HMETAFILE
:
275 FIXME( "conversion of metafile handles not supported yet\n" );
276 return LOWORD(handle
);
279 return THREAD_IdToTEB((DWORD
)handle
)->htask16
;
282 ERR( "handle %p of unknown type %d\n", handle
, type
);
283 return LOWORD(handle
);
287 /**********************************************************************
288 * K32WOWCallback16Ex (KERNEL32.55)
290 BOOL WINAPI
K32WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
291 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
296 * Arguments must be prepared in the correct order by the caller
297 * (both for PASCAL and CDECL calling convention), so we simply
298 * copy them to the 16-bit stack ...
300 memcpy( (LPBYTE
)CURRENT_STACK16
- cbArgs
, (LPBYTE
)pArgs
, cbArgs
);
303 * Actually, we should take care whether the called routine cleans up
304 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
305 * the callee to do so; after the routine has returned, the 16-bit
306 * stack pointer is always reset to the position it had before.
309 ret
= wine_call_to_16( (FARPROC16
)vpfn16
, cbArgs
);
314 return TRUE
; /* success */
317 /**********************************************************************
318 * K32WOWCallback16 (KERNEL32.54)
320 DWORD WINAPI
K32WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
324 if ( !K32WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
325 sizeof(DWORD
), &dwParam
, &ret
) )
333 * 16-bit WOW routines (in KERNEL)
336 /**********************************************************************
337 * GetVDMPointer32W (KERNEL.516)
339 DWORD WINAPI
GetVDMPointer32W16( SEGPTR vp
, UINT16 fMode
)
341 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp
)));
342 return (DWORD
)K32WOWGetVDMPointer( vp
, 0, (DWORD
)fMode
);
345 /***********************************************************************
346 * LoadLibraryEx32W (KERNEL.513)
348 DWORD WINAPI
LoadLibraryEx32W16( LPCSTR lpszLibFile
, DWORD hFile
, DWORD dwFlags
)
351 DOS_FULL_NAME full_name
;
353 UNICODE_STRING libfileW
;
355 static const WCHAR dllW
[] = {'.','D','L','L',0};
359 SetLastError(ERROR_INVALID_PARAMETER
);
363 if (!RtlCreateUnicodeStringFromAsciiz(&libfileW
, lpszLibFile
))
365 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
369 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
370 a buildin module. This case is handled in MODULE_LoadLibraryExA */
372 filenameW
= libfileW
.Buffer
;
373 if ( DIR_SearchPath( NULL
, filenameW
, dllW
, &full_name
, FALSE
) )
374 filenameW
= full_name
.short_name
;
376 ReleaseThunkLock( &mutex_count
);
377 hModule
= LoadLibraryExW( filenameW
, (HANDLE
)hFile
, dwFlags
);
378 RestoreThunkLock( mutex_count
);
380 RtlFreeUnicodeString(&libfileW
);
382 return (DWORD
)hModule
;
385 /***********************************************************************
386 * GetProcAddress32W (KERNEL.515)
388 DWORD WINAPI
GetProcAddress32W16( DWORD hModule
, LPCSTR lpszProc
)
390 return (DWORD
)GetProcAddress( (HMODULE
)hModule
, lpszProc
);
393 /***********************************************************************
394 * FreeLibrary32W (KERNEL.514)
396 DWORD WINAPI
FreeLibrary32W16( DWORD hLibModule
)
401 ReleaseThunkLock( &mutex_count
);
402 retv
= FreeLibrary( (HMODULE
)hLibModule
);
403 RestoreThunkLock( mutex_count
);
408 /**********************************************************************
411 static DWORD
WOW_CallProc32W16( BOOL Ex
)
413 DWORD nrofargs
, argconvmask
;
421 ReleaseThunkLock( &mutex_count
);
423 VA_START16( valist
);
424 nrofargs
= VA_ARG16( valist
, DWORD
);
425 argconvmask
= VA_ARG16( valist
, DWORD
);
426 proc32
= VA_ARG16( valist
, FARPROC
);
427 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
428 args
= (DWORD
*)HeapAlloc( GetProcessHeap(), 0, sizeof(DWORD
)*nrofargs
);
429 if(args
== NULL
) proc32
= NULL
; /* maybe we should WARN here? */
430 /* CallProcEx doesn't need its args reversed */
431 for (i
=0;i
<nrofargs
;i
++) {
435 aix
= nrofargs
- i
- 1;
437 if (argconvmask
& (1<<i
))
439 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
440 if (args
) args
[aix
] = (DWORD
)MapSL(ptr
);
441 if (TRACE_ON(thunk
)) DPRINTF("%08lx(%p),",ptr
,MapSL(ptr
));
445 DWORD arg
= VA_ARG16( valist
, DWORD
);
446 if (args
) args
[aix
] = arg
;
447 if (TRACE_ON(thunk
)) DPRINTF("%ld,", arg
);
450 if (TRACE_ON(thunk
)) DPRINTF("])\n");
454 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
455 * 32-bit CDECL routine ...
458 if (!proc32
) ret
= 0;
459 else switch (nrofargs
)
461 case 0: ret
= proc32();
463 case 1: ret
= proc32(args
[0]);
465 case 2: ret
= proc32(args
[0],args
[1]);
467 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
469 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
471 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
473 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
475 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
477 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
479 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
481 case 10:ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
483 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]);
485 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]);
487 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]);
490 /* FIXME: should go up to 32 arguments */
491 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
496 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
497 if (!Ex
) stack16_pop( (3 + nrofargs
) * sizeof(DWORD
) );
499 TRACE("returns %08lx\n",ret
);
500 HeapFree( GetProcessHeap(), 0, args
);
502 RestoreThunkLock( mutex_count
);
506 /**********************************************************************
507 * CallProc32W (KERNEL.517)
509 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
510 * DWORD fAddressConvert, DWORD cParams );
512 DWORD WINAPI
CallProc32W16( void )
514 return WOW_CallProc32W16( FALSE
);
517 /**********************************************************************
518 * _CallProcEx32W (KERNEL.518)
520 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
521 * DWORD lpProcAddress, DWORD p1, ... );
523 DWORD WINAPI
CallProcEx32W16( void )
525 return WOW_CallProc32W16( TRUE
);
529 /**********************************************************************
530 * WOW16Call (KERNEL.500)
535 DWORD WINAPI
WOW16Call(WORD x
,WORD y
,WORD z
)
540 FIXME("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
543 for (i
=0;i
<x
/2;i
++) {
544 WORD a
= VA_ARG16(args
,WORD
);
547 calladdr
= VA_ARG16(args
,DWORD
);
549 stack16_pop( x
+ sizeof(DWORD
) );
550 DPRINTF(") calling address was 0x%08lx\n",calladdr
);
555 /***********************************************************************
556 * CreateThread16 (KERNEL.441)
558 HANDLE WINAPI
CreateThread16( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
559 FARPROC16 start
, SEGPTR param
,
560 DWORD flags
, LPDWORD id
)
562 struct thread_args
*args
= HeapAlloc( GetProcessHeap(), 0, sizeof(*args
) );
563 if (!args
) return INVALID_HANDLE_VALUE
;
566 return CreateThread( sa
, stack
, start_thread16
, args
, flags
, id
);