- Fix prototypes of GetClassLongA/W, GetClassLongPtrA/W and
[wine] / windows / winproc.c
1 /*
2  * Window procedure callbacks
3  *
4  * Copyright 1995 Martin von Loewis
5  * Copyright 1996 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "wownt32.h"
32 #include "wine/winbase16.h"
33 #include "wine/winuser16.h"
34 #include "stackframe.h"
35 #include "controls.h"
36 #include "win.h"
37 #include "winproc.h"
38 #include "message.h"
39 #include "user_private.h"
40 #include "thread.h"
41 #include "dde.h"
42 #include "winternl.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DECLARE_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DEFAULT_DEBUG_CHANNEL(win);
49
50 #ifdef __i386__
51
52 #include "pshpack1.h"
53
54 /* Window procedure 16-to-32-bit thunk */
55 typedef struct
56 {
57     BYTE       popl_eax;             /* popl  %eax (return address) */
58     BYTE       pushl_func;           /* pushl $proc */
59     WNDPROC    proc;
60     BYTE       pushl_eax;            /* pushl %eax */
61     BYTE       ljmp;                 /* ljmp relay*/
62     DWORD      relay_offset;         /* __wine_call_wndproc_32A/W */
63     WORD       relay_sel;
64 } WINPROC_THUNK_FROM16;
65
66 /* Window procedure 32-to-16-bit thunk */
67 typedef struct
68 {
69     BYTE       popl_eax;             /* popl  %eax (return address) */
70     BYTE       pushl_func;           /* pushl $proc */
71     WNDPROC16  proc;
72     BYTE       pushl_eax;            /* pushl %eax */
73     BYTE       jmp;                  /* jmp   relay (relative jump)*/
74     void     (*relay)();             /* WINPROC_CallProc32ATo16() */
75 } WINPROC_THUNK_FROM32;
76
77 /* Simple jmp to call 32-bit procedure directly */
78 typedef struct
79 {
80     BYTE       jmp;                  /* jmp  proc (relative jump) */
81     WNDPROC    proc;
82 } WINPROC_JUMP;
83
84 #include "poppack.h"
85
86 #else /* __i386__ */
87
88 typedef struct
89 {
90     WNDPROC    proc;
91 } WINPROC_THUNK_FROM16;
92
93 typedef struct
94 {
95     WNDPROC16  proc;
96 } WINPROC_THUNK_FROM32;
97
98 typedef struct
99 {
100     WNDPROC    proc;
101 } WINPROC_JUMP;
102
103 #endif /* __i386__ */
104
105 typedef union
106 {
107     WINPROC_THUNK_FROM16  t_from16;
108     WINPROC_THUNK_FROM32  t_from32;
109 } WINPROC_THUNK;
110
111 typedef struct tagWINDOWPROC
112 {
113     WINPROC_THUNK  thunk;    /* Thunk */
114     WINPROC_JUMP   jmp;      /* Jump */
115     BYTE           type;     /* Function type */
116 } WINDOWPROC;
117
118 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
119                                                UINT msg, WPARAM wParam,
120                                                LPARAM lParam );
121 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
122                                                UINT msg, WPARAM wParam,
123                                                LPARAM lParam );
124
125 #define MAX_WINPROCS  (0x10000 / sizeof(WINDOWPROC))
126
127 static WINDOWPROC winproc_array[MAX_WINPROCS];
128 static UINT winproc_used;
129
130 static CRITICAL_SECTION winproc_cs;
131 static CRITICAL_SECTION_DEBUG critsect_debug =
132 {
133     0, 0, &winproc_cs,
134     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
135       0, 0, { 0, (DWORD)(__FILE__ ": winproc_cs") }
136 };
137 static CRITICAL_SECTION winproc_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
138
139 static BOOL is_valid_winproc( WINDOWPROC *proc )
140 {
141     /* check alignment */
142     if (((BYTE *)proc - (BYTE *)winproc_array) % sizeof(*proc)) return FALSE;
143     /* check array limits */
144     if (proc < winproc_array || proc >= winproc_array + winproc_used) return FALSE;
145     return (proc->type != WIN_PROC_INVALID);
146 }
147
148 /* find an existing winproc for a given function and type */
149 /* FIXME: probably should do something more clever than a linear search */
150 static inline WINDOWPROC *find_winproc( WNDPROC func, WINDOWPROCTYPE type )
151 {
152     unsigned int i;
153
154     if (type == WIN_PROC_16)
155     {
156         for (i = 0; i < winproc_used; i++)
157         {
158             if (winproc_array[i].type == type &&
159                 winproc_array[i].thunk.t_from32.proc == (WNDPROC16)func)
160                 return &winproc_array[i];
161         }
162     }
163     else
164     {
165         for (i = 0; i < winproc_used; i++)
166         {
167             if (winproc_array[i].type == type &&
168                 winproc_array[i].thunk.t_from16.proc == func)
169                 return &winproc_array[i];
170         }
171     }
172     return NULL;
173 }
174
175 /* initialize a new winproc */
176 static inline void set_winproc( WINDOWPROC *proc, WNDPROC func, WINDOWPROCTYPE type )
177 {
178 #ifdef __i386__
179     static FARPROC16 relay_32A, relay_32W;
180
181     switch(type)
182     {
183     case WIN_PROC_16:
184         proc->thunk.t_from32.popl_eax    = 0x58;   /* popl  %eax */
185         proc->thunk.t_from32.pushl_func  = 0x68;   /* pushl $proc */
186         proc->thunk.t_from32.proc        = (WNDPROC16)func;
187         proc->thunk.t_from32.pushl_eax   = 0x50;   /* pushl %eax */
188         proc->thunk.t_from32.jmp         = 0xe9;   /* jmp   relay*/
189         proc->thunk.t_from32.relay =  /* relative jump */
190             (void(*)())((DWORD)WINPROC_CallProc32ATo16 -
191                         (DWORD)(&proc->thunk.t_from32.relay + 1));
192         break;
193     case WIN_PROC_32A:
194         if (!relay_32A) relay_32A = GetProcAddress16( GetModuleHandle16("user"),
195                                                       "__wine_call_wndproc_32A" );
196         proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
197         proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
198         proc->thunk.t_from16.proc         = func;
199         proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
200         proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
201         proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32A);
202         proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32A);
203         proc->jmp.jmp  = 0xe9;
204         /* Fixup relative jump */
205         proc->jmp.proc = (WNDPROC)((DWORD)func - (DWORD)(&proc->jmp.proc + 1));
206         break;
207     case WIN_PROC_32W:
208         if (!relay_32W) relay_32W = GetProcAddress16( GetModuleHandle16("user"),
209                                                       "__wine_call_wndproc_32W" );
210         proc->thunk.t_from16.popl_eax     = 0x58;   /* popl  %eax */
211         proc->thunk.t_from16.pushl_func   = 0x68;   /* pushl $proc */
212         proc->thunk.t_from16.proc         = func;
213         proc->thunk.t_from16.pushl_eax    = 0x50;   /* pushl %eax */
214         proc->thunk.t_from16.ljmp         = 0xea;   /* ljmp   relay*/
215         proc->thunk.t_from16.relay_offset = OFFSETOF(relay_32W);
216         proc->thunk.t_from16.relay_sel    = SELECTOROF(relay_32W);
217         proc->jmp.jmp  = 0xe9;
218         /* Fixup relative jump */
219         proc->jmp.proc = (WNDPROC)((char *)func - (char *)(&proc->jmp.proc + 1));
220         break;
221     default:
222         /* Should not happen */
223         break;
224     }
225 #else /* __i386__ */
226     switch(type)
227     {
228     case WIN_PROC_16:
229         proc->thunk.t_from32.proc = (WNDPROC16)func;
230         break;
231     case WIN_PROC_32A:
232     case WIN_PROC_32W:
233         proc->thunk.t_from16.proc = func;
234         break;
235     default:
236         /* Should not happen */
237         break;
238     }
239 #endif /* __i386__ */
240
241     proc->type = type;
242 }
243
244 static WORD get_winproc_selector(void)
245 {
246     static LONG winproc_selector;
247     WORD ret;
248
249     if (!(ret = winproc_selector))
250     {
251         LDT_ENTRY entry;
252         WORD sel = wine_ldt_alloc_entries(1);
253         wine_ldt_set_base( &entry, winproc_array );
254         wine_ldt_set_limit( &entry, sizeof(winproc_array) - 1 );
255         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
256         wine_ldt_set_entry( sel, &entry );
257         if (!(ret = InterlockedCompareExchange( &winproc_selector, sel, 0 ))) ret = sel;
258         else wine_ldt_free_entries( sel, 1 );  /* somebody beat us to it */
259     }
260     return ret;
261 }
262
263
264 #ifdef __i386__
265 /* Some window procedures modify register they shouldn't, or are not
266  * properly declared stdcall; so we need a small assembly wrapper to
267  * call them. */
268 extern LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
269                                 WPARAM wParam, LPARAM lParam );
270 __ASM_GLOBAL_FUNC( WINPROC_wrapper,
271                    "pushl %ebp\n\t"
272                    "movl %esp,%ebp\n\t"
273                    "pushl %edi\n\t"
274                    "pushl %esi\n\t"
275                    "pushl %ebx\n\t"
276                    "pushl 24(%ebp)\n\t"
277                    "pushl 20(%ebp)\n\t"
278                    "pushl 16(%ebp)\n\t"
279                    "pushl 12(%ebp)\n\t"
280                    "movl 8(%ebp),%eax\n\t"
281                    "call *%eax\n\t"
282                    "leal -12(%ebp),%esp\n\t"
283                    "popl %ebx\n\t"
284                    "popl %esi\n\t"
285                    "popl %edi\n\t"
286                    "leave\n\t"
287                    "ret" );
288 #else
289 static inline LRESULT WINPROC_wrapper( WNDPROC proc, HWND hwnd, UINT msg,
290                                        WPARAM wParam, LPARAM lParam )
291 {
292     return proc( hwnd, msg, wParam, lParam );
293 }
294 #endif  /* __i386__ */
295
296
297 static void MINMAXINFO32to16( const MINMAXINFO *from, MINMAXINFO16 *to )
298 {
299     to->ptReserved.x     = from->ptReserved.x;
300     to->ptReserved.y     = from->ptReserved.y;
301     to->ptMaxSize.x      = from->ptMaxSize.x;
302     to->ptMaxSize.y      = from->ptMaxSize.y;
303     to->ptMaxPosition.x  = from->ptMaxPosition.x;
304     to->ptMaxPosition.y  = from->ptMaxPosition.y;
305     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
306     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
307     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
308     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
309 }
310
311 static void MINMAXINFO16to32( const MINMAXINFO16 *from, MINMAXINFO *to )
312 {
313     to->ptReserved.x     = from->ptReserved.x;
314     to->ptReserved.y     = from->ptReserved.y;
315     to->ptMaxSize.x      = from->ptMaxSize.x;
316     to->ptMaxSize.y      = from->ptMaxSize.y;
317     to->ptMaxPosition.x  = from->ptMaxPosition.x;
318     to->ptMaxPosition.y  = from->ptMaxPosition.y;
319     to->ptMinTrackSize.x = from->ptMinTrackSize.x;
320     to->ptMinTrackSize.y = from->ptMinTrackSize.y;
321     to->ptMaxTrackSize.x = from->ptMaxTrackSize.x;
322     to->ptMaxTrackSize.y = from->ptMaxTrackSize.y;
323 }
324
325 static void WINDOWPOS32to16( const WINDOWPOS* from, WINDOWPOS16* to )
326 {
327     to->hwnd            = HWND_16(from->hwnd);
328     to->hwndInsertAfter = HWND_16(from->hwndInsertAfter);
329     to->x               = from->x;
330     to->y               = from->y;
331     to->cx              = from->cx;
332     to->cy              = from->cy;
333     to->flags           = from->flags;
334 }
335
336 static void WINDOWPOS16to32( const WINDOWPOS16* from, WINDOWPOS* to )
337 {
338     to->hwnd            = WIN_Handle32(from->hwnd);
339     to->hwndInsertAfter = (from->hwndInsertAfter == (HWND16)-1) ?
340                            HWND_TOPMOST : WIN_Handle32(from->hwndInsertAfter);
341     to->x               = from->x;
342     to->y               = from->y;
343     to->cx              = from->cx;
344     to->cy              = from->cy;
345     to->flags           = from->flags;
346 }
347
348 /* The strings are not copied */
349 static void CREATESTRUCT32Ato16( const CREATESTRUCTA* from, CREATESTRUCT16* to )
350 {
351     to->lpCreateParams = (SEGPTR)from->lpCreateParams;
352     to->hInstance      = HINSTANCE_16(from->hInstance);
353     to->hMenu          = HMENU_16(from->hMenu);
354     to->hwndParent     = HWND_16(from->hwndParent);
355     to->cy             = from->cy;
356     to->cx             = from->cx;
357     to->y              = from->y;
358     to->x              = from->x;
359     to->style          = from->style;
360     to->dwExStyle      = from->dwExStyle;
361 }
362
363 static void CREATESTRUCT16to32A( const CREATESTRUCT16* from, CREATESTRUCTA *to )
364
365 {
366     to->lpCreateParams = (LPVOID)from->lpCreateParams;
367     to->hInstance      = HINSTANCE_32(from->hInstance);
368     to->hMenu          = HMENU_32(from->hMenu);
369     to->hwndParent     = WIN_Handle32(from->hwndParent);
370     to->cy             = from->cy;
371     to->cx             = from->cx;
372     to->y              = from->y;
373     to->x              = from->x;
374     to->style          = from->style;
375     to->dwExStyle      = from->dwExStyle;
376 }
377
378 /* The strings are not copied */
379 static void MDICREATESTRUCT32Ato16( const MDICREATESTRUCTA* from, MDICREATESTRUCT16* to )
380 {
381     to->hOwner = HINSTANCE_16(from->hOwner);
382     to->x      = from->x;
383     to->y      = from->y;
384     to->cx     = from->cx;
385     to->cy     = from->cy;
386     to->style  = from->style;
387     to->lParam = from->lParam;
388 }
389
390 static void MDICREATESTRUCT16to32A( const MDICREATESTRUCT16* from, MDICREATESTRUCTA *to )
391 {
392     to->hOwner = HINSTANCE_32(from->hOwner);
393     to->x      = from->x;
394     to->y      = from->y;
395     to->cx     = from->cx;
396     to->cy     = from->cy;
397     to->style  = from->style;
398     to->lParam = from->lParam;
399 }
400
401 /**********************************************************************
402  *           WINPROC_CallWndProc32
403  *
404  * Call a 32-bit WndProc.
405  */
406 static LRESULT WINPROC_CallWndProc( WNDPROC proc, HWND hwnd, UINT msg,
407                                       WPARAM wParam, LPARAM lParam )
408 {
409     LRESULT retvalue;
410     int iWndsLocks;
411
412     hwnd = WIN_GetFullHandle( hwnd );
413     if (TRACE_ON(relay))
414         DPRINTF( "%04lx:Call window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
415                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam );
416     /* To avoid any deadlocks, all the locks on the windows structures
417        must be suspended before the control is passed to the application */
418     iWndsLocks = WIN_SuspendWndsLock();
419     retvalue = WINPROC_wrapper( proc, hwnd, msg, wParam, lParam );
420     WIN_RestoreWndsLock(iWndsLocks);
421
422     if (TRACE_ON(relay))
423         DPRINTF( "%04lx:Ret  window proc %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx) retval=%08lx\n",
424                  GetCurrentThreadId(), proc, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam, retvalue );
425     return retvalue;
426 }
427
428 /***********************************************************************
429  *           WINPROC_CallWndProc16
430  *
431  * Call a 16-bit window procedure
432  */
433 static LRESULT WINAPI WINPROC_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
434                                              UINT16 msg, WPARAM16 wParam,
435                                              LPARAM lParam )
436 {
437     CONTEXT86 context;
438     LRESULT ret;
439     WORD args[5];
440     DWORD offset = 0;
441     TEB *teb = NtCurrentTeb();
442     int iWndsLocks;
443
444     /* Window procedures want ax = hInstance, ds = es = ss */
445
446     memset(&context, 0, sizeof(context));
447     context.SegDs = context.SegEs = SELECTOROF(teb->cur_stack);
448     context.SegFs = wine_get_fs();
449     context.SegGs = wine_get_gs();
450     if (!(context.Eax = GetWindowWord( HWND_32(hwnd), GWLP_HINSTANCE ))) context.Eax = context.SegDs;
451     context.SegCs = SELECTOROF(proc);
452     context.Eip   = OFFSETOF(proc);
453     context.Ebp   = OFFSETOF(teb->cur_stack)
454                         + (WORD)&((STACK16FRAME*)0)->bp;
455
456     if (lParam)
457     {
458         /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
459            work if structures passed in lParam are placed in the stack/data
460            segment. Programmers easily make the mistake of converting lParam
461            to a near rather than a far pointer, since Windows apparently
462            allows this. We copy the structures to the 16 bit stack; this is
463            ugly but makes these programs work. */
464         switch (msg)
465         {
466           case WM_CREATE:
467           case WM_NCCREATE:
468             offset = sizeof(CREATESTRUCT16); break;
469           case WM_DRAWITEM:
470             offset = sizeof(DRAWITEMSTRUCT16); break;
471           case WM_COMPAREITEM:
472             offset = sizeof(COMPAREITEMSTRUCT16); break;
473         }
474         if (offset)
475         {
476             void *s = MapSL(lParam);
477             lParam = stack16_push( offset );
478             memcpy( MapSL(lParam), s, offset );
479         }
480     }
481
482     iWndsLocks = WIN_SuspendWndsLock();
483
484     args[4] = hwnd;
485     args[3] = msg;
486     args[2] = wParam;
487     args[1] = HIWORD(lParam);
488     args[0] = LOWORD(lParam);
489     WOWCallback16Ex( 0, WCB16_REGS, sizeof(args), args, (DWORD *)&context );
490     ret = MAKELONG( LOWORD(context.Eax), LOWORD(context.Edx) );
491
492     if (offset) stack16_pop( offset );
493
494     WIN_RestoreWndsLock(iWndsLocks);
495
496     return ret;
497 }
498
499
500 /**********************************************************************
501  *           WINPROC_GetPtr
502  *
503  * Return a pointer to the win proc.
504  */
505 static WINDOWPROC *WINPROC_GetPtr( WNDPROC handle )
506 {
507     BYTE *ptr;
508     WINDOWPROC *proc;
509
510     /* ptr cannot be < 64K */
511     if (!HIWORD(handle)) return NULL;
512
513     /* Check for a linear pointer */
514
515     ptr = (BYTE *)handle;
516     /* First check if it is the jmp address */
517     proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,jmp));
518     if (is_valid_winproc(proc)) return proc;
519
520     /* Now it must be the thunk address */
521     proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
522     if (is_valid_winproc(proc)) return proc;
523
524     /* Check for a segmented pointer */
525
526     if (HIWORD(handle) == get_winproc_selector())
527     {
528         ptr = (BYTE *)winproc_array + LOWORD(handle);
529         /* It must be the thunk address */
530         proc = (WINDOWPROC *)(ptr - FIELD_OFFSET(WINDOWPROC,thunk));
531         if (is_valid_winproc(proc)) return proc;
532     }
533     return NULL;
534 }
535
536
537 /**********************************************************************
538  *           WINPROC_GetProc
539  *
540  * Get a window procedure pointer that can be passed to the Windows program.
541  */
542 WNDPROC16 WINPROC_GetProc( WNDPROC proc, WINDOWPROCTYPE type )
543 {
544     WINDOWPROC *ptr = (WINDOWPROC *)proc;
545
546     if (!proc) return NULL;
547     if (type == WIN_PROC_16)  /* We want a 16:16 address */
548     {
549         if (ptr->type == WIN_PROC_16)
550             return ptr->thunk.t_from32.proc;
551         else
552             return (WNDPROC16)MAKESEGPTR( get_winproc_selector(),
553                                           (char *)&ptr->thunk - (char *)winproc_array );
554     }
555     else  /* We want a 32-bit address */
556     {
557         if (ptr->type == WIN_PROC_16)
558             return (WNDPROC16)&ptr->thunk;
559         else if (type != ptr->type)
560             /* Have to return the jmp address if types don't match */
561             return (WNDPROC16)&ptr->jmp;
562         else
563             /* Some Win16 programs want to get back the proc they set */
564             return (WNDPROC16)ptr->thunk.t_from16.proc;
565     }
566 }
567
568
569 /**********************************************************************
570  *           WINPROC_AllocProc
571  *
572  * Allocate a window procedure for a window or class.
573  *
574  * Note that allocated winprocs are never freed; the idea is that even if an app creates a
575  * lot of windows, it will usually only have a limited number of window procedures, so the
576  * array won't grow too large, and this way we avoid the need to track allocations per window.
577  */
578 WNDPROC WINPROC_AllocProc( WNDPROC func, WINDOWPROCTYPE type )
579 {
580     WINDOWPROC *proc;
581
582     if (!func) return NULL;
583
584     EnterCriticalSection( &winproc_cs );
585
586     /* check if the function is already a win proc */
587     if (!(proc = WINPROC_GetPtr( func )))
588     {
589         /* then check if we already have a winproc for that function */
590         if (!(proc = find_winproc( func, type )))
591         {
592             if (winproc_used >= MAX_WINPROCS)
593                 FIXME( "too many winprocs, cannot allocate one for %p/%d\n", func, type );
594             else
595             {
596                 proc = &winproc_array[winproc_used++];
597                 set_winproc( proc, func, type );
598                 TRACE( "allocated %p for %p/%d (%d/%d used)\n",
599                        proc, func, type, winproc_used, MAX_WINPROCS );
600             }
601         }
602         else TRACE( "reusing %p for %p/%d\n", proc, func, type );
603     }
604
605     LeaveCriticalSection( &winproc_cs );
606     return (WNDPROC)proc;
607 }
608
609
610 /**********************************************************************
611  *           WINPROC_GetProcType
612  *
613  * Return the window procedure type.
614  */
615 WINDOWPROCTYPE WINPROC_GetProcType( WNDPROC proc )
616 {
617     if (!proc) return WIN_PROC_INVALID;
618     return ((WINDOWPROC *)proc)->type;
619 }
620 /**********************************************************************
621  *           WINPROC_TestCBForStr
622  *
623  * Return TRUE if the lparam is a string
624  */
625 inline static BOOL WINPROC_TestCBForStr( HWND hwnd )
626 {
627     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
628     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
629 }
630 /**********************************************************************
631  *           WINPROC_TestLBForStr
632  *
633  * Return TRUE if the lparam is a string
634  */
635 inline static BOOL WINPROC_TestLBForStr( HWND hwnd )
636 {
637     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
638     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
639
640 }
641 /**********************************************************************
642  *           WINPROC_MapMsg32ATo32W
643  *
644  * Map a message from Ansi to Unicode.
645  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
646  *
647  * FIXME:
648  *  WM_GETTEXT/WM_SETTEXT and static control with SS_ICON style:
649  *  the first four bytes are the handle of the icon
650  *  when the WM_SETTEXT message has been used to set the icon
651  */
652 INT WINPROC_MapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
653 {
654     switch(msg)
655     {
656     case WM_GETTEXT:
657     case WM_ASKCBFORMATNAME:
658         {
659             LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
660                                      *pwparam * sizeof(WCHAR) + sizeof(LPARAM) );
661             if (!ptr) return -1;
662             *ptr++ = *plparam;  /* Store previous lParam */
663             *plparam = (LPARAM)ptr;
664         }
665         return 1;
666     /* lparam is string (0-terminated) */
667     case WM_SETTEXT:
668     case WM_WININICHANGE:
669     case WM_DEVMODECHANGE:
670     case CB_DIR:
671     case LB_DIR:
672     case LB_ADDFILE:
673     case EM_REPLACESEL:
674         {
675             DWORD len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, NULL, 0);
676             WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
677             len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*plparam, -1, buf, len);
678             *plparam = (LPARAM)buf;
679             return (*plparam ? 1 : -1);
680         }
681     case WM_GETTEXTLENGTH:
682     case CB_GETLBTEXTLEN:
683     case LB_GETTEXTLEN:
684         return 1;  /* need to map result */
685     case WM_NCCREATE:
686     case WM_CREATE:
687         {
688             UNICODE_STRING usBuffer;
689             struct s
690             { CREATESTRUCTW cs;         /* new structure */
691               LPCWSTR lpszName;         /* allocated Name */
692               LPCWSTR lpszClass;        /* allocated Class */
693             };
694
695             struct s *xs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct s));
696             if (!xs) return -1;
697             xs->cs = *(CREATESTRUCTW *)*plparam;
698             if (HIWORD(xs->cs.lpszName))
699             {
700                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszName);
701                 xs->lpszName = xs->cs.lpszName = usBuffer.Buffer;
702             }
703             if (HIWORD(xs->cs.lpszClass))
704             {
705                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)xs->cs.lpszClass);
706                 xs->lpszClass = xs->cs.lpszClass = usBuffer.Buffer;
707             }
708
709             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
710             {
711                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
712                                                                          sizeof(*mdi_cs));
713                 *mdi_cs = *(MDICREATESTRUCTW *)xs->cs.lpCreateParams;
714                 if (HIWORD(mdi_cs->szTitle))
715                 {
716                     RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szTitle);
717                     mdi_cs->szTitle = usBuffer.Buffer;
718                 }
719                 if (HIWORD(mdi_cs->szClass))
720                 {
721                     RtlCreateUnicodeStringFromAsciiz(&usBuffer, (LPCSTR)mdi_cs->szClass);
722                     mdi_cs->szClass = usBuffer.Buffer;
723                 }
724                 xs->cs.lpCreateParams = mdi_cs;
725             }
726
727             *plparam = (LPARAM)xs;
728         }
729         return 1;
730     case WM_MDICREATE:
731         {
732             MDICREATESTRUCTW *cs =
733                 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
734             if (!cs) return -1;
735             *cs = *(MDICREATESTRUCTW *)*plparam;
736             if (HIWORD(cs->szClass))
737             {
738                 UNICODE_STRING usBuffer;
739                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szClass);
740                 cs->szClass = usBuffer.Buffer;
741             }
742             if (HIWORD(cs->szTitle))
743             {
744                 UNICODE_STRING usBuffer;
745                 RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)cs->szTitle);
746                 cs->szTitle = usBuffer.Buffer;
747             }
748             *plparam = (LPARAM)cs;
749         }
750         return 1;
751
752 /* Listbox */
753     case LB_ADDSTRING:
754     case LB_INSERTSTRING:
755     case LB_FINDSTRING:
756     case LB_FINDSTRINGEXACT:
757     case LB_SELECTSTRING:
758         if(!*plparam) return 0;
759         if ( WINPROC_TestLBForStr( hwnd ))
760         {
761             UNICODE_STRING usBuffer;
762             RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
763             *plparam = (LPARAM)usBuffer.Buffer;
764         }
765         return (*plparam ? 1 : -1);
766
767     case LB_GETTEXT:                /* FIXME: fixed sized buffer */
768         { if ( WINPROC_TestLBForStr( hwnd ))
769           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
770             if (!ptr) return -1;
771             *ptr++ = *plparam;  /* Store previous lParam */
772             *plparam = (LPARAM)ptr;
773           }
774         }
775         return 1;
776
777 /* Combobox */
778     case CB_ADDSTRING:
779     case CB_INSERTSTRING:
780     case CB_FINDSTRINGEXACT:
781     case CB_FINDSTRING:
782     case CB_SELECTSTRING:
783         if(!*plparam) return 0;
784         if ( WINPROC_TestCBForStr( hwnd ))
785         {
786             UNICODE_STRING usBuffer;
787             RtlCreateUnicodeStringFromAsciiz(&usBuffer,(LPCSTR)*plparam);
788             *plparam = (LPARAM)usBuffer.Buffer;
789         }
790         return (*plparam ? 1 : -1);
791
792     case CB_GETLBTEXT:    /* FIXME: fixed sized buffer */
793         { if ( WINPROC_TestCBForStr( hwnd ))
794           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 * sizeof(WCHAR) + sizeof(LPARAM) );
795             if (!ptr) return -1;
796             *ptr++ = *plparam;  /* Store previous lParam */
797             *plparam = (LPARAM)ptr;
798           }
799         }
800         return 1;
801
802 /* Multiline edit */
803     case EM_GETLINE:
804         { WORD len = (WORD)*plparam;
805           LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(WCHAR) );
806           if (!ptr) return -1;
807           *ptr++ = *plparam;  /* Store previous lParam */
808           *((WORD *) ptr) = len;   /* Store the length */
809           *plparam = (LPARAM)ptr;
810         }
811         return 1;
812
813     case WM_CHARTOITEM:
814     case WM_MENUCHAR:
815     case WM_CHAR:
816     case WM_DEADCHAR:
817     case WM_SYSCHAR:
818     case WM_SYSDEADCHAR:
819     case EM_SETPASSWORDCHAR:
820         {
821             BYTE ch = LOWORD(*pwparam);
822             WCHAR wch;
823             MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
824             *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
825         }
826         return 0;
827
828     case WM_IME_CHAR:
829         {
830             BYTE ch[2];
831             WCHAR wch;
832             ch[0] = (*pwparam >> 8);
833             ch[1] = *pwparam & 0xff;
834             if (ch[0])
835                 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
836             else
837                 MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
838             *pwparam = MAKEWPARAM( wch, HIWORD(*pwparam) );
839         }
840         return 0;
841
842     case WM_PAINTCLIPBOARD:
843     case WM_SIZECLIPBOARD:
844         FIXME_(msg)("message %s (0x%x) needs translation, please report\n", SPY_GetMsgName(msg, hwnd), msg );
845         return -1;
846     default:  /* No translation needed */
847         return 0;
848     }
849 }
850
851
852 /**********************************************************************
853  *           WINPROC_UnmapMsg32ATo32W
854  *
855  * Unmap a message that was mapped from Ansi to Unicode.
856  */
857 LRESULT WINPROC_UnmapMsg32ATo32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
858                                   LRESULT result )
859 {
860     switch(msg)
861     {
862     case WM_GETTEXT:
863     case WM_ASKCBFORMATNAME:
864         {
865             LPARAM *ptr = (LPARAM *)lParam - 1;
866             if (!wParam) result = 0;
867             else if (!(result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
868                                                     (LPSTR)*ptr, wParam, NULL, NULL )))
869             {
870                 ((LPSTR)*ptr)[wParam-1] = 0;
871                 result = wParam - 1;
872             }
873             else result--;  /* do not count terminating null */
874             HeapFree( GetProcessHeap(), 0, ptr );
875         }
876         break;
877     case WM_GETTEXTLENGTH:
878     case CB_GETLBTEXTLEN:
879     case LB_GETTEXTLEN:
880         /* there may be one DBCS char for each Unicode char */
881         return result * 2;
882     case WM_NCCREATE:
883     case WM_CREATE:
884         {
885             struct s
886             { CREATESTRUCTW cs;         /* new structure */
887               LPWSTR lpszName;          /* allocated Name */
888               LPWSTR lpszClass;         /* allocated Class */
889             };
890             struct s *xs = (struct s *)lParam;
891             HeapFree( GetProcessHeap(), 0, xs->lpszName );
892             HeapFree( GetProcessHeap(), 0, xs->lpszClass );
893
894             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
895             {
896                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)xs->cs.lpCreateParams;
897                 if (HIWORD(mdi_cs->szTitle))
898                     HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szTitle);
899                 if (HIWORD(mdi_cs->szClass))
900                     HeapFree(GetProcessHeap(), 0, (LPVOID)mdi_cs->szClass);
901                 HeapFree(GetProcessHeap(), 0, mdi_cs);
902             }
903             HeapFree( GetProcessHeap(), 0, xs );
904         }
905         break;
906
907     case WM_MDICREATE:
908         {
909             MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
910             if (HIWORD(cs->szTitle))
911                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
912             if (HIWORD(cs->szClass))
913                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
914             HeapFree( GetProcessHeap(), 0, cs );
915         }
916         break;
917
918     case WM_SETTEXT:
919     case WM_WININICHANGE:
920     case WM_DEVMODECHANGE:
921     case CB_DIR:
922     case LB_DIR:
923     case LB_ADDFILE:
924     case EM_REPLACESEL:
925         HeapFree( GetProcessHeap(), 0, (void *)lParam );
926         break;
927
928 /* Listbox */
929     case LB_ADDSTRING:
930     case LB_INSERTSTRING:
931     case LB_FINDSTRING:
932     case LB_FINDSTRINGEXACT:
933     case LB_SELECTSTRING:
934         if ( WINPROC_TestLBForStr( hwnd ))
935           HeapFree( GetProcessHeap(), 0, (void *)lParam );
936         break;
937
938     case LB_GETTEXT:
939         if ( WINPROC_TestLBForStr( hwnd ))
940         {
941             LPARAM *ptr = (LPARAM *)lParam - 1;
942             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
943                                           (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
944             HeapFree( GetProcessHeap(), 0, ptr );
945         }
946         break;
947
948 /* Combobox */
949     case CB_ADDSTRING:
950     case CB_INSERTSTRING:
951     case CB_FINDSTRING:
952     case CB_FINDSTRINGEXACT:
953     case CB_SELECTSTRING:
954         if ( WINPROC_TestCBForStr( hwnd ))
955           HeapFree( GetProcessHeap(), 0, (void *)lParam );
956         break;
957
958     case CB_GETLBTEXT:
959         if ( WINPROC_TestCBForStr( hwnd ))
960         {
961             LPARAM *ptr = (LPARAM *)lParam - 1;
962             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, -1,
963                                           (LPSTR)*ptr, 0x7fffffff, NULL, NULL ) - 1;
964             HeapFree( GetProcessHeap(), 0, ptr );
965         }
966         break;
967
968 /* Multiline edit */
969     case EM_GETLINE:
970         {
971             LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lParam */
972             WORD len = *(WORD *) lParam;
973             result = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)lParam, result,
974                                           (LPSTR)*ptr, len, NULL, NULL );
975             if (result < len) ((LPSTR)*ptr)[result] = 0;
976             HeapFree( GetProcessHeap(), 0, ptr );
977         }
978         break;
979     }
980     return result;
981 }
982
983
984 /**********************************************************************
985  *           WINPROC_MapMsg32WTo32A
986  *
987  * Map a message from Unicode to Ansi.
988  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
989  */
990 static INT WINPROC_MapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM *pwparam, LPARAM *plparam )
991 {
992     switch(msg)
993     {
994     case WM_GETTEXT:
995     case WM_ASKCBFORMATNAME:
996         {
997             LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0,
998                                                *pwparam + sizeof(LPARAM) );
999             if (!ptr) return -1;
1000             *ptr++ = *plparam;  /* Store previous lParam */
1001             *plparam = (LPARAM)ptr;
1002         }
1003         return 1;
1004
1005     case WM_SETTEXT:
1006     case WM_WININICHANGE:
1007     case WM_DEVMODECHANGE:
1008     case CB_DIR:
1009     case LB_DIR:
1010     case LB_ADDFILE:
1011     case EM_REPLACESEL:
1012         if (*plparam)
1013         {
1014             LPCWSTR str = (LPCWSTR)*plparam;
1015             int len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, 0, 0);
1016             *plparam = (LPARAM)HeapAlloc(GetProcessHeap(), 0, len);
1017             if (!*plparam) return -1;
1018             WideCharToMultiByte(CP_ACP, 0, str, -1, (LPSTR)*plparam, len, 0, 0);
1019             return 1;
1020         }
1021         return 0;
1022
1023     case WM_MDICREATE:
1024         {
1025             MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) );
1026             if (!cs) return -1;
1027             *cs = *(MDICREATESTRUCTA *)*plparam;
1028             if (HIWORD(cs->szTitle))
1029             {
1030                 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, NULL, 0, 0, 0);
1031                 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1032                 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szTitle, -1, buf, len, 0, 0);
1033                 cs->szTitle = buf;
1034             }
1035             if (HIWORD(cs->szClass))
1036             {
1037                 int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, NULL, 0, 0, 0);
1038                 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1039                 if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)cs->szClass, -1, buf, len, 0, 0);
1040                 cs->szClass = buf;
1041             }
1042             *plparam = (LPARAM)cs;
1043         }
1044         return 1;
1045
1046 /* Listbox */
1047     case LB_ADDSTRING:
1048     case LB_INSERTSTRING:
1049     case LB_FINDSTRING:
1050     case LB_FINDSTRINGEXACT:
1051     case LB_SELECTSTRING:
1052         if(!*plparam) return 0;
1053         if ( WINPROC_TestLBForStr( hwnd ))
1054         {
1055             int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1056             LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1057             if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1058             *plparam = (LPARAM)buf;
1059         }
1060         return (*plparam ? 1 : -1);
1061
1062     case LB_GETTEXT:                    /* FIXME: fixed sized buffer */
1063         { if ( WINPROC_TestLBForStr( hwnd ))
1064           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1065             if (!ptr) return -1;
1066             *ptr++ = *plparam;  /* Store previous lParam */
1067             *plparam = (LPARAM)ptr;
1068           }
1069         }
1070         return 1;
1071
1072 /* Combobox */
1073     case CB_ADDSTRING:
1074     case CB_INSERTSTRING:
1075     case CB_FINDSTRING:
1076     case CB_FINDSTRINGEXACT:
1077     case CB_SELECTSTRING:
1078         if(!*plparam) return 0;
1079         if ( WINPROC_TestCBForStr( hwnd ))
1080         {
1081             int len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, NULL, 0, 0, 0);
1082             LPSTR buf = HeapAlloc(GetProcessHeap(), 0, len);
1083             if (buf) WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)*plparam, -1, buf, len, 0, 0);
1084             *plparam = (LPARAM)buf;
1085         }
1086         return (*plparam ? 1 : -1);
1087
1088     case CB_GETLBTEXT:          /* FIXME: fixed sized buffer */
1089         { if ( WINPROC_TestCBForStr( hwnd ))
1090           { LPARAM *ptr = (LPARAM *)HeapAlloc( GetProcessHeap(), 0, 256 + sizeof(LPARAM) );
1091             if (!ptr) return -1;
1092             *ptr++ = *plparam;  /* Store previous lParam */
1093             *plparam = (LPARAM)ptr;
1094           }
1095         }
1096         return 1;
1097
1098 /* Multiline edit */
1099     case EM_GETLINE:
1100         { WORD len = (WORD)*plparam;
1101           LPARAM *ptr = (LPARAM *) HeapAlloc( GetProcessHeap(), 0, sizeof(LPARAM) + sizeof (WORD) + len*sizeof(CHAR) );
1102           if (!ptr) return -1;
1103           *ptr++ = *plparam;  /* Store previous lParam */
1104           *((WORD *) ptr) = len;   /* Store the length */
1105           *plparam = (LPARAM)ptr;
1106         }
1107         return 1;
1108
1109     case WM_CHARTOITEM:
1110     case WM_MENUCHAR:
1111     case WM_CHAR:
1112     case WM_DEADCHAR:
1113     case WM_SYSCHAR:
1114     case WM_SYSDEADCHAR:
1115     case EM_SETPASSWORDCHAR:
1116         {
1117             WCHAR wch = LOWORD(*pwparam);
1118             BYTE ch;
1119             WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
1120             *pwparam = MAKEWPARAM( ch, HIWORD(*pwparam) );
1121         }
1122         return 0;
1123
1124     case WM_IME_CHAR:
1125         {
1126             WCHAR wch = LOWORD(*pwparam);
1127             BYTE ch[2];
1128
1129             if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
1130                 *pwparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(*pwparam) );
1131             else
1132                 *pwparam = MAKEWPARAM( ch[0], HIWORD(*pwparam) );
1133         }
1134         return 0;
1135
1136     case WM_PAINTCLIPBOARD:
1137     case WM_SIZECLIPBOARD:
1138         FIXME_(msg)("message %s (%04x) needs translation, please report\n",SPY_GetMsgName(msg, hwnd),msg );
1139         return -1;
1140     default:  /* No translation needed */
1141         return 0;
1142     }
1143 }
1144
1145
1146 /**********************************************************************
1147  *           WINPROC_UnmapMsg32WTo32A
1148  *
1149  * Unmap a message that was mapped from Unicode to Ansi.
1150  */
1151 static LRESULT WINPROC_UnmapMsg32WTo32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, LRESULT result )
1152 {
1153     switch(msg)
1154     {
1155     case WM_GETTEXT:
1156     case WM_ASKCBFORMATNAME:
1157         {
1158             LPARAM *ptr = (LPARAM *)lParam - 1;
1159             if (!wParam) result = 0;
1160             else if (!(result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1,
1161                                                      (LPWSTR)*ptr, wParam )))
1162             {
1163                 ((LPWSTR)*ptr)[wParam-1] = 0;
1164                 result = wParam - 1;
1165             }
1166             else result--;  /* do not count terminating null */
1167             HeapFree( GetProcessHeap(), 0, ptr );
1168         }
1169         break;
1170
1171     case WM_SETTEXT:
1172     case WM_WININICHANGE:
1173     case WM_DEVMODECHANGE:
1174     case CB_DIR:
1175     case LB_DIR:
1176     case LB_ADDFILE:
1177     case EM_REPLACESEL:
1178         HeapFree( GetProcessHeap(), 0, (void *)lParam );
1179         break;
1180
1181     case WM_MDICREATE:
1182         {
1183             MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1184             if (HIWORD(cs->szTitle))
1185                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szTitle );
1186             if (HIWORD(cs->szClass))
1187                 HeapFree( GetProcessHeap(), 0, (LPVOID)cs->szClass );
1188             HeapFree( GetProcessHeap(), 0, cs );
1189         }
1190         break;
1191
1192 /* Listbox */
1193     case LB_ADDSTRING:
1194     case LB_INSERTSTRING:
1195     case LB_FINDSTRING:
1196     case LB_FINDSTRINGEXACT:
1197     case LB_SELECTSTRING:
1198         if ( WINPROC_TestLBForStr( hwnd ))
1199           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1200         break;
1201
1202     case LB_GETTEXT:
1203         if ( WINPROC_TestLBForStr( hwnd ))
1204         {
1205             LPARAM *ptr = (LPARAM *)lParam - 1;
1206             result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1207             HeapFree( GetProcessHeap(), 0, ptr );
1208         }
1209         break;
1210
1211 /* Combobox */
1212     case CB_ADDSTRING:
1213     case CB_INSERTSTRING:
1214     case CB_FINDSTRING:
1215     case CB_FINDSTRINGEXACT:
1216     case CB_SELECTSTRING:
1217         if ( WINPROC_TestCBForStr( hwnd ))
1218           HeapFree( GetProcessHeap(), 0, (void *)lParam );
1219         break;
1220
1221     case CB_GETLBTEXT:
1222         if ( WINPROC_TestCBForStr( hwnd ))
1223         {
1224             LPARAM *ptr = (LPARAM *)lParam - 1;
1225             result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, -1, (LPWSTR)*ptr, 0x7fffffff ) - 1;
1226             HeapFree( GetProcessHeap(), 0, ptr );
1227         }
1228         break;
1229
1230 /* Multiline edit */
1231     case EM_GETLINE:
1232         {
1233             LPARAM * ptr = (LPARAM *)lParam - 1;  /* get the old lparam */
1234             WORD len = *(WORD *)ptr;
1235             if (len)
1236             {
1237                 result = MultiByteToWideChar( CP_ACP, 0, (LPSTR)lParam, result, (LPWSTR)*ptr, len );
1238                 if (result < len) ((LPWSTR)*ptr)[result] = 0;
1239             }
1240             HeapFree( GetProcessHeap(), 0, ptr );
1241         }
1242         break;
1243     }
1244     return result;
1245 }
1246
1247 static UINT convert_handle_16_to_32(HANDLE16 src, unsigned int flags)
1248 {
1249     HANDLE      dst;
1250     UINT        sz = GlobalSize16(src);
1251     LPSTR       ptr16, ptr32;
1252
1253     if (!(dst = GlobalAlloc(flags, sz)))
1254         return 0;
1255     ptr16 = GlobalLock16(src);
1256     ptr32 = GlobalLock(dst);
1257     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr32, ptr16, sz);
1258     GlobalUnlock16(src);
1259     GlobalUnlock(dst);
1260
1261     return (UINT)dst;
1262 }
1263
1264 /**********************************************************************
1265  *           WINPROC_MapMsg16To32A
1266  *
1267  * Map a message from 16- to 32-bit Ansi.
1268  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1269  */
1270 INT WINPROC_MapMsg16To32A( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1271                              WPARAM *pwparam32, LPARAM *plparam )
1272 {
1273     *pmsg32 = (UINT)msg16;
1274     *pwparam32 = (WPARAM)wParam16;
1275     switch(msg16)
1276     {
1277     case WM_ACTIVATE:
1278     case WM_CHARTOITEM:
1279     case WM_COMMAND:
1280     case WM_VKEYTOITEM:
1281         *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1282         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1283         return 0;
1284     case WM_HSCROLL:
1285     case WM_VSCROLL:
1286         *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1287         *plparam   = (LPARAM)WIN_Handle32( HIWORD(*plparam) );
1288         return 0;
1289     case WM_CTLCOLOR:
1290         if ( HIWORD(*plparam) > CTLCOLOR_STATIC ) return -1;
1291         *pmsg32    = WM_CTLCOLORMSGBOX + HIWORD(*plparam);
1292         *pwparam32 = (WPARAM)HDC_32(wParam16);
1293         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1294         return 0;
1295     case WM_COMPAREITEM:
1296         {
1297             COMPAREITEMSTRUCT16* cis16 = MapSL(*plparam);
1298             COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)
1299                                         HeapAlloc(GetProcessHeap(), 0, sizeof(*cis));
1300             if (!cis) return -1;
1301             cis->CtlType    = cis16->CtlType;
1302             cis->CtlID      = cis16->CtlID;
1303             cis->hwndItem   = WIN_Handle32( cis16->hwndItem );
1304             cis->itemID1    = cis16->itemID1;
1305             cis->itemData1  = cis16->itemData1;
1306             cis->itemID2    = cis16->itemID2;
1307             cis->itemData2  = cis16->itemData2;
1308             cis->dwLocaleId = 0;  /* FIXME */
1309             *plparam = (LPARAM)cis;
1310         }
1311         return 1;
1312     case WM_DELETEITEM:
1313         {
1314             DELETEITEMSTRUCT16* dis16 = MapSL(*plparam);
1315             DELETEITEMSTRUCT *dis = (DELETEITEMSTRUCT *)
1316                                         HeapAlloc(GetProcessHeap(), 0, sizeof(*dis));
1317             if (!dis) return -1;
1318             dis->CtlType  = dis16->CtlType;
1319             dis->CtlID    = dis16->CtlID;
1320             dis->hwndItem = WIN_Handle32( dis16->hwndItem );
1321             dis->itemData = dis16->itemData;
1322             *plparam = (LPARAM)dis;
1323         }
1324         return 1;
1325     case WM_MEASUREITEM:
1326         {
1327             MEASUREITEMSTRUCT16* mis16 = MapSL(*plparam);
1328             MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)
1329                                         HeapAlloc(GetProcessHeap(), 0,
1330                                                 sizeof(*mis) + sizeof(LPARAM));
1331             if (!mis) return -1;
1332             mis->CtlType    = mis16->CtlType;
1333             mis->CtlID      = mis16->CtlID;
1334             mis->itemID     = mis16->itemID;
1335             mis->itemWidth  = mis16->itemWidth;
1336             mis->itemHeight = mis16->itemHeight;
1337             mis->itemData   = mis16->itemData;
1338             *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
1339             *plparam = (LPARAM)mis;
1340         }
1341         return 1;
1342     case WM_DRAWITEM:
1343         {
1344             DRAWITEMSTRUCT16* dis16 = MapSL(*plparam);
1345             DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT*)HeapAlloc(GetProcessHeap(), 0,
1346                                                                  sizeof(*dis));
1347             if (!dis) return -1;
1348             dis->CtlType       = dis16->CtlType;
1349             dis->CtlID         = dis16->CtlID;
1350             dis->itemID        = dis16->itemID;
1351             dis->itemAction    = dis16->itemAction;
1352             dis->itemState     = dis16->itemState;
1353             dis->hwndItem      = (dis->CtlType == ODT_MENU) ? (HWND)HMENU_32(dis16->hwndItem)
1354                                                             : WIN_Handle32( dis16->hwndItem );
1355             dis->hDC           = HDC_32(dis16->hDC);
1356             dis->itemData      = dis16->itemData;
1357             dis->rcItem.left   = dis16->rcItem.left;
1358             dis->rcItem.top    = dis16->rcItem.top;
1359             dis->rcItem.right  = dis16->rcItem.right;
1360             dis->rcItem.bottom = dis16->rcItem.bottom;
1361             *plparam = (LPARAM)dis;
1362         }
1363         return 1;
1364     case WM_GETMINMAXINFO:
1365         {
1366             MINMAXINFO *mmi = (MINMAXINFO *)HeapAlloc( GetProcessHeap(), 0,
1367                                                 sizeof(*mmi) + sizeof(LPARAM));
1368             if (!mmi) return -1;
1369             MINMAXINFO16to32( MapSL(*plparam), mmi );
1370             *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
1371             *plparam = (LPARAM)mmi;
1372         }
1373         return 1;
1374     case WM_GETTEXT:
1375     case WM_SETTEXT:
1376     case WM_WININICHANGE:
1377     case WM_DEVMODECHANGE:
1378     case WM_ASKCBFORMATNAME:
1379         *plparam = (LPARAM)MapSL(*plparam);
1380         return 0;
1381     case WM_MDICREATE:
1382         {
1383             MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1384             MDICREATESTRUCTA *cs = HeapAlloc( GetProcessHeap(), 0, sizeof(*cs) + sizeof(LPARAM) );
1385             if (!cs) return -1;
1386             MDICREATESTRUCT16to32A( cs16, cs );
1387             cs->szTitle = MapSL(cs16->szTitle);
1388             cs->szClass = MapSL(cs16->szClass);
1389             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1390             *plparam = (LPARAM)cs;
1391         }
1392         return 1;
1393     case WM_MDIGETACTIVE:
1394         *plparam = (LPARAM)HeapAlloc( GetProcessHeap(), 0, sizeof(BOOL) );
1395         *(BOOL*)(*plparam) = 0;
1396         return 1;
1397     case WM_MDISETMENU:
1398         if(wParam16) *pmsg32=WM_MDIREFRESHMENU;
1399         *pwparam32 = (WPARAM)HMENU_32(LOWORD(*plparam));
1400         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1401         return 0;
1402     case WM_MENUCHAR:
1403         *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1404         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1405         return 0;
1406     case WM_MENUSELECT:
1407         if((LOWORD(*plparam) & MF_POPUP) && (LOWORD(*plparam) != 0xFFFF))
1408         {
1409             HMENU hmenu=HMENU_32(HIWORD(*plparam));
1410             UINT Pos=MENU_FindSubMenu( &hmenu, HMENU_32(wParam16));
1411             if(Pos==0xFFFF) Pos=0; /* NO_SELECTED_ITEM */
1412             *pwparam32 = MAKEWPARAM( Pos, LOWORD(*plparam) );
1413         }
1414         else *pwparam32 = MAKEWPARAM( wParam16, LOWORD(*plparam) );
1415         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1416         return 0;
1417     case WM_MDIACTIVATE:
1418         if( *plparam )
1419         {
1420             *pwparam32 = (WPARAM)WIN_Handle32( HIWORD(*plparam) );
1421             *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1422         }
1423         else /* message sent to MDI client */
1424             *pwparam32 = wParam16;
1425         return 0;
1426     case WM_NCCALCSIZE:
1427         {
1428             NCCALCSIZE_PARAMS16 *nc16;
1429             NCCALCSIZE_PARAMS *nc;
1430
1431             nc = (NCCALCSIZE_PARAMS *)HeapAlloc( GetProcessHeap(), 0,
1432                                                 sizeof(*nc) + sizeof(LPARAM) );
1433             if (!nc) return -1;
1434             nc16 = MapSL(*plparam);
1435             nc->rgrc[0].left   = nc16->rgrc[0].left;
1436             nc->rgrc[0].top    = nc16->rgrc[0].top;
1437             nc->rgrc[0].right  = nc16->rgrc[0].right;
1438             nc->rgrc[0].bottom = nc16->rgrc[0].bottom;
1439             if (wParam16)
1440             {
1441                 nc->lppos = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1442                                                       sizeof(*nc->lppos) );
1443                 nc->rgrc[1].left   = nc16->rgrc[1].left;
1444                 nc->rgrc[1].top    = nc16->rgrc[1].top;
1445                 nc->rgrc[1].right  = nc16->rgrc[1].right;
1446                 nc->rgrc[1].bottom = nc16->rgrc[1].bottom;
1447                 nc->rgrc[2].left   = nc16->rgrc[2].left;
1448                 nc->rgrc[2].top    = nc16->rgrc[2].top;
1449                 nc->rgrc[2].right  = nc16->rgrc[2].right;
1450                 nc->rgrc[2].bottom = nc16->rgrc[2].bottom;
1451                 if (nc->lppos) WINDOWPOS16to32( MapSL(nc16->lppos), nc->lppos );
1452             }
1453             *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
1454             *plparam = (LPARAM)nc;
1455         }
1456         return 1;
1457     case WM_NCCREATE:
1458     case WM_CREATE:
1459         {
1460             CREATESTRUCT16 *cs16 = MapSL(*plparam);
1461             CREATESTRUCTA *cs = (CREATESTRUCTA *)HeapAlloc( GetProcessHeap(), 0,
1462                                                 sizeof(*cs) + sizeof(LPARAM) );
1463             if (!cs) return -1;
1464             CREATESTRUCT16to32A( cs16, cs );
1465             cs->lpszName  = MapSL(cs16->lpszName);
1466             cs->lpszClass = MapSL(cs16->lpszClass);
1467
1468             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1469             {
1470                 MDICREATESTRUCT16 *mdi_cs16;
1471                 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)HeapAlloc(GetProcessHeap(), 0,
1472                                                                            sizeof(*mdi_cs));
1473                 if (!mdi_cs)
1474                 {
1475                     HeapFree(GetProcessHeap(), 0, cs);
1476                     return -1;
1477                 }
1478                 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1479                 MDICREATESTRUCT16to32A(mdi_cs16, mdi_cs);
1480                 mdi_cs->szTitle = MapSL(mdi_cs16->szTitle);
1481                 mdi_cs->szClass = MapSL(mdi_cs16->szClass);
1482
1483                 cs->lpCreateParams = mdi_cs;
1484             }
1485             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1486             *plparam = (LPARAM)cs;
1487         }
1488         return 1;
1489     case WM_PARENTNOTIFY:
1490         if ((wParam16 == WM_CREATE) || (wParam16 == WM_DESTROY))
1491         {
1492             *pwparam32 = MAKEWPARAM( wParam16, HIWORD(*plparam) );
1493             *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1494         }
1495         return 0;
1496     case WM_WINDOWPOSCHANGING:
1497     case WM_WINDOWPOSCHANGED:
1498         {
1499             WINDOWPOS *wp = (WINDOWPOS *)HeapAlloc( GetProcessHeap(), 0,
1500                                                 sizeof(*wp) + sizeof(LPARAM) );
1501             if (!wp) return -1;
1502             WINDOWPOS16to32( MapSL(*plparam), wp );
1503             *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
1504             *plparam = (LPARAM)wp;
1505         }
1506         return 1;
1507     case WM_GETDLGCODE:
1508         if (*plparam)
1509         {
1510             LPMSG16 msg16 = MapSL(*plparam);
1511             LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1512
1513             if (!msg32) return -1;
1514             msg32->hwnd = WIN_Handle32( msg16->hwnd );
1515             msg32->lParam = msg16->lParam;
1516             msg32->time = msg16->time;
1517             msg32->pt.x = msg16->pt.x;
1518             msg32->pt.y = msg16->pt.y;
1519             /* this is right, right? */
1520             if (WINPROC_MapMsg16To32A( msg32->hwnd, msg16->message,msg16->wParam,
1521                                      &msg32->message,&msg32->wParam,
1522                                      &msg32->lParam)<0) {
1523                 HeapFree( GetProcessHeap(), 0, msg32 );
1524                 return -1;
1525             }
1526             *plparam = (LPARAM)msg32;
1527             return 1;
1528         }
1529         else return 0;
1530     case WM_NOTIFY:
1531         *plparam = (LPARAM)MapSL(*plparam);
1532         return 0;
1533     case WM_ACTIVATEAPP:
1534         /* We need this when SetActiveWindow sends a Sendmessage16() to
1535          * a 32bit window. Might be superflous with 32bit interprocess
1536          * message queues. */
1537         if (*plparam) *plparam = HTASK_32( *plparam );
1538         return 0;
1539     case WM_NEXTMENU:
1540         {
1541             MDINEXTMENU *next = HeapAlloc( GetProcessHeap(), 0, sizeof(*next) );
1542             if (!next) return -1;
1543             next->hmenuIn = (HMENU)*plparam;
1544             next->hmenuNext = 0;
1545             next->hwndNext = 0;
1546             *plparam = (LPARAM)next;
1547             return 1;
1548         }
1549     case WM_PAINTCLIPBOARD:
1550     case WM_SIZECLIPBOARD:
1551         FIXME_(msg)("message %04x needs translation\n",msg16 );
1552         return -1;
1553     case WM_DDE_INITIATE:
1554     case WM_DDE_TERMINATE:
1555     case WM_DDE_UNADVISE:
1556     case WM_DDE_REQUEST:
1557         *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1558         return 0;
1559     case WM_DDE_ADVISE:
1560     case WM_DDE_DATA:
1561     case WM_DDE_POKE:
1562         {
1563             HANDLE16    lo16;
1564             ATOM        hi;
1565             UINT lo32 = 0;
1566
1567             *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1568             lo16 = LOWORD(*plparam);
1569             hi = HIWORD(*plparam);
1570             if (lo16 && !(lo32 = convert_handle_16_to_32(lo16, GMEM_DDESHARE)))
1571                 return -1;
1572             *plparam = PackDDElParam(msg16, lo32, hi);
1573         }
1574         return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
1575     case WM_DDE_ACK:
1576         {
1577             UINT        lo, hi;
1578             int         flag = 0;
1579             char        buf[2];
1580
1581             *pwparam32 = (WPARAM)WIN_Handle32(wParam16);
1582
1583             lo = LOWORD(*plparam);
1584             hi = HIWORD(*plparam);
1585
1586             if (GlobalGetAtomNameA(hi, buf, 2) > 0) flag |= 1;
1587             if (GlobalSize16(hi) != 0) flag |= 2;
1588             switch (flag)
1589             {
1590             case 0:
1591                 if (hi)
1592                 {
1593                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
1594                     hi = 0;
1595                 }
1596                 break;
1597             case 1:
1598                 break; /* atom, nothing to do */
1599             case 3:
1600                 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
1601                 /* fall thru */
1602             case 2:
1603                 hi = convert_handle_16_to_32(hi, GMEM_DDESHARE);
1604                 break;
1605             }
1606             *plparam = PackDDElParam(WM_DDE_ACK, lo, hi);
1607         }
1608         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1609     case WM_DDE_EXECUTE:
1610         *plparam = convert_handle_16_to_32(*plparam, GMEM_DDESHARE);
1611         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
1612     default:  /* No translation needed */
1613         return 0;
1614     }
1615 }
1616
1617
1618 /**********************************************************************
1619  *           WINPROC_UnmapMsg16To32A
1620  *
1621  * Unmap a message that was mapped from 16- to 32-bit Ansi.
1622  */
1623 LRESULT WINPROC_UnmapMsg16To32A( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1624                                  LRESULT result )
1625 {
1626     switch(msg)
1627     {
1628     case WM_COMPAREITEM:
1629     case WM_DELETEITEM:
1630     case WM_DRAWITEM:
1631         HeapFree( GetProcessHeap(), 0, (LPVOID)lParam );
1632         break;
1633     case WM_MEASUREITEM:
1634         {
1635             MEASUREITEMSTRUCT16 *mis16;
1636             MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lParam;
1637             lParam = *(LPARAM *)(mis + 1);
1638             mis16 = MapSL(lParam);
1639             mis16->itemWidth  = (UINT16)mis->itemWidth;
1640             mis16->itemHeight = (UINT16)mis->itemHeight;
1641             HeapFree( GetProcessHeap(), 0, mis );
1642         }
1643         break;
1644     case WM_GETMINMAXINFO:
1645         {
1646             MINMAXINFO *mmi = (MINMAXINFO *)lParam;
1647             lParam = *(LPARAM *)(mmi + 1);
1648             MINMAXINFO32to16( mmi, MapSL(lParam));
1649             HeapFree( GetProcessHeap(), 0, mmi );
1650         }
1651         break;
1652     case WM_MDICREATE:
1653         {
1654             MDICREATESTRUCTA *cs = (MDICREATESTRUCTA *)lParam;
1655             lParam = *(LPARAM *)(cs + 1);
1656             MDICREATESTRUCT32Ato16( cs, MapSL(lParam) );
1657             HeapFree( GetProcessHeap(), 0, cs );
1658         }
1659         break;
1660     case WM_MDIGETACTIVE:
1661         result = MAKELONG( LOWORD(result), (BOOL16)(*(BOOL *)lParam) );
1662         HeapFree( GetProcessHeap(), 0, (BOOL *)lParam );
1663         break;
1664     case WM_NCCALCSIZE:
1665         {
1666             NCCALCSIZE_PARAMS16 *nc16;
1667             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lParam;
1668             lParam = *(LPARAM *)(nc + 1);
1669             nc16 = MapSL(lParam);
1670             nc16->rgrc[0].left   = nc->rgrc[0].left;
1671             nc16->rgrc[0].top    = nc->rgrc[0].top;
1672             nc16->rgrc[0].right  = nc->rgrc[0].right;
1673             nc16->rgrc[0].bottom = nc->rgrc[0].bottom;
1674             if (wParam)
1675             {
1676                 nc16->rgrc[1].left   = nc->rgrc[1].left;
1677                 nc16->rgrc[1].top    = nc->rgrc[1].top;
1678                 nc16->rgrc[1].right  = nc->rgrc[1].right;
1679                 nc16->rgrc[1].bottom = nc->rgrc[1].bottom;
1680                 nc16->rgrc[2].left   = nc->rgrc[2].left;
1681                 nc16->rgrc[2].top    = nc->rgrc[2].top;
1682                 nc16->rgrc[2].right  = nc->rgrc[2].right;
1683                 nc16->rgrc[2].bottom = nc->rgrc[2].bottom;
1684                 if (nc->lppos)
1685                 {
1686                     WINDOWPOS32to16( nc->lppos, MapSL(nc16->lppos));
1687                     HeapFree( GetProcessHeap(), 0, nc->lppos );
1688                 }
1689             }
1690             HeapFree( GetProcessHeap(), 0, nc );
1691         }
1692         break;
1693     case WM_NCCREATE:
1694     case WM_CREATE:
1695         {
1696             CREATESTRUCTA *cs = (CREATESTRUCTA *)lParam;
1697             lParam = *(LPARAM *)(cs + 1);
1698             CREATESTRUCT32Ato16( cs, MapSL(lParam) );
1699
1700             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1701                 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1702
1703             HeapFree( GetProcessHeap(), 0, cs );
1704         }
1705         break;
1706     case WM_WINDOWPOSCHANGING:
1707     case WM_WINDOWPOSCHANGED:
1708         {
1709             WINDOWPOS *wp = (WINDOWPOS *)lParam;
1710             lParam = *(LPARAM *)(wp + 1);
1711             WINDOWPOS32to16(wp, MapSL(lParam));
1712             HeapFree( GetProcessHeap(), 0, wp );
1713         }
1714         break;
1715     case WM_GETDLGCODE:
1716         if (lParam)
1717         {
1718             LPMSG msg32 = (LPMSG)lParam;
1719
1720             WINPROC_UnmapMsg16To32A( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1721                                      result);
1722             HeapFree( GetProcessHeap(), 0, msg32 );
1723         }
1724         break;
1725     case WM_NEXTMENU:
1726         {
1727             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
1728             result = MAKELONG( HMENU_16(next->hmenuNext), HWND_16(next->hwndNext) );
1729             HeapFree( GetProcessHeap(), 0, next );
1730         }
1731         break;
1732     }
1733     return result;
1734 }
1735
1736
1737 /**********************************************************************
1738  *           WINPROC_MapMsg16To32W
1739  *
1740  * Map a message from 16- to 32-bit Unicode.
1741  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1742  */
1743 INT WINPROC_MapMsg16To32W( HWND hwnd, UINT16 msg16, WPARAM16 wParam16, UINT *pmsg32,
1744                            WPARAM *pwparam32, LPARAM *plparam )
1745 {
1746     BYTE ch;
1747     WCHAR wch;
1748
1749     *pmsg32=(UINT)msg16;
1750     *pwparam32 = (WPARAM)wParam16;
1751     switch(msg16)
1752     {
1753     case WM_GETTEXT:
1754     case WM_SETTEXT:
1755     case WM_WININICHANGE:
1756     case WM_DEVMODECHANGE:
1757     case WM_ASKCBFORMATNAME:
1758         *plparam = (LPARAM)MapSL(*plparam);
1759         return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1760     case WM_GETTEXTLENGTH:
1761     case CB_GETLBTEXTLEN:
1762     case LB_GETTEXTLEN:
1763         return 1;  /* need to map result */
1764     case WM_NCCREATE:
1765     case WM_CREATE:
1766         {
1767             CREATESTRUCT16 *cs16 = MapSL(*plparam);
1768             CREATESTRUCTW *cs = (CREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1769                                                 sizeof(*cs) + sizeof(LPARAM) );
1770             if (!cs) return -1;
1771             CREATESTRUCT16to32A( cs16, (CREATESTRUCTA *)cs );
1772             cs->lpszName  = map_str_16_to_32W(cs16->lpszName);
1773             cs->lpszClass = map_str_16_to_32W(cs16->lpszClass);
1774
1775             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1776             {
1777                 MDICREATESTRUCT16 *mdi_cs16;
1778                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)HeapAlloc(GetProcessHeap(), 0,
1779                                                                            sizeof(*mdi_cs));
1780                 if (!mdi_cs)
1781                 {
1782                     HeapFree(GetProcessHeap(), 0, cs);
1783                     return -1;
1784                 }
1785                 mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs16->lpCreateParams);
1786                 MDICREATESTRUCT16to32A(mdi_cs16, (MDICREATESTRUCTA *)mdi_cs);
1787                 mdi_cs->szTitle = map_str_16_to_32W(mdi_cs16->szTitle);
1788                 mdi_cs->szClass = map_str_16_to_32W(mdi_cs16->szClass);
1789
1790                 cs->lpCreateParams = mdi_cs;
1791             }
1792             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1793             *plparam = (LPARAM)cs;
1794         }
1795         return 1;
1796     case WM_MDICREATE:
1797         {
1798             MDICREATESTRUCT16 *cs16 = MapSL(*plparam);
1799             MDICREATESTRUCTW *cs =
1800                 (MDICREATESTRUCTW *)HeapAlloc( GetProcessHeap(), 0,
1801                                                 sizeof(*cs) + sizeof(LPARAM) );
1802             if (!cs) return -1;
1803             MDICREATESTRUCT16to32A( cs16, (MDICREATESTRUCTA *)cs );
1804             cs->szTitle = map_str_16_to_32W(cs16->szTitle);
1805             cs->szClass = map_str_16_to_32W(cs16->szClass);
1806             *(LPARAM *)(cs + 1) = *plparam;  /* Store the previous lParam */
1807             *plparam = (LPARAM)cs;
1808         }
1809         return 1;
1810     case WM_GETDLGCODE:
1811         if (*plparam)
1812         {
1813             LPMSG16 msg16 = MapSL(*plparam);
1814             LPMSG msg32 = (LPMSG)HeapAlloc( GetProcessHeap(), 0, sizeof(MSG) );
1815
1816             if (!msg32) return -1;
1817             msg32->hwnd = WIN_Handle32( msg16->hwnd );
1818             msg32->lParam = msg16->lParam;
1819             msg32->time = msg16->time;
1820             msg32->pt.x = msg16->pt.x;
1821             msg32->pt.y = msg16->pt.y;
1822             /* this is right, right? */
1823             if (WINPROC_MapMsg16To32W(hwnd, msg16->message,msg16->wParam,
1824                                      &msg32->message,&msg32->wParam,
1825                                      &msg32->lParam)<0) {
1826                 HeapFree( GetProcessHeap(), 0, msg32 );
1827                 return -1;
1828             }
1829             *plparam = (LPARAM)msg32;
1830             return 1;
1831         }
1832         else return 0;
1833
1834     case WM_CHARTOITEM:
1835         ch = wParam16;
1836         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1837         *pwparam32 = MAKEWPARAM( wch, HIWORD(*plparam) );
1838         *plparam   = (LPARAM)WIN_Handle32( LOWORD(*plparam) );
1839         return 0;
1840     case WM_MENUCHAR:
1841         ch = wParam16;
1842         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1843         *pwparam32 = MAKEWPARAM( wch, LOWORD(*plparam) );
1844         *plparam   = (LPARAM)HMENU_32(HIWORD(*plparam));
1845         return 0;
1846     case WM_CHAR:
1847     case WM_DEADCHAR:
1848     case WM_SYSCHAR:
1849     case WM_SYSDEADCHAR:
1850         ch = wParam16;
1851         MultiByteToWideChar( CP_ACP, 0, &ch, 1, &wch, 1);
1852         *pwparam32 = wch;
1853         return 0;
1854     case WM_IME_CHAR:
1855         return WINPROC_MapMsg32ATo32W( hwnd, *pmsg32, pwparam32, plparam );
1856
1857     default:  /* No Unicode translation needed */
1858         return WINPROC_MapMsg16To32A( hwnd, msg16, wParam16, pmsg32,
1859                                       pwparam32, plparam );
1860     }
1861 }
1862
1863
1864 /**********************************************************************
1865  *           WINPROC_UnmapMsg16To32W
1866  *
1867  * Unmap a message that was mapped from 16- to 32-bit Unicode.
1868  */
1869 LRESULT WINPROC_UnmapMsg16To32W( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
1870                                  LRESULT result )
1871 {
1872     switch(msg)
1873     {
1874     case WM_GETTEXT:
1875     case WM_SETTEXT:
1876     case WM_GETTEXTLENGTH:
1877     case CB_GETLBTEXTLEN:
1878     case LB_GETTEXTLEN:
1879     case WM_ASKCBFORMATNAME:
1880         return WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
1881     case WM_NCCREATE:
1882     case WM_CREATE:
1883         {
1884             CREATESTRUCTW *cs = (CREATESTRUCTW *)lParam;
1885             lParam = *(LPARAM *)(cs + 1);
1886             CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs, MapSL(lParam) );
1887             unmap_str_16_to_32W( cs->lpszName );
1888             unmap_str_16_to_32W( cs->lpszClass );
1889
1890             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
1891             {
1892                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs->lpCreateParams;
1893                 unmap_str_16_to_32W( mdi_cs->szTitle );
1894                 unmap_str_16_to_32W( mdi_cs->szClass );
1895                 HeapFree(GetProcessHeap(), 0, cs->lpCreateParams);
1896             }
1897             HeapFree( GetProcessHeap(), 0, cs );
1898         }
1899         break;
1900     case WM_MDICREATE:
1901         {
1902             MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lParam;
1903             lParam = *(LPARAM *)(cs + 1);
1904             MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs, MapSL(lParam) );
1905             unmap_str_16_to_32W( cs->szTitle );
1906             unmap_str_16_to_32W( cs->szClass );
1907             HeapFree( GetProcessHeap(), 0, cs );
1908         }
1909         break;
1910     case WM_GETDLGCODE:
1911         if (lParam)
1912         {
1913             LPMSG msg32 = (LPMSG)lParam;
1914
1915             WINPROC_UnmapMsg16To32W( hwnd, msg32->message, msg32->wParam, msg32->lParam,
1916                                      result);
1917             HeapFree( GetProcessHeap(), 0, msg32 );
1918         }
1919         break;
1920     default:
1921         return WINPROC_UnmapMsg16To32A( hwnd, msg, wParam, lParam, result );
1922     }
1923     return result;
1924 }
1925
1926 static HANDLE16 convert_handle_32_to_16(UINT src, unsigned int flags)
1927 {
1928     HANDLE16    dst;
1929     UINT        sz = GlobalSize((HANDLE)src);
1930     LPSTR       ptr16, ptr32;
1931
1932     if (!(dst = GlobalAlloc16(flags, sz)))
1933         return 0;
1934     ptr32 = GlobalLock((HANDLE)src);
1935     ptr16 = GlobalLock16(dst);
1936     if (ptr16 != NULL && ptr32 != NULL) memcpy(ptr16, ptr32, sz);
1937     GlobalUnlock((HANDLE)src);
1938     GlobalUnlock16(dst);
1939
1940     return dst;
1941 }
1942
1943
1944 /**********************************************************************
1945  *           WINPROC_MapMsg32ATo16
1946  *
1947  * Map a message from 32-bit Ansi to 16-bit.
1948  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
1949  */
1950 INT WINPROC_MapMsg32ATo16( HWND hwnd, UINT msg32, WPARAM wParam32,
1951                              UINT16 *pmsg16, WPARAM16 *pwparam16,
1952                              LPARAM *plparam )
1953 {
1954     *pmsg16 = (UINT16)msg32;
1955     *pwparam16 = (WPARAM16)LOWORD(wParam32);
1956     switch(msg32)
1957     {
1958     case SBM_SETRANGE:
1959         *pmsg16 = SBM_SETRANGE16;
1960         *plparam = MAKELPARAM(wParam32, *plparam);
1961         *pwparam16 = 0;
1962         return 0;
1963
1964     case SBM_GETRANGE:
1965         *pmsg16 = SBM_GETRANGE16;
1966         return 1;
1967
1968     case BM_GETCHECK:
1969     case BM_SETCHECK:
1970     case BM_GETSTATE:
1971     case BM_SETSTATE:
1972     case BM_SETSTYLE:
1973         *pmsg16 = (UINT16)msg32 + (BM_GETCHECK16 - BM_GETCHECK);
1974         return 0;
1975
1976     case EM_GETSEL:
1977     case EM_GETRECT:
1978     case EM_SETRECT:
1979     case EM_SETRECTNP:
1980     case EM_SCROLL:
1981     case EM_LINESCROLL:
1982     case EM_SCROLLCARET:
1983     case EM_GETMODIFY:
1984     case EM_SETMODIFY:
1985     case EM_GETLINECOUNT:
1986     case EM_LINEINDEX:
1987     case EM_SETHANDLE:
1988     case EM_GETHANDLE:
1989     case EM_GETTHUMB:
1990     case EM_LINELENGTH:
1991     case EM_REPLACESEL:
1992     case EM_GETLINE:
1993     case EM_LIMITTEXT:
1994     case EM_CANUNDO:
1995     case EM_UNDO:
1996     case EM_FMTLINES:
1997     case EM_LINEFROMCHAR:
1998     case EM_SETTABSTOPS:
1999     case EM_SETPASSWORDCHAR:
2000     case EM_EMPTYUNDOBUFFER:
2001     case EM_GETFIRSTVISIBLELINE:
2002     case EM_SETREADONLY:
2003     case EM_SETWORDBREAKPROC:
2004     case EM_GETWORDBREAKPROC:
2005     case EM_GETPASSWORDCHAR:
2006         *pmsg16 = (UINT16)msg32 + (EM_GETSEL16 - EM_GETSEL);
2007         return 0;
2008
2009     case LB_CARETOFF:
2010     case LB_CARETON:
2011     case LB_DELETESTRING:
2012     case LB_GETANCHORINDEX:
2013     case LB_GETCARETINDEX:
2014     case LB_GETCOUNT:
2015     case LB_GETCURSEL:
2016     case LB_GETHORIZONTALEXTENT:
2017     case LB_GETITEMDATA:
2018     case LB_GETITEMHEIGHT:
2019     case LB_GETSEL:
2020     case LB_GETSELCOUNT:
2021     case LB_GETTEXTLEN:
2022     case LB_GETTOPINDEX:
2023     case LB_RESETCONTENT:
2024     case LB_SELITEMRANGE:
2025     case LB_SELITEMRANGEEX:
2026     case LB_SETANCHORINDEX:
2027     case LB_SETCARETINDEX:
2028     case LB_SETCOLUMNWIDTH:
2029     case LB_SETCURSEL:
2030     case LB_SETHORIZONTALEXTENT:
2031     case LB_SETITEMDATA:
2032     case LB_SETITEMHEIGHT:
2033     case LB_SETSEL:
2034     case LB_SETTOPINDEX:
2035         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2036         return 0;
2037     case CB_DELETESTRING:
2038     case CB_GETCOUNT:
2039     case CB_GETLBTEXTLEN:
2040     case CB_LIMITTEXT:
2041     case CB_RESETCONTENT:
2042     case CB_SETEDITSEL:
2043     case CB_GETCURSEL:
2044     case CB_SETCURSEL:
2045     case CB_SHOWDROPDOWN:
2046     case CB_SETITEMDATA:
2047     case CB_SETITEMHEIGHT:
2048     case CB_GETITEMHEIGHT:
2049     case CB_SETEXTENDEDUI:
2050     case CB_GETEXTENDEDUI:
2051     case CB_GETDROPPEDSTATE:
2052         *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2053         return 0;
2054     case CB_GETEDITSEL:
2055         *pmsg16 = CB_GETEDITSEL16;
2056         return 1;
2057
2058     case LB_ADDSTRING:
2059     case LB_FINDSTRING:
2060     case LB_FINDSTRINGEXACT:
2061     case LB_INSERTSTRING:
2062     case LB_SELECTSTRING:
2063     case LB_DIR:
2064     case LB_ADDFILE:
2065         *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2066         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2067         return 1;
2068
2069     case CB_ADDSTRING:
2070     case CB_FINDSTRING:
2071     case CB_FINDSTRINGEXACT:
2072     case CB_INSERTSTRING:
2073     case CB_SELECTSTRING:
2074     case CB_DIR:
2075         *plparam = (LPARAM)MapLS( (LPSTR)*plparam );
2076         *pmsg16 = (UINT16)msg32 + (CB_GETEDITSEL16 - CB_GETEDITSEL);
2077         return 1;
2078
2079     case LB_GETITEMRECT:
2080         {
2081             RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2082             if (!rect) return -1;
2083             *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2084             *plparam = MapLS( rect );
2085         }
2086         *pmsg16 = LB_GETITEMRECT16;
2087         return 1;
2088     case LB_GETSELITEMS:
2089         {
2090             LPARAM *items; /* old LPARAM first, then *pwparam16 x INT16 entries */
2091
2092             *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2093             if (!(items = HeapAlloc( GetProcessHeap(), 0,
2094                                      *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2095             *items++ = *plparam;  /* Store the previous lParam */
2096             *plparam = MapLS( items );
2097         }
2098         *pmsg16 = LB_GETSELITEMS16;
2099         return 1;
2100     case LB_SETTABSTOPS:
2101         if (wParam32)
2102         {
2103             INT i;
2104             LPINT16 stops;
2105             *pwparam16 = (WPARAM16)min( wParam32, 0x7f80 ); /* Must be < 64K */
2106             if (!(stops = HeapAlloc( GetProcessHeap(), 0,
2107                                      *pwparam16 * sizeof(INT16) + sizeof(LPARAM)))) return -1;
2108             for (i = 0; i < *pwparam16; i++) stops[i] = *((LPINT)*plparam+i);
2109             *plparam = MapLS( stops );
2110             return 1;
2111         }
2112         *pmsg16 = LB_SETTABSTOPS16;
2113         return 0;
2114
2115     case CB_GETDROPPEDCONTROLRECT:
2116         {
2117             RECT16 *rect = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT16) + sizeof(LPARAM) );
2118             if (!rect) return -1;
2119             *(LPARAM *)(rect + 1) = *plparam;  /* Store the previous lParam */
2120             *plparam = (LPARAM)MapLS(rect);
2121         }
2122         *pmsg16 = CB_GETDROPPEDCONTROLRECT16;
2123         return 1;
2124
2125     case LB_GETTEXT:
2126         *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2127         *pmsg16 = LB_GETTEXT16;
2128         return 1;
2129
2130     case CB_GETLBTEXT:
2131         *plparam = (LPARAM)MapLS( (LPVOID)(*plparam) );
2132         *pmsg16 = CB_GETLBTEXT16;
2133         return 1;
2134
2135     case EM_SETSEL:
2136         *pwparam16 = 0;
2137         *plparam = MAKELONG( (INT16)(INT)wParam32, (INT16)*plparam );
2138         *pmsg16 = EM_SETSEL16;
2139         return 0;
2140
2141     case WM_ACTIVATE:
2142     case WM_CHARTOITEM:
2143     case WM_COMMAND:
2144     case WM_VKEYTOITEM:
2145         *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2146         return 0;
2147     case WM_HSCROLL:
2148     case WM_VSCROLL:
2149         *plparam = MAKELPARAM( HIWORD(wParam32), (HWND16)*plparam );
2150         return 0;
2151     case WM_CTLCOLORMSGBOX:
2152     case WM_CTLCOLOREDIT:
2153     case WM_CTLCOLORLISTBOX:
2154     case WM_CTLCOLORBTN:
2155     case WM_CTLCOLORDLG:
2156     case WM_CTLCOLORSCROLLBAR:
2157     case WM_CTLCOLORSTATIC:
2158         *pmsg16  = WM_CTLCOLOR;
2159         *plparam = MAKELPARAM( (HWND16)*plparam,
2160                                (WORD)msg32 - WM_CTLCOLORMSGBOX );
2161         return 0;
2162     case WM_COMPAREITEM:
2163         {
2164             COMPAREITEMSTRUCT *cis32 = (COMPAREITEMSTRUCT *)*plparam;
2165             COMPAREITEMSTRUCT16 *cis = HeapAlloc( GetProcessHeap(), 0, sizeof(COMPAREITEMSTRUCT16));
2166             if (!cis) return -1;
2167             cis->CtlType    = (UINT16)cis32->CtlType;
2168             cis->CtlID      = (UINT16)cis32->CtlID;
2169             cis->hwndItem   = HWND_16( cis32->hwndItem );
2170             cis->itemID1    = (UINT16)cis32->itemID1;
2171             cis->itemData1  = cis32->itemData1;
2172             cis->itemID2    = (UINT16)cis32->itemID2;
2173             cis->itemData2  = cis32->itemData2;
2174             *plparam = MapLS( cis );
2175         }
2176         return 1;
2177     case WM_DELETEITEM:
2178         {
2179             DELETEITEMSTRUCT *dis32 = (DELETEITEMSTRUCT *)*plparam;
2180             DELETEITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DELETEITEMSTRUCT16) );
2181             if (!dis) return -1;
2182             dis->CtlType  = (UINT16)dis32->CtlType;
2183             dis->CtlID    = (UINT16)dis32->CtlID;
2184             dis->itemID   = (UINT16)dis32->itemID;
2185             dis->hwndItem = (dis->CtlType == ODT_MENU) ? (HWND16)LOWORD(dis32->hwndItem)
2186                                                        : HWND_16( dis32->hwndItem );
2187             dis->itemData = dis32->itemData;
2188             *plparam = MapLS( dis );
2189         }
2190         return 1;
2191     case WM_DRAWITEM:
2192         {
2193             DRAWITEMSTRUCT *dis32 = (DRAWITEMSTRUCT *)*plparam;
2194             DRAWITEMSTRUCT16 *dis = HeapAlloc( GetProcessHeap(), 0, sizeof(DRAWITEMSTRUCT16) );
2195             if (!dis) return -1;
2196             dis->CtlType       = (UINT16)dis32->CtlType;
2197             dis->CtlID         = (UINT16)dis32->CtlID;
2198             dis->itemID        = (UINT16)dis32->itemID;
2199             dis->itemAction    = (UINT16)dis32->itemAction;
2200             dis->itemState     = (UINT16)dis32->itemState;
2201             dis->hwndItem      = HWND_16( dis32->hwndItem );
2202             dis->hDC           = HDC_16(dis32->hDC);
2203             dis->itemData      = dis32->itemData;
2204             dis->rcItem.left   = dis32->rcItem.left;
2205             dis->rcItem.top    = dis32->rcItem.top;
2206             dis->rcItem.right  = dis32->rcItem.right;
2207             dis->rcItem.bottom = dis32->rcItem.bottom;
2208             *plparam = MapLS( dis );
2209         }
2210         return 1;
2211     case WM_MEASUREITEM:
2212         {
2213             MEASUREITEMSTRUCT *mis32 = (MEASUREITEMSTRUCT *)*plparam;
2214             MEASUREITEMSTRUCT16 *mis = HeapAlloc( GetProcessHeap(), 0, sizeof(*mis)+sizeof(LPARAM));
2215             if (!mis) return -1;
2216             mis->CtlType    = (UINT16)mis32->CtlType;
2217             mis->CtlID      = (UINT16)mis32->CtlID;
2218             mis->itemID     = (UINT16)mis32->itemID;
2219             mis->itemWidth  = (UINT16)mis32->itemWidth;
2220             mis->itemHeight = (UINT16)mis32->itemHeight;
2221             mis->itemData   = mis32->itemData;
2222             *(LPARAM *)(mis + 1) = *plparam;  /* Store the previous lParam */
2223             *plparam = MapLS( mis );
2224         }
2225         return 1;
2226     case WM_GETMINMAXINFO:
2227         {
2228             MINMAXINFO16 *mmi = HeapAlloc( GetProcessHeap(), 0, sizeof(*mmi) + sizeof(LPARAM) );
2229             if (!mmi) return -1;
2230             MINMAXINFO32to16( (MINMAXINFO *)*plparam, mmi );
2231             *(LPARAM *)(mmi + 1) = *plparam;  /* Store the previous lParam */
2232             *plparam = MapLS( mmi );
2233         }
2234         return 1;
2235     case WM_GETTEXT:
2236     case WM_ASKCBFORMATNAME:
2237         {
2238             LPARAM *str; /* store LPARAM, then *pwparam16 char space */
2239             *pwparam16 = (WPARAM16)min( wParam32, 0xff80 ); /* Must be < 64K */
2240             if (!(str = HeapAlloc( GetProcessHeap(), 0, *pwparam16 + sizeof(LPARAM)))) return -1;
2241             *str++ = *plparam;  /* Store the previous lParam */
2242             *plparam = MapLS( str );
2243         }
2244         return 1;
2245     case WM_MDICREATE:
2246         {
2247             MDICREATESTRUCT16 *cs;
2248             MDICREATESTRUCTA *cs32 = (MDICREATESTRUCTA *)*plparam;
2249
2250             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2251             MDICREATESTRUCT32Ato16( cs32, cs );
2252             cs->szTitle = MapLS( cs32->szTitle );
2253             cs->szClass = MapLS( cs32->szClass );
2254             *plparam = MapLS( cs );
2255         }
2256         return 1;
2257     case WM_MDIGETACTIVE:
2258         return 1;
2259     case WM_MDISETMENU:
2260         *plparam   = MAKELPARAM( (HMENU16)LOWORD(wParam32),
2261                                  (HMENU16)LOWORD(*plparam) );
2262         *pwparam16 = (*plparam == 0);
2263         return 0;
2264     case WM_MENUSELECT:
2265         if(HIWORD(wParam32) & MF_POPUP)
2266         {
2267             HMENU hmenu;
2268             if (((UINT)HIWORD(wParam32) != 0xFFFF) || (*plparam))
2269             {
2270                 if((hmenu = GetSubMenu((HMENU)*plparam, *pwparam16)))
2271                     *pwparam16=HMENU_16(hmenu);
2272             }
2273         }
2274         /* fall through */
2275     case WM_MENUCHAR:
2276         *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2277         return 0;
2278     case WM_MDIACTIVATE:
2279         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_MDICHILD)
2280         {
2281             *pwparam16 = ((HWND)*plparam == hwnd);
2282             *plparam = MAKELPARAM( (HWND16)LOWORD(*plparam),
2283                                    (HWND16)LOWORD(wParam32) );
2284         }
2285         else
2286         {
2287             *pwparam16 = HWND_16( (HWND)wParam32 );
2288             *plparam = 0;
2289         }
2290         return 0;
2291     case WM_NCCALCSIZE:
2292         {
2293             NCCALCSIZE_PARAMS *nc32 = (NCCALCSIZE_PARAMS *)*plparam;
2294             NCCALCSIZE_PARAMS16 *nc = HeapAlloc( GetProcessHeap(), 0, sizeof(*nc) + sizeof(LPARAM));
2295             if (!nc) return -1;
2296
2297             nc->rgrc[0].left   = nc32->rgrc[0].left;
2298             nc->rgrc[0].top    = nc32->rgrc[0].top;
2299             nc->rgrc[0].right  = nc32->rgrc[0].right;
2300             nc->rgrc[0].bottom = nc32->rgrc[0].bottom;
2301             if (wParam32)
2302             {
2303                 WINDOWPOS16 *wp;
2304                 nc->rgrc[1].left   = nc32->rgrc[1].left;
2305                 nc->rgrc[1].top    = nc32->rgrc[1].top;
2306                 nc->rgrc[1].right  = nc32->rgrc[1].right;
2307                 nc->rgrc[1].bottom = nc32->rgrc[1].bottom;
2308                 nc->rgrc[2].left   = nc32->rgrc[2].left;
2309                 nc->rgrc[2].top    = nc32->rgrc[2].top;
2310                 nc->rgrc[2].right  = nc32->rgrc[2].right;
2311                 nc->rgrc[2].bottom = nc32->rgrc[2].bottom;
2312                 if (!(wp = HeapAlloc( GetProcessHeap(), 0, sizeof(WINDOWPOS16) )))
2313                 {
2314                     HeapFree( GetProcessHeap(), 0, nc );
2315                     return -1;
2316                 }
2317                 WINDOWPOS32to16( nc32->lppos, wp );
2318                 nc->lppos = MapLS( wp );
2319             }
2320             *(LPARAM *)(nc + 1) = *plparam;  /* Store the previous lParam */
2321             *plparam = MapLS( nc );
2322         }
2323         return 1;
2324     case WM_NCCREATE:
2325     case WM_CREATE:
2326         {
2327             CREATESTRUCT16 *cs;
2328             CREATESTRUCTA *cs32 = (CREATESTRUCTA *)*plparam;
2329
2330             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2331             CREATESTRUCT32Ato16( cs32, cs );
2332             cs->lpszName  = MapLS( cs32->lpszName );
2333             cs->lpszClass = MapLS( cs32->lpszClass );
2334
2335             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2336             {
2337                 MDICREATESTRUCT16 *mdi_cs16;
2338                 MDICREATESTRUCTA *mdi_cs = (MDICREATESTRUCTA *)cs32->lpCreateParams;
2339                 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2340                 if (!mdi_cs16)
2341                 {
2342                     HeapFree(GetProcessHeap(), 0, cs);
2343                     return -1;
2344                 }
2345                 MDICREATESTRUCT32Ato16(mdi_cs, mdi_cs16);
2346                 mdi_cs16->szTitle = MapLS( mdi_cs->szTitle );
2347                 mdi_cs16->szClass = MapLS( mdi_cs->szClass );
2348                 cs->lpCreateParams = MapLS( mdi_cs16 );
2349             }
2350             *plparam = MapLS( cs );
2351         }
2352         return 1;
2353     case WM_PARENTNOTIFY:
2354         if ((LOWORD(wParam32)==WM_CREATE) || (LOWORD(wParam32)==WM_DESTROY))
2355             *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32));
2356         /* else nothing to do */
2357         return 0;
2358     case WM_NOTIFY:
2359         *plparam = MapLS( (NMHDR *)*plparam ); /* NMHDR is already 32-bit */
2360         return 1;
2361     case WM_SETTEXT:
2362     case WM_WININICHANGE:
2363     case WM_DEVMODECHANGE:
2364         *plparam = MapLS( (LPSTR)*plparam );
2365         return 1;
2366     case WM_WINDOWPOSCHANGING:
2367     case WM_WINDOWPOSCHANGED:
2368         {
2369             WINDOWPOS16 *wp = HeapAlloc( GetProcessHeap(), 0, sizeof(*wp) + sizeof(LPARAM) );
2370             if (!wp) return -1;
2371             WINDOWPOS32to16( (WINDOWPOS *)*plparam, wp );
2372             *(LPARAM *)(wp + 1) = *plparam;  /* Store the previous lParam */
2373             *plparam = MapLS( wp );
2374         }
2375         return 1;
2376     case WM_GETDLGCODE:
2377          if (*plparam) {
2378             LPMSG msg32 = (LPMSG) *plparam;
2379             LPMSG16 msg16 = HeapAlloc( GetProcessHeap(), 0, sizeof(MSG16) );
2380
2381             if (!msg16) return -1;
2382             msg16->hwnd = HWND_16( msg32->hwnd );
2383             msg16->lParam = msg32->lParam;
2384             msg16->time = msg32->time;
2385             msg16->pt.x = msg32->pt.x;
2386             msg16->pt.y = msg32->pt.y;
2387             /* this is right, right? */
2388             if (WINPROC_MapMsg32ATo16(msg32->hwnd,msg32->message,msg32->wParam,
2389                          &msg16->message,&msg16->wParam, &msg16->lParam)<0)
2390             {
2391                 HeapFree( GetProcessHeap(), 0, msg16 );
2392                 return -1;
2393             }
2394             *plparam = MapLS( msg16 );
2395             return 1;
2396         }
2397         return 0;
2398
2399     case WM_ACTIVATEAPP:
2400         if (*plparam) *plparam = HTASK_16( (HANDLE)*plparam );
2401         return 0;
2402     case WM_NEXTMENU:
2403         {
2404             MDINEXTMENU *next = (MDINEXTMENU *)*plparam;
2405             *plparam = (LPARAM)next->hmenuIn;
2406             return 1;
2407         }
2408     case WM_PAINT:
2409         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2410         {
2411             *pmsg16 = WM_PAINTICON;
2412             *pwparam16 = 1;
2413         }
2414         return 0;
2415     case WM_ERASEBKGND:
2416         if (IsIconic( hwnd ) && GetClassLongPtrW( hwnd, GCLP_HICON ))
2417             *pmsg16 = WM_ICONERASEBKGND;
2418         return 0;
2419     case WM_PAINTCLIPBOARD:
2420     case WM_SIZECLIPBOARD:
2421         FIXME_(msg)("message %04x needs translation\n", msg32 );
2422         return -1;
2423     /* following messages should not be sent to 16-bit apps */
2424     case WM_SIZING:
2425     case WM_MOVING:
2426     case WM_CAPTURECHANGED:
2427     case WM_STYLECHANGING:
2428     case WM_STYLECHANGED:
2429         return -1;
2430     case WM_DDE_INITIATE:
2431     case WM_DDE_TERMINATE:
2432     case WM_DDE_UNADVISE:
2433     case WM_DDE_REQUEST:
2434         *pwparam16 = HWND_16((HWND)wParam32);
2435         return 0;
2436     case WM_DDE_ADVISE:
2437     case WM_DDE_DATA:
2438     case WM_DDE_POKE:
2439         {
2440             UINT lo32, hi;
2441             HANDLE16    lo16 = 0;
2442
2443             *pwparam16 = HWND_16((HWND)wParam32);
2444             UnpackDDElParam(msg32, *plparam, &lo32, &hi);
2445             if (lo32 && !(lo16 = convert_handle_32_to_16(lo32, GMEM_DDESHARE)))
2446                 return -1;
2447             *plparam = MAKELPARAM(lo16, hi);
2448         }
2449         return 0; /* FIXME don't know how to free allocated memory (handle)  !! */
2450     case WM_DDE_ACK:
2451         {
2452             UINT        lo, hi;
2453             int         flag = 0;
2454             char        buf[2];
2455
2456             *pwparam16 = HWND_16((HWND)wParam32);
2457
2458             UnpackDDElParam(msg32, *plparam, &lo, &hi);
2459
2460             if (GlobalGetAtomNameA((ATOM)hi, buf, sizeof(buf)) > 0) flag |= 1;
2461             if (GlobalSize((HANDLE)hi) != 0) flag |= 2;
2462             switch (flag)
2463             {
2464             case 0:
2465                 if (hi)
2466                 {
2467                     MESSAGE("DDE_ACK: neither atom nor handle!!!\n");
2468                     hi = 0;
2469                 }
2470                 break;
2471             case 1:
2472                 break; /* atom, nothing to do */
2473             case 3:
2474                 MESSAGE("DDE_ACK: %x both atom and handle... choosing handle\n", hi);
2475                 /* fall thru */
2476             case 2:
2477                 hi = convert_handle_32_to_16(hi, GMEM_DDESHARE);
2478                 break;
2479             }
2480             *plparam = MAKELPARAM(lo, hi);
2481         }
2482         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2483     case WM_DDE_EXECUTE:
2484         *plparam = convert_handle_32_to_16(*plparam, GMEM_DDESHARE);
2485         return 0; /* FIXME don't know how to free allocated memory (handle) !! */
2486     default:  /* No translation needed */
2487         return 0;
2488     }
2489 }
2490
2491
2492 /**********************************************************************
2493  *           WINPROC_UnmapMsg32ATo16
2494  *
2495  * Unmap a message that was mapped from 32-bit Ansi to 16-bit.
2496  */
2497 void WINPROC_UnmapMsg32ATo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2498                               MSGPARAM16* p16 )
2499 {
2500     switch(msg)
2501     {
2502     case SBM_GETRANGE:
2503         *(LPINT)wParam = LOWORD(p16->lResult);
2504         *(LPINT)lParam = HIWORD(p16->lResult);
2505         break;
2506
2507     case LB_ADDFILE:
2508     case LB_ADDSTRING:
2509     case LB_DIR:
2510     case LB_FINDSTRING:
2511     case LB_FINDSTRINGEXACT:
2512     case LB_INSERTSTRING:
2513     case LB_SELECTSTRING:
2514     case LB_GETTEXT:
2515     case CB_ADDSTRING:
2516     case CB_FINDSTRING:
2517     case CB_FINDSTRINGEXACT:
2518     case CB_INSERTSTRING:
2519     case CB_SELECTSTRING:
2520     case CB_DIR:
2521     case CB_GETLBTEXT:
2522     case WM_SETTEXT:
2523     case WM_WININICHANGE:
2524     case WM_DEVMODECHANGE:
2525         UnMapLS( (SEGPTR)p16->lParam );
2526         break;
2527     case LB_SETTABSTOPS:
2528     case WM_COMPAREITEM:
2529     case WM_DELETEITEM:
2530     case WM_DRAWITEM:
2531         {
2532             void *ptr = MapSL( p16->lParam );
2533             UnMapLS( p16->lParam );
2534             HeapFree( GetProcessHeap(), 0, ptr );
2535         }
2536         break;
2537     case CB_GETDROPPEDCONTROLRECT:
2538     case LB_GETITEMRECT:
2539         {
2540             RECT *r32;
2541             RECT16 *rect = MapSL(p16->lParam);
2542             UnMapLS( p16->lParam );
2543             p16->lParam = *(LPARAM *)(rect + 1);
2544             r32 = (RECT *)p16->lParam;
2545             r32->left   = rect->left;
2546             r32->top    = rect->top;
2547             r32->right  = rect->right;
2548             r32->bottom = rect->bottom;
2549             HeapFree( GetProcessHeap(), 0, rect );
2550         }
2551         break;
2552     case LB_GETSELITEMS:
2553         {
2554             INT i;
2555             LPINT16 items = MapSL(p16->lParam);
2556             UnMapLS( p16->lParam );
2557             p16->lParam = *((LPARAM *)items - 1);
2558             for (i = 0; i < p16->wParam; i++) *((LPINT)(p16->lParam) + i) = items[i];
2559             HeapFree( GetProcessHeap(), 0, (LPARAM *)items - 1 );
2560         }
2561         break;
2562
2563     case CB_GETEDITSEL:
2564         if( wParam )
2565             *((PUINT)(wParam)) = LOWORD(p16->lResult);
2566         if( lParam )
2567             *((PUINT)(lParam)) = HIWORD(p16->lResult);  /* FIXME: substract 1? */
2568         break;
2569
2570     case WM_MEASUREITEM:
2571         {
2572             MEASUREITEMSTRUCT16 *mis = MapSL(p16->lParam);
2573             MEASUREITEMSTRUCT *mis32 = *(MEASUREITEMSTRUCT **)(mis + 1);
2574             mis32->itemWidth  = mis->itemWidth;
2575             mis32->itemHeight = mis->itemHeight;
2576             UnMapLS( p16->lParam );
2577             HeapFree( GetProcessHeap(), 0, mis );
2578         }
2579         break;
2580     case WM_GETMINMAXINFO:
2581         {
2582             MINMAXINFO16 *mmi = MapSL(p16->lParam);
2583             UnMapLS( p16->lParam );
2584             p16->lParam = *(LPARAM *)(mmi + 1);
2585             MINMAXINFO16to32( mmi, (MINMAXINFO *)(p16->lParam) );
2586             HeapFree( GetProcessHeap(), 0, mmi );
2587         }
2588         break;
2589     case WM_GETTEXT:
2590     case WM_ASKCBFORMATNAME:
2591         {
2592             LPSTR str = MapSL(p16->lParam);
2593             UnMapLS( p16->lParam );
2594             p16->lParam = *((LPARAM *)str - 1);
2595             lstrcpynA( (LPSTR)(p16->lParam), str, p16->wParam );
2596             HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2597         }
2598         break;
2599     case WM_MDICREATE:
2600         {
2601             MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2602             UnMapLS( cs->szTitle );
2603             UnMapLS( cs->szClass );
2604             UnMapLS( p16->lParam );
2605             HeapFree( GetProcessHeap(), 0, cs );
2606         }
2607         break;
2608     case WM_MDIGETACTIVE:
2609         if (lParam) *(BOOL *)lParam = (BOOL16)HIWORD(p16->lResult);
2610         p16->lResult = (LRESULT)WIN_Handle32( LOWORD(p16->lResult) );
2611         break;
2612     case WM_NCCALCSIZE:
2613         {
2614             NCCALCSIZE_PARAMS *nc32;
2615             NCCALCSIZE_PARAMS16 *nc = MapSL(p16->lParam);
2616             UnMapLS( p16->lParam );
2617             p16->lParam = *(LPARAM *)(nc + 1);
2618             nc32 = (NCCALCSIZE_PARAMS *)(p16->lParam);
2619             nc32->rgrc[0].left   = nc->rgrc[0].left;
2620             nc32->rgrc[0].top    = nc->rgrc[0].top;
2621             nc32->rgrc[0].right  = nc->rgrc[0].right;
2622             nc32->rgrc[0].bottom = nc->rgrc[0].bottom;
2623             if (p16->wParam)
2624             {
2625                 WINDOWPOS16 *pos = MapSL(nc->lppos);
2626                 UnMapLS( nc->lppos );
2627                 nc32->rgrc[1].left   = nc->rgrc[1].left;
2628                 nc32->rgrc[1].top    = nc->rgrc[1].top;
2629                 nc32->rgrc[1].right  = nc->rgrc[1].right;
2630                 nc32->rgrc[1].bottom = nc->rgrc[1].bottom;
2631                 nc32->rgrc[2].left   = nc->rgrc[2].left;
2632                 nc32->rgrc[2].top    = nc->rgrc[2].top;
2633                 nc32->rgrc[2].right  = nc->rgrc[2].right;
2634                 nc32->rgrc[2].bottom = nc->rgrc[2].bottom;
2635                 WINDOWPOS16to32( pos, nc32->lppos );
2636                 HeapFree( GetProcessHeap(), 0, pos );
2637             }
2638             HeapFree( GetProcessHeap(), 0, nc );
2639         }
2640         break;
2641     case WM_NCCREATE:
2642     case WM_CREATE:
2643         {
2644             CREATESTRUCT16 *cs = MapSL(p16->lParam);
2645             UnMapLS( p16->lParam );
2646             UnMapLS( cs->lpszName );
2647             UnMapLS( cs->lpszClass );
2648             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2649             {
2650                 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2651                 UnMapLS( cs->lpCreateParams );
2652                 UnMapLS( mdi_cs16->szTitle );
2653                 UnMapLS( mdi_cs16->szClass );
2654                 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2655             }
2656             HeapFree( GetProcessHeap(), 0, cs );
2657         }
2658         break;
2659     case WM_WINDOWPOSCHANGING:
2660     case WM_WINDOWPOSCHANGED:
2661         {
2662             WINDOWPOS16 *wp = MapSL(p16->lParam);
2663             UnMapLS( p16->lParam );
2664             p16->lParam = *(LPARAM *)(wp + 1);
2665             WINDOWPOS16to32( wp, (WINDOWPOS *)p16->lParam );
2666             HeapFree( GetProcessHeap(), 0, wp );
2667         }
2668         break;
2669     case WM_NOTIFY:
2670         UnMapLS(p16->lParam);
2671         break;
2672     case WM_GETDLGCODE:
2673         if (p16->lParam)
2674         {
2675             LPMSG16 msg16 = MapSL(p16->lParam);
2676             MSGPARAM16 msgp16;
2677             UnMapLS( p16->lParam );
2678             msgp16.wParam=msg16->wParam;
2679             msgp16.lParam=msg16->lParam;
2680             WINPROC_UnmapMsg32ATo16(((LPMSG)lParam)->hwnd, ((LPMSG)lParam)->message,
2681                     ((LPMSG)lParam)->wParam, ((LPMSG)lParam)->lParam,
2682                     &msgp16 );
2683             HeapFree( GetProcessHeap(), 0, msg16 );
2684         }
2685         break;
2686     case WM_NEXTMENU:
2687         {
2688             MDINEXTMENU *next = (MDINEXTMENU *)lParam;
2689             next->hmenuNext = HMENU_32( LOWORD(p16->lResult) );
2690             next->hwndNext = WIN_Handle32( HIWORD(p16->lResult) );
2691             p16->lResult = 0;
2692         }
2693         break;
2694     }
2695 }
2696
2697
2698 /**********************************************************************
2699  *           WINPROC_MapMsg32WTo16
2700  *
2701  * Map a message from 32-bit Unicode to 16-bit.
2702  * Return value is -1 on error, 0 if OK, 1 if an UnmapMsg call is needed.
2703  */
2704 INT WINPROC_MapMsg32WTo16( HWND hwnd, UINT msg32, WPARAM wParam32,
2705                              UINT16 *pmsg16, WPARAM16 *pwparam16,
2706                              LPARAM *plparam )
2707 {
2708     BYTE ch;
2709     WCHAR wch;
2710
2711     *pmsg16    = LOWORD(msg32);
2712     *pwparam16 = LOWORD(wParam32);
2713     switch(msg32)
2714     {
2715     case LB_ADDSTRING:
2716     case LB_FINDSTRING:
2717     case LB_FINDSTRINGEXACT:
2718     case LB_INSERTSTRING:
2719     case LB_SELECTSTRING:
2720     case LB_DIR:
2721     case LB_ADDFILE:
2722         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2723         *pmsg16 = (UINT16)msg32 + (LB_ADDSTRING16 - LB_ADDSTRING);
2724         return 1;
2725
2726     case CB_ADDSTRING:
2727     case CB_FINDSTRING:
2728     case CB_FINDSTRINGEXACT:
2729     case CB_INSERTSTRING:
2730     case CB_SELECTSTRING:
2731     case CB_DIR:
2732         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2733         *pmsg16 = (UINT16)msg32 + (CB_ADDSTRING16 - CB_ADDSTRING);
2734         return 1;
2735
2736     case WM_NCCREATE:
2737     case WM_CREATE:
2738         {
2739             CREATESTRUCT16 *cs;
2740             CREATESTRUCTW *cs32 = (CREATESTRUCTW *)*plparam;
2741
2742             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(CREATESTRUCT16) ))) return -1;
2743             CREATESTRUCT32Ato16( (CREATESTRUCTA *)cs32, cs );
2744             cs->lpszName  = map_str_32W_to_16( cs32->lpszName );
2745             cs->lpszClass = map_str_32W_to_16( cs32->lpszClass );
2746
2747             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2748             {
2749                 MDICREATESTRUCT16 *mdi_cs16;
2750                 MDICREATESTRUCTW *mdi_cs = (MDICREATESTRUCTW *)cs32->lpCreateParams;
2751                 mdi_cs16 = HeapAlloc(GetProcessHeap(), 0, sizeof(*mdi_cs16));
2752                 if (!mdi_cs16)
2753                 {
2754                     HeapFree(GetProcessHeap(), 0, cs);
2755                     return -1;
2756                 }
2757                 MDICREATESTRUCT32Ato16((MDICREATESTRUCTA *)mdi_cs, mdi_cs16);
2758                 mdi_cs16->szTitle = map_str_32W_to_16(mdi_cs->szTitle);
2759                 mdi_cs16->szClass = map_str_32W_to_16(mdi_cs->szClass);
2760                 cs->lpCreateParams = MapLS(mdi_cs16);
2761             }
2762             *plparam   = MapLS(cs);
2763         }
2764         return 1;
2765     case WM_MDICREATE:
2766         {
2767             MDICREATESTRUCT16 *cs;
2768             MDICREATESTRUCTW *cs32 = (MDICREATESTRUCTW *)*plparam;
2769
2770             if (!(cs = HeapAlloc( GetProcessHeap(), 0, sizeof(MDICREATESTRUCT16) ))) return -1;
2771             MDICREATESTRUCT32Ato16( (MDICREATESTRUCTA *)cs32, cs );
2772             cs->szTitle = map_str_32W_to_16( cs32->szTitle );
2773             cs->szClass = map_str_32W_to_16( cs32->szClass );
2774             *plparam   = MapLS(cs);
2775         }
2776         return 1;
2777     case WM_SETTEXT:
2778     case WM_WININICHANGE:
2779     case WM_DEVMODECHANGE:
2780         *plparam = map_str_32W_to_16( (LPWSTR)*plparam );
2781         return 1;
2782     case LB_GETTEXT:
2783         if ( WINPROC_TestLBForStr( hwnd ))
2784         {
2785             LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2786             if (!str) return -1;
2787             *pmsg16    = LB_GETTEXT16;
2788             *plparam   = (LPARAM)MapLS(str);
2789         }
2790         return 1;
2791     case CB_GETLBTEXT:
2792         if ( WINPROC_TestCBForStr( hwnd ))
2793         {
2794             LPSTR str = HeapAlloc( GetProcessHeap(), 0, 256 ); /* FIXME: fixed sized buffer */
2795             if (!str) return -1;
2796             *pmsg16    = CB_GETLBTEXT16;
2797             *plparam   = (LPARAM)MapLS(str);
2798         }
2799         return 1;
2800
2801     case WM_CHARTOITEM:
2802         wch = LOWORD(wParam32);
2803         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2804         *pwparam16 = ch;
2805         *plparam = MAKELPARAM( (HWND16)*plparam, HIWORD(wParam32) );
2806         return 0;
2807     case WM_MENUCHAR:
2808         wch = LOWORD(wParam32);
2809         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2810         *pwparam16 = ch;
2811         *plparam = MAKELPARAM( HIWORD(wParam32), (HMENU16)*plparam );
2812         return 0;
2813     case WM_CHAR:
2814     case WM_DEADCHAR:
2815     case WM_SYSCHAR:
2816     case WM_SYSDEADCHAR:
2817         wch = wParam32;
2818         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL);
2819         *pwparam16 = ch;
2820         return 0;
2821     case WM_IME_CHAR:
2822         {
2823             BYTE ch[2];
2824
2825             wch = wParam32;
2826             if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL ) == 2)
2827                 *pwparam16 = (ch[0] << 8) | ch[1];
2828             else
2829                 *pwparam16 = ch[0];
2830         }
2831         return 0;
2832
2833     default:  /* No Unicode translation needed (?) */
2834         return WINPROC_MapMsg32ATo16( hwnd, msg32, wParam32, pmsg16,
2835                                       pwparam16, plparam );
2836     }
2837 }
2838
2839
2840 /**********************************************************************
2841  *           WINPROC_UnmapMsg32WTo16
2842  *
2843  * Unmap a message that was mapped from 32-bit Unicode to 16-bit.
2844  */
2845 void WINPROC_UnmapMsg32WTo16( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
2846                               MSGPARAM16* p16 )
2847 {
2848     switch(msg)
2849     {
2850     case LB_ADDSTRING:
2851     case LB_FINDSTRING:
2852     case LB_FINDSTRINGEXACT:
2853     case LB_INSERTSTRING:
2854     case LB_SELECTSTRING:
2855     case LB_DIR:
2856     case LB_ADDFILE:
2857     case CB_ADDSTRING:
2858     case CB_FINDSTRING:
2859     case CB_FINDSTRINGEXACT:
2860     case CB_INSERTSTRING:
2861     case CB_SELECTSTRING:
2862     case CB_DIR:
2863     case WM_SETTEXT:
2864     case WM_WININICHANGE:
2865     case WM_DEVMODECHANGE:
2866         unmap_str_32W_to_16( p16->lParam );
2867         break;
2868     case WM_NCCREATE:
2869     case WM_CREATE:
2870         {
2871             CREATESTRUCT16 *cs = MapSL(p16->lParam);
2872             UnMapLS( p16->lParam );
2873             unmap_str_32W_to_16( cs->lpszName );
2874             unmap_str_32W_to_16( cs->lpszClass );
2875
2876             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
2877             {
2878                 MDICREATESTRUCT16 *mdi_cs16 = (MDICREATESTRUCT16 *)MapSL(cs->lpCreateParams);
2879                 UnMapLS( cs->lpCreateParams );
2880                 unmap_str_32W_to_16(mdi_cs16->szTitle);
2881                 unmap_str_32W_to_16(mdi_cs16->szClass);
2882                 HeapFree(GetProcessHeap(), 0, mdi_cs16);
2883             }
2884             HeapFree( GetProcessHeap(), 0, cs );
2885         }
2886         break;
2887     case WM_MDICREATE:
2888         {
2889             MDICREATESTRUCT16 *cs = MapSL(p16->lParam);
2890             UnMapLS( p16->lParam );
2891             unmap_str_32W_to_16( cs->szTitle );
2892             unmap_str_32W_to_16( cs->szClass );
2893             HeapFree( GetProcessHeap(), 0, cs );
2894         }
2895         break;
2896     case WM_GETTEXT:
2897     case WM_ASKCBFORMATNAME:
2898         {
2899             LPSTR str = MapSL(p16->lParam);
2900             UnMapLS( p16->lParam );
2901             p16->lParam = *((LPARAM *)str - 1);
2902             MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)p16->lParam, 0x7fffffff );
2903             p16->lResult = strlenW( (LPWSTR)p16->lParam );
2904             HeapFree( GetProcessHeap(), 0, (LPARAM *)str - 1 );
2905         }
2906         break;
2907     case LB_GETTEXT:
2908         if ( WINPROC_TestLBForStr( hwnd ))
2909         {
2910             LPSTR str = MapSL(p16->lParam);
2911             UnMapLS( p16->lParam );
2912             p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2913             HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2914         }
2915         break;
2916     case CB_GETLBTEXT:
2917         if ( WINPROC_TestCBForStr( hwnd ))
2918         {
2919             LPSTR str = MapSL(p16->lParam);
2920             UnMapLS( p16->lParam );
2921             p16->lResult = MultiByteToWideChar( CP_ACP, 0, str, -1, (LPWSTR)lParam, 0x7fffffff ) - 1;
2922             HeapFree( GetProcessHeap(), 0, (LPARAM *)str );
2923         }
2924         break;
2925     default:
2926         WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, p16 );
2927         break;
2928     }
2929 }
2930
2931
2932 /**********************************************************************
2933  *           WINPROC_CallProc32ATo32W
2934  *
2935  * Call a window procedure, translating args from Ansi to Unicode.
2936  */
2937 static LRESULT WINPROC_CallProc32ATo32W( WNDPROC func, HWND hwnd,
2938                                          UINT msg, WPARAM wParam,
2939                                          LPARAM lParam )
2940 {
2941     LRESULT result;
2942     int unmap;
2943
2944     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
2945         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
2946
2947     if( (unmap = WINPROC_MapMsg32ATo32W( hwnd, msg, &wParam, &lParam )) == -1) {
2948         ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
2949                        SPY_GetMsgName(msg, hwnd), wParam, lParam );
2950         return 0;
2951     }
2952     result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
2953     if (unmap) result = WINPROC_UnmapMsg32ATo32W( hwnd, msg, wParam, lParam, result );
2954     return result;
2955 }
2956
2957
2958 /**********************************************************************
2959  *           WINPROC_CallProc32WTo32A_fast
2960  *
2961  */
2962 static BOOL WINPROC_CallProc32WTo32A_fast( WNDPROC func, HWND hwnd,
2963                                            UINT msg, WPARAM wParam,
2964                                            LPARAM lParam, LRESULT *result )
2965 {
2966     switch(msg)
2967     {
2968     case WM_NCCREATE:
2969     case WM_CREATE:
2970         {   /* csW->lpszName and csW->lpszClass are NOT supposed to be atoms
2971              * at this point.
2972              */
2973             char buffer[1024];
2974             char *cls = buffer, *name;
2975             CREATESTRUCTW *csW = (CREATESTRUCTW *)lParam;
2976             CREATESTRUCTA csA = *(CREATESTRUCTA *)csW;
2977             DWORD name_lenA, name_lenW, class_lenA, class_lenW;
2978
2979             class_lenW = strlenW(csW->lpszClass) * sizeof(WCHAR);
2980             RtlUnicodeToMultiByteSize(&class_lenA, csW->lpszClass, class_lenW);
2981
2982             if (csW->lpszName)
2983             {
2984                 name_lenW = strlenW(csW->lpszName) * sizeof(WCHAR);
2985                 RtlUnicodeToMultiByteSize(&name_lenA, csW->lpszName, name_lenW);
2986             }
2987             else
2988                 name_lenW = name_lenA = 0;
2989
2990             if (class_lenA + name_lenA + 2 > sizeof(buffer))
2991             {
2992                 cls = HeapAlloc(GetProcessHeap(), 0, class_lenA + name_lenA + 2);
2993                 if (!cls) return FALSE;
2994             }
2995
2996             RtlUnicodeToMultiByteN(cls, class_lenA, NULL, csW->lpszClass, class_lenW);
2997             cls[class_lenA] = 0;
2998             csA.lpszClass = cls;
2999
3000             if (csW->lpszName)
3001             {
3002                 name = cls + class_lenA + 1;
3003                 RtlUnicodeToMultiByteN(name, name_lenA, NULL, csW->lpszName, name_lenW);
3004                 name[name_lenA] = 0;
3005                 csA.lpszName = name;
3006             }
3007
3008             if (GetWindowLongW(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD)
3009             {
3010                 MDICREATESTRUCTA mdi_cs;
3011
3012                 mdi_cs = *(MDICREATESTRUCTA *)csW->lpCreateParams;
3013                 mdi_cs.szTitle = csA.lpszName;
3014                 mdi_cs.szClass = csA.lpszClass;
3015                 csA.lpCreateParams = &mdi_cs;
3016             }
3017
3018             lParam = (LPARAM)&csA;
3019             *result = WINPROC_CallWndProc(func, hwnd, msg, wParam, lParam);
3020
3021             if (cls != buffer) HeapFree(GetProcessHeap(), 0, cls);
3022         }
3023         return TRUE;
3024
3025     default:
3026         return FALSE;
3027     }
3028 }
3029
3030 /**********************************************************************
3031  *           WINPROC_CallProc32WTo32A
3032  *
3033  * Call a window procedure, translating args from Unicode to Ansi.
3034  */
3035 static LRESULT WINPROC_CallProc32WTo32A( WNDPROC func, HWND hwnd,
3036                                          UINT msg, WPARAM wParam,
3037                                          LPARAM lParam )
3038 {
3039     LRESULT result;
3040     int unmap;
3041
3042     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3043         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3044
3045     if (WINPROC_CallProc32WTo32A_fast( func, hwnd, msg, wParam, lParam, &result ))
3046         return result;
3047
3048     if ((unmap = WINPROC_MapMsg32WTo32A( hwnd, msg, &wParam, &lParam )) == -1) {
3049         ERR_(msg)("Message translation failed. (msg=%s,wp=%08x,lp=%08lx)\n",
3050                        SPY_GetMsgName(msg, hwnd), wParam, lParam );
3051         return 0;
3052     }
3053     result = WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3054     if( unmap ) result = WINPROC_UnmapMsg32WTo32A( hwnd, msg, wParam, lParam, result );
3055     return result;
3056 }
3057
3058
3059 /**********************************************************************
3060  *           __wine_call_wndproc_32A   (USER.1010)
3061  */
3062 LRESULT WINAPI __wine_call_wndproc_32A( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3063                                         WNDPROC func )
3064 {
3065     LRESULT result;
3066     UINT msg32;
3067     WPARAM wParam32;
3068     HWND hwnd32 = WIN_Handle32( hwnd );
3069
3070     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3071                  func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3072
3073     if (WINPROC_MapMsg16To32A( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3074         return 0;
3075     result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3076     return WINPROC_UnmapMsg16To32A( hwnd32, msg32, wParam32, lParam, result );
3077 }
3078
3079
3080 /**********************************************************************
3081  *           __wine_call_wndproc_32W   (USER.1011)
3082  */
3083 LRESULT WINAPI  __wine_call_wndproc_32W( HWND16 hwnd, UINT16 msg, WPARAM16 wParam, LPARAM lParam,
3084                                          WNDPROC func )
3085 {
3086     LRESULT result;
3087     UINT msg32;
3088     WPARAM wParam32;
3089     HWND hwnd32 = WIN_Handle32( hwnd );
3090
3091     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3092                  func, hwnd32, SPY_GetMsgName(msg, hwnd32), wParam, lParam);
3093
3094     if (WINPROC_MapMsg16To32W( hwnd32, msg, wParam, &msg32, &wParam32, &lParam ) == -1)
3095         return 0;
3096     result = WINPROC_CallWndProc( func, hwnd32, msg32, wParam32, lParam );
3097     return WINPROC_UnmapMsg16To32W( hwnd32, msg32, wParam32, lParam, result );
3098 }
3099
3100
3101 /**********************************************************************
3102  *           WINPROC_CallProc32ATo16
3103  *
3104  * Call a 16-bit window procedure, translating the 32-bit args.
3105  */
3106 static LRESULT WINAPI WINPROC_CallProc32ATo16( WNDPROC16 func, HWND hwnd,
3107                                                UINT msg, WPARAM wParam,
3108                                                LPARAM lParam )
3109 {
3110     UINT16 msg16;
3111     MSGPARAM16 mp16;
3112
3113     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3114         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3115
3116     mp16.lParam = lParam;
3117     if (WINPROC_MapMsg32ATo16( hwnd, msg, wParam, &msg16, &mp16.wParam, &mp16.lParam ) == -1)
3118         return 0;
3119     mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3120                                           mp16.wParam, mp16.lParam );
3121     WINPROC_UnmapMsg32ATo16( hwnd, msg, wParam, lParam, &mp16 );
3122     return mp16.lResult;
3123 }
3124
3125
3126 /**********************************************************************
3127  *           WINPROC_CallProc32WTo16
3128  *
3129  * Call a 16-bit window procedure, translating the 32-bit args.
3130  */
3131 static LRESULT WINAPI WINPROC_CallProc32WTo16( WNDPROC16 func, HWND hwnd,
3132                                                UINT msg, WPARAM wParam,
3133                                                LPARAM lParam )
3134 {
3135     UINT16 msg16;
3136     MSGPARAM16 mp16;
3137
3138     TRACE_(msg)("func %p (hwnd=%p,msg=%s,wp=%08x,lp=%08lx)\n",
3139         func, hwnd, SPY_GetMsgName(msg, hwnd), wParam, lParam);
3140
3141     mp16.lParam = lParam;
3142     if (WINPROC_MapMsg32WTo16( hwnd, msg, wParam, &msg16, &mp16.wParam,
3143                                &mp16.lParam ) == -1)
3144         return 0;
3145     mp16.lResult = WINPROC_CallWndProc16( func, HWND_16(hwnd), msg16,
3146                                           mp16.wParam, mp16.lParam );
3147     WINPROC_UnmapMsg32WTo16( hwnd, msg, wParam, lParam, &mp16 );
3148     return mp16.lResult;
3149 }
3150
3151
3152 /**********************************************************************
3153  *              CallWindowProc (USER.122)
3154  */
3155 LRESULT WINAPI CallWindowProc16( WNDPROC16 func, HWND16 hwnd, UINT16 msg,
3156                                  WPARAM16 wParam, LPARAM lParam )
3157 {
3158     WINDOWPROC *proc;
3159
3160     if (!func) return 0;
3161
3162     if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3163         return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3164
3165 #if testing
3166     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_16 );
3167     return WINPROC_CallWndProc16( func, hwnd, msg, wParam, lParam );
3168 #endif
3169
3170     switch(proc->type)
3171     {
3172     case WIN_PROC_16:
3173         if (!proc->thunk.t_from32.proc) return 0;
3174         return WINPROC_CallWndProc16( proc->thunk.t_from32.proc,
3175                                       hwnd, msg, wParam, lParam );
3176     case WIN_PROC_32A:
3177         if (!proc->thunk.t_from16.proc) return 0;
3178         return __wine_call_wndproc_32A( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3179     case WIN_PROC_32W:
3180         if (!proc->thunk.t_from16.proc) return 0;
3181         return __wine_call_wndproc_32W( hwnd, msg, wParam, lParam, proc->thunk.t_from16.proc );
3182     default:
3183         WARN_(relay)("Invalid proc %p\n", proc );
3184         return 0;
3185     }
3186 }
3187
3188
3189 /**********************************************************************
3190  *              CallWindowProcA (USER32.@)
3191  *
3192  * The CallWindowProc() function invokes the windows procedure _func_,
3193  * with _hwnd_ as the target window, the message specified by _msg_, and
3194  * the message parameters _wParam_ and _lParam_.
3195  *
3196  * Some kinds of argument conversion may be done, I'm not sure what.
3197  *
3198  * CallWindowProc() may be used for windows subclassing. Use
3199  * SetWindowLong() to set a new windows procedure for windows of the
3200  * subclass, and handle subclassed messages in the new windows
3201  * procedure. The new windows procedure may then use CallWindowProc()
3202  * with _func_ set to the parent class's windows procedure to dispatch
3203  * the message to the superclass.
3204  *
3205  * RETURNS
3206  *
3207  *    The return value is message dependent.
3208  *
3209  * CONFORMANCE
3210  *
3211  *   ECMA-234, Win32
3212  */
3213 LRESULT WINAPI CallWindowProcA(
3214     WNDPROC func,  /* [in] window procedure */
3215     HWND hwnd,     /* [in] target window */
3216     UINT msg,      /* [in] message */
3217     WPARAM wParam, /* [in] message dependent parameter */
3218     LPARAM lParam  /* [in] message dependent parameter */
3219 ) {
3220     WINDOWPROC *proc;
3221
3222     if (!func) return 0;
3223
3224     if (!(proc = WINPROC_GetPtr( func )))
3225         return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3226
3227 #if testing
3228     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32A );
3229     return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3230 #endif
3231
3232     switch(proc->type)
3233     {
3234     case WIN_PROC_16:
3235         if (!proc->thunk.t_from32.proc) return 0;
3236         return WINPROC_CallProc32ATo16( proc->thunk.t_from32.proc,
3237                                         hwnd, msg, wParam, lParam );
3238     case WIN_PROC_32A:
3239         if (!proc->thunk.t_from16.proc) return 0;
3240         return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3241                                       hwnd, msg, wParam, lParam );
3242     case WIN_PROC_32W:
3243         if (!proc->thunk.t_from16.proc) return 0;
3244         return WINPROC_CallProc32ATo32W( proc->thunk.t_from16.proc,
3245                                          hwnd, msg, wParam, lParam );
3246     default:
3247         WARN_(relay)("Invalid proc %p\n", proc );
3248         return 0;
3249     }
3250 }
3251
3252
3253 /**********************************************************************
3254  *              CallWindowProcW (USER32.@)
3255  */
3256 LRESULT WINAPI CallWindowProcW( WNDPROC func, HWND hwnd, UINT msg,
3257                                   WPARAM wParam, LPARAM lParam )
3258 {
3259     WINDOWPROC *proc;
3260
3261     if (!func) return 0;
3262
3263     if (!(proc = WINPROC_GetPtr( (WNDPROC)func )))
3264         return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3265
3266 #if testing
3267     func = WINPROC_GetProc( (WNDPROC)proc, WIN_PROC_32W );
3268     return WINPROC_CallWndProc( func, hwnd, msg, wParam, lParam );
3269 #endif
3270
3271     switch(proc->type)
3272     {
3273     case WIN_PROC_16:
3274         if (!proc->thunk.t_from32.proc) return 0;
3275         return WINPROC_CallProc32WTo16( proc->thunk.t_from32.proc,
3276                                         hwnd, msg, wParam, lParam );
3277     case WIN_PROC_32A:
3278         if (!proc->thunk.t_from16.proc) return 0;
3279         return WINPROC_CallProc32WTo32A( proc->thunk.t_from16.proc,
3280                                          hwnd, msg, wParam, lParam );
3281     case WIN_PROC_32W:
3282         if (!proc->thunk.t_from16.proc) return 0;
3283         return WINPROC_CallWndProc( proc->thunk.t_from16.proc,
3284                                       hwnd, msg, wParam, lParam );
3285     default:
3286         WARN_(relay)("Invalid proc %p\n", proc );
3287         return 0;
3288     }
3289 }