Added regedit unit test, a couple minor changes to regedit.
[wine] / dlls / user / message.c
1 /*
2  * Window messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "dde.h"
30 #include "wine/unicode.h"
31 #include "wine/server.h"
32 #include "queue.h"
33 #include "input.h"
34 #include "message.h"
35 #include "hook.h"
36 #include "spy.h"
37 #include "user.h"
38 #include "win.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msg);
42
43 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
44 #define WM_NCMOUSELAST  WM_NCMBUTTONDBLCLK
45
46 #define MAX_PACK_COUNT 4
47
48 /* description of the data fields that need to be packed along with a sent message */
49 struct packed_message
50 {
51     int         count;
52     const void *data[MAX_PACK_COUNT];
53     size_t      size[MAX_PACK_COUNT];
54 };
55
56 /* info about the message currently being received by the current thread */
57 struct received_message_info
58 {
59     enum message_type type;
60     MSG               msg;
61     UINT              flags;  /* InSendMessageEx return flags */
62 };
63
64 /* structure to group all parameters for sent messages of the various kinds */
65 struct send_message_info
66 {
67     enum message_type type;
68     HWND              hwnd;
69     UINT              msg;
70     WPARAM            wparam;
71     LPARAM            lparam;
72     UINT              flags;      /* flags for SendMessageTimeout */
73     UINT              timeout;    /* timeout for SendMessageTimeout */
74     SENDASYNCPROC     callback;   /* callback function for SendMessageCallback */
75     ULONG_PTR         data;       /* callback data */
76 };
77
78
79 /* flag for messages that contain pointers */
80 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
81
82 #define SET(msg) (1 << ((msg) & 31))
83
84 static const unsigned int message_pointer_flags[] =
85 {
86     /* 0x00 - 0x1f */
87     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
88     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
89     /* 0x20 - 0x3f */
90     SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
91     SET(WM_COMPAREITEM),
92     /* 0x40 - 0x5f */
93     SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
94     SET(WM_NOTIFY) | SET(WM_HELP),
95     /* 0x60 - 0x7f */
96     SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
97     /* 0x80 - 0x9f */
98     SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
99     /* 0xa0 - 0xbf */
100     SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
101     /* 0xc0 - 0xdf */
102     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
103     /* 0xe0 - 0xff */
104     SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
105     /* 0x100 - 0x11f */
106     0,
107     /* 0x120 - 0x13f */
108     0,
109     /* 0x140 - 0x15f */
110     SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
111     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
112     SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
113     /* 0x160 - 0x17f */
114     0,
115     /* 0x180 - 0x19f */
116     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
117     SET(LB_DIR) | SET(LB_FINDSTRING) |
118     SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
119     /* 0x1a0 - 0x1bf */
120     SET(LB_FINDSTRINGEXACT),
121     /* 0x1c0 - 0x1df */
122     0,
123     /* 0x1e0 - 0x1ff */
124     0,
125     /* 0x200 - 0x21f */
126     SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
127     /* 0x220 - 0x23f */
128     SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
129     SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
130     /* 0x240 - 0x25f */
131     0,
132     /* 0x260 - 0x27f */
133     0,
134     /* 0x280 - 0x29f */
135     0,
136     /* 0x2a0 - 0x2bf */
137     0,
138     /* 0x2c0 - 0x2df */
139     0,
140     /* 0x2e0 - 0x2ff */
141     0,
142     /* 0x300 - 0x31f */
143     SET(WM_ASKCBFORMATNAME)
144 };
145
146 /* flags for messages that contain Unicode strings */
147 static const unsigned int message_unicode_flags[] =
148 {
149     /* 0x00 - 0x1f */
150     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
151     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
152     /* 0x20 - 0x3f */
153     SET(WM_CHARTOITEM),
154     /* 0x40 - 0x5f */
155     0,
156     /* 0x60 - 0x7f */
157     0,
158     /* 0x80 - 0x9f */
159     SET(WM_NCCREATE),
160     /* 0xa0 - 0xbf */
161     0,
162     /* 0xc0 - 0xdf */
163     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
164     /* 0xe0 - 0xff */
165     0,
166     /* 0x100 - 0x11f */
167     SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
168     /* 0x120 - 0x13f */
169     SET(WM_MENUCHAR),
170     /* 0x140 - 0x15f */
171     SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
172     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
173     /* 0x160 - 0x17f */
174     0,
175     /* 0x180 - 0x19f */
176     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
177     SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
178     /* 0x1a0 - 0x1bf */
179     SET(LB_FINDSTRINGEXACT),
180     /* 0x1c0 - 0x1df */
181     0,
182     /* 0x1e0 - 0x1ff */
183     0,
184     /* 0x200 - 0x21f */
185     0,
186     /* 0x220 - 0x23f */
187     SET(WM_MDICREATE),
188     /* 0x240 - 0x25f */
189     0,
190     /* 0x260 - 0x27f */
191     0,
192     /* 0x280 - 0x29f */
193     0,
194     /* 0x2a0 - 0x2bf */
195     0,
196     /* 0x2c0 - 0x2df */
197     0,
198     /* 0x2e0 - 0x2ff */
199     0,
200     /* 0x300 - 0x31f */
201     SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
202 };
203
204 /* check whether a given message type includes pointers */
205 inline static int is_pointer_message( UINT message )
206 {
207     if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
208     return (message_pointer_flags[message / 32] & SET(message)) != 0;
209 }
210
211 /* check whether a given message type contains Unicode (or ASCII) chars */
212 inline static int is_unicode_message( UINT message )
213 {
214     if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
215     return (message_unicode_flags[message / 32] & SET(message)) != 0;
216 }
217
218 #undef SET
219
220 /* add a data field to a packed message */
221 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
222 {
223     data->data[data->count] = ptr;
224     data->size[data->count] = size;
225     data->count++;
226 }
227
228 /* add a string to a packed message */
229 inline static void push_string( struct packed_message *data, LPCWSTR str )
230 {
231     push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
232 }
233
234 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
235 inline static void *get_data( void **buffer, size_t size )
236 {
237     void *ret = *buffer;
238     *buffer = (char *)*buffer + size;
239     return ret;
240 }
241
242 /* make sure that the buffer contains a valid null-terminated Unicode string */
243 inline static BOOL check_string( LPCWSTR str, size_t size )
244 {
245     for (size /= sizeof(WCHAR); size; size--, str++)
246         if (!*str) return TRUE;
247     return FALSE;
248 }
249
250 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
251 inline static void *get_buffer_space( void **buffer, size_t size )
252 {
253     void *ret;
254     if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
255         HeapFree( GetProcessHeap(), 0, *buffer );
256     *buffer = ret;
257     return ret;
258 }
259
260 /* retrieve a string pointer from packed data */
261 inline static LPWSTR get_string( void **buffer )
262 {
263     return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
264 }
265
266 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
267 inline static BOOL combobox_has_strings( HWND hwnd )
268 {
269     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
270     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
271 }
272
273 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
274 inline static BOOL listbox_has_strings( HWND hwnd )
275 {
276     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
277     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
278 }
279
280 /* check if hwnd is a broadcast magic handle */
281 inline static BOOL is_broadcast( HWND hwnd )
282 {
283     return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
284 }
285
286
287 /***********************************************************************
288  *              broadcast_message_callback
289  *
290  * Helper callback for broadcasting messages.
291  */
292 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
293 {
294     struct send_message_info *info = (struct send_message_info *)lparam;
295     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
296     switch(info->type)
297     {
298     case MSG_UNICODE:
299         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
300                              info->flags, info->timeout, NULL );
301         break;
302     case MSG_ASCII:
303         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
304                              info->flags, info->timeout, NULL );
305         break;
306     case MSG_NOTIFY:
307         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
308         break;
309     case MSG_CALLBACK:
310         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
311                               info->callback, info->data );
312         break;
313     case MSG_POSTED:
314         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
315         break;
316     default:
317         ERR( "bad type %d\n", info->type );
318         break;
319     }
320     return TRUE;
321 }
322
323
324 /***********************************************************************
325  *              map_wparam_AtoW
326  *
327  * Convert the wparam of an ASCII message to Unicode.
328  */
329 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
330 {
331     if (message == WM_CHARTOITEM ||
332         message == EM_SETPASSWORDCHAR ||
333         message == WM_CHAR ||
334         message == WM_DEADCHAR ||
335         message == WM_SYSCHAR ||
336         message == WM_SYSDEADCHAR ||
337         message == WM_MENUCHAR)
338     {
339         char ch = LOWORD(wparam);
340         WCHAR wch;
341         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
342         wparam = MAKEWPARAM( wch, HIWORD(wparam) );
343     }
344     return wparam;
345 }
346
347
348 /***********************************************************************
349  *              map_wparam_WtoA
350  *
351  * Convert the wparam of a Unicode message to ASCII.
352  */
353 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
354 {
355     if (message == WM_CHARTOITEM ||
356         message == EM_SETPASSWORDCHAR ||
357         message == WM_CHAR ||
358         message == WM_DEADCHAR ||
359         message == WM_SYSCHAR ||
360         message == WM_SYSDEADCHAR ||
361         message == WM_MENUCHAR)
362     {
363         WCHAR wch = LOWORD(wparam);
364         char ch;
365         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
366         wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
367     }
368     return wparam;
369 }
370
371
372 /***********************************************************************
373  *              pack_message
374  *
375  * Pack a message for sending to another process.
376  * Return the size of the data we expect in the message reply.
377  * Set data->count to -1 if there is an error.
378  */
379 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
380                             struct packed_message *data )
381 {
382     data->count = 0;
383     switch(message)
384     {
385     case WM_NCCREATE:
386     case WM_CREATE:
387     {
388         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
389         push_data( data, cs, sizeof(*cs) );
390         if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
391         if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
392         return sizeof(*cs);
393     }
394     case WM_GETTEXT:
395     case WM_ASKCBFORMATNAME:
396         return wparam * sizeof(WCHAR);
397     case WM_SETTEXT:
398     case WM_WININICHANGE:
399     case WM_DEVMODECHANGE:
400     case CB_DIR:
401     case LB_DIR:
402     case LB_ADDFILE:
403     case EM_REPLACESEL:
404         push_string( data, (LPWSTR)lparam );
405         return 0;
406     case WM_GETMINMAXINFO:
407         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
408         return sizeof(MINMAXINFO);
409     case WM_DRAWITEM:
410         push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
411         return 0;
412     case WM_MEASUREITEM:
413         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
414         return sizeof(MEASUREITEMSTRUCT);
415     case WM_DELETEITEM:
416         push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
417         return 0;
418     case WM_COMPAREITEM:
419         push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
420         return 0;
421     case WM_WINDOWPOSCHANGING:
422     case WM_WINDOWPOSCHANGED:
423         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
424         return sizeof(WINDOWPOS);
425     case WM_COPYDATA:
426     {
427         COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
428         push_data( data, cp, sizeof(*cp) );
429         if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
430         return 0;
431     }
432     case WM_NOTIFY:
433         /* WM_NOTIFY cannot be sent across processes (MSDN) */
434         data->count = -1;
435         return 0;
436     case WM_HELP:
437         push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
438         return 0;
439     case WM_STYLECHANGING:
440     case WM_STYLECHANGED:
441         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
442         return 0;
443     case WM_NCCALCSIZE:
444         if (!wparam)
445         {
446             push_data( data, (RECT *)lparam, sizeof(RECT) );
447             return sizeof(RECT);
448         }
449         else
450         {
451             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
452             push_data( data, nc, sizeof(*nc) );
453             push_data( data, nc->lppos, sizeof(*nc->lppos) );
454             return sizeof(*nc) + sizeof(*nc->lppos);
455         }
456     case WM_GETDLGCODE:
457         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
458         return sizeof(MSG);
459     case SBM_SETSCROLLINFO:
460         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
461         return 0;
462     case SBM_GETSCROLLINFO:
463         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
464         return sizeof(SCROLLINFO);
465     case EM_GETSEL:
466     case SBM_GETRANGE:
467     case CB_GETEDITSEL:
468     {
469         size_t size = 0;
470         if (wparam) size += sizeof(DWORD);
471         if (lparam) size += sizeof(DWORD);
472         return size;
473     }
474     case EM_GETRECT:
475     case LB_GETITEMRECT:
476     case CB_GETDROPPEDCONTROLRECT:
477         return sizeof(RECT);
478     case EM_SETRECT:
479     case EM_SETRECTNP:
480         push_data( data, (RECT *)lparam, sizeof(RECT) );
481         return 0;
482     case EM_GETLINE:
483     {
484         WORD *pw = (WORD *)lparam;
485         push_data( data, pw, sizeof(*pw) );
486         return *pw * sizeof(WCHAR);
487     }
488     case EM_SETTABSTOPS:
489     case LB_SETTABSTOPS:
490         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
491         return 0;
492     case CB_ADDSTRING:
493     case CB_INSERTSTRING:
494     case CB_FINDSTRING:
495     case CB_FINDSTRINGEXACT:
496     case CB_SELECTSTRING:
497         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
498         return 0;
499     case CB_GETLBTEXT:
500         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
501         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
502     case LB_ADDSTRING:
503     case LB_INSERTSTRING:
504     case LB_FINDSTRING:
505     case LB_FINDSTRINGEXACT:
506     case LB_SELECTSTRING:
507         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
508         return 0;
509     case LB_GETTEXT:
510         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
511         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
512     case LB_GETSELITEMS:
513         return wparam * sizeof(UINT);
514     case WM_NEXTMENU:
515         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
516         return sizeof(MDINEXTMENU);
517     case WM_SIZING:
518     case WM_MOVING:
519         push_data( data, (RECT *)lparam, sizeof(RECT) );
520         return sizeof(RECT);
521     case WM_MDICREATE:
522     {
523         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
524         push_data( data, cs, sizeof(*cs) );
525         if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
526         if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
527         return sizeof(*cs);
528     }
529     case WM_MDIGETACTIVE:
530         if (lparam) return sizeof(BOOL);
531         return 0;
532     case WM_WINE_SETWINDOWPOS:
533         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
534         return 0;
535
536     /* these contain an HFONT */
537     case WM_SETFONT:
538     case WM_GETFONT:
539     /* these contain an HDC */
540     case WM_PAINT:
541     case WM_ERASEBKGND:
542     case WM_ICONERASEBKGND:
543     case WM_NCPAINT:
544     case WM_CTLCOLORMSGBOX:
545     case WM_CTLCOLOREDIT:
546     case WM_CTLCOLORLISTBOX:
547     case WM_CTLCOLORBTN:
548     case WM_CTLCOLORDLG:
549     case WM_CTLCOLORSCROLLBAR:
550     case WM_CTLCOLORSTATIC:
551     case WM_PRINT:
552     case WM_PRINTCLIENT:
553     /* these contain an HGLOBAL */
554     case WM_PAINTCLIPBOARD:
555     case WM_SIZECLIPBOARD:
556     /* these contain pointers */
557     case WM_DROPOBJECT:
558     case WM_QUERYDROPOBJECT:
559     case WM_DRAGLOOP:
560     case WM_DRAGSELECT:
561     case WM_DRAGMOVE:
562     case WM_DEVICECHANGE:
563         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
564         data->count = -1;
565         return 0;
566     }
567     return 0;
568 }
569
570
571 /***********************************************************************
572  *              unpack_message
573  *
574  * Unpack a message received from another process.
575  */
576 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
577                             void **buffer, size_t size )
578 {
579     size_t minsize = 0;
580
581     switch(message)
582     {
583     case WM_NCCREATE:
584     case WM_CREATE:
585     {
586         CREATESTRUCTW *cs = *buffer;
587         WCHAR *str = (WCHAR *)(cs + 1);
588         if (size < sizeof(*cs)) return FALSE;
589         size -= sizeof(*cs);
590         if (HIWORD(cs->lpszName))
591         {
592             if (!check_string( str, size )) return FALSE;
593             cs->lpszName = str;
594             size -= (strlenW(str) + 1) * sizeof(WCHAR);
595             str += strlenW(str) + 1;
596         }
597         if (HIWORD(cs->lpszClass))
598         {
599             if (!check_string( str, size )) return FALSE;
600             cs->lpszClass = str;
601         }
602         break;
603     }
604     case WM_GETTEXT:
605     case WM_ASKCBFORMATNAME:
606         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
607         break;
608     case WM_SETTEXT:
609     case WM_WININICHANGE:
610     case WM_DEVMODECHANGE:
611     case CB_DIR:
612     case LB_DIR:
613     case LB_ADDFILE:
614     case EM_REPLACESEL:
615         if (!check_string( *buffer, size )) return FALSE;
616         break;
617     case WM_GETMINMAXINFO:
618         minsize = sizeof(MINMAXINFO);
619         break;
620     case WM_DRAWITEM:
621         minsize = sizeof(DRAWITEMSTRUCT);
622         break;
623     case WM_MEASUREITEM:
624         minsize = sizeof(MEASUREITEMSTRUCT);
625         break;
626     case WM_DELETEITEM:
627         minsize = sizeof(DELETEITEMSTRUCT);
628         break;
629     case WM_COMPAREITEM:
630         minsize = sizeof(COMPAREITEMSTRUCT);
631         break;
632     case WM_WINDOWPOSCHANGING:
633     case WM_WINDOWPOSCHANGED:
634     case WM_WINE_SETWINDOWPOS:
635         minsize = sizeof(WINDOWPOS);
636         break;
637     case WM_COPYDATA:
638     {
639         COPYDATASTRUCT *cp = *buffer;
640         if (size < sizeof(*cp)) return FALSE;
641         if (cp->lpData)
642         {
643             minsize = sizeof(*cp) + cp->cbData;
644             cp->lpData = cp + 1;
645         }
646         break;
647     }
648     case WM_NOTIFY:
649         /* WM_NOTIFY cannot be sent across processes (MSDN) */
650         return FALSE;
651     case WM_HELP:
652         minsize = sizeof(HELPINFO);
653         break;
654     case WM_STYLECHANGING:
655     case WM_STYLECHANGED:
656         minsize = sizeof(STYLESTRUCT);
657         break;
658     case WM_NCCALCSIZE:
659         if (!*wparam) minsize = sizeof(RECT);
660         else
661         {
662             NCCALCSIZE_PARAMS *nc = *buffer;
663             if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
664             nc->lppos = (WINDOWPOS *)(nc + 1);
665         }
666         break;
667     case WM_GETDLGCODE:
668         if (!*lparam) return TRUE;
669         minsize = sizeof(MSG);
670         break;
671     case SBM_SETSCROLLINFO:
672         minsize = sizeof(SCROLLINFO);
673         break;
674     case SBM_GETSCROLLINFO:
675         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
676         break;
677     case EM_GETSEL:
678     case SBM_GETRANGE:
679     case CB_GETEDITSEL:
680         if (*wparam || *lparam)
681         {
682             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
683             if (*wparam) *wparam = (WPARAM)*buffer;
684             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
685         }
686         return TRUE;
687     case EM_GETRECT:
688     case LB_GETITEMRECT:
689     case CB_GETDROPPEDCONTROLRECT:
690         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
691         break;
692     case EM_SETRECT:
693     case EM_SETRECTNP:
694         minsize = sizeof(RECT);
695         break;
696     case EM_GETLINE:
697     {
698         WORD len;
699         if (size < sizeof(WORD)) return FALSE;
700         len = *(WORD *)*buffer;
701         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
702         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
703         return TRUE;
704     }
705     case EM_SETTABSTOPS:
706     case LB_SETTABSTOPS:
707         if (!*wparam) return TRUE;
708         minsize = *wparam * sizeof(UINT);
709         break;
710     case CB_ADDSTRING:
711     case CB_INSERTSTRING:
712     case CB_FINDSTRING:
713     case CB_FINDSTRINGEXACT:
714     case CB_SELECTSTRING:
715     case LB_ADDSTRING:
716     case LB_INSERTSTRING:
717     case LB_FINDSTRING:
718     case LB_FINDSTRINGEXACT:
719     case LB_SELECTSTRING:
720         if (!*buffer) return TRUE;
721         if (!check_string( *buffer, size )) return FALSE;
722         break;
723     case CB_GETLBTEXT:
724     {
725         size = sizeof(ULONG_PTR);
726         if (combobox_has_strings( hwnd ))
727             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
728         if (!get_buffer_space( buffer, size )) return FALSE;
729         break;
730     }
731     case LB_GETTEXT:
732     {
733         size = sizeof(ULONG_PTR);
734         if (listbox_has_strings( hwnd ))
735             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
736         if (!get_buffer_space( buffer, size )) return FALSE;
737         break;
738     }
739     case LB_GETSELITEMS:
740         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
741         break;
742     case WM_NEXTMENU:
743         minsize = sizeof(MDINEXTMENU);
744         if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
745         break;
746     case WM_SIZING:
747     case WM_MOVING:
748         minsize = sizeof(RECT);
749         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
750         break;
751     case WM_MDICREATE:
752     {
753         MDICREATESTRUCTW *cs = *buffer;
754         WCHAR *str = (WCHAR *)(cs + 1);
755         if (size < sizeof(*cs)) return FALSE;
756         size -= sizeof(*cs);
757         if (HIWORD(cs->szTitle))
758         {
759             if (!check_string( str, size )) return FALSE;
760             cs->szTitle = str;
761             size -= (strlenW(str) + 1) * sizeof(WCHAR);
762             str += strlenW(str) + 1;
763         }
764         if (HIWORD(cs->szClass))
765         {
766             if (!check_string( str, size )) return FALSE;
767             cs->szClass = str;
768         }
769         break;
770     }
771     case WM_MDIGETACTIVE:
772         if (!*lparam) return TRUE;
773         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
774         break;
775     /* these contain an HFONT */
776     case WM_SETFONT:
777     case WM_GETFONT:
778     /* these contain an HDC */
779     case WM_PAINT:
780     case WM_ERASEBKGND:
781     case WM_ICONERASEBKGND:
782     case WM_NCPAINT:
783     case WM_CTLCOLORMSGBOX:
784     case WM_CTLCOLOREDIT:
785     case WM_CTLCOLORLISTBOX:
786     case WM_CTLCOLORBTN:
787     case WM_CTLCOLORDLG:
788     case WM_CTLCOLORSCROLLBAR:
789     case WM_CTLCOLORSTATIC:
790     case WM_PRINT:
791     case WM_PRINTCLIENT:
792     /* these contain an HGLOBAL */
793     case WM_PAINTCLIPBOARD:
794     case WM_SIZECLIPBOARD:
795     /* these contain pointers */
796     case WM_DROPOBJECT:
797     case WM_QUERYDROPOBJECT:
798     case WM_DRAGLOOP:
799     case WM_DRAGSELECT:
800     case WM_DRAGMOVE:
801     case WM_DEVICECHANGE:
802         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
803         return FALSE;
804
805     default:
806         return TRUE; /* message doesn't need any unpacking */
807     }
808
809     /* default exit for most messages: check minsize and store buffer in lparam */
810     if (size < minsize) return FALSE;
811     *lparam = (LPARAM)*buffer;
812     return TRUE;
813 }
814
815
816 /***********************************************************************
817  *              pack_reply
818  *
819  * Pack a reply to a message for sending to another process.
820  */
821 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
822                         LRESULT res, struct packed_message *data )
823 {
824     data->count = 0;
825     switch(message)
826     {
827     case WM_NCCREATE:
828     case WM_CREATE:
829         push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
830         break;
831     case WM_GETTEXT:
832     case CB_GETLBTEXT:
833     case LB_GETTEXT:
834         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
835         break;
836     case WM_GETMINMAXINFO:
837         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
838         break;
839     case WM_MEASUREITEM:
840         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
841         break;
842     case WM_WINDOWPOSCHANGING:
843     case WM_WINDOWPOSCHANGED:
844         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
845         break;
846     case WM_GETDLGCODE:
847         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
848         break;
849     case SBM_GETSCROLLINFO:
850         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
851         break;
852     case EM_GETRECT:
853     case LB_GETITEMRECT:
854     case CB_GETDROPPEDCONTROLRECT:
855     case WM_SIZING:
856     case WM_MOVING:
857         push_data( data, (RECT *)lparam, sizeof(RECT) );
858         break;
859     case EM_GETLINE:
860     {
861         WORD *ptr = (WORD *)lparam;
862         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
863         break;
864     }
865     case LB_GETSELITEMS:
866         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
867         break;
868     case WM_MDIGETACTIVE:
869         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
870         break;
871     case WM_NCCALCSIZE:
872         if (!wparam)
873             push_data( data, (RECT *)lparam, sizeof(RECT) );
874         else
875         {
876             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
877             push_data( data, nc, sizeof(*nc) );
878             push_data( data, nc->lppos, sizeof(*nc->lppos) );
879         }
880         break;
881     case EM_GETSEL:
882     case SBM_GETRANGE:
883     case CB_GETEDITSEL:
884         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
885         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
886         break;
887     case WM_NEXTMENU:
888         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
889         break;
890     case WM_MDICREATE:
891         push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
892         break;
893     case WM_ASKCBFORMATNAME:
894         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
895         break;
896     }
897 }
898
899
900 /***********************************************************************
901  *              unpack_reply
902  *
903  * Unpack a message reply received from another process.
904  */
905 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
906                           void *buffer, size_t size )
907 {
908     switch(message)
909     {
910     case WM_NCCREATE:
911     case WM_CREATE:
912     {
913         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
914         LPCWSTR name = cs->lpszName, class = cs->lpszClass;
915         memcpy( cs, buffer, min( sizeof(*cs), size ));
916         cs->lpszName = name;  /* restore the original pointers */
917         cs->lpszClass = class;
918         break;
919     }
920     case WM_GETTEXT:
921     case WM_ASKCBFORMATNAME:
922         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
923         break;
924     case WM_GETMINMAXINFO:
925         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
926         break;
927     case WM_MEASUREITEM:
928         memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
929         break;
930     case WM_WINDOWPOSCHANGING:
931     case WM_WINDOWPOSCHANGED:
932         memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
933         break;
934     case WM_GETDLGCODE:
935         if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
936         break;
937     case SBM_GETSCROLLINFO:
938         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
939         break;
940     case EM_GETRECT:
941     case CB_GETDROPPEDCONTROLRECT:
942     case LB_GETITEMRECT:
943     case WM_SIZING:
944     case WM_MOVING:
945         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
946         break;
947     case EM_GETLINE:
948         size = min( size, (size_t)*(WORD *)lparam );
949         memcpy( (WCHAR *)lparam, buffer, size );
950         break;
951     case LB_GETSELITEMS:
952         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
953         break;
954     case LB_GETTEXT:
955     case CB_GETLBTEXT:
956         memcpy( (WCHAR *)lparam, buffer, size );
957         break;
958     case WM_NEXTMENU:
959         memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
960         break;
961     case WM_MDIGETACTIVE:
962         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
963         break;
964     case WM_NCCALCSIZE:
965         if (!wparam)
966             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
967         else
968         {
969             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
970             WINDOWPOS *wp = nc->lppos;
971             memcpy( nc, buffer, min( sizeof(*nc), size ));
972             if (size > sizeof(*nc))
973             {
974                 size -= sizeof(*nc);
975                 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
976             }
977             nc->lppos = wp;  /* restore the original pointer */
978         }
979         break;
980     case EM_GETSEL:
981     case SBM_GETRANGE:
982     case CB_GETEDITSEL:
983         if (wparam)
984         {
985             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
986             if (size <= sizeof(DWORD)) break;
987             size -= sizeof(DWORD);
988             buffer = (DWORD *)buffer + 1;
989         }
990         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
991         break;
992     case WM_MDICREATE:
993     {
994         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
995         LPCWSTR title = cs->szTitle, class = cs->szClass;
996         memcpy( cs, buffer, min( sizeof(*cs), size ));
997         cs->szTitle = title;  /* restore the original pointers */
998         cs->szClass = class;
999         break;
1000     }
1001     default:
1002         ERR( "should not happen: unexpected message %x\n", message );
1003         break;
1004     }
1005 }
1006
1007
1008 /***********************************************************************
1009  *           reply_message
1010  *
1011  * Send a reply to a sent message.
1012  */
1013 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1014 {
1015     struct packed_message data;
1016     int i, replied = info->flags & ISMEX_REPLIED;
1017
1018     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1019     if (!remove && replied) return;  /* replied already */
1020
1021     data.count = 0;
1022     info->flags |= ISMEX_REPLIED;
1023
1024     if (info->type == MSG_OTHER_PROCESS && !replied)
1025     {
1026         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1027                     info->msg.lParam, result, &data );
1028     }
1029
1030     SERVER_START_REQ( reply_message )
1031     {
1032         req->result = result;
1033         req->remove = remove;
1034         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1035         wine_server_call( req );
1036     }
1037     SERVER_END_REQ;
1038 }
1039
1040
1041 /***********************************************************************
1042  *           handle_internal_message
1043  *
1044  * Handle an internal Wine message instead of calling the window proc.
1045  */
1046 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1047 {
1048     if (hwnd == GetDesktopWindow()) return 0;
1049     switch(msg)
1050     {
1051     case WM_WINE_DESTROYWINDOW:
1052         return WIN_DestroyWindow( hwnd );
1053     case WM_WINE_SETWINDOWPOS:
1054         return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1055     case WM_WINE_SHOWWINDOW:
1056         return ShowWindow( hwnd, wparam );
1057     case WM_WINE_SETPARENT:
1058         return (LRESULT)SetParent( hwnd, (HWND)wparam );
1059     case WM_WINE_SETWINDOWLONG:
1060         return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1061     case WM_WINE_ENABLEWINDOW:
1062         return EnableWindow( hwnd, wparam );
1063     default:
1064         FIXME( "unknown internal message %x\n", msg );
1065         return 0;
1066     }
1067 }
1068
1069 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1070  * to the memory handle, we keep track (in the server side) of all pairs of handle
1071  * used (the client passes its value and the content of the memory handle), and
1072  * the server stored both values (the client, and the local one, created after the
1073  * content). When a ACK message is generated, the list of pair is searched for a
1074  * matching pair, so that the client memory handle can be returned.
1075  */
1076 struct DDE_pair {
1077     HGLOBAL     client_hMem;
1078     HGLOBAL     server_hMem;
1079 };
1080
1081 static      struct DDE_pair*    dde_pairs;
1082 static      int                 dde_num_alloc;
1083 static      int                 dde_num_used;
1084 static      CRITICAL_SECTION    dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1085
1086 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1087 {
1088     int  i;
1089 #define GROWBY  4
1090
1091     EnterCriticalSection(&dde_crst);
1092
1093     /* now remember the pair of hMem on both sides */
1094     if (dde_num_used == dde_num_alloc)
1095     {
1096         struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1097                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1098         if (!tmp)
1099         {
1100             LeaveCriticalSection(&dde_crst);
1101             return FALSE;
1102         }
1103         dde_pairs = tmp;
1104         /* zero out newly allocated part */
1105         memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1106         dde_num_alloc += GROWBY;
1107     }
1108 #undef GROWBY
1109     for (i = 0; i < dde_num_alloc; i++)
1110     {
1111         if (dde_pairs[i].server_hMem == 0)
1112         {
1113             dde_pairs[i].client_hMem = chm;
1114             dde_pairs[i].server_hMem = shm;
1115             dde_num_used++;
1116             break;
1117         }
1118     }
1119     LeaveCriticalSection(&dde_crst);
1120     return TRUE;
1121 }
1122
1123 static HGLOBAL dde_get_pair(HGLOBAL shm)
1124 {
1125     int  i;
1126     HGLOBAL     ret = 0;
1127
1128     EnterCriticalSection(&dde_crst);
1129     for (i = 0; i < dde_num_alloc; i++)
1130     {
1131         if (dde_pairs[i].server_hMem == shm)
1132         {
1133             /* free this pair */
1134             dde_pairs[i].server_hMem = 0;
1135             dde_num_used--;
1136             ret = dde_pairs[i].client_hMem;
1137             break;
1138         }
1139     }
1140     LeaveCriticalSection(&dde_crst);
1141     return ret;
1142 }
1143
1144 /***********************************************************************
1145  *              post_dde_message
1146  *
1147  * Post a DDE messag
1148  */
1149 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1150 {
1151     void*       ptr = NULL;
1152     int         size = 0;
1153     UINT        uiLo, uiHi;
1154     LPARAM      lp = 0;
1155     HGLOBAL     hunlock = 0;
1156     int         i;
1157     DWORD       res;
1158
1159     if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1160         return FALSE;
1161
1162     lp = info->lparam;
1163     switch (info->msg)
1164     {
1165         /* DDE messages which don't require packing are:
1166          * WM_DDE_INITIATE
1167          * WM_DDE_TERMINATE
1168          * WM_DDE_REQUEST
1169          * WM_DDE_UNADVISE
1170          */
1171     case WM_DDE_ACK:
1172         if (HIWORD(uiHi))
1173         {
1174             /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1175             HGLOBAL h = dde_get_pair( uiHi );
1176             if (h)
1177             {
1178                 /* send back the value of h on the other side */
1179                 push_data( data, &h, sizeof(HGLOBAL) );
1180                 lp = uiLo;
1181                 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1182             }
1183         }
1184         else
1185         {
1186             /* uiHi should contain either an atom or 0 */
1187             TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1188             lp = MAKELONG( uiLo, uiHi );
1189         }
1190         break;
1191     case WM_DDE_ADVISE:
1192     case WM_DDE_DATA:
1193     case WM_DDE_POKE:
1194         size = 0;
1195         if (uiLo)
1196         {
1197             size = GlobalSize( (HGLOBAL)uiLo ) ;
1198             if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1199                 (info->msg == WM_DDE_DATA   && size < sizeof(DDEDATA))   ||
1200                 (info->msg == WM_DDE_POKE   && size < sizeof(DDEPOKE))
1201                 )
1202             return FALSE;
1203         }
1204         else if (info->msg != WM_DDE_DATA) return FALSE;
1205
1206         lp = uiHi;
1207         if (uiLo)
1208         {
1209             if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1210             {
1211                 push_data( data, ptr, size );
1212                 hunlock = (HGLOBAL)uiLo;
1213             }
1214         }
1215         TRACE( "send ddepack %u %x\n", size, uiHi );
1216         break;
1217     case WM_DDE_EXECUTE:
1218         if (info->lparam)
1219         {
1220             if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1221             {
1222                 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1223                 /* so that the other side can send it back on ACK */
1224                 lp = info->lparam;
1225                 hunlock = (HGLOBAL)info->lparam;
1226             }
1227         }
1228         break;
1229     }
1230     SERVER_START_REQ( send_message )
1231     {
1232         req->id      = (void *)dest_tid;
1233         req->type    = info->type;
1234         req->win     = info->hwnd;
1235         req->msg     = info->msg;
1236         req->wparam  = info->wparam;
1237         req->lparam  = lp;
1238         req->time    = GetCurrentTime();
1239         req->timeout = -1;
1240         for (i = 0; i < data->count; i++)
1241             wine_server_add_data( req, data->data[i], data->size[i] );
1242         if ((res = wine_server_call( req )))
1243         {
1244             if (res == STATUS_INVALID_PARAMETER)
1245                 /* FIXME: find a STATUS_ value for this one */
1246                 SetLastError( ERROR_INVALID_THREAD_ID );
1247             else
1248                 SetLastError( RtlNtStatusToDosError(res) );
1249         }
1250         else
1251             FreeDDElParam(info->msg, info->lparam);
1252     }
1253     SERVER_END_REQ;
1254     if (hunlock) GlobalUnlock(hunlock);
1255
1256     return !res;
1257 }
1258
1259 /***********************************************************************
1260  *              unpack_dde_message
1261  *
1262  * Unpack a posted DDE message received from another process.
1263  */
1264 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1265                                 void **buffer, size_t size )
1266 {
1267     UINT        uiLo, uiHi;
1268     HGLOBAL     hMem = 0;
1269     void*       ptr;
1270
1271     switch (message)
1272     {
1273     case WM_DDE_ACK:
1274         if (size)
1275         {
1276             /* hMem is being passed */
1277             if (size != sizeof(HGLOBAL)) return FALSE;
1278             if (!buffer || !*buffer) return FALSE;
1279             uiLo = *lparam;
1280             memcpy( &hMem, *buffer, size );
1281             uiHi = hMem;
1282             TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( uiHi ));
1283         }
1284         else
1285         {
1286             uiLo = LOWORD( *lparam );
1287             uiHi = HIWORD( *lparam );
1288             TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1289         }
1290         *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1291         break;
1292     case WM_DDE_ADVISE:
1293     case WM_DDE_DATA:
1294     case WM_DDE_POKE:
1295         if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1296         uiHi = *lparam;
1297         TRACE( "recv ddepack %u %x\n", size, uiHi );
1298         if (size)
1299         {
1300             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1301             if (hMem && (ptr = GlobalLock( hMem )))
1302             {
1303                 memcpy( ptr, *buffer, size );
1304                 GlobalUnlock( hMem );
1305             }
1306             else return FALSE;
1307         }
1308         uiLo = hMem;
1309
1310         *lparam = PackDDElParam( message, uiLo, uiHi );
1311         break;
1312     case WM_DDE_EXECUTE:
1313         if (size)
1314         {
1315             if (!buffer || !*buffer) return FALSE;
1316             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1317             if (hMem && (ptr = GlobalLock( hMem )))
1318             {
1319                 memcpy( ptr, *buffer, size );
1320                 GlobalUnlock( hMem );
1321                 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1322                 if (!dde_add_pair( *lparam, hMem ))
1323                 {
1324                     GlobalFree( hMem );
1325                     return FALSE;
1326                 }
1327             }
1328         } else return FALSE;
1329         *lparam = hMem;
1330         break;
1331     }
1332     return TRUE;
1333 }
1334
1335 /***********************************************************************
1336  *           call_window_proc
1337  *
1338  * Call a window procedure and the corresponding hooks.
1339  */
1340 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1341 {
1342     LRESULT result;
1343     WNDPROC winproc;
1344
1345     if (msg & 0x80000000) return handle_internal_message( hwnd, msg, wparam, lparam );
1346
1347     /* first the WH_CALLWNDPROC hook */
1348     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1349     {
1350         CWPSTRUCT cwp;
1351         cwp.lParam  = lparam;
1352         cwp.wParam  = wparam;
1353         cwp.message = msg;
1354         cwp.hwnd    = WIN_GetFullHandle( hwnd );
1355         if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1356         else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1357         lparam = cwp.lParam;
1358         wparam = cwp.wParam;
1359         msg    = cwp.message;
1360         hwnd   = cwp.hwnd;
1361     }
1362
1363     /* now call the window procedure */
1364     if (unicode)
1365     {
1366         if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1367         result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1368     }
1369     else
1370     {
1371         if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1372         result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1373     }
1374
1375     /* and finally the WH_CALLWNDPROCRET hook */
1376     if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1377     {
1378         CWPRETSTRUCT cwp;
1379         cwp.lResult = result;
1380         cwp.lParam  = lparam;
1381         cwp.wParam  = wparam;
1382         cwp.message = msg;
1383         cwp.hwnd    = WIN_GetFullHandle( hwnd );
1384         if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1385         else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1386     }
1387     return result;
1388 }
1389
1390
1391 /***********************************************************************
1392  *           MSG_peek_message
1393  *
1394  * Peek for a message matching the given parameters. Return FALSE if none available.
1395  * All pending sent messages are processed before returning.
1396  */
1397 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1398 {
1399     LRESULT result;
1400     ULONG_PTR extra_info = 0;
1401     MESSAGEQUEUE *queue = QUEUE_Current();
1402     struct received_message_info info, *old_info;
1403
1404     if (!first && !last) last = ~0;
1405
1406     for (;;)
1407     {
1408         NTSTATUS res;
1409         void *buffer = NULL;
1410         size_t size = 0, buffer_size = 0;
1411
1412         do  /* loop while buffer is too small */
1413         {
1414             if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1415                 return FALSE;
1416             SERVER_START_REQ( get_message )
1417             {
1418                 req->flags     = flags;
1419                 req->get_win   = hwnd;
1420                 req->get_first = first;
1421                 req->get_last  = last;
1422                 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1423                 if (!(res = wine_server_call( req )))
1424                 {
1425                     size = wine_server_reply_size( reply );
1426                     info.type        = reply->type;
1427                     info.msg.hwnd    = reply->win;
1428                     info.msg.message = reply->msg;
1429                     info.msg.wParam  = reply->wparam;
1430                     info.msg.lParam  = reply->lparam;
1431                     info.msg.time    = reply->time;
1432                     info.msg.pt.x    = reply->x;
1433                     info.msg.pt.y    = reply->y;
1434                     extra_info       = reply->info;
1435                 }
1436                 else
1437                 {
1438                     if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1439                     buffer_size = reply->total;
1440                 }
1441             }
1442             SERVER_END_REQ;
1443         } while (res == STATUS_BUFFER_OVERFLOW);
1444
1445         if (res) return FALSE;
1446
1447         TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1448                info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1449
1450         switch(info.type)
1451         {
1452         case MSG_ASCII:
1453         case MSG_UNICODE:
1454             info.flags = ISMEX_SEND;
1455             break;
1456         case MSG_NOTIFY:
1457             info.flags = ISMEX_NOTIFY;
1458             break;
1459         case MSG_CALLBACK:
1460             info.flags = ISMEX_CALLBACK;
1461             break;
1462         case MSG_OTHER_PROCESS:
1463             info.flags = ISMEX_SEND;
1464             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1465                                  &info.msg.lParam, &buffer, size ))
1466             {
1467                 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1468                      info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1469                      info.msg.wParam, info.msg.lParam, size );
1470                 /* ignore it */
1471                 reply_message( &info, 0, TRUE );
1472                 goto next;
1473             }
1474             break;
1475         case MSG_HARDWARE_RAW:
1476             if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1477                                                    hwnd, first, last, flags & GET_MSG_REMOVE ))
1478                 goto next;
1479             /* fall through */
1480         case MSG_HARDWARE_COOKED:
1481             if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1482                                                       flags & GET_MSG_REMOVE ))
1483             {
1484                 flags |= GET_MSG_REMOVE_LAST;
1485                 goto next;
1486             }
1487             queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1488             /* fall through */
1489         case MSG_POSTED:
1490             queue->GetMessageExtraInfoVal = extra_info;
1491             if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1492             {
1493                 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1494                                          &info.msg.lParam, &buffer, size ))
1495                 {
1496                     ERR( "invalid packed dde-message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1497                          info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1498                          info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1499                     /* ignore it */
1500                     continue;
1501                 }
1502             }
1503             *msg = info.msg;
1504             if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1505             return TRUE;
1506         }
1507
1508         /* if we get here, we have a sent message; call the window procedure */
1509         old_info = queue->receive_info;
1510         queue->receive_info = &info;
1511         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1512                                    info.msg.lParam, (info.type != MSG_ASCII) );
1513         reply_message( &info, result, TRUE );
1514         queue->receive_info = old_info;
1515     next:
1516         if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1517     }
1518 }
1519
1520
1521 /***********************************************************************
1522  *           wait_message_reply
1523  *
1524  * Wait until a sent message gets replied to.
1525  */
1526 static void wait_message_reply( UINT flags )
1527 {
1528     MESSAGEQUEUE *queue;
1529
1530     if (!(queue = QUEUE_Current())) return;
1531
1532     for (;;)
1533     {
1534         unsigned int wake_bits = 0, changed_bits = 0;
1535         DWORD dwlc, res;
1536
1537         SERVER_START_REQ( set_queue_mask )
1538         {
1539             req->wake_mask    = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1540             req->changed_mask = QS_SMRESULT | req->wake_mask;
1541             req->skip_wait    = 1;
1542             if (!wine_server_call( req ))
1543             {
1544                 wake_bits    = reply->wake_bits;
1545                 changed_bits = reply->changed_bits;
1546             }
1547         }
1548         SERVER_END_REQ;
1549
1550         if (changed_bits & QS_SMRESULT) return;  /* got a result */
1551         if (wake_bits & QS_SENDMESSAGE)
1552         {
1553             /* Process the sent message immediately */
1554             MSG msg;
1555             MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1556             continue;
1557         }
1558
1559         /* now wait for it */
1560
1561         ReleaseThunkLock( &dwlc );
1562
1563         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1564             res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1565                                                             INFINITE, 0, 0 );
1566         else
1567             res = WaitForSingleObject( queue->server_queue, INFINITE );
1568
1569         if (dwlc) RestoreThunkLock( dwlc );
1570     }
1571 }
1572
1573 /***********************************************************************
1574  *              put_message_in_queue
1575  *
1576  * Put a sent message into the destination queue.
1577  * For inter-process message, reply_size is set to expected size of reply data.
1578  */
1579 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1580                                   size_t *reply_size )
1581 {
1582     struct packed_message data;
1583     unsigned int res;
1584     int i, timeout = -1;
1585
1586     if (info->type != MSG_NOTIFY &&
1587         info->type != MSG_CALLBACK &&
1588         info->type != MSG_POSTED &&
1589         info->timeout != INFINITE)
1590         timeout = info->timeout;
1591
1592     data.count = 0;
1593     if (info->type == MSG_OTHER_PROCESS)
1594     {
1595         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1596         if (data.count == -1)
1597         {
1598             WARN( "cannot pack message %x\n", info->msg );
1599             return FALSE;
1600         }
1601     }
1602     else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1603     {
1604         return post_dde_message( dest_tid, &data, info );
1605     }
1606
1607     SERVER_START_REQ( send_message )
1608     {
1609         req->id      = (void *)dest_tid;
1610         req->type    = info->type;
1611         req->win     = info->hwnd;
1612         req->msg     = info->msg;
1613         req->wparam  = info->wparam;
1614         req->lparam  = info->lparam;
1615         req->time    = GetCurrentTime();
1616         req->timeout = timeout;
1617         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1618         if ((res = wine_server_call( req )))
1619         {
1620             if (res == STATUS_INVALID_PARAMETER)
1621                 /* FIXME: find a STATUS_ value for this one */
1622                 SetLastError( ERROR_INVALID_THREAD_ID );
1623             else
1624                 SetLastError( RtlNtStatusToDosError(res) );
1625         }
1626     }
1627     SERVER_END_REQ;
1628     return !res;
1629 }
1630
1631
1632 /***********************************************************************
1633  *              retrieve_reply
1634  *
1635  * Retrieve a message reply from the server.
1636  */
1637 static LRESULT retrieve_reply( const struct send_message_info *info,
1638                                size_t reply_size, LRESULT *result )
1639 {
1640     NTSTATUS status;
1641     void *reply_data = NULL;
1642
1643     if (reply_size)
1644     {
1645         if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1646         {
1647             WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1648             reply_size = 0;
1649         }
1650     }
1651     SERVER_START_REQ( get_message_reply )
1652     {
1653         req->cancel = 1;
1654         if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1655         if (!(status = wine_server_call( req ))) *result = reply->result;
1656         reply_size = wine_server_reply_size( reply );
1657     }
1658     SERVER_END_REQ;
1659     if (!status && reply_size)
1660         unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1661
1662     if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1663
1664     TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1665            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1666            info->lparam, *result, status );
1667
1668     if (!status) return 1;
1669     if (status == STATUS_TIMEOUT) SetLastError(0);  /* timeout */
1670     else SetLastError( RtlNtStatusToDosError(status) );
1671     return 0;
1672 }
1673
1674
1675 /***********************************************************************
1676  *              send_inter_thread_message
1677  */
1678 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1679                                           LRESULT *res_ptr )
1680 {
1681     LRESULT ret;
1682     int locks;
1683     size_t reply_size = 0;
1684
1685     TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1686            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1687
1688     if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1689
1690     /* there's no reply to wait for on notify/callback messages */
1691     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1692
1693     locks = WIN_SuspendWndsLock();
1694
1695     wait_message_reply( info->flags );
1696     ret = retrieve_reply( info, reply_size, res_ptr );
1697
1698     WIN_RestoreWndsLock( locks );
1699     return ret;
1700 }
1701
1702
1703 /***********************************************************************
1704  *              SendMessageTimeoutW  (USER32.@)
1705  */
1706 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1707                                     UINT flags, UINT timeout, LPDWORD res_ptr )
1708 {
1709     struct send_message_info info;
1710     DWORD dest_tid, dest_pid;
1711     LRESULT ret, result;
1712
1713     info.type    = MSG_UNICODE;
1714     info.hwnd    = hwnd;
1715     info.msg     = msg;
1716     info.wparam  = wparam;
1717     info.lparam  = lparam;
1718     info.flags   = flags;
1719     info.timeout = timeout;
1720
1721     if (is_broadcast(hwnd))
1722     {
1723         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1724         if (res_ptr) *res_ptr = 1;
1725         return 1;
1726     }
1727
1728     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1729
1730     if (USER_IsExitingThread( dest_tid )) return 0;
1731
1732     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1733
1734     if (dest_tid == GetCurrentThreadId())
1735     {
1736         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1737         ret = 1;
1738     }
1739     else
1740     {
1741         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1742         ret = send_inter_thread_message( dest_tid, &info, &result );
1743     }
1744
1745     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1746     if (ret && res_ptr) *res_ptr = result;
1747     return ret;
1748 }
1749
1750
1751 /***********************************************************************
1752  *              SendMessageTimeoutA  (USER32.@)
1753  */
1754 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1755                                     UINT flags, UINT timeout, LPDWORD res_ptr )
1756 {
1757     struct send_message_info info;
1758     DWORD dest_tid, dest_pid;
1759     LRESULT ret, result;
1760
1761     info.type    = MSG_ASCII;
1762     info.hwnd    = hwnd;
1763     info.msg     = msg;
1764     info.wparam  = wparam;
1765     info.lparam  = lparam;
1766     info.flags   = flags;
1767     info.timeout = timeout;
1768
1769     if (is_broadcast(hwnd))
1770     {
1771         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1772         if (res_ptr) *res_ptr = 1;
1773         return 1;
1774     }
1775
1776     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1777
1778     if (USER_IsExitingThread( dest_tid )) return 0;
1779
1780     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1781
1782     if (dest_tid == GetCurrentThreadId())
1783     {
1784         result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1785         ret = 1;
1786     }
1787     else if (dest_pid == GetCurrentProcessId())
1788     {
1789         ret = send_inter_thread_message( dest_tid, &info, &result );
1790     }
1791     else
1792     {
1793         /* inter-process message: need to map to Unicode */
1794         info.type = MSG_OTHER_PROCESS;
1795         if (is_unicode_message( info.msg ))
1796         {
1797             if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1798                 return 0;
1799             ret = send_inter_thread_message( dest_tid, &info, &result );
1800             result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1801                                                info.lparam, result );
1802         }
1803         else ret = send_inter_thread_message( dest_tid, &info, &result );
1804     }
1805     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1806     if (ret && res_ptr) *res_ptr = result;
1807     return ret;
1808 }
1809
1810
1811 /***********************************************************************
1812  *              SendMessageW  (USER32.@)
1813  */
1814 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1815 {
1816     LRESULT res = 0;
1817     SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1818     return res;
1819 }
1820
1821
1822 /***********************************************************************
1823  *              SendMessageA  (USER32.@)
1824  */
1825 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1826 {
1827     LRESULT res = 0;
1828     SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1829     return res;
1830 }
1831
1832
1833 /***********************************************************************
1834  *              SendNotifyMessageA  (USER32.@)
1835  */
1836 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1837 {
1838     return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1839 }
1840
1841
1842 /***********************************************************************
1843  *              SendNotifyMessageW  (USER32.@)
1844  */
1845 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1846 {
1847     struct send_message_info info;
1848     DWORD dest_tid;
1849     LRESULT result;
1850
1851     if (is_pointer_message(msg))
1852     {
1853         SetLastError(ERROR_INVALID_PARAMETER);
1854         return FALSE;
1855     }
1856
1857     info.type    = MSG_NOTIFY;
1858     info.hwnd    = hwnd;
1859     info.msg     = msg;
1860     info.wparam  = wparam;
1861     info.lparam  = lparam;
1862
1863     if (is_broadcast(hwnd))
1864     {
1865         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1866         return TRUE;
1867     }
1868
1869     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1870
1871     if (USER_IsExitingThread( dest_tid )) return TRUE;
1872
1873     if (dest_tid == GetCurrentThreadId())
1874     {
1875         call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1876         return TRUE;
1877     }
1878     return send_inter_thread_message( dest_tid, &info, &result );
1879 }
1880
1881
1882 /***********************************************************************
1883  *              SendMessageCallbackA  (USER32.@)
1884  */
1885 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1886                                   SENDASYNCPROC callback, ULONG_PTR data )
1887 {
1888     return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1889                                  lparam, callback, data );
1890 }
1891
1892
1893 /***********************************************************************
1894  *              SendMessageCallbackW  (USER32.@)
1895  */
1896 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1897                                   SENDASYNCPROC callback, ULONG_PTR data )
1898 {
1899     struct send_message_info info;
1900     LRESULT result;
1901     DWORD dest_tid;
1902
1903     if (is_pointer_message(msg))
1904     {
1905         SetLastError(ERROR_INVALID_PARAMETER);
1906         return FALSE;
1907     }
1908
1909     info.type     = MSG_CALLBACK;
1910     info.hwnd     = hwnd;
1911     info.msg      = msg;
1912     info.wparam   = wparam;
1913     info.lparam   = lparam;
1914     info.callback = callback;
1915     info.data     = data;
1916
1917     if (is_broadcast(hwnd))
1918     {
1919         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1920         return TRUE;
1921     }
1922
1923     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1924
1925     if (USER_IsExitingThread( dest_tid )) return TRUE;
1926
1927     if (dest_tid == GetCurrentThreadId())
1928     {
1929         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1930         callback( hwnd, msg, data, result );
1931         return TRUE;
1932     }
1933     FIXME( "callback will not be called\n" );
1934     return send_inter_thread_message( dest_tid, &info, &result );
1935 }
1936
1937
1938 /***********************************************************************
1939  *              ReplyMessage  (USER32.@)
1940  */
1941 BOOL WINAPI ReplyMessage( LRESULT result )
1942 {
1943     MESSAGEQUEUE *queue = QUEUE_Current();
1944     struct received_message_info *info = queue->receive_info;
1945
1946     if (!info) return FALSE;
1947     reply_message( info, result, FALSE );
1948     return TRUE;
1949 }
1950
1951
1952 /***********************************************************************
1953  *              InSendMessage  (USER32.@)
1954  */
1955 BOOL WINAPI InSendMessage(void)
1956 {
1957     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1958 }
1959
1960
1961 /***********************************************************************
1962  *              InSendMessageEx  (USER32.@)
1963  */
1964 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1965 {
1966     MESSAGEQUEUE *queue = QUEUE_Current();
1967     struct received_message_info *info = queue->receive_info;
1968
1969     if (info) return info->flags;
1970     return ISMEX_NOSEND;
1971 }
1972
1973
1974 /***********************************************************************
1975  *              PostMessageA  (USER32.@)
1976  */
1977 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1978 {
1979     return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1980 }
1981
1982
1983 /***********************************************************************
1984  *              PostMessageW  (USER32.@)
1985  */
1986 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1987 {
1988     struct send_message_info info;
1989     DWORD dest_tid;
1990
1991     if (is_pointer_message( msg ))
1992     {
1993         SetLastError( ERROR_INVALID_PARAMETER );
1994         return FALSE;
1995     }
1996
1997     info.type   = MSG_POSTED;
1998     info.hwnd   = hwnd;
1999     info.msg    = msg;
2000     info.wparam = wparam;
2001     info.lparam = lparam;
2002
2003     if (is_broadcast(hwnd))
2004     {
2005         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2006         return TRUE;
2007     }
2008     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2009
2010     if (USER_IsExitingThread( dest_tid )) return TRUE;
2011
2012     return put_message_in_queue( dest_tid, &info, NULL );
2013 }
2014
2015
2016 /**********************************************************************
2017  *              PostThreadMessageA  (USER32.@)
2018  */
2019 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2020 {
2021     return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2022 }
2023
2024
2025 /**********************************************************************
2026  *              PostThreadMessageW  (USER32.@)
2027  */
2028 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2029 {
2030     struct send_message_info info;
2031
2032     if (is_pointer_message( msg ))
2033     {
2034         SetLastError( ERROR_INVALID_PARAMETER );
2035         return FALSE;
2036     }
2037     if (USER_IsExitingThread( thread )) return TRUE;
2038
2039     info.type   = MSG_POSTED;
2040     info.hwnd   = 0;
2041     info.msg    = msg;
2042     info.wparam = wparam;
2043     info.lparam = lparam;
2044     return put_message_in_queue( thread, &info, NULL );
2045 }
2046
2047
2048 /***********************************************************************
2049  *              PostQuitMessage  (USER32.@)
2050  */
2051 void WINAPI PostQuitMessage( INT exitCode )
2052 {
2053     PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2054 }
2055
2056
2057 /***********************************************************************
2058  *              PeekMessageW  (USER32.@)
2059  */
2060 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2061 {
2062     MESSAGEQUEUE *queue;
2063     MSG msg;
2064     int locks;
2065
2066     /* check for graphics events */
2067     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2068         USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2069
2070     hwnd = WIN_GetFullHandle( hwnd );
2071     locks = WIN_SuspendWndsLock();
2072
2073     if (!MSG_peek_message( &msg, hwnd, first, last,
2074                            (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2075     {
2076         /* FIXME: should be done before checking for hw events */
2077         MSG_JournalPlayBackMsg();
2078
2079         if (!(flags & PM_NOYIELD))
2080         {
2081             DWORD count;
2082             ReleaseThunkLock(&count);
2083             if (count) RestoreThunkLock(count);
2084         }
2085         WIN_RestoreWndsLock( locks );
2086         return FALSE;
2087     }
2088
2089     WIN_RestoreWndsLock( locks );
2090
2091     /* need to fill the window handle for WM_PAINT message */
2092     if (msg.message == WM_PAINT)
2093     {
2094         if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2095         {
2096             msg.message = WM_PAINTICON;
2097             msg.wParam = 1;
2098         }
2099         /* clear internal paint flag */
2100         RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2101     }
2102
2103     if ((queue = QUEUE_Current()))
2104     {
2105         queue->GetMessageTimeVal = msg.time;
2106         msg.pt.x = LOWORD( queue->GetMessagePosVal );
2107         msg.pt.y = HIWORD( queue->GetMessagePosVal );
2108     }
2109
2110     HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
2111
2112     /* copy back our internal safe copy of message data to msg_out.
2113      * msg_out is a variable from the *program*, so it can't be used
2114      * internally as it can get "corrupted" by our use of SendMessage()
2115      * (back to the program) inside the message handling itself. */
2116     *msg_out = msg;
2117     return TRUE;
2118 }
2119
2120
2121 /***********************************************************************
2122  *              PeekMessageA  (USER32.@)
2123  */
2124 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2125 {
2126     BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2127     if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2128     return ret;
2129 }
2130
2131
2132 /***********************************************************************
2133  *              GetMessageW  (USER32.@)
2134  */
2135 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2136 {
2137     MESSAGEQUEUE *queue = QUEUE_Current();
2138     int mask, locks;
2139
2140     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
2141     if (first || last)
2142     {
2143         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2144         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2145              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2146         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2147         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2148         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2149     }
2150     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2151
2152     locks = WIN_SuspendWndsLock();
2153
2154     while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2155     {
2156         /* wait until one of the bits is set */
2157         unsigned int wake_bits = 0, changed_bits = 0;
2158         DWORD dwlc;
2159
2160         SERVER_START_REQ( set_queue_mask )
2161         {
2162             req->wake_mask    = QS_SENDMESSAGE;
2163             req->changed_mask = mask;
2164             req->skip_wait    = 1;
2165             if (!wine_server_call( req ))
2166             {
2167                 wake_bits    = reply->wake_bits;
2168                 changed_bits = reply->changed_bits;
2169             }
2170         }
2171         SERVER_END_REQ;
2172
2173         if (changed_bits & mask) continue;
2174         if (wake_bits & QS_SENDMESSAGE) continue;
2175
2176         TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2177                queue->self, mask, wake_bits, changed_bits );
2178
2179         ReleaseThunkLock( &dwlc );
2180         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2181             USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2182         else
2183             WaitForSingleObject( queue->server_queue, INFINITE );
2184         if (dwlc) RestoreThunkLock( dwlc );
2185     }
2186
2187     WIN_RestoreWndsLock( locks );
2188
2189     return (msg->message != WM_QUIT);
2190 }
2191
2192
2193 /***********************************************************************
2194  *              GetMessageA  (USER32.@)
2195  */
2196 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2197 {
2198     GetMessageW( msg, hwnd, first, last );
2199     msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2200     return (msg->message != WM_QUIT);
2201 }
2202
2203
2204 /***********************************************************************
2205  *              SetMessageQueue (USER32.@)
2206  */
2207 BOOL WINAPI SetMessageQueue( INT size )
2208 {
2209     /* now obsolete the message queue will be expanded dynamically as necessary */
2210     return TRUE;
2211 }