Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[wine] / dlls / kernel / wowthunk.c
1 /*
2  * Win32 WOW Generic Thunk API
3  *
4  * Copyright 1999 Ulrich Weigand
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26
27 #include "wine/winbase16.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wownt32.h"
32 #include "excpt.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "syslevel.h"
36 #include "file.h"
37 #include "task.h"
38 #include "miscemu.h"
39 #include "stackframe.h"
40 #include "wine/exception.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45
46 /*
47  * These are the 16-bit side WOW routines.  They reside in wownt16.h
48  * in the SDK; since we don't support Win16 source code anyway, I've
49  * placed them here for compilation with Wine ...
50  */
51
52 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
53
54 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
55 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
56 DWORD WINAPI FreeLibrary32W16(DWORD);
57
58 #define CPEX_DEST_STDCALL   0x00000000L
59 #define CPEX_DEST_CDECL     0x80000000L
60
61 /* thunk for 16-bit CreateThread */
62 struct thread_args
63 {
64     FARPROC16 proc;
65     DWORD     param;
66 };
67
68 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
69 {
70     struct thread_args args = *(struct thread_args *)threadArgs;
71     HeapFree( GetProcessHeap(), 0, threadArgs );
72     return K32WOWCallback16( (DWORD)args.proc, args.param );
73 }
74
75
76 #ifdef __i386__
77
78 /* symbols exported from relay16.s */
79 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
80 extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
81 extern void Call16_Ret_Start(), Call16_Ret_End();
82 extern void CallTo16_Ret();
83 extern void CALL32_CBClient_Ret();
84 extern void CALL32_CBClientEx_Ret();
85 extern DWORD CallTo16_DataSelector;
86 extern SEGPTR CALL32_CBClient_RetAddr;
87 extern SEGPTR CALL32_CBClientEx_RetAddr;
88 extern BYTE Call16_Start;
89 extern BYTE Call16_End;
90
91 static SEGPTR call16_ret_addr;  /* segptr to CallTo16_Ret routine */
92
93 /***********************************************************************
94  *           WOWTHUNK_Init
95  */
96 BOOL WOWTHUNK_Init(void)
97 {
98     /* allocate the code selector for CallTo16 routines */
99     WORD codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
100                                         (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
101                                         WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
102     if (!codesel) return FALSE;
103
104       /* Patch the return addresses for CallTo16 routines */
105
106     CallTo16_DataSelector = wine_get_ds();
107     call16_ret_addr = MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
108     CALL32_CBClient_RetAddr =
109         MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
110     CALL32_CBClientEx_RetAddr =
111         MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
112     return TRUE;
113 }
114
115
116 /*************************************************************
117  *            fix_selector
118  *
119  * Fix a selector load that caused an exception if it's in the
120  * 16-bit relay code.
121  */
122 static BOOL fix_selector( CONTEXT *context )
123 {
124     WORD *stack;
125     BYTE *instr = (BYTE *)context->Eip;
126
127     if (instr < &Call16_Start || instr >= &Call16_End) return FALSE;
128
129     /* skip prefixes */
130     while (*instr == 0x66 || *instr == 0x67) instr++;
131
132     switch(instr[0])
133     {
134     case 0x07: /* pop es */
135     case 0x17: /* pop ss */
136     case 0x1f: /* pop ds */
137         break;
138     case 0x0f: /* extended instruction */
139         switch(instr[1])
140         {
141         case 0xa1: /* pop fs */
142         case 0xa9: /* pop gs */
143             break;
144         default:
145             return FALSE;
146         }
147         break;
148     default:
149         return FALSE;
150     }
151     stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
152     TRACE( "fixing up selector %x for pop instruction\n", *stack );
153     *stack = 0;
154     return TRUE;
155 }
156
157
158 /*************************************************************
159  *            call16_handler
160  *
161  * Handler for exceptions occurring in 16-bit code.
162  */
163 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
164                              CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
165 {
166     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
167     {
168         /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
169         STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
170         NtCurrentTeb()->cur_stack = frame32->frame16;
171         _LeaveWin16Lock();
172     }
173     else
174     {
175         if (IS_SELECTOR_SYSTEM(context->SegCs))
176         {
177             if (fix_selector( context )) return ExceptionContinueExecution;
178         }
179         else /* check for Win16 __GP handler */
180         {
181             SEGPTR gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) );
182             if (gpHandler)
183             {
184                 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
185                 *--stack = context->SegCs;
186                 *--stack = context->Eip;
187
188                 if (!IS_SELECTOR_32BIT(context->SegSs))
189                     context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
190                                              HIWORD(context->Esp) );
191                 else
192                     context->Esp -= 2*sizeof(WORD);
193
194                 context->SegCs = SELECTOROF( gpHandler );
195                 context->Eip   = OFFSETOF( gpHandler );
196                 return ExceptionContinueExecution;
197             }
198         }
199     }
200     return ExceptionContinueSearch;
201 }
202
203 #else  /* __i386__ */
204
205 BOOL WOWTHUNK_Init(void)
206 {
207     return TRUE;
208 }
209
210 #endif  /* __i386__ */
211
212
213 /*
214  *  32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
215  */
216
217 /**********************************************************************
218  *           K32WOWGetDescriptor        (KERNEL32.70)
219  */
220 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
221 {
222     return GetThreadSelectorEntry( GetCurrentThread(),
223                                    segptr >> 16, ldtent );
224 }
225
226 /**********************************************************************
227  *           K32WOWGetVDMPointer        (KERNEL32.56)
228  */
229 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
230 {
231     /* FIXME: add size check too */
232
233     if ( fProtectedMode )
234         return MapSL( vp );
235     else
236         return DOSMEM_MapRealToLinear( vp );
237 }
238
239 /**********************************************************************
240  *           K32WOWGetVDMPointerFix     (KERNEL32.68)
241  */
242 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
243 {
244     /*
245      * Hmmm. According to the docu, we should call:
246      *
247      *          GlobalFix16( SELECTOROF(vp) );
248      *
249      * But this is unnecessary under Wine, as we never move global
250      * memory segments in linear memory anyway.
251      *
252      * (I'm not so sure what we are *supposed* to do if
253      *  fProtectedMode is TRUE, anyway ...)
254      */
255
256     return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
257 }
258
259 /**********************************************************************
260  *           K32WOWGetVDMPointerUnfix   (KERNEL32.69)
261  */
262 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
263 {
264     /*
265      * See above why we don't call:
266      *
267      * GlobalUnfix16( SELECTOROF(vp) );
268      *
269      */
270 }
271
272 /**********************************************************************
273  *           K32WOWGlobalAlloc16        (KERNEL32.59)
274  */
275 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
276 {
277     return (WORD)GlobalAlloc16( wFlags, cb );
278 }
279
280 /**********************************************************************
281  *           K32WOWGlobalFree16         (KERNEL32.62)
282  */
283 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
284 {
285     return (WORD)GlobalFree16( (HGLOBAL16)hMem );
286 }
287
288 /**********************************************************************
289  *           K32WOWGlobalUnlock16       (KERNEL32.61)
290  */
291 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
292 {
293     return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
294 }
295
296 /**********************************************************************
297  *           K32WOWGlobalAllocLock16    (KERNEL32.63)
298  */
299 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
300 {
301     WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
302     if (phMem) *phMem = hMem;
303
304     return K32WOWGlobalLock16( hMem );
305 }
306
307 /**********************************************************************
308  *           K32WOWGlobalLockSize16     (KERNEL32.65)
309  */
310 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
311 {
312     if ( pcb )
313         *pcb = GlobalSize16( (HGLOBAL16)hMem );
314
315     return K32WOWGlobalLock16( hMem );
316 }
317
318 /**********************************************************************
319  *           K32WOWGlobalUnlockFree16   (KERNEL32.64)
320  */
321 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
322 {
323     if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
324         return FALSE;
325
326     return K32WOWGlobalFree16( HIWORD(vpMem) );
327 }
328
329
330 /**********************************************************************
331  *           K32WOWYield16              (KERNEL32.66)
332  */
333 VOID WINAPI K32WOWYield16( void )
334 {
335     /*
336      * This does the right thing for both Win16 and Win32 tasks.
337      * More or less, at least :-/
338      */
339     Yield16();
340 }
341
342 /**********************************************************************
343  *           K32WOWDirectedYield16       (KERNEL32.67)
344  */
345 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
346 {
347     /*
348      * Argh.  Our scheduler doesn't like DirectedYield by Win32
349      * tasks at all.  So we do hope that this routine is indeed
350      * only ever called by Win16 tasks that have thunked up ...
351      */
352     DirectedYield16( (HTASK16)htask16 );
353 }
354
355
356 /***********************************************************************
357  *           K32WOWHandle32              (KERNEL32.57)
358  */
359 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
360 {
361     switch ( type )
362     {
363     case WOW_TYPE_HWND:
364     case WOW_TYPE_HMENU:
365     case WOW_TYPE_HDWP:
366     case WOW_TYPE_HDROP:
367     case WOW_TYPE_HDC:
368     case WOW_TYPE_HFONT:
369     case WOW_TYPE_HRGN:
370     case WOW_TYPE_HBITMAP:
371     case WOW_TYPE_HBRUSH:
372     case WOW_TYPE_HPALETTE:
373     case WOW_TYPE_HPEN:
374     case WOW_TYPE_HACCEL:
375         return (HANDLE)(ULONG_PTR)handle;
376
377     case WOW_TYPE_HMETAFILE:
378         FIXME( "conversion of metafile handles not supported yet\n" );
379         return (HANDLE)(ULONG_PTR)handle;
380
381     case WOW_TYPE_HTASK:
382         return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
383
384     case WOW_TYPE_FULLHWND:
385         FIXME( "conversion of full window handles not supported yet\n" );
386         return (HANDLE)(ULONG_PTR)handle;
387
388     default:
389         ERR( "handle 0x%04x of unknown type %d\n", handle, type );
390         return (HANDLE)(ULONG_PTR)handle;
391     }
392 }
393
394 /***********************************************************************
395  *           K32WOWHandle16              (KERNEL32.58)
396  */
397 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
398 {
399     switch ( type )
400     {
401     case WOW_TYPE_HWND:
402     case WOW_TYPE_HMENU:
403     case WOW_TYPE_HDWP:
404     case WOW_TYPE_HDROP:
405     case WOW_TYPE_HDC:
406     case WOW_TYPE_HFONT:
407     case WOW_TYPE_HRGN:
408     case WOW_TYPE_HBITMAP:
409     case WOW_TYPE_HBRUSH:
410     case WOW_TYPE_HPALETTE:
411     case WOW_TYPE_HPEN:
412     case WOW_TYPE_HACCEL:
413     case WOW_TYPE_FULLHWND:
414         if ( HIWORD(handle ) )
415                 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
416         return LOWORD(handle);
417
418     case WOW_TYPE_HMETAFILE:
419         FIXME( "conversion of metafile handles not supported yet\n" );
420         return LOWORD(handle);
421
422     case WOW_TYPE_HTASK:
423         return TASK_GetTaskFromThread( (DWORD)handle );
424
425     default:
426         ERR( "handle %p of unknown type %d\n", handle, type );
427         return LOWORD(handle);
428     }
429 }
430
431 /**********************************************************************
432  *           K32WOWCallback16Ex         (KERNEL32.55)
433  */
434 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
435                                 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
436 {
437 #ifdef __i386__
438     /*
439      * Arguments must be prepared in the correct order by the caller
440      * (both for PASCAL and CDECL calling convention), so we simply
441      * copy them to the 16-bit stack ...
442      */
443     WORD *stack = (WORD *)CURRENT_STACK16 - cbArgs / sizeof(WORD);
444
445     memcpy( stack, pArgs, cbArgs );
446
447     if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
448     {
449         CONTEXT *context = (CONTEXT *)pdwRetCode;
450
451         if (TRACE_ON(relay))
452         {
453             DWORD count = cbArgs / sizeof(WORD);
454
455             DPRINTF("%04lx:CallTo16(func=%04lx:%04x,ds=%04lx",
456                     GetCurrentThreadId(),
457                     context->SegCs, LOWORD(context->Eip), context->SegDs );
458             while (count) DPRINTF( ",%04x", stack[--count] );
459             DPRINTF(") ss:sp=%04x:%04x",
460                     SELECTOROF(NtCurrentTeb()->cur_stack), OFFSETOF(NtCurrentTeb()->cur_stack) );
461             DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
462                     (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
463                     (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
464                     (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
465             SYSLEVEL_CheckNotLevel( 2 );
466         }
467
468         /* push return address */
469         if (dwFlags & WCB16_REGS_LONG)
470         {
471             *((DWORD *)stack - 1) = HIWORD(call16_ret_addr);
472             *((DWORD *)stack - 2) = LOWORD(call16_ret_addr);
473             cbArgs += 2 * sizeof(DWORD);
474         }
475         else
476         {
477             *((SEGPTR *)stack - 1) = call16_ret_addr;
478             cbArgs += sizeof(SEGPTR);
479         }
480
481         _EnterWin16Lock();
482         wine_call_to_16_regs( context, cbArgs, call16_handler );
483         _LeaveWin16Lock();
484
485         if (TRACE_ON(relay))
486         {
487             DPRINTF("%04lx:RetFrom16() ss:sp=%04x:%04x ",
488                     GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->cur_stack),
489                     OFFSETOF(NtCurrentTeb()->cur_stack));
490             DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
491                     (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
492                     (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
493             SYSLEVEL_CheckNotLevel( 2 );
494         }
495     }
496     else
497     {
498         DWORD ret;
499
500         if (TRACE_ON(relay))
501         {
502             DWORD count = cbArgs / sizeof(WORD);
503
504             DPRINTF("%04lx:CallTo16(func=%04x:%04x,ds=%04x",
505                     GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
506                     SELECTOROF(NtCurrentTeb()->cur_stack) );
507             while (count) DPRINTF( ",%04x", stack[--count] );
508             DPRINTF(") ss:sp=%04x:%04x\n",
509                     SELECTOROF(NtCurrentTeb()->cur_stack), OFFSETOF(NtCurrentTeb()->cur_stack) );
510             SYSLEVEL_CheckNotLevel( 2 );
511         }
512
513         /* push return address */
514         *((SEGPTR *)stack - 1) = call16_ret_addr;
515         cbArgs += sizeof(SEGPTR);
516
517         /*
518          * Actually, we should take care whether the called routine cleans up
519          * its stack or not.  Fortunately, our wine_call_to_16 core doesn't rely on
520          * the callee to do so; after the routine has returned, the 16-bit
521          * stack pointer is always reset to the position it had before.
522          */
523         _EnterWin16Lock();
524         ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
525         if (pdwRetCode) *pdwRetCode = ret;
526         _LeaveWin16Lock();
527
528         if (TRACE_ON(relay))
529         {
530             DPRINTF("%04lx:RetFrom16() ss:sp=%04x:%04x retval=%08lx\n",
531                     GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->cur_stack),
532                     OFFSETOF(NtCurrentTeb()->cur_stack), ret);
533             SYSLEVEL_CheckNotLevel( 2 );
534         }
535     }
536 #else
537     assert(0);  /* cannot call to 16-bit on non-Intel architectures */
538 #endif  /* __i386__ */
539
540     return TRUE;  /* success */
541 }
542
543 /**********************************************************************
544  *           K32WOWCallback16            (KERNEL32.54)
545  */
546 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
547 {
548     DWORD ret;
549
550     if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
551                            sizeof(DWORD), &dwParam, &ret ) )
552         ret = 0L;
553
554     return ret;
555 }
556
557
558 /*
559  *  16-bit WOW routines (in KERNEL)
560  */
561
562 /**********************************************************************
563  *           GetVDMPointer32W      (KERNEL.516)
564  */
565 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
566 {
567     GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
568     return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
569 }
570
571 /***********************************************************************
572  *           LoadLibraryEx32W      (KERNEL.513)
573  */
574 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
575 {
576     HMODULE hModule;
577     DOS_FULL_NAME full_name;
578     DWORD mutex_count;
579     UNICODE_STRING libfileW;
580     LPCWSTR filenameW;
581     static const WCHAR dllW[] = {'.','D','L','L',0};
582
583     if (!lpszLibFile)
584     {
585         SetLastError(ERROR_INVALID_PARAMETER);
586         return 0;
587     }
588
589     if (!RtlCreateUnicodeStringFromAsciiz(&libfileW, lpszLibFile))
590     {
591         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
592         return 0;
593     }
594
595     /* if the file can not be found, call LoadLibraryExA anyway, since it might be
596        a buildin module. This case is handled in MODULE_LoadLibraryExA */
597
598     filenameW = libfileW.Buffer;
599     if ( DIR_SearchPath( NULL, filenameW, dllW, &full_name, FALSE ) )
600         filenameW = full_name.short_name;
601
602     ReleaseThunkLock( &mutex_count );
603     hModule = LoadLibraryExW( filenameW, (HANDLE)hFile, dwFlags );
604     RestoreThunkLock( mutex_count );
605
606     RtlFreeUnicodeString(&libfileW);
607
608     return (DWORD)hModule;
609 }
610
611 /***********************************************************************
612  *           GetProcAddress32W     (KERNEL.515)
613  */
614 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
615 {
616     return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
617 }
618
619 /***********************************************************************
620  *           FreeLibrary32W        (KERNEL.514)
621  */
622 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
623 {
624     BOOL retv;
625     DWORD mutex_count;
626
627     ReleaseThunkLock( &mutex_count );
628     retv = FreeLibrary( (HMODULE)hLibModule );
629     RestoreThunkLock( mutex_count );
630     return (DWORD)retv;
631 }
632
633
634 /**********************************************************************
635  *           WOW_CallProc32W
636  */
637 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
638 {
639     DWORD ret;
640     DWORD mutex_count;
641
642     ReleaseThunkLock( &mutex_count );
643
644     /*
645      * FIXME:  If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
646      *         32-bit CDECL routine ...
647      */
648
649     if (!proc32) ret = 0;
650     else switch (nrofargs)
651     {
652     case 0: ret = proc32();
653             break;
654     case 1: ret = proc32(args[0]);
655             break;
656     case 2: ret = proc32(args[0],args[1]);
657             break;
658     case 3: ret = proc32(args[0],args[1],args[2]);
659             break;
660     case 4: ret = proc32(args[0],args[1],args[2],args[3]);
661             break;
662     case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
663             break;
664     case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
665             break;
666     case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
667             break;
668     case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
669             break;
670     case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
671             break;
672     case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
673             break;
674     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]);
675             break;
676     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]);
677             break;
678     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]);
679             break;
680     case 14: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],args[13]);
681             break;
682     case 15: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],args[13],args[14]);
683             break;
684     default:
685             /* FIXME: should go up to 32  arguments */
686             ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
687             ret = 0;
688             break;
689     }
690
691     RestoreThunkLock( mutex_count );
692
693     TRACE("returns %08lx\n",ret);
694     return ret;
695 }
696
697 /**********************************************************************
698  *           CallProc32W           (KERNEL.517)
699  */
700 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
701 {
702     DWORD args[32];
703     unsigned int i;
704
705     TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
706
707     for (i=0;i<nrofargs;i++)
708     {
709         if (argconvmask & (1<<i))
710         {
711             SEGPTR ptr = VA_ARG16( valist, SEGPTR );
712             /* pascal convention, have to reverse the arguments order */
713             args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
714             TRACE("%08lx(%p),",ptr,MapSL(ptr));
715         }
716         else
717         {
718             DWORD arg = VA_ARG16( valist, DWORD );
719             /* pascal convention, have to reverse the arguments order */
720             args[nrofargs - i - 1] = arg;
721             TRACE("%ld,", arg);
722         }
723     }
724     TRACE("])\n");
725
726     /* POP nrofargs DWORD arguments and 3 DWORD parameters */
727     stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
728
729     return WOW_CallProc32W16( proc32, nrofargs, args );
730 }
731
732 /**********************************************************************
733  *           _CallProcEx32W         (KERNEL.518)
734  */
735 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
736 {
737     DWORD args[32];
738     unsigned int i;
739
740     TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
741
742     for (i=0;i<nrofargs;i++)
743     {
744         if (argconvmask & (1<<i))
745         {
746             SEGPTR ptr = VA_ARG16( valist, SEGPTR );
747             args[i] = (DWORD)MapSL(ptr);
748             TRACE("%08lx(%p),",ptr,MapSL(ptr));
749         }
750         else
751         {
752             DWORD arg = VA_ARG16( valist, DWORD );
753             args[i] = arg;
754             TRACE("%ld,", arg);
755         }
756     }
757     TRACE("])\n");
758     return WOW_CallProc32W16( proc32, nrofargs, args );
759 }
760
761
762 /**********************************************************************
763  *           WOW16Call               (KERNEL.500)
764  *
765  * FIXME!!!
766  *
767  */
768 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
769 {
770         int     i;
771         DWORD   calladdr;
772         FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
773
774         for (i=0;i<x/2;i++) {
775                 WORD    a = VA_ARG16(args,WORD);
776                 DPRINTF("%04x ",a);
777         }
778         calladdr = VA_ARG16(args,DWORD);
779         stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
780         DPRINTF(") calling address was 0x%08lx\n",calladdr);
781         return 0;
782 }
783
784
785 /***********************************************************************
786  *           CreateThread16   (KERNEL.441)
787  */
788 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
789                               FARPROC16 start, SEGPTR param,
790                               DWORD flags, LPDWORD id )
791 {
792     struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
793     if (!args) return INVALID_HANDLE_VALUE;
794     args->proc = start;
795     args->param = param;
796     return CreateThread( sa, stack, start_thread16, args, flags, id );
797 }