Implemented inter-thread SendMessageCallback.
[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 <assert.h>
25
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winerror.h"
30 #include "winnls.h"
31 #include "dde.h"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
34 #include "message.h"
35 #include "user.h"
36 #include "win.h"
37 #include "winproc.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msg);
41 WINE_DECLARE_DEBUG_CHANNEL(relay);
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     SET(WM_IME_CHAR),
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
281 /***********************************************************************
282  *              broadcast_message_callback
283  *
284  * Helper callback for broadcasting messages.
285  */
286 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
287 {
288     struct send_message_info *info = (struct send_message_info *)lparam;
289     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
290     switch(info->type)
291     {
292     case MSG_UNICODE:
293         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
294                              info->flags, info->timeout, NULL );
295         break;
296     case MSG_ASCII:
297         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
298                              info->flags, info->timeout, NULL );
299         break;
300     case MSG_NOTIFY:
301         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
302         break;
303     case MSG_CALLBACK:
304         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
305                               info->callback, info->data );
306         break;
307     case MSG_POSTED:
308         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
309         break;
310     default:
311         ERR( "bad type %d\n", info->type );
312         break;
313     }
314     return TRUE;
315 }
316
317
318 /***********************************************************************
319  *              map_wparam_AtoW
320  *
321  * Convert the wparam of an ASCII message to Unicode.
322  */
323 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
324 {
325     switch(message)
326     {
327     case WM_CHARTOITEM:
328     case EM_SETPASSWORDCHAR:
329     case WM_CHAR:
330     case WM_DEADCHAR:
331     case WM_SYSCHAR:
332     case WM_SYSDEADCHAR:
333     case WM_MENUCHAR:
334         {
335             char ch = LOWORD(wparam);
336             WCHAR wch;
337             MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
338             wparam = MAKEWPARAM( wch, HIWORD(wparam) );
339         }
340         break;
341     case WM_IME_CHAR:
342         {
343             char ch[2];
344             WCHAR wch;
345             ch[0] = (wparam >> 8);
346             ch[1] = (wparam & 0xff);
347             MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
348             wparam = MAKEWPARAM( wch, HIWORD(wparam) );
349         }
350         break;
351     }
352     return wparam;
353 }
354
355
356 /***********************************************************************
357  *              map_wparam_WtoA
358  *
359  * Convert the wparam of a Unicode message to ASCII.
360  */
361 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
362 {
363     switch(message)
364     {
365     case WM_CHARTOITEM:
366     case EM_SETPASSWORDCHAR:
367     case WM_CHAR:
368     case WM_DEADCHAR:
369     case WM_SYSCHAR:
370     case WM_SYSDEADCHAR:
371     case WM_MENUCHAR:
372         {
373             WCHAR wch = LOWORD(wparam);
374             BYTE ch;
375             WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
376             wparam = MAKEWPARAM( ch, HIWORD(wparam) );
377         }
378         break;
379     case WM_IME_CHAR:
380         {
381             WCHAR wch = LOWORD(wparam);
382             BYTE ch[2];
383
384             ch[1] = 0;
385             WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL );
386             wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
387         }
388         break;
389     }
390     return wparam;
391 }
392
393
394 /***********************************************************************
395  *              pack_message
396  *
397  * Pack a message for sending to another process.
398  * Return the size of the data we expect in the message reply.
399  * Set data->count to -1 if there is an error.
400  */
401 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
402                             struct packed_message *data )
403 {
404     data->count = 0;
405     switch(message)
406     {
407     case WM_NCCREATE:
408     case WM_CREATE:
409     {
410         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
411         push_data( data, cs, sizeof(*cs) );
412         if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
413         if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
414         return sizeof(*cs);
415     }
416     case WM_GETTEXT:
417     case WM_ASKCBFORMATNAME:
418         return wparam * sizeof(WCHAR);
419     case WM_WININICHANGE:
420         if (lparam) push_string(data, (LPWSTR)lparam );
421         return 0;
422     case WM_SETTEXT:
423     case WM_DEVMODECHANGE:
424     case CB_DIR:
425     case LB_DIR:
426     case LB_ADDFILE:
427     case EM_REPLACESEL:
428         push_string( data, (LPWSTR)lparam );
429         return 0;
430     case WM_GETMINMAXINFO:
431         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
432         return sizeof(MINMAXINFO);
433     case WM_DRAWITEM:
434         push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
435         return 0;
436     case WM_MEASUREITEM:
437         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
438         return sizeof(MEASUREITEMSTRUCT);
439     case WM_DELETEITEM:
440         push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
441         return 0;
442     case WM_COMPAREITEM:
443         push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
444         return 0;
445     case WM_WINDOWPOSCHANGING:
446     case WM_WINDOWPOSCHANGED:
447         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
448         return sizeof(WINDOWPOS);
449     case WM_COPYDATA:
450     {
451         COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
452         push_data( data, cp, sizeof(*cp) );
453         if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
454         return 0;
455     }
456     case WM_NOTIFY:
457         /* WM_NOTIFY cannot be sent across processes (MSDN) */
458         data->count = -1;
459         return 0;
460     case WM_HELP:
461         push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
462         return 0;
463     case WM_STYLECHANGING:
464     case WM_STYLECHANGED:
465         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
466         return 0;
467     case WM_NCCALCSIZE:
468         if (!wparam)
469         {
470             push_data( data, (RECT *)lparam, sizeof(RECT) );
471             return sizeof(RECT);
472         }
473         else
474         {
475             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
476             push_data( data, nc, sizeof(*nc) );
477             push_data( data, nc->lppos, sizeof(*nc->lppos) );
478             return sizeof(*nc) + sizeof(*nc->lppos);
479         }
480     case WM_GETDLGCODE:
481         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
482         return sizeof(MSG);
483     case SBM_SETSCROLLINFO:
484         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
485         return 0;
486     case SBM_GETSCROLLINFO:
487         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
488         return sizeof(SCROLLINFO);
489     case EM_GETSEL:
490     case SBM_GETRANGE:
491     case CB_GETEDITSEL:
492     {
493         size_t size = 0;
494         if (wparam) size += sizeof(DWORD);
495         if (lparam) size += sizeof(DWORD);
496         return size;
497     }
498     case EM_GETRECT:
499     case LB_GETITEMRECT:
500     case CB_GETDROPPEDCONTROLRECT:
501         return sizeof(RECT);
502     case EM_SETRECT:
503     case EM_SETRECTNP:
504         push_data( data, (RECT *)lparam, sizeof(RECT) );
505         return 0;
506     case EM_GETLINE:
507     {
508         WORD *pw = (WORD *)lparam;
509         push_data( data, pw, sizeof(*pw) );
510         return *pw * sizeof(WCHAR);
511     }
512     case EM_SETTABSTOPS:
513     case LB_SETTABSTOPS:
514         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
515         return 0;
516     case CB_ADDSTRING:
517     case CB_INSERTSTRING:
518     case CB_FINDSTRING:
519     case CB_FINDSTRINGEXACT:
520     case CB_SELECTSTRING:
521         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
522         return 0;
523     case CB_GETLBTEXT:
524         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
525         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
526     case LB_ADDSTRING:
527     case LB_INSERTSTRING:
528     case LB_FINDSTRING:
529     case LB_FINDSTRINGEXACT:
530     case LB_SELECTSTRING:
531         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
532         return 0;
533     case LB_GETTEXT:
534         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
535         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
536     case LB_GETSELITEMS:
537         return wparam * sizeof(UINT);
538     case WM_NEXTMENU:
539         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
540         return sizeof(MDINEXTMENU);
541     case WM_SIZING:
542     case WM_MOVING:
543         push_data( data, (RECT *)lparam, sizeof(RECT) );
544         return sizeof(RECT);
545     case WM_MDICREATE:
546     {
547         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
548         push_data( data, cs, sizeof(*cs) );
549         if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
550         if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
551         return sizeof(*cs);
552     }
553     case WM_MDIGETACTIVE:
554         if (lparam) return sizeof(BOOL);
555         return 0;
556     case WM_WINE_SETWINDOWPOS:
557         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
558         return 0;
559     case WM_WINE_KEYBOARD_LL_HOOK:
560         push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
561         return 0;
562     case WM_WINE_MOUSE_LL_HOOK:
563         push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
564         return 0;
565
566     /* these contain an HFONT */
567     case WM_SETFONT:
568     case WM_GETFONT:
569     /* these contain an HDC */
570     case WM_PAINT:
571     case WM_ERASEBKGND:
572     case WM_ICONERASEBKGND:
573     case WM_NCPAINT:
574     case WM_CTLCOLORMSGBOX:
575     case WM_CTLCOLOREDIT:
576     case WM_CTLCOLORLISTBOX:
577     case WM_CTLCOLORBTN:
578     case WM_CTLCOLORDLG:
579     case WM_CTLCOLORSCROLLBAR:
580     case WM_CTLCOLORSTATIC:
581     case WM_PRINT:
582     case WM_PRINTCLIENT:
583     /* these contain an HGLOBAL */
584     case WM_PAINTCLIPBOARD:
585     case WM_SIZECLIPBOARD:
586     /* these contain pointers */
587     case WM_DROPOBJECT:
588     case WM_QUERYDROPOBJECT:
589     case WM_DRAGLOOP:
590     case WM_DRAGSELECT:
591     case WM_DRAGMOVE:
592     case WM_DEVICECHANGE:
593         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
594         data->count = -1;
595         return 0;
596     }
597     return 0;
598 }
599
600
601 /***********************************************************************
602  *              unpack_message
603  *
604  * Unpack a message received from another process.
605  */
606 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
607                             void **buffer, size_t size )
608 {
609     size_t minsize = 0;
610
611     switch(message)
612     {
613     case WM_NCCREATE:
614     case WM_CREATE:
615     {
616         CREATESTRUCTW *cs = *buffer;
617         WCHAR *str = (WCHAR *)(cs + 1);
618         if (size < sizeof(*cs)) return FALSE;
619         size -= sizeof(*cs);
620         if (HIWORD(cs->lpszName))
621         {
622             if (!check_string( str, size )) return FALSE;
623             cs->lpszName = str;
624             size -= (strlenW(str) + 1) * sizeof(WCHAR);
625             str += strlenW(str) + 1;
626         }
627         if (HIWORD(cs->lpszClass))
628         {
629             if (!check_string( str, size )) return FALSE;
630             cs->lpszClass = str;
631         }
632         break;
633     }
634     case WM_GETTEXT:
635     case WM_ASKCBFORMATNAME:
636         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
637         break;
638     case WM_WININICHANGE:
639         if (!*lparam) return TRUE;
640         /* fall through */
641     case WM_SETTEXT:
642     case WM_DEVMODECHANGE:
643     case CB_DIR:
644     case LB_DIR:
645     case LB_ADDFILE:
646     case EM_REPLACESEL:
647         if (!check_string( *buffer, size )) return FALSE;
648         break;
649     case WM_GETMINMAXINFO:
650         minsize = sizeof(MINMAXINFO);
651         break;
652     case WM_DRAWITEM:
653         minsize = sizeof(DRAWITEMSTRUCT);
654         break;
655     case WM_MEASUREITEM:
656         minsize = sizeof(MEASUREITEMSTRUCT);
657         break;
658     case WM_DELETEITEM:
659         minsize = sizeof(DELETEITEMSTRUCT);
660         break;
661     case WM_COMPAREITEM:
662         minsize = sizeof(COMPAREITEMSTRUCT);
663         break;
664     case WM_WINDOWPOSCHANGING:
665     case WM_WINDOWPOSCHANGED:
666     case WM_WINE_SETWINDOWPOS:
667         minsize = sizeof(WINDOWPOS);
668         break;
669     case WM_COPYDATA:
670     {
671         COPYDATASTRUCT *cp = *buffer;
672         if (size < sizeof(*cp)) return FALSE;
673         if (cp->lpData)
674         {
675             minsize = sizeof(*cp) + cp->cbData;
676             cp->lpData = cp + 1;
677         }
678         break;
679     }
680     case WM_NOTIFY:
681         /* WM_NOTIFY cannot be sent across processes (MSDN) */
682         return FALSE;
683     case WM_HELP:
684         minsize = sizeof(HELPINFO);
685         break;
686     case WM_STYLECHANGING:
687     case WM_STYLECHANGED:
688         minsize = sizeof(STYLESTRUCT);
689         break;
690     case WM_NCCALCSIZE:
691         if (!*wparam) minsize = sizeof(RECT);
692         else
693         {
694             NCCALCSIZE_PARAMS *nc = *buffer;
695             if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
696             nc->lppos = (WINDOWPOS *)(nc + 1);
697         }
698         break;
699     case WM_GETDLGCODE:
700         if (!*lparam) return TRUE;
701         minsize = sizeof(MSG);
702         break;
703     case SBM_SETSCROLLINFO:
704         minsize = sizeof(SCROLLINFO);
705         break;
706     case SBM_GETSCROLLINFO:
707         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
708         break;
709     case EM_GETSEL:
710     case SBM_GETRANGE:
711     case CB_GETEDITSEL:
712         if (*wparam || *lparam)
713         {
714             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
715             if (*wparam) *wparam = (WPARAM)*buffer;
716             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
717         }
718         return TRUE;
719     case EM_GETRECT:
720     case LB_GETITEMRECT:
721     case CB_GETDROPPEDCONTROLRECT:
722         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
723         break;
724     case EM_SETRECT:
725     case EM_SETRECTNP:
726         minsize = sizeof(RECT);
727         break;
728     case EM_GETLINE:
729     {
730         WORD len;
731         if (size < sizeof(WORD)) return FALSE;
732         len = *(WORD *)*buffer;
733         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
734         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
735         return TRUE;
736     }
737     case EM_SETTABSTOPS:
738     case LB_SETTABSTOPS:
739         if (!*wparam) return TRUE;
740         minsize = *wparam * sizeof(UINT);
741         break;
742     case CB_ADDSTRING:
743     case CB_INSERTSTRING:
744     case CB_FINDSTRING:
745     case CB_FINDSTRINGEXACT:
746     case CB_SELECTSTRING:
747     case LB_ADDSTRING:
748     case LB_INSERTSTRING:
749     case LB_FINDSTRING:
750     case LB_FINDSTRINGEXACT:
751     case LB_SELECTSTRING:
752         if (!*buffer) return TRUE;
753         if (!check_string( *buffer, size )) return FALSE;
754         break;
755     case CB_GETLBTEXT:
756     {
757         size = sizeof(ULONG_PTR);
758         if (combobox_has_strings( hwnd ))
759             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
760         if (!get_buffer_space( buffer, size )) return FALSE;
761         break;
762     }
763     case LB_GETTEXT:
764     {
765         size = sizeof(ULONG_PTR);
766         if (listbox_has_strings( hwnd ))
767             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
768         if (!get_buffer_space( buffer, size )) return FALSE;
769         break;
770     }
771     case LB_GETSELITEMS:
772         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
773         break;
774     case WM_NEXTMENU:
775         minsize = sizeof(MDINEXTMENU);
776         if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
777         break;
778     case WM_SIZING:
779     case WM_MOVING:
780         minsize = sizeof(RECT);
781         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
782         break;
783     case WM_MDICREATE:
784     {
785         MDICREATESTRUCTW *cs = *buffer;
786         WCHAR *str = (WCHAR *)(cs + 1);
787         if (size < sizeof(*cs)) return FALSE;
788         size -= sizeof(*cs);
789         if (HIWORD(cs->szTitle))
790         {
791             if (!check_string( str, size )) return FALSE;
792             cs->szTitle = str;
793             size -= (strlenW(str) + 1) * sizeof(WCHAR);
794             str += strlenW(str) + 1;
795         }
796         if (HIWORD(cs->szClass))
797         {
798             if (!check_string( str, size )) return FALSE;
799             cs->szClass = str;
800         }
801         break;
802     }
803     case WM_MDIGETACTIVE:
804         if (!*lparam) return TRUE;
805         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
806         break;
807     case WM_WINE_KEYBOARD_LL_HOOK:
808         minsize = sizeof(KBDLLHOOKSTRUCT);
809         break;
810     case WM_WINE_MOUSE_LL_HOOK:
811         minsize = sizeof(MSLLHOOKSTRUCT);
812         break;
813
814     /* these contain an HFONT */
815     case WM_SETFONT:
816     case WM_GETFONT:
817     /* these contain an HDC */
818     case WM_PAINT:
819     case WM_ERASEBKGND:
820     case WM_ICONERASEBKGND:
821     case WM_NCPAINT:
822     case WM_CTLCOLORMSGBOX:
823     case WM_CTLCOLOREDIT:
824     case WM_CTLCOLORLISTBOX:
825     case WM_CTLCOLORBTN:
826     case WM_CTLCOLORDLG:
827     case WM_CTLCOLORSCROLLBAR:
828     case WM_CTLCOLORSTATIC:
829     case WM_PRINT:
830     case WM_PRINTCLIENT:
831     /* these contain an HGLOBAL */
832     case WM_PAINTCLIPBOARD:
833     case WM_SIZECLIPBOARD:
834     /* these contain pointers */
835     case WM_DROPOBJECT:
836     case WM_QUERYDROPOBJECT:
837     case WM_DRAGLOOP:
838     case WM_DRAGSELECT:
839     case WM_DRAGMOVE:
840     case WM_DEVICECHANGE:
841         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
842         return FALSE;
843
844     default:
845         return TRUE; /* message doesn't need any unpacking */
846     }
847
848     /* default exit for most messages: check minsize and store buffer in lparam */
849     if (size < minsize) return FALSE;
850     *lparam = (LPARAM)*buffer;
851     return TRUE;
852 }
853
854
855 /***********************************************************************
856  *              pack_reply
857  *
858  * Pack a reply to a message for sending to another process.
859  */
860 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
861                         LRESULT res, struct packed_message *data )
862 {
863     data->count = 0;
864     switch(message)
865     {
866     case WM_NCCREATE:
867     case WM_CREATE:
868         push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
869         break;
870     case WM_GETTEXT:
871     case CB_GETLBTEXT:
872     case LB_GETTEXT:
873         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
874         break;
875     case WM_GETMINMAXINFO:
876         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
877         break;
878     case WM_MEASUREITEM:
879         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
880         break;
881     case WM_WINDOWPOSCHANGING:
882     case WM_WINDOWPOSCHANGED:
883         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
884         break;
885     case WM_GETDLGCODE:
886         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
887         break;
888     case SBM_GETSCROLLINFO:
889         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
890         break;
891     case EM_GETRECT:
892     case LB_GETITEMRECT:
893     case CB_GETDROPPEDCONTROLRECT:
894     case WM_SIZING:
895     case WM_MOVING:
896         push_data( data, (RECT *)lparam, sizeof(RECT) );
897         break;
898     case EM_GETLINE:
899     {
900         WORD *ptr = (WORD *)lparam;
901         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
902         break;
903     }
904     case LB_GETSELITEMS:
905         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
906         break;
907     case WM_MDIGETACTIVE:
908         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
909         break;
910     case WM_NCCALCSIZE:
911         if (!wparam)
912             push_data( data, (RECT *)lparam, sizeof(RECT) );
913         else
914         {
915             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
916             push_data( data, nc, sizeof(*nc) );
917             push_data( data, nc->lppos, sizeof(*nc->lppos) );
918         }
919         break;
920     case EM_GETSEL:
921     case SBM_GETRANGE:
922     case CB_GETEDITSEL:
923         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
924         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
925         break;
926     case WM_NEXTMENU:
927         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
928         break;
929     case WM_MDICREATE:
930         push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
931         break;
932     case WM_ASKCBFORMATNAME:
933         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
934         break;
935     }
936 }
937
938
939 /***********************************************************************
940  *              unpack_reply
941  *
942  * Unpack a message reply received from another process.
943  */
944 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
945                           void *buffer, size_t size )
946 {
947     switch(message)
948     {
949     case WM_NCCREATE:
950     case WM_CREATE:
951     {
952         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
953         LPCWSTR name = cs->lpszName, class = cs->lpszClass;
954         memcpy( cs, buffer, min( sizeof(*cs), size ));
955         cs->lpszName = name;  /* restore the original pointers */
956         cs->lpszClass = class;
957         break;
958     }
959     case WM_GETTEXT:
960     case WM_ASKCBFORMATNAME:
961         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
962         break;
963     case WM_GETMINMAXINFO:
964         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
965         break;
966     case WM_MEASUREITEM:
967         memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
968         break;
969     case WM_WINDOWPOSCHANGING:
970     case WM_WINDOWPOSCHANGED:
971         memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
972         break;
973     case WM_GETDLGCODE:
974         if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
975         break;
976     case SBM_GETSCROLLINFO:
977         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
978         break;
979     case EM_GETRECT:
980     case CB_GETDROPPEDCONTROLRECT:
981     case LB_GETITEMRECT:
982     case WM_SIZING:
983     case WM_MOVING:
984         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
985         break;
986     case EM_GETLINE:
987         size = min( size, (size_t)*(WORD *)lparam );
988         memcpy( (WCHAR *)lparam, buffer, size );
989         break;
990     case LB_GETSELITEMS:
991         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
992         break;
993     case LB_GETTEXT:
994     case CB_GETLBTEXT:
995         memcpy( (WCHAR *)lparam, buffer, size );
996         break;
997     case WM_NEXTMENU:
998         memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
999         break;
1000     case WM_MDIGETACTIVE:
1001         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1002         break;
1003     case WM_NCCALCSIZE:
1004         if (!wparam)
1005             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1006         else
1007         {
1008             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1009             WINDOWPOS *wp = nc->lppos;
1010             memcpy( nc, buffer, min( sizeof(*nc), size ));
1011             if (size > sizeof(*nc))
1012             {
1013                 size -= sizeof(*nc);
1014                 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1015             }
1016             nc->lppos = wp;  /* restore the original pointer */
1017         }
1018         break;
1019     case EM_GETSEL:
1020     case SBM_GETRANGE:
1021     case CB_GETEDITSEL:
1022         if (wparam)
1023         {
1024             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1025             if (size <= sizeof(DWORD)) break;
1026             size -= sizeof(DWORD);
1027             buffer = (DWORD *)buffer + 1;
1028         }
1029         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1030         break;
1031     case WM_MDICREATE:
1032     {
1033         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1034         LPCWSTR title = cs->szTitle, class = cs->szClass;
1035         memcpy( cs, buffer, min( sizeof(*cs), size ));
1036         cs->szTitle = title;  /* restore the original pointers */
1037         cs->szClass = class;
1038         break;
1039     }
1040     default:
1041         ERR( "should not happen: unexpected message %x\n", message );
1042         break;
1043     }
1044 }
1045
1046
1047 /***********************************************************************
1048  *           reply_message
1049  *
1050  * Send a reply to a sent message.
1051  */
1052 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1053 {
1054     struct packed_message data;
1055     int i, replied = info->flags & ISMEX_REPLIED;
1056
1057     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1058     if (!remove && replied) return;  /* replied already */
1059
1060     data.count = 0;
1061     info->flags |= ISMEX_REPLIED;
1062
1063     if (info->type == MSG_OTHER_PROCESS && !replied)
1064     {
1065         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1066                     info->msg.lParam, result, &data );
1067     }
1068
1069     SERVER_START_REQ( reply_message )
1070     {
1071         req->type   = info->type;
1072         req->result = result;
1073         req->remove = remove;
1074         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1075         wine_server_call( req );
1076     }
1077     SERVER_END_REQ;
1078 }
1079
1080
1081 /***********************************************************************
1082  *           handle_internal_message
1083  *
1084  * Handle an internal Wine message instead of calling the window proc.
1085  */
1086 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1087 {
1088     if (hwnd == GetDesktopWindow()) return 0;
1089     switch(msg)
1090     {
1091     case WM_WINE_DESTROYWINDOW:
1092         return WIN_DestroyWindow( hwnd );
1093     case WM_WINE_SETWINDOWPOS:
1094         return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1095     case WM_WINE_SHOWWINDOW:
1096         return ShowWindow( hwnd, wparam );
1097     case WM_WINE_SETPARENT:
1098         return (LRESULT)SetParent( hwnd, (HWND)wparam );
1099     case WM_WINE_SETWINDOWLONG:
1100         return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1101     case WM_WINE_ENABLEWINDOW:
1102         return EnableWindow( hwnd, wparam );
1103     case WM_WINE_SETACTIVEWINDOW:
1104         return (LRESULT)SetActiveWindow( (HWND)wparam );
1105     case WM_WINE_KEYBOARD_LL_HOOK:
1106         return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1107     case WM_WINE_MOUSE_LL_HOOK:
1108         return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1109     default:
1110         FIXME( "unknown internal message %x\n", msg );
1111         return 0;
1112     }
1113 }
1114
1115 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1116  * to the memory handle, we keep track (in the server side) of all pairs of handle
1117  * used (the client passes its value and the content of the memory handle), and
1118  * the server stored both values (the client, and the local one, created after the
1119  * content). When a ACK message is generated, the list of pair is searched for a
1120  * matching pair, so that the client memory handle can be returned.
1121  */
1122 struct DDE_pair {
1123     HGLOBAL     client_hMem;
1124     HGLOBAL     server_hMem;
1125 };
1126
1127 static      struct DDE_pair*    dde_pairs;
1128 static      int                 dde_num_alloc;
1129 static      int                 dde_num_used;
1130 static      CRITICAL_SECTION    dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1131
1132 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1133 {
1134     int  i;
1135 #define GROWBY  4
1136
1137     EnterCriticalSection(&dde_crst);
1138
1139     /* now remember the pair of hMem on both sides */
1140     if (dde_num_used == dde_num_alloc)
1141     {
1142         struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1143                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1144         if (!tmp)
1145         {
1146             LeaveCriticalSection(&dde_crst);
1147             return FALSE;
1148         }
1149         dde_pairs = tmp;
1150         /* zero out newly allocated part */
1151         memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1152         dde_num_alloc += GROWBY;
1153     }
1154 #undef GROWBY
1155     for (i = 0; i < dde_num_alloc; i++)
1156     {
1157         if (dde_pairs[i].server_hMem == 0)
1158         {
1159             dde_pairs[i].client_hMem = chm;
1160             dde_pairs[i].server_hMem = shm;
1161             dde_num_used++;
1162             break;
1163         }
1164     }
1165     LeaveCriticalSection(&dde_crst);
1166     return TRUE;
1167 }
1168
1169 static HGLOBAL dde_get_pair(HGLOBAL shm)
1170 {
1171     int  i;
1172     HGLOBAL     ret = 0;
1173
1174     EnterCriticalSection(&dde_crst);
1175     for (i = 0; i < dde_num_alloc; i++)
1176     {
1177         if (dde_pairs[i].server_hMem == shm)
1178         {
1179             /* free this pair */
1180             dde_pairs[i].server_hMem = 0;
1181             dde_num_used--;
1182             ret = dde_pairs[i].client_hMem;
1183             break;
1184         }
1185     }
1186     LeaveCriticalSection(&dde_crst);
1187     return ret;
1188 }
1189
1190 /***********************************************************************
1191  *              post_dde_message
1192  *
1193  * Post a DDE message
1194  */
1195 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1196 {
1197     void*       ptr = NULL;
1198     int         size = 0;
1199     UINT        uiLo, uiHi;
1200     LPARAM      lp = 0;
1201     HGLOBAL     hunlock = 0;
1202     int         i;
1203     DWORD       res;
1204
1205     if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1206         return FALSE;
1207
1208     lp = info->lparam;
1209     switch (info->msg)
1210     {
1211         /* DDE messages which don't require packing are:
1212          * WM_DDE_INITIATE
1213          * WM_DDE_TERMINATE
1214          * WM_DDE_REQUEST
1215          * WM_DDE_UNADVISE
1216          */
1217     case WM_DDE_ACK:
1218         if (HIWORD(uiHi))
1219         {
1220             /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1221             HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1222             if (h)
1223             {
1224                 /* send back the value of h on the other side */
1225                 push_data( data, &h, sizeof(HGLOBAL) );
1226                 lp = uiLo;
1227                 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1228             }
1229         }
1230         else
1231         {
1232             /* uiHi should contain either an atom or 0 */
1233             TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1234             lp = MAKELONG( uiLo, uiHi );
1235         }
1236         break;
1237     case WM_DDE_ADVISE:
1238     case WM_DDE_DATA:
1239     case WM_DDE_POKE:
1240         size = 0;
1241         if (uiLo)
1242         {
1243             size = GlobalSize( (HGLOBAL)uiLo ) ;
1244             if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1245                 (info->msg == WM_DDE_DATA   && size < sizeof(DDEDATA))   ||
1246                 (info->msg == WM_DDE_POKE   && size < sizeof(DDEPOKE))
1247                 )
1248             return FALSE;
1249         }
1250         else if (info->msg != WM_DDE_DATA) return FALSE;
1251
1252         lp = uiHi;
1253         if (uiLo)
1254         {
1255             if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1256             {
1257                 DDEDATA *dde_data = (DDEDATA *)ptr;
1258                 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1259                        dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1260                        dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1261                 push_data( data, ptr, size );
1262                 hunlock = (HGLOBAL)uiLo;
1263             }
1264         }
1265         TRACE( "send ddepack %u %x\n", size, uiHi );
1266         break;
1267     case WM_DDE_EXECUTE:
1268         if (info->lparam)
1269         {
1270             if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1271             {
1272                 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1273                 /* so that the other side can send it back on ACK */
1274                 lp = info->lparam;
1275                 hunlock = (HGLOBAL)info->lparam;
1276             }
1277         }
1278         break;
1279     }
1280     SERVER_START_REQ( send_message )
1281     {
1282         req->id      = dest_tid;
1283         req->type    = info->type;
1284         req->flags   = 0;
1285         req->win     = info->hwnd;
1286         req->msg     = info->msg;
1287         req->wparam  = info->wparam;
1288         req->lparam  = lp;
1289         req->time    = GetCurrentTime();
1290         req->timeout = -1;
1291         for (i = 0; i < data->count; i++)
1292             wine_server_add_data( req, data->data[i], data->size[i] );
1293         if ((res = wine_server_call( req )))
1294         {
1295             if (res == STATUS_INVALID_PARAMETER)
1296                 /* FIXME: find a STATUS_ value for this one */
1297                 SetLastError( ERROR_INVALID_THREAD_ID );
1298             else
1299                 SetLastError( RtlNtStatusToDosError(res) );
1300         }
1301         else
1302             FreeDDElParam(info->msg, info->lparam);
1303     }
1304     SERVER_END_REQ;
1305     if (hunlock) GlobalUnlock(hunlock);
1306
1307     return !res;
1308 }
1309
1310 /***********************************************************************
1311  *              unpack_dde_message
1312  *
1313  * Unpack a posted DDE message received from another process.
1314  */
1315 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1316                                 void **buffer, size_t size )
1317 {
1318     UINT        uiLo, uiHi;
1319     HGLOBAL     hMem = 0;
1320     void*       ptr;
1321
1322     switch (message)
1323     {
1324     case WM_DDE_ACK:
1325         if (size)
1326         {
1327             /* hMem is being passed */
1328             if (size != sizeof(HGLOBAL)) return FALSE;
1329             if (!buffer || !*buffer) return FALSE;
1330             uiLo = *lparam;
1331             memcpy( &hMem, *buffer, size );
1332             uiHi = (UINT)hMem;
1333             TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1334         }
1335         else
1336         {
1337             uiLo = LOWORD( *lparam );
1338             uiHi = HIWORD( *lparam );
1339             TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1340         }
1341         *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1342         break;
1343     case WM_DDE_ADVISE:
1344     case WM_DDE_DATA:
1345     case WM_DDE_POKE:
1346         if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1347         uiHi = *lparam;
1348         TRACE( "recv ddepack %u %x\n", size, uiHi );
1349         if (size)
1350         {
1351             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1352             if (hMem && (ptr = GlobalLock( hMem )))
1353             {
1354                 memcpy( ptr, *buffer, size );
1355                 GlobalUnlock( hMem );
1356             }
1357             else return FALSE;
1358         }
1359         uiLo = (UINT)hMem;
1360
1361         *lparam = PackDDElParam( message, uiLo, uiHi );
1362         break;
1363     case WM_DDE_EXECUTE:
1364         if (size)
1365         {
1366             if (!buffer || !*buffer) return FALSE;
1367             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1368             if (hMem && (ptr = GlobalLock( hMem )))
1369             {
1370                 memcpy( ptr, *buffer, size );
1371                 GlobalUnlock( hMem );
1372                 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1373                 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1374                 {
1375                     GlobalFree( hMem );
1376                     return FALSE;
1377                 }
1378             }
1379         } else return FALSE;
1380         *lparam = (LPARAM)hMem;
1381         break;
1382     }
1383     return TRUE;
1384 }
1385
1386 /***********************************************************************
1387  *           call_window_proc
1388  *
1389  * Call a window procedure and the corresponding hooks.
1390  */
1391 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1392                                  BOOL unicode, BOOL same_thread )
1393 {
1394     LRESULT result = 0;
1395     WNDPROC winproc;
1396     CWPSTRUCT cwp;
1397     CWPRETSTRUCT cwpret;
1398     MESSAGEQUEUE *queue = QUEUE_Current();
1399
1400     if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1401     queue->recursion_count++;
1402
1403     if (msg & 0x80000000)
1404     {
1405         result = handle_internal_message( hwnd, msg, wparam, lparam );
1406         goto done;
1407     }
1408
1409     /* first the WH_CALLWNDPROC hook */
1410     hwnd = WIN_GetFullHandle( hwnd );
1411     cwp.lParam  = lparam;
1412     cwp.wParam  = wparam;
1413     cwp.message = msg;
1414     cwp.hwnd    = hwnd;
1415     HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1416
1417     /* now call the window procedure */
1418     if (unicode)
1419     {
1420         if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1421         result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1422     }
1423     else
1424     {
1425         if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1426         result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1427     }
1428
1429     /* and finally the WH_CALLWNDPROCRET hook */
1430     cwpret.lResult = result;
1431     cwpret.lParam  = lparam;
1432     cwpret.wParam  = wparam;
1433     cwpret.message = msg;
1434     cwpret.hwnd    = hwnd;
1435     HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1436  done:
1437     queue->recursion_count--;
1438     return result;
1439 }
1440
1441
1442 /***********************************************************************
1443  *           process_hardware_message
1444  *
1445  * Process a hardware message; return TRUE if message should be passed on to the app
1446  */
1447 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1448                                       UINT first, UINT last, BOOL remove )
1449 {
1450     BOOL ret;
1451
1452     if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1453         return FALSE;
1454
1455     ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1456
1457     /* tell the server we have passed it to the app
1458      * (even though we may end up dropping it later on)
1459      */
1460     SERVER_START_REQ( reply_message )
1461     {
1462         req->type   = MSG_HARDWARE;
1463         req->result = 0;
1464         req->remove = remove || !ret;
1465         wine_server_call( req );
1466     }
1467     SERVER_END_REQ;
1468     return ret;
1469 }
1470
1471
1472 /***********************************************************************
1473  *           call_sendmsg_callback
1474  *
1475  * Call the callback function of SendMessageCallback.
1476  */
1477 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1478                                           ULONG_PTR data, LRESULT result )
1479 {
1480     if (TRACE_ON(relay))
1481         DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1482                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1483                  data, result );
1484     callback( hwnd, msg, data, result );
1485     if (TRACE_ON(relay))
1486         DPRINTF( "%04lx:Ret  message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1487                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1488                  data, result );
1489 }
1490
1491
1492 /***********************************************************************
1493  *           MSG_peek_message
1494  *
1495  * Peek for a message matching the given parameters. Return FALSE if none available.
1496  * All pending sent messages are processed before returning.
1497  */
1498 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1499 {
1500     LRESULT result;
1501     ULONG_PTR extra_info = 0;
1502     MESSAGEQUEUE *queue = QUEUE_Current();
1503     struct received_message_info info, *old_info;
1504
1505     if (!first && !last) last = ~0;
1506
1507     for (;;)
1508     {
1509         NTSTATUS res;
1510         void *buffer = NULL;
1511         size_t size = 0, buffer_size = 0;
1512
1513         do  /* loop while buffer is too small */
1514         {
1515             if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1516                 return FALSE;
1517             SERVER_START_REQ( get_message )
1518             {
1519                 req->flags     = flags;
1520                 req->get_win   = hwnd;
1521                 req->get_first = first;
1522                 req->get_last  = last;
1523                 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1524                 if (!(res = wine_server_call( req )))
1525                 {
1526                     size = wine_server_reply_size( reply );
1527                     info.type        = reply->type;
1528                     info.msg.hwnd    = reply->win;
1529                     info.msg.message = reply->msg;
1530                     info.msg.wParam  = reply->wparam;
1531                     info.msg.lParam  = reply->lparam;
1532                     info.msg.time    = reply->time;
1533                     info.msg.pt.x    = reply->x;
1534                     info.msg.pt.y    = reply->y;
1535                     extra_info       = reply->info;
1536                 }
1537                 else
1538                 {
1539                     if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1540                     buffer_size = reply->total;
1541                 }
1542             }
1543             SERVER_END_REQ;
1544         } while (res == STATUS_BUFFER_OVERFLOW);
1545
1546         if (res) return FALSE;
1547
1548         TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1549                info.type, info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1550                info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1551
1552         switch(info.type)
1553         {
1554         case MSG_ASCII:
1555         case MSG_UNICODE:
1556             info.flags = ISMEX_SEND;
1557             break;
1558         case MSG_NOTIFY:
1559             info.flags = ISMEX_NOTIFY;
1560             break;
1561         case MSG_CALLBACK:
1562             info.flags = ISMEX_CALLBACK;
1563             break;
1564         case MSG_CALLBACK_RESULT:
1565             call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
1566                                    info.msg.message, extra_info, info.msg.lParam );
1567             goto next;
1568         case MSG_OTHER_PROCESS:
1569             info.flags = ISMEX_SEND;
1570             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1571                                  &info.msg.lParam, &buffer, size ))
1572             {
1573                 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1574                      info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1575                      info.msg.wParam, info.msg.lParam, size );
1576                 /* ignore it */
1577                 reply_message( &info, 0, TRUE );
1578                 goto next;
1579             }
1580             break;
1581         case MSG_HARDWARE:
1582             if (!process_hardware_message( &info.msg, extra_info,
1583                                            hwnd, first, last, flags & GET_MSG_REMOVE ))
1584             {
1585                 TRACE("dropping msg %x\n", info.msg.message );
1586                 goto next;  /* ignore it */
1587             }
1588             queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1589             /* fall through */
1590         case MSG_POSTED:
1591             queue->GetMessageExtraInfoVal = extra_info;
1592             if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1593             {
1594                 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1595                                          &info.msg.lParam, &buffer, size ))
1596                 {
1597                     ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1598                          info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1599                          info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1600                     goto next;  /* ignore it */
1601                 }
1602             }
1603             *msg = info.msg;
1604             if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1605             return TRUE;
1606         }
1607
1608         /* if we get here, we have a sent message; call the window procedure */
1609         old_info = queue->receive_info;
1610         queue->receive_info = &info;
1611         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1612                                    info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1613         reply_message( &info, result, TRUE );
1614         queue->receive_info = old_info;
1615     next:
1616         if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1617     }
1618 }
1619
1620
1621 /***********************************************************************
1622  *           wait_message_reply
1623  *
1624  * Wait until a sent message gets replied to.
1625  */
1626 static void wait_message_reply( UINT flags )
1627 {
1628     MESSAGEQUEUE *queue;
1629
1630     if (!(queue = QUEUE_Current())) return;
1631
1632     for (;;)
1633     {
1634         unsigned int wake_bits = 0, changed_bits = 0;
1635         DWORD dwlc, res;
1636
1637         SERVER_START_REQ( set_queue_mask )
1638         {
1639             req->wake_mask    = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1640             req->changed_mask = req->wake_mask;
1641             req->skip_wait    = 1;
1642             if (!wine_server_call( req ))
1643             {
1644                 wake_bits    = reply->wake_bits;
1645                 changed_bits = reply->changed_bits;
1646             }
1647         }
1648         SERVER_END_REQ;
1649
1650         if (wake_bits & QS_SMRESULT) return;  /* got a result */
1651         if (wake_bits & QS_SENDMESSAGE)
1652         {
1653             /* Process the sent message immediately */
1654             MSG msg;
1655             MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1656             continue;
1657         }
1658
1659         /* now wait for it */
1660
1661         ReleaseThunkLock( &dwlc );
1662
1663         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1664             res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1665                                                             INFINITE, 0, 0 );
1666         else
1667             res = WaitForSingleObject( queue->server_queue, INFINITE );
1668
1669         if (dwlc) RestoreThunkLock( dwlc );
1670     }
1671 }
1672
1673 /***********************************************************************
1674  *              put_message_in_queue
1675  *
1676  * Put a sent message into the destination queue.
1677  * For inter-process message, reply_size is set to expected size of reply data.
1678  */
1679 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1680                                   size_t *reply_size )
1681 {
1682     struct packed_message data;
1683     unsigned int res;
1684     int i, timeout = -1;
1685
1686     if (info->type != MSG_NOTIFY &&
1687         info->type != MSG_CALLBACK &&
1688         info->type != MSG_POSTED &&
1689         info->timeout != INFINITE)
1690         timeout = info->timeout;
1691
1692     data.count = 0;
1693     if (info->type == MSG_OTHER_PROCESS)
1694     {
1695         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1696         if (data.count == -1)
1697         {
1698             WARN( "cannot pack message %x\n", info->msg );
1699             return FALSE;
1700         }
1701     }
1702     else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1703     {
1704         return post_dde_message( dest_tid, &data, info );
1705     }
1706
1707     SERVER_START_REQ( send_message )
1708     {
1709         req->id      = dest_tid;
1710         req->type    = info->type;
1711         req->flags   = 0;
1712         req->win     = info->hwnd;
1713         req->msg     = info->msg;
1714         req->wparam  = info->wparam;
1715         req->lparam  = info->lparam;
1716         req->time    = GetCurrentTime();
1717         req->timeout = timeout;
1718
1719         if (info->type == MSG_CALLBACK)
1720         {
1721             req->callback = info->callback;
1722             req->info     = info->data;
1723         }
1724
1725         if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
1726         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1727         if ((res = wine_server_call( req )))
1728         {
1729             if (res == STATUS_INVALID_PARAMETER)
1730                 /* FIXME: find a STATUS_ value for this one */
1731                 SetLastError( ERROR_INVALID_THREAD_ID );
1732             else
1733                 SetLastError( RtlNtStatusToDosError(res) );
1734         }
1735     }
1736     SERVER_END_REQ;
1737     return !res;
1738 }
1739
1740
1741 /***********************************************************************
1742  *              retrieve_reply
1743  *
1744  * Retrieve a message reply from the server.
1745  */
1746 static LRESULT retrieve_reply( const struct send_message_info *info,
1747                                size_t reply_size, LRESULT *result )
1748 {
1749     NTSTATUS status;
1750     void *reply_data = NULL;
1751
1752     if (reply_size)
1753     {
1754         if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1755         {
1756             WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1757             reply_size = 0;
1758         }
1759     }
1760     SERVER_START_REQ( get_message_reply )
1761     {
1762         req->cancel = 1;
1763         if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1764         if (!(status = wine_server_call( req ))) *result = reply->result;
1765         reply_size = wine_server_reply_size( reply );
1766     }
1767     SERVER_END_REQ;
1768     if (!status && reply_size)
1769         unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1770
1771     if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1772
1773     TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1774            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1775            info->lparam, *result, status );
1776
1777     /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
1778     if (status) SetLastError( RtlNtStatusToDosError(status) );
1779     return !status;
1780 }
1781
1782
1783 /***********************************************************************
1784  *              send_inter_thread_message
1785  */
1786 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1787                                           LRESULT *res_ptr )
1788 {
1789     LRESULT ret;
1790     int locks;
1791     size_t reply_size = 0;
1792
1793     TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1794            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1795
1796     if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1797
1798     /* there's no reply to wait for on notify/callback messages */
1799     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1800
1801     locks = WIN_SuspendWndsLock();
1802
1803     wait_message_reply( info->flags );
1804     ret = retrieve_reply( info, reply_size, res_ptr );
1805
1806     WIN_RestoreWndsLock( locks );
1807     return ret;
1808 }
1809
1810
1811 /***********************************************************************
1812  *              MSG_SendInternalMessageTimeout
1813  *
1814  * Same as SendMessageTimeoutW but sends the message to a specific thread
1815  * without requiring a window handle. Only works for internal Wine messages.
1816  */
1817 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
1818                                         UINT msg, WPARAM wparam, LPARAM lparam,
1819                                         UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1820 {
1821     struct send_message_info info;
1822     LRESULT ret, result;
1823
1824     assert( msg & 0x80000000 );  /* must be an internal Wine message */
1825
1826     info.type    = MSG_UNICODE;
1827     info.hwnd    = 0;
1828     info.msg     = msg;
1829     info.wparam  = wparam;
1830     info.lparam  = lparam;
1831     info.flags   = flags;
1832     info.timeout = timeout;
1833
1834     if (USER_IsExitingThread( dest_tid )) return 0;
1835
1836     if (dest_tid == GetCurrentThreadId())
1837     {
1838         result = handle_internal_message( 0, msg, wparam, lparam );
1839         ret = 1;
1840     }
1841     else
1842     {
1843         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1844         ret = send_inter_thread_message( dest_tid, &info, &result );
1845     }
1846     if (ret && res_ptr) *res_ptr = result;
1847     return ret;
1848 }
1849
1850
1851 /***********************************************************************
1852  *              SendMessageTimeoutW  (USER32.@)
1853  */
1854 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1855                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1856 {
1857     struct send_message_info info;
1858     DWORD dest_tid, dest_pid;
1859     LRESULT ret, result;
1860
1861     info.type    = MSG_UNICODE;
1862     info.hwnd    = hwnd;
1863     info.msg     = msg;
1864     info.wparam  = wparam;
1865     info.lparam  = lparam;
1866     info.flags   = flags;
1867     info.timeout = timeout;
1868
1869     if (is_broadcast(hwnd))
1870     {
1871         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1872         if (res_ptr) *res_ptr = 1;
1873         return 1;
1874     }
1875
1876     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1877
1878     if (USER_IsExitingThread( dest_tid )) return 0;
1879
1880     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1881
1882     if (dest_tid == GetCurrentThreadId())
1883     {
1884         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1885         ret = 1;
1886     }
1887     else
1888     {
1889         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1890         ret = send_inter_thread_message( dest_tid, &info, &result );
1891     }
1892
1893     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1894     if (ret && res_ptr) *res_ptr = result;
1895     return ret;
1896 }
1897
1898
1899 /***********************************************************************
1900  *              SendMessageTimeoutA  (USER32.@)
1901  */
1902 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1903                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1904 {
1905     struct send_message_info info;
1906     DWORD dest_tid, dest_pid;
1907     LRESULT ret, result;
1908
1909     info.type    = MSG_ASCII;
1910     info.hwnd    = hwnd;
1911     info.msg     = msg;
1912     info.wparam  = wparam;
1913     info.lparam  = lparam;
1914     info.flags   = flags;
1915     info.timeout = timeout;
1916
1917     if (is_broadcast(hwnd))
1918     {
1919         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1920         if (res_ptr) *res_ptr = 1;
1921         return 1;
1922     }
1923
1924     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1925
1926     if (USER_IsExitingThread( dest_tid )) return 0;
1927
1928     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1929
1930     if (dest_tid == GetCurrentThreadId())
1931     {
1932         result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1933         ret = 1;
1934     }
1935     else if (dest_pid == GetCurrentProcessId())
1936     {
1937         ret = send_inter_thread_message( dest_tid, &info, &result );
1938     }
1939     else
1940     {
1941         /* inter-process message: need to map to Unicode */
1942         info.type = MSG_OTHER_PROCESS;
1943         if (is_unicode_message( info.msg ))
1944         {
1945             if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1946                 return 0;
1947             ret = send_inter_thread_message( dest_tid, &info, &result );
1948             result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1949                                                info.lparam, result );
1950         }
1951         else ret = send_inter_thread_message( dest_tid, &info, &result );
1952     }
1953     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1954     if (ret && res_ptr) *res_ptr = result;
1955     return ret;
1956 }
1957
1958
1959 /***********************************************************************
1960  *              SendMessageW  (USER32.@)
1961  */
1962 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1963 {
1964     LRESULT res = 0;
1965     SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1966     return res;
1967 }
1968
1969
1970 /***********************************************************************
1971  *              SendMessageA  (USER32.@)
1972  */
1973 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1974 {
1975     LRESULT res = 0;
1976     SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1977     return res;
1978 }
1979
1980
1981 /***********************************************************************
1982  *              SendNotifyMessageA  (USER32.@)
1983  */
1984 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1985 {
1986     return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1987 }
1988
1989
1990 /***********************************************************************
1991  *              SendNotifyMessageW  (USER32.@)
1992  */
1993 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1994 {
1995     struct send_message_info info;
1996     DWORD dest_tid;
1997     LRESULT result;
1998
1999     if (is_pointer_message(msg))
2000     {
2001         SetLastError(ERROR_INVALID_PARAMETER);
2002         return FALSE;
2003     }
2004
2005     info.type    = MSG_NOTIFY;
2006     info.hwnd    = hwnd;
2007     info.msg     = msg;
2008     info.wparam  = wparam;
2009     info.lparam  = lparam;
2010
2011     if (is_broadcast(hwnd))
2012     {
2013         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2014         return TRUE;
2015     }
2016
2017     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2018
2019     if (USER_IsExitingThread( dest_tid )) return TRUE;
2020
2021     if (dest_tid == GetCurrentThreadId())
2022     {
2023         call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2024         return TRUE;
2025     }
2026     return send_inter_thread_message( dest_tid, &info, &result );
2027 }
2028
2029
2030 /***********************************************************************
2031  *              SendMessageCallbackA  (USER32.@)
2032  */
2033 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2034                                   SENDASYNCPROC callback, ULONG_PTR data )
2035 {
2036     return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2037                                  lparam, callback, data );
2038 }
2039
2040
2041 /***********************************************************************
2042  *              SendMessageCallbackW  (USER32.@)
2043  */
2044 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2045                                   SENDASYNCPROC callback, ULONG_PTR data )
2046 {
2047     struct send_message_info info;
2048     LRESULT result;
2049     DWORD dest_tid;
2050
2051     if (is_pointer_message(msg))
2052     {
2053         SetLastError(ERROR_INVALID_PARAMETER);
2054         return FALSE;
2055     }
2056
2057     info.type     = MSG_CALLBACK;
2058     info.hwnd     = hwnd;
2059     info.msg      = msg;
2060     info.wparam   = wparam;
2061     info.lparam   = lparam;
2062     info.callback = callback;
2063     info.data     = data;
2064
2065     if (is_broadcast(hwnd))
2066     {
2067         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2068         return TRUE;
2069     }
2070
2071     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2072
2073     if (USER_IsExitingThread( dest_tid )) return TRUE;
2074
2075     if (dest_tid == GetCurrentThreadId())
2076     {
2077         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2078         call_sendmsg_callback( callback, hwnd, msg, data, result );
2079         return TRUE;
2080     }
2081     FIXME( "callback will not be called\n" );
2082     return send_inter_thread_message( dest_tid, &info, &result );
2083 }
2084
2085
2086 /***********************************************************************
2087  *              ReplyMessage  (USER32.@)
2088  */
2089 BOOL WINAPI ReplyMessage( LRESULT result )
2090 {
2091     MESSAGEQUEUE *queue = QUEUE_Current();
2092     struct received_message_info *info = queue->receive_info;
2093
2094     if (!info) return FALSE;
2095     reply_message( info, result, FALSE );
2096     return TRUE;
2097 }
2098
2099
2100 /***********************************************************************
2101  *              InSendMessage  (USER32.@)
2102  */
2103 BOOL WINAPI InSendMessage(void)
2104 {
2105     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2106 }
2107
2108
2109 /***********************************************************************
2110  *              InSendMessageEx  (USER32.@)
2111  */
2112 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2113 {
2114     MESSAGEQUEUE *queue = QUEUE_Current();
2115     struct received_message_info *info = queue->receive_info;
2116
2117     if (info) return info->flags;
2118     return ISMEX_NOSEND;
2119 }
2120
2121
2122 /***********************************************************************
2123  *              PostMessageA  (USER32.@)
2124  */
2125 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2126 {
2127     return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2128 }
2129
2130
2131 /***********************************************************************
2132  *              PostMessageW  (USER32.@)
2133  */
2134 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2135 {
2136     struct send_message_info info;
2137     DWORD dest_tid;
2138
2139     if (is_pointer_message( msg ))
2140     {
2141         SetLastError( ERROR_INVALID_PARAMETER );
2142         return FALSE;
2143     }
2144
2145     TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2146            hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2147
2148     info.type   = MSG_POSTED;
2149     info.hwnd   = hwnd;
2150     info.msg    = msg;
2151     info.wparam = wparam;
2152     info.lparam = lparam;
2153
2154     if (is_broadcast(hwnd))
2155     {
2156         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2157         return TRUE;
2158     }
2159
2160     if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2161
2162     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2163
2164     if (USER_IsExitingThread( dest_tid )) return TRUE;
2165
2166     return put_message_in_queue( dest_tid, &info, NULL );
2167 }
2168
2169
2170 /**********************************************************************
2171  *              PostThreadMessageA  (USER32.@)
2172  */
2173 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2174 {
2175     return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2176 }
2177
2178
2179 /**********************************************************************
2180  *              PostThreadMessageW  (USER32.@)
2181  */
2182 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2183 {
2184     struct send_message_info info;
2185
2186     if (is_pointer_message( msg ))
2187     {
2188         SetLastError( ERROR_INVALID_PARAMETER );
2189         return FALSE;
2190     }
2191     if (USER_IsExitingThread( thread )) return TRUE;
2192
2193     info.type   = MSG_POSTED;
2194     info.hwnd   = 0;
2195     info.msg    = msg;
2196     info.wparam = wparam;
2197     info.lparam = lparam;
2198     return put_message_in_queue( thread, &info, NULL );
2199 }
2200
2201
2202 /***********************************************************************
2203  *              PostQuitMessage  (USER32.@)
2204  */
2205 void WINAPI PostQuitMessage( INT exitCode )
2206 {
2207     PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2208 }
2209
2210
2211 /***********************************************************************
2212  *              PeekMessageW  (USER32.@)
2213  */
2214 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2215 {
2216     MESSAGEQUEUE *queue;
2217     MSG msg;
2218     int locks;
2219
2220     /* check for graphics events */
2221     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2222         USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2223
2224     hwnd = WIN_GetFullHandle( hwnd );
2225     locks = WIN_SuspendWndsLock();
2226
2227     if (!MSG_peek_message( &msg, hwnd, first, last,
2228                            (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2229     {
2230         if (!(flags & PM_NOYIELD))
2231         {
2232             DWORD count;
2233             ReleaseThunkLock(&count);
2234             if (count) RestoreThunkLock(count);
2235         }
2236         WIN_RestoreWndsLock( locks );
2237         return FALSE;
2238     }
2239
2240     WIN_RestoreWndsLock( locks );
2241
2242     /* need to fill the window handle for WM_PAINT message */
2243     if (msg.message == WM_PAINT)
2244     {
2245         if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2246         {
2247             msg.message = WM_PAINTICON;
2248             msg.wParam = 1;
2249         }
2250         /* clear internal paint flag */
2251         RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2252     }
2253
2254     if ((queue = QUEUE_Current()))
2255     {
2256         queue->GetMessageTimeVal = msg.time;
2257         msg.pt.x = LOWORD( queue->GetMessagePosVal );
2258         msg.pt.y = HIWORD( queue->GetMessagePosVal );
2259     }
2260
2261     HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2262
2263     /* copy back our internal safe copy of message data to msg_out.
2264      * msg_out is a variable from the *program*, so it can't be used
2265      * internally as it can get "corrupted" by our use of SendMessage()
2266      * (back to the program) inside the message handling itself. */
2267     *msg_out = msg;
2268     return TRUE;
2269 }
2270
2271
2272 /***********************************************************************
2273  *              PeekMessageA  (USER32.@)
2274  */
2275 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2276 {
2277     BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2278     if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2279     return ret;
2280 }
2281
2282
2283 /***********************************************************************
2284  *              GetMessageW  (USER32.@)
2285  */
2286 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2287 {
2288     MESSAGEQUEUE *queue = QUEUE_Current();
2289     int mask, locks;
2290
2291     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
2292     if (first || last)
2293     {
2294         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2295         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2296              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2297         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2298         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2299         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2300     }
2301     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2302
2303     locks = WIN_SuspendWndsLock();
2304
2305     while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2306     {
2307         /* wait until one of the bits is set */
2308         unsigned int wake_bits = 0, changed_bits = 0;
2309         DWORD dwlc;
2310
2311         SERVER_START_REQ( set_queue_mask )
2312         {
2313             req->wake_mask    = QS_SENDMESSAGE;
2314             req->changed_mask = mask;
2315             req->skip_wait    = 1;
2316             if (!wine_server_call( req ))
2317             {
2318                 wake_bits    = reply->wake_bits;
2319                 changed_bits = reply->changed_bits;
2320             }
2321         }
2322         SERVER_END_REQ;
2323
2324         if (changed_bits & mask) continue;
2325         if (wake_bits & QS_SENDMESSAGE) continue;
2326
2327         TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2328                queue->self, mask, wake_bits, changed_bits );
2329
2330         ReleaseThunkLock( &dwlc );
2331         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2332             USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2333         else
2334             WaitForSingleObject( queue->server_queue, INFINITE );
2335         if (dwlc) RestoreThunkLock( dwlc );
2336     }
2337
2338     WIN_RestoreWndsLock( locks );
2339
2340     return (msg->message != WM_QUIT);
2341 }
2342
2343
2344 /***********************************************************************
2345  *              GetMessageA  (USER32.@)
2346  */
2347 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2348 {
2349     GetMessageW( msg, hwnd, first, last );
2350     msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2351     return (msg->message != WM_QUIT);
2352 }
2353
2354
2355 /***********************************************************************
2356  *              IsDialogMessage  (USER32.@)
2357  *              IsDialogMessageA (USER32.@)
2358  */
2359 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2360 {
2361     MSG msg = *pmsg;
2362     msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2363     return IsDialogMessageW( hwndDlg, &msg );
2364 }
2365
2366
2367 /***********************************************************************
2368  *              SetMessageQueue (USER32.@)
2369  */
2370 BOOL WINAPI SetMessageQueue( INT size )
2371 {
2372     /* now obsolete the message queue will be expanded dynamically as necessary */
2373     return TRUE;
2374 }
2375
2376
2377 /**********************************************************************
2378  *              AttachThreadInput (USER32.@)
2379  *
2380  * Attaches the input processing mechanism of one thread to that of
2381  * another thread.
2382  */
2383 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2384 {
2385     BOOL ret;
2386
2387     SERVER_START_REQ( attach_thread_input )
2388     {
2389         req->tid_from = from;
2390         req->tid_to   = to;
2391         req->attach   = attach;
2392         ret = !wine_server_call_err( req );
2393     }
2394     SERVER_END_REQ;
2395     return ret;
2396 }
2397
2398
2399 /**********************************************************************
2400  *              GetGUIThreadInfo  (USER32.@)
2401  */
2402 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2403 {
2404     BOOL ret;
2405
2406     SERVER_START_REQ( get_thread_input )
2407     {
2408         req->tid = id;
2409         if ((ret = !wine_server_call_err( req )))
2410         {
2411             info->flags          = 0;
2412             info->hwndActive     = reply->active;
2413             info->hwndFocus      = reply->focus;
2414             info->hwndCapture    = reply->capture;
2415             info->hwndMenuOwner  = reply->menu_owner;
2416             info->hwndMoveSize   = reply->move_size;
2417             info->hwndCaret      = reply->caret;
2418             info->rcCaret.left   = reply->rect.left;
2419             info->rcCaret.top    = reply->rect.top;
2420             info->rcCaret.right  = reply->rect.right;
2421             info->rcCaret.bottom = reply->rect.bottom;
2422             if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2423             if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2424             if (reply->caret) info->flags |= GUI_CARETBLINKING;
2425         }
2426     }
2427     SERVER_END_REQ;
2428     return ret;
2429 }
2430
2431
2432 /**********************************************************************
2433  *              GetKeyState (USER32.@)
2434  *
2435  * An application calls the GetKeyState function in response to a
2436  * keyboard-input message.  This function retrieves the state of the key
2437  * at the time the input message was generated.
2438  */
2439 SHORT WINAPI GetKeyState(INT vkey)
2440 {
2441     SHORT retval = 0;
2442
2443     SERVER_START_REQ( get_key_state )
2444     {
2445         req->tid = GetCurrentThreadId();
2446         req->key = vkey;
2447         if (!wine_server_call( req )) retval = (signed char)reply->state;
2448     }
2449     SERVER_END_REQ;
2450     TRACE("key (0x%x) -> %x\n", vkey, retval);
2451     return retval;
2452 }
2453
2454
2455 /**********************************************************************
2456  *              GetKeyboardState (USER32.@)
2457  */
2458 BOOL WINAPI GetKeyboardState( LPBYTE state )
2459 {
2460     BOOL ret;
2461
2462     TRACE("(%p)\n", state);
2463
2464     memset( state, 0, 256 );
2465     SERVER_START_REQ( get_key_state )
2466     {
2467         req->tid = GetCurrentThreadId();
2468         req->key = -1;
2469         wine_server_set_reply( req, state, 256 );
2470         ret = !wine_server_call_err( req );
2471     }
2472     SERVER_END_REQ;
2473     return ret;
2474 }
2475
2476
2477 /**********************************************************************
2478  *              SetKeyboardState (USER32.@)
2479  */
2480 BOOL WINAPI SetKeyboardState( LPBYTE state )
2481 {
2482     BOOL ret;
2483
2484     TRACE("(%p)\n", state);
2485
2486     SERVER_START_REQ( set_key_state )
2487     {
2488         req->tid = GetCurrentThreadId();
2489         wine_server_add_data( req, state, 256 );
2490         ret = !wine_server_call_err( req );
2491     }
2492     SERVER_END_REQ;
2493     return ret;
2494 }