Popup windows will be hidden when minimizing the main frame.
[wine.git] / relay32 / wowthunk.c
blob41760af02eeb9caa50924dd66531fe687fcf46cd
1 /*
2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
5 */
7 #include "wine/winbase16.h"
8 #include "winbase.h"
9 #include "wownt32.h"
10 #include "heap.h"
11 #include "miscemu.h"
12 #include "syslevel.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( GetCurrentThreadId(),
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 */
39 if ( fProtectedMode )
40 return PTR_SEG_TO_LIN( vp );
41 else
42 return DOSMEM_MapRealToLinear( vp );
45 /**********************************************************************
46 * WOWGetVDMPointerFix (WOW32.6) (KERNEL32.68)
48 LPVOID WINAPI WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
50 /*
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 )
126 if ( 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) ) )
138 return FALSE;
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 :-/
153 Yield16();
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 )
175 switch ( type )
177 case WOW_TYPE_HWND:
178 case WOW_TYPE_HMENU:
179 case WOW_TYPE_HDWP:
180 case WOW_TYPE_HDROP:
181 case WOW_TYPE_HDC:
182 case WOW_TYPE_HFONT:
183 case WOW_TYPE_HMETAFILE:
184 case WOW_TYPE_HRGN:
185 case WOW_TYPE_HBITMAP:
186 case WOW_TYPE_HBRUSH:
187 case WOW_TYPE_HPALETTE:
188 case WOW_TYPE_HPEN:
189 case WOW_TYPE_HACCEL:
190 case WOW_TYPE_HTASK:
191 case WOW_TYPE_FULLHWND:
192 return (HANDLE)handle;
194 default:
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 );
208 switch ( type )
210 case WOW_TYPE_HWND:
211 case WOW_TYPE_HMENU:
212 case WOW_TYPE_HDWP:
213 case WOW_TYPE_HDROP:
214 case WOW_TYPE_HDC:
215 case WOW_TYPE_HFONT:
216 case WOW_TYPE_HMETAFILE:
217 case WOW_TYPE_HRGN:
218 case WOW_TYPE_HBITMAP:
219 case WOW_TYPE_HBRUSH:
220 case WOW_TYPE_HPALETTE:
221 case WOW_TYPE_HPEN:
222 case WOW_TYPE_HACCEL:
223 case WOW_TYPE_HTASK:
224 case WOW_TYPE_FULLHWND:
225 return LOWORD(handle);
227 default:
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 )
238 DWORD ret;
240 if ( !WOWCallback16Ex( vpfn16, WCB16_PASCAL,
241 sizeof(DWORD), &dwParam, &ret ) )
242 ret = 0L;
244 return ret;
247 /**********************************************************************
248 * WOWCallback16Ex (WOW32.3) (KERNEL32.55)
250 BOOL WINAPI WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
251 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
253 DWORD ret;
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 );
272 if ( pdwRetCode )
273 *pdwRetCode = ret;
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 )
297 HMODULE hModule;
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 )
319 BOOL retv;
321 SYSLEVEL_ReleaseWin16Lock();
322 retv = FreeLibrary( (HMODULE)hLibModule );
323 SYSLEVEL_RestoreWin16Lock();
325 return (DWORD)retv;
329 /**********************************************************************
330 * WOW_CallProc32W
332 static DWORD WOW_CallProc32W16( BOOL Ex )
334 DWORD nrofargs, argconvmask;
335 FARPROC proc32;
336 DWORD *args, ret;
337 VA_LIST16 valist;
338 int i;
339 int aix;
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*)HEAP_xalloc( GetProcessHeap(), 0,
349 sizeof(DWORD)*nrofargs );
350 /* CallProcEx doesn't need its args reversed */
351 for (i=0;i<nrofargs;i++) {
352 if (Ex) {
353 aix = i;
354 } else {
355 aix = nrofargs - i - 1;
357 if (argconvmask & (1<<i))
359 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
360 args[aix] = (DWORD)PTR_SEG_TO_LIN(ptr);
361 if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
363 else
365 args[aix] = VA_ARG16( valist, DWORD );
366 if (TRACE_ON(thunk)) DPRINTF("%ld,",args[aix]);
369 if (TRACE_ON(thunk)) DPRINTF("])\n");
370 VA_END16( valist );
373 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
374 * 32-bit CDECL routine ...
377 if (!proc32) ret = 0;
378 else switch (nrofargs)
380 case 0: ret = proc32();
381 break;
382 case 1: ret = proc32(args[0]);
383 break;
384 case 2: ret = proc32(args[0],args[1]);
385 break;
386 case 3: ret = proc32(args[0],args[1],args[2]);
387 break;
388 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
389 break;
390 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
391 break;
392 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
393 break;
394 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
395 break;
396 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
397 break;
398 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
399 break;
400 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
401 break;
402 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]);
403 break;
404 default:
405 /* FIXME: should go up to 32 arguments */
406 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
407 ret = 0;
408 break;
410 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
411 if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
413 TRACE("returns %08lx\n",ret);
414 HeapFree( GetProcessHeap(), 0, args );
416 SYSLEVEL_RestoreWin16Lock();
418 return ret;
421 /**********************************************************************
422 * CallProc32W16 (KERNEL.517)
424 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
425 * DWORD fAddressConvert, DWORD cParams );
427 DWORD WINAPI CallProc32W16( void )
429 return WOW_CallProc32W16( FALSE );
432 /**********************************************************************
433 * CallProcEx32W16 (KERNEL.518)
435 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
436 * DWORD lpProcAddress, DWORD p1, ... );
438 DWORD WINAPI CallProcEx32W16( void )
440 return WOW_CallProc32W16( TRUE );
444 /**********************************************************************
445 * WOW16Call (KERNEL.501)
447 * FIXME!!!
450 DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z)
452 int i;
453 DWORD calladdr;
454 VA_LIST16 args;
455 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
457 VA_START16(args);
458 for (i=0;i<x/2;i++) {
459 WORD a = VA_ARG16(args,WORD);
460 DPRINTF("%04x ",a);
462 calladdr = VA_ARG16(args,DWORD);
463 VA_END16(args);
464 stack16_pop( x + sizeof(DWORD) );
465 DPRINTF(") calling address was 0x%08lx\n",calladdr);
466 return 0;