comctl32: Add implementation of LVS_EX_ONECLICKACTIVATE.
[wine] / dlls / user32 / tests / dde.c
1 /*
2  * Unit tests for DDE functions
3  *
4  * Copyright (c) 2004 Dmitry Timoshkov
5  * Copyright (c) 2007 James Hawkins
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "dde.h"
30 #include "ddeml.h"
31 #include "winerror.h"
32
33 #include "wine/test.h"
34
35 static const WCHAR TEST_DDE_SERVICE[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
36
37 static char exec_cmdA[] = "ANSI dde command";
38 static WCHAR exec_cmdW[] = {'u','n','i','c','o','d','e',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
39
40 static WNDPROC old_dde_client_wndproc;
41
42 static const DWORD default_timeout = 200;
43
44 static void flush_events(void)
45 {
46     MSG msg;
47     int diff = default_timeout;
48     int min_timeout = 50;
49     DWORD time = GetTickCount() + diff;
50
51     while (diff > 0)
52     {
53         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
54         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
55         diff = time - GetTickCount();
56         min_timeout = 10;
57     }
58 }
59
60 static void create_dde_window(HWND *hwnd, LPCSTR name, WNDPROC wndproc)
61 {
62     WNDCLASSA wcA;
63
64     memset(&wcA, 0, sizeof(wcA));
65     wcA.lpfnWndProc = wndproc;
66     wcA.lpszClassName = name;
67     wcA.hInstance = GetModuleHandleA(0);
68     assert(RegisterClassA(&wcA));
69
70     *hwnd = CreateWindowExA(0, name, NULL, WS_POPUP,
71                             500, 500, CW_USEDEFAULT, CW_USEDEFAULT,
72                             GetDesktopWindow(), 0, GetModuleHandleA(0), NULL);
73     assert(*hwnd);
74 }
75
76 static void destroy_dde_window(HWND *hwnd, LPCSTR name)
77 {
78     DestroyWindow(*hwnd);
79     UnregisterClass(name, GetModuleHandleA(0));
80 }
81
82 static LRESULT WINAPI dde_server_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
83 {
84     UINT_PTR lo, hi;
85     char str[MAX_PATH], *ptr;
86     HGLOBAL hglobal;
87     DDEDATA *data;
88     DDEPOKE *poke;
89     DWORD size;
90
91     static int msg_index = 0;
92     static HWND client = 0;
93     static BOOL executed = FALSE;
94
95     if (msg < WM_DDE_FIRST || msg > WM_DDE_LAST)
96         return DefWindowProcA(hwnd, msg, wparam, lparam);
97
98     msg_index++;
99
100     switch (msg)
101     {
102     case WM_DDE_INITIATE:
103     {
104         client = (HWND)wparam;
105         ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
106
107         GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
108         ok(!lstrcmpA(str, "TestDDEService"), "Expected TestDDEService, got %s\n", str);
109
110         GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
111         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
112
113         SendMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
114
115         break;
116     }
117
118     case WM_DDE_REQUEST:
119     {
120         ok((msg_index >= 2 && msg_index <= 4) ||
121            (msg_index >= 7 && msg_index <= 8),
122            "Expected 2, 3, 4, 7 or 8, got %d\n", msg_index);
123         ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
124         ok(LOWORD(lparam) == CF_TEXT, "Expected CF_TEXT, got %d\n", LOWORD(lparam));
125
126         GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
127         if (msg_index < 8)
128             ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
129         else
130             ok(!lstrcmpA(str, "executed"), "Expected executed, got %s\n", str);
131
132         if (msg_index == 8)
133         {
134             if (executed)
135                 lstrcpyA(str, "command executed\r\n");
136             else
137                 lstrcpyA(str, "command not executed\r\n");
138         }
139         else
140             lstrcpyA(str, "requested data\r\n");
141
142         size = sizeof(DDEDATA) + lstrlenA(str) + 1;
143         hglobal = GlobalAlloc(GMEM_MOVEABLE, size);
144         ok(hglobal != NULL, "Expected non-NULL hglobal\n");
145
146         data = GlobalLock(hglobal);
147         ZeroMemory(data, size);
148
149         /* setting fResponse to FALSE at this point destroys
150          * the internal messaging state of native dde
151          */
152         data->fResponse = TRUE;
153
154         if (msg_index == 2)
155             data->fRelease = TRUE;
156         else if (msg_index == 3)
157             data->fAckReq = TRUE;
158
159         data->cfFormat = CF_TEXT;
160         lstrcpyA((LPSTR)data->Value, str);
161         GlobalUnlock(hglobal);
162
163         lparam = PackDDElParam(WM_DDE_ACK, (UINT)hglobal, HIWORD(lparam));
164         PostMessageA(client, WM_DDE_DATA, (WPARAM)hwnd, lparam);
165
166         break;
167     }
168
169     case WM_DDE_POKE:
170     {
171         ok(msg_index == 5 || msg_index == 6, "Expected 5 or 6, got %d\n", msg_index);
172         ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
173
174         UnpackDDElParam(WM_DDE_POKE, lparam, &lo, &hi);
175
176         GlobalGetAtomNameA(hi, str, MAX_PATH);
177         ok(!lstrcmpA(str, "poker"), "Expected poker, got %s\n", str);
178
179         poke = GlobalLock((HGLOBAL)lo);
180         ok(poke != NULL, "Expected non-NULL poke\n");
181         ok(poke->fReserved == 0, "Expected 0, got %d\n", poke->fReserved);
182         ok(poke->unused == 0, "Expected 0, got %d\n", poke->unused);
183         ok(poke->fRelease == TRUE, "Expected TRUE, got %d\n", poke->fRelease);
184         ok(poke->cfFormat == CF_TEXT, "Expected CF_TEXT, got %d\n", poke->cfFormat);
185
186         if (msg_index == 5)
187         {
188             size = GlobalSize((HGLOBAL)lo);
189             ok(size == 4, "got %d\n", size);
190         }
191         else
192             ok(!lstrcmpA((LPSTR)poke->Value, "poke data\r\n"),
193                "Expected 'poke data\\r\\n', got %s\n", poke->Value);
194
195         GlobalUnlock((HGLOBAL)lo);
196
197         lparam = PackDDElParam(WM_DDE_ACK, DDE_FACK, hi);
198         PostMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
199
200         break;
201     }
202
203     case WM_DDE_EXECUTE:
204     {
205         ok(msg_index == 7, "Expected 7, got %d\n", msg_index);
206         ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
207
208         ptr = GlobalLock((HGLOBAL)lparam);
209         ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected [Command(Var)], got %s\n", ptr);
210         GlobalUnlock((HGLOBAL)lparam);
211
212         executed = TRUE;
213
214         lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, DDE_FACK, HIWORD(lparam));
215         PostMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
216
217         break;
218     }
219
220     case WM_DDE_TERMINATE:
221     {
222         ok(msg_index == 9, "Expected 9, got %d\n", msg_index);
223         ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
224         ok(lparam == 0, "Expected 0, got %08lx\n", lparam);
225
226         PostMessageA(client, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
227
228         break;
229     }
230
231     default:
232         ok(FALSE, "Unhandled msg: %08x\n", msg);
233     }
234
235     return DefWindowProcA(hwnd, msg, wparam, lparam);
236 }
237
238 static void test_msg_server(HANDLE hproc, HANDLE hthread)
239 {
240     MSG msg;
241     HWND hwnd;
242     DWORD res;
243
244     create_dde_window(&hwnd, "dde_server", dde_server_wndproc);
245     ResumeThread( hthread );
246
247     while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
248     {
249         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
250     }
251
252     destroy_dde_window(&hwnd, "dde_server");
253     GetExitCodeProcess( hproc, &res );
254     ok( !res, "client failed with %u error(s)\n", res );
255 }
256
257 static HDDEDATA CALLBACK client_ddeml_callback(UINT uType, UINT uFmt, HCONV hconv,
258                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
259                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
260 {
261     ok(FALSE, "Unhandled msg: %08x\n", uType);
262     return 0;
263 }
264
265 static void test_ddeml_client(void)
266 {
267     UINT ret;
268     char buffer[32];
269     LPSTR str;
270     DWORD size, res;
271     HDDEDATA hdata, op;
272     HSZ server, topic, item;
273     DWORD client_pid;
274     HCONV conversation;
275
276     client_pid = 0;
277     ret = DdeInitializeA(&client_pid, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
278     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
279
280     /* FIXME: make these atoms global and check them in the server */
281
282     server = DdeCreateStringHandleA(client_pid, "TestDDEService", CP_WINANSI);
283     topic = DdeCreateStringHandleA(client_pid, "TestDDETopic", CP_WINANSI);
284
285     DdeGetLastError(client_pid);
286     conversation = DdeConnect(client_pid, server, topic, NULL);
287     ok(conversation != NULL, "Expected non-NULL conversation\n");
288     ret = DdeGetLastError(client_pid);
289     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
290
291     DdeFreeStringHandle(client_pid, server);
292
293     item = DdeCreateStringHandleA(client_pid, "request", CP_WINANSI);
294
295     /* XTYP_REQUEST, fRelease = TRUE */
296     res = 0xdeadbeef;
297     DdeGetLastError(client_pid);
298     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
299     ret = DdeGetLastError(client_pid);
300     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
301     ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %08x\n", res);
302     if (hdata == NULL)
303         ok(FALSE, "hdata is NULL\n");
304     else
305     {
306         str = (LPSTR)DdeAccessData(hdata, &size);
307         ok(!lstrcmpA(str, "requested data\r\n"), "Expected 'requested data\\r\\n', got %s\n", str);
308         ok(size == 19, "Expected 19, got %d\n", size);
309
310         ret = DdeUnaccessData(hdata);
311         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
312     }
313
314     /* XTYP_REQUEST, fAckReq = TRUE */
315     res = 0xdeadbeef;
316     DdeGetLastError(client_pid);
317     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
318     ret = DdeGetLastError(client_pid);
319     ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
320 todo_wine
321     ok(ret == DMLERR_MEMORY_ERROR, "Expected DMLERR_MEMORY_ERROR, got %d\n", ret);
322     if (hdata == NULL)
323         ok(FALSE, "hdata is NULL\n");
324     else
325     {
326         str = (LPSTR)DdeAccessData(hdata, &size);
327         ok(!lstrcmpA(str, "requested data\r\n"), "Expected 'requested data\\r\\n', got %s\n", str);
328         ok(size == 19, "Expected 19, got %d\n", size);
329
330         ret = DdeUnaccessData(hdata);
331         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
332     }
333
334     /* XTYP_REQUEST, all params normal */
335     res = 0xdeadbeef;
336     DdeGetLastError(client_pid);
337     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
338     ret = DdeGetLastError(client_pid);
339     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
340     ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
341     if (hdata == NULL)
342         ok(FALSE, "hdata is NULL\n");
343     else
344     {
345         str = (LPSTR)DdeAccessData(hdata, &size);
346         ok(!lstrcmpA(str, "requested data\r\n"), "Expected 'requested data\\r\\n', got %s\n", str);
347         ok(size == 19, "Expected 19, got %d\n", size);
348
349         ret = DdeUnaccessData(hdata);
350         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
351     }
352
353     /* XTYP_REQUEST, no item */
354     res = 0xdeadbeef;
355     DdeGetLastError(client_pid);
356     hdata = DdeClientTransaction(NULL, 0, conversation, 0, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
357     ret = DdeGetLastError(client_pid);
358     ok(hdata == NULL, "Expected NULL hdata, got %p\n", hdata);
359     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %08x\n", res);
360     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
361
362     DdeFreeStringHandle(client_pid, item);
363
364     item = DdeCreateStringHandleA(client_pid, "poker", CP_WINANSI);
365
366     lstrcpyA(buffer, "poke data\r\n");
367     hdata = DdeCreateDataHandle(client_pid, (LPBYTE)buffer, lstrlenA(buffer) + 1,
368                                 0, item, CF_TEXT, 0);
369     ok(hdata != NULL, "Expected non-NULL hdata\n");
370
371     /* XTYP_POKE, no item */
372     res = 0xdeadbeef;
373     DdeGetLastError(client_pid);
374     op = DdeClientTransaction((LPBYTE)hdata, -1, conversation, 0, CF_TEXT, XTYP_POKE, default_timeout, &res);
375     ret = DdeGetLastError(client_pid);
376     ok(op == NULL, "Expected NULL, got %p\n", op);
377     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
378     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
379
380     /* XTYP_POKE, no data */
381     res = 0xdeadbeef;
382     DdeGetLastError(client_pid);
383     op = DdeClientTransaction(NULL, 0, conversation, 0, CF_TEXT, XTYP_POKE, default_timeout, &res);
384     ret = DdeGetLastError(client_pid);
385     ok(op == NULL, "Expected NULL, got %p\n", op);
386     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
387     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
388
389     /* XTYP_POKE, wrong size */
390     res = 0xdeadbeef;
391     DdeGetLastError(client_pid);
392     op = DdeClientTransaction((LPBYTE)hdata, 0, conversation, item, CF_TEXT, XTYP_POKE, default_timeout, &res);
393     ret = DdeGetLastError(client_pid);
394     ok(op == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", op);
395     ok(res == DDE_FACK, "Expected DDE_FACK, got %d\n", res);
396     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
397
398     /* XTYP_POKE, correct params */
399     res = 0xdeadbeef;
400     DdeGetLastError(client_pid);
401     op = DdeClientTransaction((LPBYTE)hdata, -1, conversation, item, CF_TEXT, XTYP_POKE, default_timeout, &res);
402     ret = DdeGetLastError(client_pid);
403     ok(op == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", op);
404     ok(res == DDE_FACK, "Expected DDE_FACK, got %d\n", res);
405     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
406
407     DdeFreeDataHandle(hdata);
408
409     lstrcpyA(buffer, "[Command(Var)]");
410     hdata = DdeCreateDataHandle(client_pid, (LPBYTE)buffer, lstrlenA(buffer) + 1,
411                                 0, NULL, CF_TEXT, 0);
412     ok(hdata != NULL, "Expected non-NULL hdata\n");
413
414     /* XTYP_EXECUTE, correct params */
415     res = 0xdeadbeef;
416     DdeGetLastError(client_pid);
417     op = DdeClientTransaction((LPBYTE)hdata, -1, conversation, NULL, 0, XTYP_EXECUTE, default_timeout, &res);
418     ret = DdeGetLastError(client_pid);
419     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
420     ok(op == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", op);
421     ok(res == DDE_FACK, "Expected DDE_FACK, got %d\n", res);
422
423     /* XTYP_EXECUTE, no data */
424     res = 0xdeadbeef;
425     DdeGetLastError(client_pid);
426     op = DdeClientTransaction(NULL, 0, conversation, NULL, 0, XTYP_EXECUTE, default_timeout, &res);
427     ret = DdeGetLastError(client_pid);
428     ok(op == NULL, "Expected NULL, got %p\n", op);
429     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
430     todo_wine
431     {
432         ok(ret == DMLERR_MEMORY_ERROR, "Expected DMLERR_MEMORY_ERROR, got %d\n", ret);
433     }
434
435     /* XTYP_EXECUTE, no data, -1 size */
436     res = 0xdeadbeef;
437     DdeGetLastError(client_pid);
438     op = DdeClientTransaction(NULL, -1, conversation, NULL, 0, XTYP_EXECUTE, default_timeout, &res);
439     ret = DdeGetLastError(client_pid);
440     ok(op == NULL, "Expected NULL, got %p\n", op);
441     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
442     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
443
444     DdeFreeStringHandle(client_pid, topic);
445     DdeFreeDataHandle(hdata);
446
447     item = DdeCreateStringHandleA(client_pid, "executed", CP_WINANSI);
448
449     /* verify the execute */
450     res = 0xdeadbeef;
451     DdeGetLastError(client_pid);
452     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
453     ret = DdeGetLastError(client_pid);
454     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
455     ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
456     if (hdata == NULL)
457         ok(FALSE, "hdata is NULL\n");
458     else
459     {
460         str = (LPSTR)DdeAccessData(hdata, &size);
461         ok(!lstrcmpA(str, "command executed\r\n"), "Expected 'command executed\\r\\n', got %s\n", str);
462         ok(size == 21, "Expected 21, got %d\n", size);
463
464         ret = DdeUnaccessData(hdata);
465         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
466     }
467
468     /* invalid transactions */
469     res = 0xdeadbeef;
470     DdeGetLastError(client_pid);
471     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_ADVREQ, default_timeout, &res);
472     ret = DdeGetLastError(client_pid);
473     ok(op == NULL, "Expected NULL, got %p\n", op);
474     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
475     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
476
477     res = 0xdeadbeef;
478     DdeGetLastError(client_pid);
479     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_CONNECT, default_timeout, &res);
480     ret = DdeGetLastError(client_pid);
481     ok(op == NULL, "Expected NULL, got %p\n", op);
482     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
483     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
484
485     res = 0xdeadbeef;
486     DdeGetLastError(client_pid);
487     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_CONNECT_CONFIRM, default_timeout, &res);
488     ret = DdeGetLastError(client_pid);
489     ok(op == NULL, "Expected NULL, got %p\n", op);
490     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
491     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
492
493     res = 0xdeadbeef;
494     DdeGetLastError(client_pid);
495     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_DISCONNECT, default_timeout, &res);
496     ret = DdeGetLastError(client_pid);
497     ok(op == NULL, "Expected NULL, got %p\n", op);
498     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
499     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
500
501     res = 0xdeadbeef;
502     DdeGetLastError(client_pid);
503     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_ERROR, default_timeout, &res);
504     ret = DdeGetLastError(client_pid);
505     ok(op == NULL, "Expected NULL, got %p\n", op);
506     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
507     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
508
509     res = 0xdeadbeef;
510     DdeGetLastError(client_pid);
511     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_MONITOR, default_timeout, &res);
512     ret = DdeGetLastError(client_pid);
513     ok(op == NULL, "Expected NULL, got %p\n", op);
514     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
515     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
516
517     res = 0xdeadbeef;
518     DdeGetLastError(client_pid);
519     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REGISTER, default_timeout, &res);
520     ret = DdeGetLastError(client_pid);
521     ok(op == NULL, "Expected NULL, got %p\n", op);
522     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
523     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
524
525     res = 0xdeadbeef;
526     DdeGetLastError(client_pid);
527     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_UNREGISTER, default_timeout, &res);
528     ret = DdeGetLastError(client_pid);
529     ok(op == NULL, "Expected NULL, got %p\n", op);
530     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
531     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
532
533     res = 0xdeadbeef;
534     DdeGetLastError(client_pid);
535     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_WILDCONNECT, default_timeout, &res);
536     ret = DdeGetLastError(client_pid);
537     ok(op == NULL, "Expected NULL, got %p\n", op);
538     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
539     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
540
541     res = 0xdeadbeef;
542     DdeGetLastError(client_pid);
543     op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_XACT_COMPLETE, default_timeout, &res);
544     ret = DdeGetLastError(client_pid);
545     ok(op == NULL, "Expected NULL, got %p\n", op);
546     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
547     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
548
549     DdeFreeStringHandle(client_pid, item);
550
551     ret = DdeDisconnect(conversation);
552     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
553
554     ret = DdeUninitialize(client_pid);
555     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
556 }
557
558 static DWORD server_pid;
559
560 static HDDEDATA CALLBACK server_ddeml_callback(UINT uType, UINT uFmt, HCONV hconv,
561                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
562                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
563 {
564     char str[MAX_PATH], *ptr;
565     HDDEDATA ret;
566     DWORD size;
567
568     static int msg_index = 0;
569     static HCONV conversation = 0;
570
571     msg_index++;
572
573     switch (uType)
574     {
575     case XTYP_REGISTER:
576     {
577         ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
578         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
579         ok(hconv == 0, "Expected 0, got %p\n", hconv);
580         ok(hdata == 0, "Expected 0, got %p\n", hdata);
581         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
582         ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
583
584         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
585         ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
586         ok(size == 13, "Expected 13, got %d\n", size);
587
588         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
589         ok(!strncmp(str, "TestDDEServer(", 14), "Expected TestDDEServer(, got %s\n", str);
590         ok(str[size - 1] == ')', "Expected ')', got %c\n", str[size - 1]);
591         ok(size == 25, "Expected 25, got %d\n", size);
592
593         return (HDDEDATA)TRUE;
594     }
595
596     case XTYP_CONNECT:
597     {
598         ok(msg_index == 2, "Expected 2, got %d\n", msg_index);
599         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
600         ok(hconv == 0, "Expected 0, got %p\n", hconv);
601         ok(hdata == 0, "Expected 0, got %p\n", hdata);
602         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
603         ok(dwData2 == FALSE, "Expected FALSE, got %08lx\n", dwData2);
604
605         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
606         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
607         ok(size == 12, "Expected 12, got %d\n", size);
608
609         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
610         ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
611         ok(size == 13, "Expected 13, got %d\n", size);
612
613         return (HDDEDATA)TRUE;
614     }
615
616     case XTYP_CONNECT_CONFIRM:
617     {
618         conversation = hconv;
619
620         ok(msg_index == 3, "Expected 3, got %d\n", msg_index);
621         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
622         ok(hconv != NULL, "Expected non-NULL hconv\n");
623         ok(hdata == 0, "Expected 0, got %p\n", hdata);
624         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
625         ok(dwData2 == FALSE, "Expected FALSE, got %08lx\n", dwData2);
626
627         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
628         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
629         ok(size == 12, "Expected 12, got %d\n", size);
630
631         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
632         ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
633         ok(size == 13, "Expected 13, got %d\n", size);
634
635         return (HDDEDATA)TRUE;
636     }
637
638     case XTYP_REQUEST:
639     {
640         ok(msg_index == 4 || msg_index == 5 || msg_index == 6,
641            "Expected 4, 5 or 6, got %d\n", msg_index);
642         ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
643         ok(hdata == 0, "Expected 0, got %p\n", hdata);
644         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
645         ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
646
647         if (msg_index == 4)
648             ok(uFmt == 0xbeef, "Expected 0xbeef, got %08x\n", uFmt);
649         else
650             ok(uFmt == CF_TEXT, "Expected CF_TEXT, got %08x\n", uFmt);
651
652         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
653         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
654         ok(size == 12, "Expected 12, got %d\n", size);
655
656         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
657
658         if (msg_index == 5)
659         {
660             {
661                 ok(!lstrcmpA(str, ""), "Expected empty string, got %s\n", str);
662                 ok(size == 1, "Expected 1, got %d\n", size);
663             }
664         }
665         else if (msg_index == 6)
666         {
667             ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
668             ok(size == 7, "Expected 7, got %d\n", size);
669         }
670
671         if (msg_index == 6)
672         {
673             lstrcpyA(str, "requested data\r\n");
674             return DdeCreateDataHandle(server_pid, (LPBYTE)str, lstrlenA(str) + 1,
675                                         0, hsz2, CF_TEXT, 0);
676         }
677
678         return NULL;
679     }
680
681     case XTYP_POKE:
682     {
683         ok(msg_index == 7 || msg_index == 8, "Expected 7 or 8, got %d\n", msg_index);
684         ok(uFmt == CF_TEXT, "Expected CF_TEXT, got %d\n", uFmt);
685         ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
686         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
687         ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
688
689         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
690         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
691         ok(size == 12, "Expected 12, got %d\n", size);
692
693         ptr = (LPSTR)DdeAccessData(hdata, &size);
694         ok(!lstrcmpA(ptr, "poke data\r\n"), "Expected 'poke data\\r\\n', got %s\n", ptr);
695         ok(size == 12, "Expected 12, got %d\n", size);
696         DdeUnaccessData(hdata);
697
698         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
699         if (msg_index == 7)
700         {
701             {
702                 ok(!lstrcmpA(str, ""), "Expected empty string, got %s\n", str);
703                 ok(size == 1, "Expected 1, got %d\n", size);
704             }
705         }
706         else
707         {
708             ok(!lstrcmpA(str, "poke"), "Expected poke, got %s\n", str);
709             ok(size == 4, "Expected 4, got %d\n", size);
710         }
711
712         return (HDDEDATA)DDE_FACK;
713     }
714
715     case XTYP_EXECUTE:
716     {
717         ok(msg_index == 9 || msg_index == 10, "Expected 9 or 10, got %d\n", msg_index);
718         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
719         ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
720         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
721         ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
722         ok(hsz2 == 0, "Expected 0, got %p\n", hsz2);
723
724         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
725         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
726         ok(size == 12, "Expected 12, got %d\n", size);
727
728         ptr = (LPSTR)DdeAccessData(hdata, &size);
729
730         if (msg_index == 9)
731         {
732             ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected '[Command(Var)]', got %s\n", ptr);
733             ok(size == 15, "Expected 15, got %d\n", size);
734             ret = (HDDEDATA)DDE_FACK;
735         }
736         else
737         {
738             ok(!lstrcmpA(ptr, "[BadCommand(Var)]"), "Expected '[BadCommand(Var)]', got %s\n", ptr);
739             ok(size == 18, "Expected 18, got %d\n", size);
740             ret = (HDDEDATA)DDE_FNOTPROCESSED;
741         }
742
743         DdeUnaccessData(hdata);
744
745         return ret;
746     }
747
748     case XTYP_DISCONNECT:
749     {
750         ok(msg_index == 11, "Expected 11, got %d\n", msg_index);
751         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
752         ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
753         ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
754         ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
755         ok(hsz1 == 0, "Expected 0, got %p\n", hsz2);
756         ok(hsz2 == 0, "Expected 0, got %p\n", hsz2);
757
758         return 0;
759     }
760
761     default:
762         ok(FALSE, "Unhandled msg: %08x\n", uType);
763     }
764
765     return 0;
766 }
767
768 static void test_ddeml_server(HANDLE hproc)
769 {
770     MSG msg;
771     UINT res;
772     BOOL ret;
773     HSZ server;
774     HDDEDATA hdata;
775
776     /* set up DDE server */
777     server_pid = 0;
778     res = DdeInitialize(&server_pid, server_ddeml_callback, APPCLASS_STANDARD, 0);
779     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
780
781     server = DdeCreateStringHandle(server_pid, "TestDDEServer", CP_WINANSI);
782     ok(server != NULL, "Expected non-NULL string handle\n");
783
784     hdata = DdeNameService(server_pid, server, 0, DNS_REGISTER);
785     ok(hdata == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", hdata);
786
787     while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
788     {
789         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
790     }
791     ret = DdeUninitialize(server_pid);
792     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
793     GetExitCodeProcess( hproc, &res );
794     ok( !res, "client failed with %u error(s)\n", res );
795 }
796
797 static HWND client_hwnd, server_hwnd;
798 static ATOM server, topic, item;
799 static HGLOBAL execute_hglobal;
800
801 static LRESULT WINAPI dde_msg_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
802 {
803     char str[MAX_PATH];
804     UINT_PTR lo, hi;
805     DDEDATA *data;
806     DDEACK *ack;
807     DWORD size;
808     LPSTR ptr;
809
810     static int msg_index = 0;
811
812     if (msg < WM_DDE_FIRST || msg > WM_DDE_LAST)
813         return DefWindowProcA(hwnd, msg, wparam, lparam);
814
815     msg_index++;
816
817     switch (msg)
818     {
819     case WM_DDE_INITIATE:
820     {
821         ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
822         ok(wparam == (WPARAM)client_hwnd, "Expected client hwnd, got %08lx\n", wparam);
823
824         size = GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
825         ok(LOWORD(lparam) == server, "Expected server atom, got %08x\n", LOWORD(lparam));
826         ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
827         ok(size == 13, "Expected 13, got %d\n", size);
828
829         size = GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
830         ok(HIWORD(lparam) == topic, "Expected topic atom, got %08x\n", HIWORD(lparam));
831         ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
832         ok(size == 12, "Expected 12, got %d\n", size);
833
834         break;
835     }
836
837     case WM_DDE_ACK:
838     {
839         ok((msg_index >= 2 && msg_index <= 4) || (msg_index >= 6 && msg_index <= 10),
840            "Expected 2, 3, 4, 6, 7, 8, 9 or 10, got %d\n", msg_index);
841
842         if (msg_index == 2)
843         {
844             server_hwnd = (HWND)wparam;
845             ok(wparam != 0, "Expected non-NULL wparam, got %08lx\n", wparam);
846
847             size = GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
848             ok(LOWORD(lparam) == server, "Expected server atom, got %08x\n", LOWORD(lparam));
849             ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
850             ok(size == 13, "Expected 13, got %d\n", size);
851
852             size = GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
853             ok(HIWORD(lparam) == topic, "Expected topic atom, got %08x\n", HIWORD(lparam));
854             ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
855             ok(size == 12, "Expected 12, got %d\n", size);
856         }
857         else if (msg_index == 9 || msg_index == 10)
858         {
859             ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
860
861             UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
862
863             ack = (DDEACK *)&lo;
864             ok(ack->bAppReturnCode == 0, "Expected 0, got %d\n", ack->bAppReturnCode);
865             ok(ack->reserved == 0, "Expected 0, got %d\n", ack->reserved);
866             ok(ack->fBusy == FALSE, "Expected FALSE, got %d\n", ack->fBusy);
867
868             ok(hi == (UINT_PTR)execute_hglobal, "Execpted execute hglobal, got %08lx\n", hi);
869             ptr = GlobalLock((HGLOBAL)hi);
870
871             if (msg_index == 9)
872             {
873                 ok(ack->fAck == TRUE, "Expected TRUE, got %d\n", ack->fAck);
874                 ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected '[Command(Var)]', got %s\n", ptr);
875             }
876             else
877             {
878                 ok(ack->fAck == FALSE, "Expected FALSE, got %d\n", ack->fAck);
879                 ok(!lstrcmpA(ptr, "[BadCommand(Var)]"), "Expected '[BadCommand(Var)]', got %s\n", ptr);
880             }
881
882             GlobalUnlock((HGLOBAL)hi);
883         }
884         else
885         {
886             ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
887
888             UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
889
890             ack = (DDEACK *)&lo;
891             ok(ack->bAppReturnCode == 0, "Expected 0, got %d\n", ack->bAppReturnCode);
892             ok(ack->reserved == 0, "Expected 0, got %d\n", ack->reserved);
893             ok(ack->fBusy == FALSE, "Expected FALSE, got %d\n", ack->fBusy);
894
895             if (msg_index >= 7)
896                 ok(ack->fAck == TRUE, "Expected TRUE, got %d\n", ack->fAck);
897             else
898             {
899                 if (msg_index == 6) todo_wine
900                 ok(ack->fAck == FALSE, "Expected FALSE, got %d\n", ack->fAck);
901             }
902
903             size = GlobalGetAtomNameA(hi, str, MAX_PATH);
904             if (msg_index == 3)
905             {
906                 ok(hi == item, "Expected item atom, got %08lx\n", hi);
907                 ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
908                 ok(size == 7, "Expected 7, got %d\n", size);
909             }
910             else if (msg_index == 4 || msg_index == 7)
911             {
912                 ok(hi == 0, "Expected 0, got %08lx\n", hi);
913                 ok(size == 0, "Expected empty string, got %d\n", size);
914             }
915             else
916             {
917                 ok(hi == item, "Expected item atom, got %08lx\n", hi);
918                 if (msg_index == 6) todo_wine
919                 {
920                     ok(!lstrcmpA(str, "poke"), "Expected poke, got %s\n", str);
921                     ok(size == 4, "Expected 4, got %d\n", size);
922                 }
923             }
924         }
925
926         break;
927     }
928
929     case WM_DDE_DATA:
930     {
931         ok(msg_index == 5, "Expected 5, got %d\n", msg_index);
932         ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
933
934         UnpackDDElParam(WM_DDE_DATA, lparam, &lo, &hi);
935
936         data = GlobalLock((HGLOBAL)lo);
937         ok(data->unused == 0, "Expected 0, got %d\n", data->unused);
938         ok(data->fResponse == TRUE, "Expected TRUE, got %d\n", data->fResponse);
939         todo_wine
940         {
941             ok(data->fRelease == TRUE, "Expected TRUE, got %d\n", data->fRelease);
942         }
943         ok(data->fAckReq == 0, "Expected 0, got %d\n", data->fAckReq);
944         ok(data->cfFormat == CF_TEXT, "Expected CF_TEXT, got %d\n", data->cfFormat);
945         ok(!lstrcmpA((LPSTR)data->Value, "requested data\r\n"),
946            "Expeted 'requested data\\r\\n', got %s\n", data->Value);
947         GlobalUnlock((HGLOBAL)lo);
948
949         size = GlobalGetAtomNameA(hi, str, MAX_PATH);
950         ok(hi == item, "Expected item atom, got %08x\n", HIWORD(lparam));
951         ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
952         ok(size == 7, "Expected 7, got %d\n", size);
953
954         GlobalFree((HGLOBAL)lo);
955         GlobalDeleteAtom(hi);
956
957         break;
958     }
959
960     default:
961         ok(FALSE, "Unhandled msg: %08x\n", msg);
962     }
963
964     return DefWindowProcA(hwnd, msg, wparam, lparam);
965 }
966
967 static HGLOBAL create_poke()
968 {
969     HGLOBAL hglobal;
970     DDEPOKE *poke;
971     DWORD size;
972
973     size = FIELD_OFFSET(DDEPOKE, Value[sizeof("poke data\r\n")]);
974     hglobal = GlobalAlloc(GMEM_DDESHARE, size);
975     ok(hglobal != 0, "Expected non-NULL hglobal\n");
976
977     poke = GlobalLock(hglobal);
978     poke->unused = 0;
979     poke->fRelease = TRUE;
980     poke->fReserved = TRUE;
981     poke->cfFormat = CF_TEXT;
982     lstrcpyA((LPSTR)poke->Value, "poke data\r\n");
983     GlobalUnlock(hglobal);
984
985     return hglobal;
986 }
987
988 static HGLOBAL create_execute(LPCSTR command)
989 {
990     HGLOBAL hglobal;
991     LPSTR ptr;
992
993     hglobal = GlobalAlloc(GMEM_DDESHARE, lstrlenA(command) + 1);
994     ok(hglobal != 0, "Expected non-NULL hglobal\n");
995
996     ptr = GlobalLock(hglobal);
997     lstrcpyA(ptr, command);
998     GlobalUnlock(hglobal);
999
1000     return hglobal;
1001 }
1002
1003 static void test_msg_client()
1004 {
1005     HGLOBAL hglobal;
1006     LPARAM lparam;
1007
1008     create_dde_window(&client_hwnd, "dde_client", dde_msg_client_wndproc);
1009
1010     server = GlobalAddAtomA("TestDDEServer");
1011     ok(server != 0, "Expected non-NULL server\n");
1012
1013     topic = GlobalAddAtomA("TestDDETopic");
1014     ok(topic != 0, "Expected non-NULL topic\n");
1015
1016     SendMessageA(HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)client_hwnd, MAKELONG(server, topic));
1017
1018     GlobalDeleteAtom(server);
1019     GlobalDeleteAtom(topic);
1020
1021     flush_events();
1022
1023     item = GlobalAddAtom("request");
1024     ok(item != 0, "Expected non-NULL item\n");
1025
1026     /* WM_DDE_REQUEST, bad clipboard format */
1027     lparam = PackDDElParam(WM_DDE_REQUEST, 0xdeadbeef, item);
1028     PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1029
1030     flush_events();
1031
1032     /* WM_DDE_REQUEST, no item */
1033     lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, 0);
1034     PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1035
1036     flush_events();
1037
1038     /* WM_DDE_REQUEST, no client hwnd */
1039     lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, item);
1040     PostMessageA(server_hwnd, WM_DDE_REQUEST, 0, lparam);
1041
1042     flush_events();
1043
1044     /* WM_DDE_REQUEST, correct params */
1045     lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, item);
1046     PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1047
1048     flush_events();
1049
1050     GlobalDeleteAtom(item);
1051     item = GlobalAddAtomA("poke");
1052     ok(item != 0, "Expected non-NULL item\n");
1053
1054     hglobal = create_poke();
1055
1056     /* WM_DDE_POKE, no ddepoke */
1057     lparam = PackDDElParam(WM_DDE_POKE, 0, item);
1058     PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1059
1060     flush_events();
1061
1062     /* WM_DDE_POKE, no item */
1063     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, 0);
1064     PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1065
1066     flush_events();
1067
1068     hglobal = create_poke();
1069
1070     /* WM_DDE_POKE, no client hwnd */
1071     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1072     PostMessageA(server_hwnd, WM_DDE_POKE, 0, lparam);
1073
1074     flush_events();
1075
1076     /* WM_DDE_POKE, all params correct */
1077     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1078     PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1079
1080     flush_events();
1081
1082     execute_hglobal = create_execute("[Command(Var)]");
1083
1084     /* WM_DDE_EXECUTE, no lparam */
1085     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, 0);
1086
1087     flush_events();
1088
1089     /* WM_DDE_EXECUTE, no hglobal */
1090     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, 0);
1091     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1092
1093     flush_events();
1094
1095     /* WM_DDE_EXECUTE, no client hwnd */
1096     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1097     PostMessageA(server_hwnd, WM_DDE_EXECUTE, 0, lparam);
1098
1099     flush_events();
1100
1101     /* WM_DDE_EXECUTE, all params correct */
1102     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1103     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1104
1105     flush_events();
1106
1107     GlobalFree(execute_hglobal);
1108     execute_hglobal = create_execute("[BadCommand(Var)]");
1109
1110     /* WM_DDE_EXECUTE that will get rejected */
1111     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1112     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1113
1114     flush_events();
1115
1116     destroy_dde_window(&client_hwnd, "dde_client");
1117 }
1118
1119 static LRESULT WINAPI hook_dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1120 {
1121     UINT_PTR lo, hi;
1122
1123     trace("hook_dde_client_wndproc: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1124
1125     switch (msg)
1126     {
1127     case WM_DDE_ACK:
1128         UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1129         trace("WM_DDE_ACK: status %04lx hglobal %p\n", lo, (HGLOBAL)hi);
1130         break;
1131
1132     default:
1133         break;
1134     }
1135     return CallWindowProcA(old_dde_client_wndproc, hwnd, msg, wparam, lparam);
1136 }
1137
1138 static LRESULT WINAPI dde_server_wndprocW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1139 {
1140     trace("dde_server_wndprocW: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1141
1142     switch (msg)
1143     {
1144     case WM_DDE_INITIATE:
1145     {
1146         ATOM aService = GlobalAddAtomW(TEST_DDE_SERVICE);
1147
1148         trace("server: got WM_DDE_INITIATE from %p with %08lx\n", (HWND)wparam, lparam);
1149
1150         if (LOWORD(lparam) == aService)
1151         {
1152             ok(!IsWindowUnicode((HWND)wparam), "client should be an ANSI window\n");
1153             old_dde_client_wndproc = (WNDPROC)SetWindowLongPtrA((HWND)wparam, GWLP_WNDPROC, (ULONG_PTR)hook_dde_client_wndproc);
1154             trace("server: sending WM_DDE_ACK to %p\n", (HWND)wparam);
1155             SendMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, MAKELPARAM(aService, 0));
1156         }
1157         else
1158             GlobalDeleteAtom(aService);
1159         return 0;
1160     }
1161
1162     case WM_DDE_EXECUTE:
1163     {
1164         DDEACK ack;
1165         WORD status;
1166         LPCSTR cmd;
1167         UINT_PTR lo, hi;
1168
1169         trace("server: got WM_DDE_EXECUTE from %p with %08lx\n", (HWND)wparam, lparam);
1170
1171         UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1172         trace("%08lx => lo %04lx hi %04lx\n", lparam, lo, hi);
1173
1174         ack.bAppReturnCode = 0;
1175         ack.reserved = 0;
1176         ack.fBusy = 0;
1177
1178         cmd = GlobalLock((HGLOBAL)hi);
1179         if (!cmd || (lstrcmpA(cmd, exec_cmdA) && lstrcmpW((LPCWSTR)cmd, exec_cmdW)))
1180         {
1181             trace("ignoring unknown WM_DDE_EXECUTE command\n");
1182             /* We have to send a negative acknowledge even if we don't
1183              * accept the command, otherwise Windows goes mad and next time
1184              * we send an acknowledge DDEML drops the connection.
1185              * Not sure how to call it: a bug or a feature.
1186              */
1187             ack.fAck = 0;
1188         }
1189         else
1190             ack.fAck = 1;
1191         GlobalUnlock((HGLOBAL)hi);
1192
1193         trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1194
1195         status = *((WORD *)&ack);
1196         lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, status, hi);
1197
1198         PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1199         return 0;
1200     }
1201
1202     case WM_DDE_TERMINATE:
1203     {
1204         DDEACK ack;
1205         WORD status;
1206
1207         trace("server: got WM_DDE_TERMINATE from %p with %08lx\n", (HWND)wparam, lparam);
1208
1209         ack.bAppReturnCode = 0;
1210         ack.reserved = 0;
1211         ack.fBusy = 0;
1212         ack.fAck = 1;
1213
1214         trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1215
1216         status = *((WORD *)&ack);
1217         lparam = PackDDElParam(WM_DDE_ACK, status, 0);
1218
1219         PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1220         return 0;
1221     }
1222
1223     default:
1224         break;
1225     }
1226
1227     return DefWindowProcW(hwnd, msg, wparam, lparam);
1228 }
1229
1230 static LRESULT WINAPI dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1231 {
1232     return DefWindowProcA(hwnd, msg, wparam, lparam);
1233 }
1234
1235 static BOOL create_dde_windows(HWND *client, HWND *server)
1236 {
1237     WNDCLASSA wcA;
1238     WNDCLASSW wcW;
1239     static const WCHAR server_class_name[] = {'d','d','e','_','s','e','r','v','e','r','_','w','i','n','d','o','w',0};
1240     static const char client_class_name[] = "dde_client_window";
1241
1242     memset(&wcW, 0, sizeof(wcW));
1243     wcW.lpfnWndProc = dde_server_wndprocW;
1244     wcW.lpszClassName = server_class_name;
1245     wcW.hInstance = GetModuleHandleA(0);
1246     if (!RegisterClassW(&wcW)) return FALSE;
1247
1248     memset(&wcA, 0, sizeof(wcA));
1249     wcA.lpfnWndProc = dde_client_wndproc;
1250     wcA.lpszClassName = client_class_name;
1251     wcA.hInstance = GetModuleHandleA(0);
1252     assert(RegisterClassA(&wcA));
1253
1254     *server = CreateWindowExW(0, server_class_name, NULL,
1255                               WS_POPUP,
1256                               100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1257                               GetDesktopWindow(), 0,
1258                               GetModuleHandleA(0), NULL);
1259     assert(*server);
1260
1261     *client = CreateWindowExA(0, client_class_name, NULL,
1262                               WS_POPUP,
1263                               100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1264                               GetDesktopWindow(), 0,
1265                               GetModuleHandleA(0), NULL);
1266     assert(*client);
1267
1268     trace("server hwnd %p, client hwnd %p\n", *server, *client);
1269
1270     ok(IsWindowUnicode(*server), "server has to be a unicode window\n");
1271     ok(!IsWindowUnicode(*client), "client has to be an ANSI window\n");
1272
1273     return TRUE;
1274 }
1275
1276 static HDDEDATA CALLBACK client_dde_callback(UINT uType, UINT uFmt, HCONV hconv,
1277                                      HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
1278                                      ULONG_PTR dwData1, ULONG_PTR dwData2)
1279 {
1280     static const char * const cmd_type[15] = {
1281         "XTYP_ERROR", "XTYP_ADVDATA", "XTYP_ADVREQ", "XTYP_ADVSTART",
1282         "XTYP_ADVSTOP", "XTYP_EXECUTE", "XTYP_CONNECT", "XTYP_CONNECT_CONFIRM",
1283         "XTYP_XACT_COMPLETE", "XTYP_POKE", "XTYP_REGISTER", "XTYP_REQUEST",
1284         "XTYP_DISCONNECT", "XTYP_UNREGISTER", "XTYP_WILDCONNECT" };
1285     UINT type;
1286     const char *cmd_name;
1287
1288     type = (uType & XTYP_MASK) >> XTYP_SHIFT;
1289     cmd_name = (type <= 14) ? cmd_type[type] : "unknown";
1290
1291     trace("client_dde_callback: %04x (%s) %d %p %p %p %p %08lx %08lx\n",
1292           uType, cmd_name, uFmt, hconv, hsz1, hsz2, hdata, dwData1, dwData2);
1293     return 0;
1294 }
1295
1296 static void test_dde_aw_transaction(void)
1297 {
1298     HSZ hsz_server;
1299     DWORD dde_inst, ret, err;
1300     HCONV hconv;
1301     HWND hwnd_client, hwnd_server;
1302     CONVINFO info;
1303     HDDEDATA hdata;
1304     static char test_cmd[] = "test dde command";
1305
1306     /* server: unicode, client: ansi */
1307     if (!create_dde_windows(&hwnd_client, &hwnd_server)) return;
1308
1309     dde_inst = 0;
1310     ret = DdeInitializeA(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
1311     ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04x (%x)\n",
1312        ret, DdeGetLastError(dde_inst));
1313
1314     hsz_server = DdeCreateStringHandleW(dde_inst, TEST_DDE_SERVICE, CP_WINUNICODE);
1315
1316     hconv = DdeConnect(dde_inst, hsz_server, 0, NULL);
1317     ok(hconv != 0, "DdeConnect error %x\n", DdeGetLastError(dde_inst));
1318     err = DdeGetLastError(dde_inst);
1319     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1320
1321     info.cb = sizeof(info);
1322     ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1323     ok(ret, "wrong info size %d, DdeQueryConvInfo error %x\n", ret, DdeGetLastError(dde_inst));
1324     /* should be CP_WINANSI since we used DdeInitializeA */
1325     ok(info.ConvCtxt.iCodePage == CP_WINANSI, "wrong iCodePage %d\n", info.ConvCtxt.iCodePage);
1326     ok(!info.hConvPartner, "unexpected info.hConvPartner: %p\n", info.hConvPartner);
1327 todo_wine {
1328     ok((info.wStatus & DDE_FACK), "unexpected info.wStatus: %04x\n", info.wStatus);
1329 }
1330     ok((info.wStatus & (ST_CONNECTED | ST_CLIENT)) == (ST_CONNECTED | ST_CLIENT), "unexpected info.wStatus: %04x\n", info.wStatus);
1331     ok(info.wConvst == XST_CONNECTED, "unexpected info.wConvst: %04x\n", info.wConvst);
1332     ok(info.wType == 0, "unexpected info.wType: %04x\n", info.wType);
1333
1334     trace("hwnd %p, hwndPartner %p\n", info.hwnd, info.hwndPartner);
1335
1336     trace("sending test client transaction command\n");
1337     ret = 0xdeadbeef;
1338     hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1, hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
1339     ok(!hdata, "DdeClientTransaction succeeded\n");
1340     ok(ret == DDE_FNOTPROCESSED, "wrong status code %04x\n", ret);
1341     err = DdeGetLastError(dde_inst);
1342     ok(err == DMLERR_NOTPROCESSED, "wrong dde error %x\n", err);
1343
1344     trace("sending ANSI client transaction command\n");
1345     ret = 0xdeadbeef;
1346     hdata = DdeClientTransaction((LPBYTE)exec_cmdA, lstrlenA(exec_cmdA) + 1, hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1347     ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1348     ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1349
1350     err = DdeGetLastError(dde_inst);
1351     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1352
1353     trace("sending unicode client transaction command\n");
1354     ret = 0xdeadbeef;
1355     hdata = DdeClientTransaction((LPBYTE)exec_cmdW, (lstrlenW(exec_cmdW) + 1) * sizeof(WCHAR), hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1356     ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1357     ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1358     err = DdeGetLastError(dde_inst);
1359     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1360
1361     ok(DdeDisconnect(hconv), "DdeDisconnect error %x\n", DdeGetLastError(dde_inst));
1362
1363     info.cb = sizeof(info);
1364     ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1365     ok(!ret, "DdeQueryConvInfo should fail\n");
1366     err = DdeGetLastError(dde_inst);
1367 todo_wine {
1368     ok(err == DMLERR_INVALIDPARAMETER, "wrong dde error %x\n", err);
1369 }
1370
1371     ok(DdeFreeStringHandle(dde_inst, hsz_server), "DdeFreeStringHandle error %x\n", DdeGetLastError(dde_inst));
1372
1373     /* This call hangs on win2k SP4 and XP SP1.
1374     DdeUninitialize(dde_inst);*/
1375
1376     DestroyWindow(hwnd_client);
1377     DestroyWindow(hwnd_server);
1378 }
1379
1380 static void test_initialisation(void)
1381 {
1382     UINT ret;
1383     DWORD res;
1384     HDDEDATA hdata;
1385     HSZ server, topic, item;
1386     DWORD client_pid;
1387     HCONV conversation;
1388
1389     /* Initialise without a valid server window. */
1390     client_pid = 0;
1391     ret = DdeInitializeA(&client_pid, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1392     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
1393
1394
1395     server = DdeCreateStringHandleA(client_pid, "TestDDEService", CP_WINANSI);
1396     topic = DdeCreateStringHandleA(client_pid, "TestDDETopic", CP_WINANSI);
1397
1398     DdeGetLastError(client_pid);
1399
1400     /* There is no server window so no conversation can be extracted */
1401     conversation = DdeConnect(client_pid, server, topic, NULL);
1402     ok(conversation == NULL, "Expected NULL conversation, %p\n", conversation);
1403     ret = DdeGetLastError(client_pid);
1404     ok(ret == DMLERR_NO_CONV_ESTABLISHED, "Expected DMLERR_NO_CONV_ESTABLISHED, got %d\n", ret);
1405
1406     DdeFreeStringHandle(client_pid, server);
1407
1408     item = DdeCreateStringHandleA(client_pid, "request", CP_WINANSI);
1409
1410     /* There is no converstation so an invalild parameter results */
1411     res = 0xdeadbeef;
1412     DdeGetLastError(client_pid);
1413     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
1414     ret = DdeGetLastError(client_pid);
1415 todo_wine
1416     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
1417     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %08x\n", res);
1418
1419     DdeFreeStringHandle(client_pid, server);
1420     ret = DdeDisconnect(conversation);
1421     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1422
1423     ret = DdeUninitialize(client_pid);
1424     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1425 }
1426
1427 static void test_DdeCreateStringHandleW(DWORD dde_inst, int codepage)
1428 {
1429     static const WCHAR dde_string[] = {'D','D','E',' ','S','t','r','i','n','g',0};
1430     HSZ str_handle;
1431     WCHAR bufW[256];
1432     char buf[256];
1433     ATOM atom;
1434     int ret;
1435
1436     str_handle = DdeCreateStringHandleW(dde_inst, dde_string, codepage);
1437     ok(str_handle != 0, "DdeCreateStringHandleW failed with error %08x\n",
1438        DdeGetLastError(dde_inst));
1439
1440     ret = DdeQueryStringW(dde_inst, str_handle, NULL, 0, codepage);
1441     if (codepage == CP_WINANSI)
1442         ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1443     else
1444         ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1445
1446     ret = DdeQueryStringW(dde_inst, str_handle, bufW, 256, codepage);
1447     if (codepage == CP_WINANSI)
1448     {
1449         ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1450         ok(!lstrcmpA("D", (LPCSTR)bufW), "DdeQueryStringW returned wrong string\n");
1451     }
1452     else
1453     {
1454         ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1455         ok(!lstrcmpW(dde_string, bufW), "DdeQueryStringW returned wrong string\n");
1456     }
1457
1458     ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINANSI);
1459     if (codepage == CP_WINANSI)
1460     {
1461         ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1462         ok(!lstrcmpA("D", buf), "DdeQueryStringW returned wrong string\n");
1463     }
1464     else
1465     {
1466         ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1467         ok(!lstrcmpA("DDE String", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1468     }
1469
1470     ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINUNICODE);
1471     if (codepage == CP_WINANSI)
1472     {
1473         ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1474         ok(!lstrcmpA("D", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1475     }
1476     else
1477     {
1478         ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1479         ok(!lstrcmpW(dde_string, (LPCWSTR)buf), "DdeQueryStringW returned wrong string\n");
1480     }
1481
1482     if (codepage == CP_WINANSI)
1483     {
1484         atom = FindAtomA((LPSTR)dde_string);
1485         ok(atom != 0, "Expected a valid atom\n");
1486
1487         SetLastError(0xdeadbeef);
1488         atom = GlobalFindAtomA((LPSTR)dde_string);
1489         ok(atom == 0, "Expected 0, got %d\n", atom);
1490         ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1491            "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1492     }
1493     else
1494     {
1495         atom = FindAtomW(dde_string);
1496         ok(atom != 0, "Expected a valid atom\n");
1497
1498         SetLastError(0xdeadbeef);
1499         atom = GlobalFindAtomW(dde_string);
1500         ok(atom == 0, "Expected 0, got %d\n", atom);
1501         ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1502            "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1503     }
1504
1505     ok(DdeFreeStringHandle(dde_inst, str_handle), "DdeFreeStringHandle failed\n");
1506 }
1507
1508 static void test_DdeCreateDataHandle(void)
1509 {
1510     HDDEDATA hdata;
1511     DWORD dde_inst;
1512     DWORD size;
1513     UINT res, err;
1514     BOOL ret;
1515     HSZ item;
1516     LPBYTE ptr;
1517
1518     dde_inst = 0;
1519     res = DdeInitializeA(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1520     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1521
1522     item = DdeCreateStringHandleA(dde_inst, "item", CP_WINANSI);
1523     ok(item != NULL, "Expected non-NULL hsz\n");
1524
1525     /* invalid instance id */
1526     DdeGetLastError(dde_inst);
1527     hdata = DdeCreateDataHandle(0xdeadbeef, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1528     err = DdeGetLastError(dde_inst);
1529     todo_wine
1530     {
1531         ok(hdata == NULL, "Expected NULL, got %p\n", hdata);
1532         ok(err == DMLERR_INVALIDPARAMETER,
1533            "Expected DMLERR_INVALIDPARAMETER, got %d\n", err);
1534     }
1535
1536     /* 0 instance id */
1537     DdeGetLastError(dde_inst);
1538     hdata = DdeCreateDataHandle(0, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1539     err = DdeGetLastError(dde_inst);
1540     todo_wine
1541     {
1542         ok(hdata == NULL, "Expected NULL, got %p\n", hdata);
1543         ok(err == DMLERR_INVALIDPARAMETER,
1544            "Expected DMLERR_INVALIDPARAMETER, got %d\n", err);
1545     }
1546
1547     /* NULL pSrc */
1548     DdeGetLastError(dde_inst);
1549     hdata = DdeCreateDataHandle(dde_inst, NULL, MAX_PATH, 0, item, CF_TEXT, 0);
1550     err = DdeGetLastError(dde_inst);
1551     ok(hdata != NULL, "Expected non-NULL hdata\n");
1552     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1553
1554     ptr = GlobalLock(hdata);
1555     todo_wine
1556     {
1557         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1558     }
1559
1560     ptr = DdeAccessData(hdata, &size);
1561     ok(ptr != NULL, "Expected non-NULL ptr\n");
1562     ok(size == 260, "Expected 260, got %d\n", size);
1563
1564     ret = DdeUnaccessData(hdata);
1565     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1566
1567     ret = DdeFreeDataHandle(hdata);
1568     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1569
1570     /* cb is zero */
1571     DdeGetLastError(dde_inst);
1572     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", 0, 0, item, CF_TEXT, 0);
1573     err = DdeGetLastError(dde_inst);
1574     ok(hdata != NULL, "Expected non-NULL hdata\n");
1575     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1576
1577     ptr = GlobalLock(hdata);
1578     todo_wine
1579     {
1580         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1581     }
1582
1583     ptr = DdeAccessData(hdata, &size);
1584     ok(ptr != NULL, "Expected non-NULL ptr\n");
1585     ok(size == 0, "Expected 0, got %d\n", size);
1586
1587     ret = DdeUnaccessData(hdata);
1588     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1589
1590     ret = DdeFreeDataHandle(hdata);
1591     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1592
1593     /* cbOff is non-zero */
1594     DdeGetLastError(dde_inst);
1595     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 2, item, CF_TEXT, 0);
1596     err = DdeGetLastError(dde_inst);
1597     ok(hdata != NULL, "Expected non-NULL hdata\n");
1598     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1599
1600     ptr = GlobalLock(hdata);
1601     todo_wine
1602     {
1603         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1604     }
1605
1606     ptr = DdeAccessData(hdata, &size);
1607     ok(ptr != NULL, "Expected non-NULL ptr\n");
1608     ok(size == 262, "Expected 262, got %d\n", size);
1609     todo_wine
1610     {
1611         ok(lstrlenA((LPSTR)ptr) == 0, "Expected 0, got %d\n", lstrlenA((LPSTR)ptr));
1612     }
1613
1614     ret = DdeUnaccessData(hdata);
1615     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1616
1617     ret = DdeFreeDataHandle(hdata);
1618     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1619
1620     /* NULL item */
1621     DdeGetLastError(dde_inst);
1622     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, 0, CF_TEXT, 0);
1623     err = DdeGetLastError(dde_inst);
1624     ok(hdata != NULL, "Expected non-NULL hdata\n");
1625     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1626
1627     ptr = GlobalLock(hdata);
1628     todo_wine
1629     {
1630         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1631     }
1632
1633     ptr = DdeAccessData(hdata, &size);
1634     ok(ptr != NULL, "Expected non-NULL ptr\n");
1635     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1636     ok(size == 260, "Expected 260, got %d\n", size);
1637
1638     ret = DdeUnaccessData(hdata);
1639     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1640
1641     ret = DdeFreeDataHandle(hdata);
1642     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1643
1644     /* NULL item */
1645     DdeGetLastError(dde_inst);
1646     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, (HSZ)0xdeadbeef, CF_TEXT, 0);
1647     err = DdeGetLastError(dde_inst);
1648     ok(hdata != NULL, "Expected non-NULL hdata\n");
1649     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1650
1651     ptr = GlobalLock(hdata);
1652     todo_wine
1653     {
1654         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1655     }
1656
1657     ptr = DdeAccessData(hdata, &size);
1658     ok(ptr != NULL, "Expected non-NULL ptr\n");
1659     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1660     ok(size == 260, "Expected 260, got %d\n", size);
1661
1662     ret = DdeUnaccessData(hdata);
1663     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1664
1665     ret = DdeFreeDataHandle(hdata);
1666     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1667
1668     /* invalid clipboard format */
1669     DdeGetLastError(dde_inst);
1670     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, item, 0xdeadbeef, 0);
1671     err = DdeGetLastError(dde_inst);
1672     ok(hdata != NULL, "Expected non-NULL hdata\n");
1673     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1674
1675     ptr = GlobalLock(hdata);
1676     todo_wine
1677     {
1678         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1679     }
1680
1681     ptr = DdeAccessData(hdata, &size);
1682     ok(ptr != NULL, "Expected non-NULL ptr\n");
1683     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1684     ok(size == 260, "Expected 260, got %d\n", size);
1685
1686     ret = DdeUnaccessData(hdata);
1687     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1688
1689     ret = DdeFreeDataHandle(hdata);
1690     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1691
1692     ret = DdeUninitialize(dde_inst);
1693     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1694 }
1695
1696 static void test_DdeCreateStringHandle(void)
1697 {
1698     DWORD dde_inst, ret;
1699
1700     dde_inst = 0xdeadbeef;
1701     SetLastError(0xdeadbeef);
1702     ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1703     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1704     {
1705         skip("DdeInitialize is unimplemented\n");
1706         return;
1707     }
1708
1709     ok(ret == DMLERR_INVALIDPARAMETER, "DdeInitializeW should fail, but got %04x instead\n", ret);
1710     ok(DdeGetLastError(dde_inst) == DMLERR_INVALIDPARAMETER, "expected DMLERR_INVALIDPARAMETER\n");
1711
1712     dde_inst = 0;
1713     ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1714     ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04x (%08x)\n",
1715        ret, DdeGetLastError(dde_inst));
1716
1717     test_DdeCreateStringHandleW(dde_inst, 0);
1718     test_DdeCreateStringHandleW(dde_inst, CP_WINUNICODE);
1719     test_DdeCreateStringHandleW(dde_inst, CP_WINANSI);
1720
1721     ok(DdeUninitialize(dde_inst), "DdeUninitialize failed\n");
1722 }
1723
1724 static void test_FreeDDElParam(void)
1725 {
1726     HGLOBAL val, hglobal;
1727     BOOL ret;
1728
1729     ret = FreeDDElParam(WM_DDE_INITIATE, 0);
1730     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1731
1732     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1733     ret = FreeDDElParam(WM_DDE_INITIATE, (LPARAM)hglobal);
1734     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1735     val = GlobalFree(hglobal);
1736     ok(val == NULL, "Expected NULL, got %p\n", val);
1737
1738     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1739     ret = FreeDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal);
1740     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1741     val = GlobalFree(hglobal);
1742     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1743     ok(GetLastError() == ERROR_INVALID_HANDLE,
1744        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1745
1746     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1747     ret = FreeDDElParam(WM_DDE_UNADVISE, (LPARAM)hglobal);
1748     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1749     val = GlobalFree(hglobal);
1750     ok(val == NULL, "Expected NULL, got %p\n", val);
1751
1752     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1753     ret = FreeDDElParam(WM_DDE_ACK, (LPARAM)hglobal);
1754     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1755     val = GlobalFree(hglobal);
1756     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1757     ok(GetLastError() == ERROR_INVALID_HANDLE,
1758        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1759
1760     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1761     ret = FreeDDElParam(WM_DDE_DATA, (LPARAM)hglobal);
1762     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1763     val = GlobalFree(hglobal);
1764     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1765     ok(GetLastError() == ERROR_INVALID_HANDLE,
1766        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1767
1768     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1769     ret = FreeDDElParam(WM_DDE_REQUEST, (LPARAM)hglobal);
1770     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1771     val = GlobalFree(hglobal);
1772     ok(val == NULL, "Expected NULL, got %p\n", val);
1773
1774     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1775     ret = FreeDDElParam(WM_DDE_POKE, (LPARAM)hglobal);
1776     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1777     val = GlobalFree(hglobal);
1778     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1779     ok(GetLastError() == ERROR_INVALID_HANDLE,
1780        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1781
1782     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1783     ret = FreeDDElParam(WM_DDE_EXECUTE, (LPARAM)hglobal);
1784     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1785     val = GlobalFree(hglobal);
1786     ok(val == NULL, "Expected NULL, got %p\n", val);
1787 }
1788
1789 static void test_PackDDElParam(void)
1790 {
1791     UINT_PTR lo, hi, *ptr;
1792     HGLOBAL hglobal;
1793     LPARAM lparam;
1794     BOOL ret;
1795
1796     lparam = PackDDElParam(WM_DDE_INITIATE, 0xcafe, 0xbeef);
1797     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1798     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1799        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1800     ok(GetLastError() == ERROR_INVALID_HANDLE,
1801        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1802
1803     lo = hi = 0;
1804     ret = UnpackDDElParam(WM_DDE_INITIATE, lparam, &lo, &hi);
1805     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1806     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1807     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1808
1809     ret = FreeDDElParam(WM_DDE_INITIATE, lparam);
1810     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1811
1812     lparam = PackDDElParam(WM_DDE_TERMINATE, 0xcafe, 0xbeef);
1813     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1814     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1815        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1816     ok(GetLastError() == ERROR_INVALID_HANDLE,
1817        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1818
1819     lo = hi = 0;
1820     ret = UnpackDDElParam(WM_DDE_TERMINATE, lparam, &lo, &hi);
1821     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1822     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1823     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1824
1825     ret = FreeDDElParam(WM_DDE_TERMINATE, lparam);
1826     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1827
1828     lparam = PackDDElParam(WM_DDE_ADVISE, 0xcafe, 0xbeef);
1829     ptr = GlobalLock((HGLOBAL)lparam);
1830     ok(ptr != NULL, "Expected non-NULL ptr\n");
1831     ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1832     ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1833
1834     ret = GlobalUnlock((HGLOBAL)lparam);
1835     ok(ret == 1, "Expected 1, got %d\n", ret);
1836
1837     lo = hi = 0;
1838     ret = UnpackDDElParam(WM_DDE_ADVISE, lparam, &lo, &hi);
1839     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1840     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1841     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1842
1843     ret = FreeDDElParam(WM_DDE_ADVISE, lparam);
1844     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1845
1846     hglobal = GlobalFree((HGLOBAL)lparam);
1847     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1848     ok(GetLastError() == ERROR_INVALID_HANDLE,
1849        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1850
1851     lparam = PackDDElParam(WM_DDE_UNADVISE, 0xcafe, 0xbeef);
1852     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1853     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1854        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1855     ok(GetLastError() == ERROR_INVALID_HANDLE,
1856        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1857
1858     lo = hi = 0;
1859     ret = UnpackDDElParam(WM_DDE_UNADVISE, lparam, &lo, &hi);
1860     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1861     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1862     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1863
1864     ret = FreeDDElParam(WM_DDE_UNADVISE, lparam);
1865     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1866
1867     lparam = PackDDElParam(WM_DDE_ACK, 0xcafe, 0xbeef);
1868     ptr = GlobalLock((HGLOBAL)lparam);
1869     ok(ptr != NULL, "Expected non-NULL ptr\n");
1870     ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1871     ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1872
1873     ret = GlobalUnlock((HGLOBAL)lparam);
1874     ok(ret == 1, "Expected 1, got %d\n", ret);
1875
1876     lo = hi = 0;
1877     ret = UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1878     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1879     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1880     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1881
1882     ret = FreeDDElParam(WM_DDE_ACK, lparam);
1883     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1884
1885     hglobal = GlobalFree((HGLOBAL)lparam);
1886     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1887     ok(GetLastError() == ERROR_INVALID_HANDLE,
1888        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1889
1890     lparam = PackDDElParam(WM_DDE_DATA, 0xcafe, 0xbeef);
1891     ptr = GlobalLock((HGLOBAL)lparam);
1892     ok(ptr != NULL, "Expected non-NULL ptr\n");
1893     ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1894     ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1895
1896     ret = GlobalUnlock((HGLOBAL)lparam);
1897     ok(ret == 1, "Expected 1, got %d\n", ret);
1898
1899     lo = hi = 0;
1900     ret = UnpackDDElParam(WM_DDE_DATA, lparam, &lo, &hi);
1901     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1902     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1903     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1904
1905     ret = FreeDDElParam(WM_DDE_DATA, lparam);
1906     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1907
1908     hglobal = GlobalFree((HGLOBAL)lparam);
1909     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1910     ok(GetLastError() == ERROR_INVALID_HANDLE,
1911        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1912
1913     lparam = PackDDElParam(WM_DDE_REQUEST, 0xcafe, 0xbeef);
1914     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1915     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1916        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1917     ok(GetLastError() == ERROR_INVALID_HANDLE,
1918        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1919
1920     lo = hi = 0;
1921     ret = UnpackDDElParam(WM_DDE_REQUEST, lparam, &lo, &hi);
1922     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1923     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1924     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1925
1926     ret = FreeDDElParam(WM_DDE_REQUEST, lparam);
1927     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1928
1929     lparam = PackDDElParam(WM_DDE_POKE, 0xcafe, 0xbeef);
1930     ptr = GlobalLock((HGLOBAL)lparam);
1931     ok(ptr != NULL, "Expected non-NULL ptr\n");
1932     ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1933     ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1934
1935     ret = GlobalUnlock((HGLOBAL)lparam);
1936     ok(ret == 1, "Expected 1, got %d\n", ret);
1937
1938     lo = hi = 0;
1939     ret = UnpackDDElParam(WM_DDE_POKE, lparam, &lo, &hi);
1940     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1941     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1942     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1943
1944     ret = FreeDDElParam(WM_DDE_POKE, lparam);
1945     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1946
1947     hglobal = GlobalFree((HGLOBAL)lparam);
1948     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1949     ok(GetLastError() == ERROR_INVALID_HANDLE,
1950        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1951
1952     lparam = PackDDElParam(WM_DDE_EXECUTE, 0xcafe, 0xbeef);
1953     ok(lparam == 0xbeef, "Expected 0xbeef, got %08lx\n", lparam);
1954     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1955        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1956     ok(GetLastError() == ERROR_INVALID_HANDLE,
1957        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1958
1959     lo = hi = 0;
1960     ret = UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1961     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1962     ok(lo == 0, "Expected 0, got %08lx\n", lo);
1963     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1964
1965     ret = FreeDDElParam(WM_DDE_EXECUTE, lparam);
1966     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1967 }
1968
1969 static void test_UnpackDDElParam(void)
1970 {
1971     UINT_PTR lo, hi, *ptr;
1972     HGLOBAL hglobal;
1973     BOOL ret;
1974
1975     /* NULL lParam */
1976     lo = 0xdead;
1977     hi = 0xbeef;
1978     ret = UnpackDDElParam(WM_DDE_INITIATE, 0, &lo, &hi);
1979     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1980     ok(lo == 0, "Expected 0, got %08lx\n", lo);
1981     ok(hi == 0, "Expected 0, got %08lx\n", hi);
1982
1983     /* NULL lo */
1984     lo = 0xdead;
1985     hi = 0xbeef;
1986     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, NULL, &hi);
1987     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1988     ok(lo == 0xdead, "Expected 0xdead, got %08lx\n", lo);
1989     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
1990
1991     /* NULL hi */
1992     lo = 0xdead;
1993     hi = 0xbeef;
1994     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, NULL);
1995     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1996     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
1997     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1998
1999     lo = 0xdead;
2000     hi = 0xbeef;
2001     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, &hi);
2002     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2003     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2004     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2005
2006     lo = 0xdead;
2007     hi = 0xbeef;
2008     ret = UnpackDDElParam(WM_DDE_TERMINATE, 0xcafebabe, &lo, &hi);
2009     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2010     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2011     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2012
2013     lo = 0xdead;
2014     hi = 0xbeef;
2015     ret = UnpackDDElParam(WM_DDE_ADVISE, 0, &lo, &hi);
2016     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2017     ok(lo == 0 ||
2018        broken(lo == 0xdead), /* win2k */
2019        "Expected 0, got %08lx\n", lo);
2020     ok(hi == 0 ||
2021        broken(hi == 0xbeef), /* win2k */
2022        "Expected 0, got %08lx\n", hi);
2023
2024     lo = 0xdead;
2025     hi = 0xbeef;
2026     ret = UnpackDDElParam(WM_DDE_ADVISE, 0xcafebabe, &lo, &hi);
2027     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2028     ok(lo == 0 ||
2029        broken(lo == 0xdead), /* win2k */
2030        "Expected 0, got %08lx\n", lo);
2031     ok(hi == 0 ||
2032        broken(hi == 0xbeef), /* win2k */
2033        "Expected 0, got %08lx\n", hi);
2034
2035     hglobal = GlobalAlloc(GMEM_DDESHARE, 2);
2036     ptr = GlobalLock(hglobal);
2037     ptr[0] = 0xcafebabe;
2038     ptr[1] = 0xdeadbeef;
2039     GlobalUnlock(hglobal);
2040
2041     lo = 0xdead;
2042     hi = 0xbeef;
2043     ret = UnpackDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal, &lo, &hi);
2044     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2045     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2046     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2047
2048     lo = 0xdead;
2049     hi = 0xbeef;
2050     ret = UnpackDDElParam(WM_DDE_UNADVISE, 0xcafebabe, &lo, &hi);
2051     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2052     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2053     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2054
2055     lo = 0xdead;
2056     hi = 0xbeef;
2057     ret = UnpackDDElParam(WM_DDE_ACK, 0xcafebabe, &lo, &hi);
2058     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2059     ok(lo == 0 ||
2060        broken(lo == 0xdead), /* win2k */
2061        "Expected 0, got %08lx\n", lo);
2062     ok(hi == 0 ||
2063        broken(hi == 0xbeef), /* win2k */
2064        "Expected 0, got %08lx\n", hi);
2065
2066     lo = 0xdead;
2067     hi = 0xbeef;
2068     ret = UnpackDDElParam(WM_DDE_ACK, (LPARAM)hglobal, &lo, &hi);
2069     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2070     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2071     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2072
2073     lo = 0xdead;
2074     hi = 0xbeef;
2075     ret = UnpackDDElParam(WM_DDE_DATA, 0xcafebabe, &lo, &hi);
2076     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2077     ok(lo == 0 ||
2078        broken(lo == 0xdead), /* win2k */
2079        "Expected 0, got %08lx\n", lo);
2080     ok(hi == 0 ||
2081        broken(hi == 0xbeef), /* win2k */
2082        "Expected 0, got %08lx\n", hi);
2083
2084     lo = 0xdead;
2085     hi = 0xbeef;
2086     ret = UnpackDDElParam(WM_DDE_DATA, (LPARAM)hglobal, &lo, &hi);
2087     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2088     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2089     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2090
2091     lo = 0xdead;
2092     hi = 0xbeef;
2093     ret = UnpackDDElParam(WM_DDE_REQUEST, 0xcafebabe, &lo, &hi);
2094     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2095     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2096     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2097
2098     lo = 0xdead;
2099     hi = 0xbeef;
2100     ret = UnpackDDElParam(WM_DDE_POKE, 0xcafebabe, &lo, &hi);
2101     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2102     ok(lo == 0 ||
2103        broken(lo == 0xdead), /* win2k */
2104        "Expected 0, got %08lx\n", lo);
2105     ok(hi == 0 ||
2106        broken(hi == 0xbeef), /* win2k */
2107        "Expected 0, got %08lx\n", hi);
2108
2109     lo = 0xdead;
2110     hi = 0xbeef;
2111     ret = UnpackDDElParam(WM_DDE_POKE, (LPARAM)hglobal, &lo, &hi);
2112     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2113     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2114     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2115
2116     lo = 0xdead;
2117     hi = 0xbeef;
2118     ret = UnpackDDElParam(WM_DDE_EXECUTE, 0xcafebabe, &lo, &hi);
2119     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2120     ok(lo == 0, "Expected 0, got %08lx\n", lo);
2121     ok(hi == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", hi);
2122 }
2123
2124 static HDDEDATA CALLBACK server_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2125                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2126                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
2127 {
2128     DWORD size, rsize;
2129     char str[MAX_PATH];
2130     static int msg_index = 0;
2131     static HCONV conversation = 0;
2132     static char test_cmd[] = "test dde command";
2133     static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2134     static char test_service [] = "TestDDEService";
2135     static char test_topic [] = "TestDDETopic";
2136
2137     msg_index++;
2138
2139     switch (uType)
2140     {
2141     case XTYP_REGISTER:
2142     {
2143         ok(msg_index == 1 || msg_index == 7 || msg_index == 13 || msg_index == 19,
2144                              "Expected 1, 7, 13 or 19, got %d\n", msg_index);
2145         return (HDDEDATA)TRUE;
2146     }
2147
2148     case XTYP_CONNECT:
2149     {
2150         ok(msg_index == 2 || msg_index == 8 || msg_index == 14 || msg_index == 20,
2151                              "Expected 2, 8, 14 or 20, got %d\n", msg_index);
2152         ok(uFmt == 0, "Expected 0, got %d, msg_index=%d\n", uFmt, msg_index);
2153         ok(hconv == 0, "Expected 0, got %p, msg_index=%d\n", hconv, msg_index);
2154         ok(hdata == 0, "Expected 0, got %p, msg_index=%d\n", hdata, msg_index);
2155         ok(dwData1 != 0, "Expected not 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2156         ok(dwData2 == FALSE, "Expected FALSE, got %08lx, msg_index=%d\n", dwData2, msg_index);
2157
2158         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2159         ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2160                              test_topic, str, msg_index);
2161         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2162
2163         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
2164         ok(!lstrcmpA(str, test_service), "Expected %s, got %s, msg_index=%d\n",
2165                              test_service, str, msg_index);
2166         ok(size == 14, "Expected 14, got %d, msg_index=%d\n", size, msg_index);
2167
2168         return (HDDEDATA) TRUE;
2169     }
2170     case XTYP_CONNECT_CONFIRM:
2171     {
2172         ok(msg_index == 3 || msg_index == 9  ||  msg_index == 15 ||  msg_index == 21,
2173                              "Expected 3, 9, 15 or 21 got %d\n", msg_index);
2174         conversation = hconv;
2175         return (HDDEDATA) TRUE;
2176     }
2177     case XTYP_EXECUTE:
2178     {
2179         BYTE *buffer = NULL;
2180
2181         ok(msg_index == 4 || msg_index == 5 || msg_index == 10 || msg_index == 11 ||
2182            msg_index == 16 || msg_index == 17 || msg_index == 22 || msg_index == 23,
2183            "Expected 4, 5, 10, 11, 16, 17, 22 or 23, got %d\n", msg_index);
2184         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
2185         ok(hconv == conversation, "Expected conversation handle, got %p, msg_index=%d\n",
2186                              hconv, msg_index);
2187         ok(dwData1 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2188         ok(dwData2 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData2, msg_index);
2189         ok(hsz2 == 0, "Expected 0, got %p, msg_index=%d\n", hsz2, msg_index);
2190         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2191         ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2192                              test_topic, str, msg_index);
2193         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2194         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2195
2196         size = DdeGetData(hdata, NULL, 0, 0);
2197         if (msg_index == 10 || msg_index ==11 || msg_index == 16 || msg_index ==17)
2198           if (msg_index == 10 || msg_index == 16)
2199           todo_wine
2200             ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2201                              size, msg_index);
2202           else
2203             ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2204                              size, msg_index);
2205         else
2206         if (msg_index ==22)
2207         todo_wine
2208             ok(size == 9, "Expected that size should be 9 not %d, msg_index=%d\n",
2209                              size, msg_index);
2210         else
2211           if (msg_index == 5)
2212           todo_wine
2213             ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2214                              size, msg_index);
2215           else
2216             ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2217                              size, msg_index);
2218         ok((buffer = HeapAlloc(GetProcessHeap(), 0, size)) != NULL, "should not be null\n");
2219         rsize = DdeGetData(hdata, buffer, size, 0);
2220         if (msg_index == 10 || msg_index == 11 || msg_index == 16 || msg_index ==17)
2221         {
2222             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2223                              size, rsize, msg_index);
2224           if (msg_index == 10 || msg_index == 16)
2225           todo_wine {
2226             ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2227                              "Expected \"Test dde command\", msg_index=%d\n",
2228                              msg_index);
2229             ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2230           } else
2231           {
2232             ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2233                              "Expected \"Test dde command\", msg_index=%d\n",
2234                              msg_index);
2235             ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2236           }
2237         }else if (msg_index == 22)
2238         {
2239             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2240                              size, rsize, msg_index);
2241         } else
2242         {
2243             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2244                              size, rsize, msg_index);
2245           if (msg_index == 5)
2246           todo_wine {
2247             ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2248                              test_cmd, buffer, msg_index);
2249             ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2250           }
2251           else
2252           {
2253             ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2254                              test_cmd, buffer, msg_index);
2255             ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2256           }
2257
2258         }
2259
2260         return (HDDEDATA) DDE_FACK;
2261     }
2262     case XTYP_DISCONNECT:
2263         return (HDDEDATA) TRUE;
2264
2265     default:
2266         ok(FALSE, "Unhandled msg: %08x, msg_index=%d\n", uType, msg_index);
2267     }
2268
2269     return NULL;
2270 }
2271
2272 static HDDEDATA CALLBACK client_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2273                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2274                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
2275 {
2276     switch (uType)
2277     {
2278     case XTYP_DISCONNECT:
2279         return (HDDEDATA) TRUE;
2280
2281     default:
2282         ok(FALSE, "Unhandled msg: %08x\n", uType);
2283     }
2284
2285     return NULL;
2286 }
2287
2288 static void test_end_to_end_client(BOOL type_a)
2289 {
2290     DWORD  ret, err;
2291     DWORD client_pid = 0;
2292     HSZ server, topic;
2293     HCONV hconv;
2294     HDDEDATA hdata;
2295     static char test_cmd[] = "test dde command";
2296     static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2297     static char test_service[] = "TestDDEService";
2298     static WCHAR test_service_w[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
2299     static char test_topic[] = "TestDDETopic";
2300     static WCHAR test_topic_w[] = {'T','e','s','t','D','D','E','T','o','p','i','c',0};
2301
2302     trace("Start end to end client %d\n", type_a);
2303
2304     if (type_a)
2305         ret = DdeInitializeA(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2306     else
2307         ret = DdeInitializeW(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2308     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2309
2310     if (type_a)
2311     {
2312         server = DdeCreateStringHandleA(client_pid, test_service, CP_WINANSI);
2313         topic = DdeCreateStringHandleA(client_pid, test_topic, CP_WINANSI);
2314     }
2315     else {
2316         server = DdeCreateStringHandleW(client_pid, test_service_w, CP_WINUNICODE);
2317         topic = DdeCreateStringHandleW(client_pid, test_topic_w, CP_WINUNICODE);
2318     }
2319
2320     DdeGetLastError(client_pid);
2321     hconv = DdeConnect(client_pid, server, topic, NULL);
2322     ok(hconv != NULL, "Expected non-NULL conversation\n");
2323     ret = DdeGetLastError(client_pid);
2324     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2325     DdeFreeStringHandle(client_pid, server);
2326
2327     /* Test both A and W data being passed to DdeClientTransaction */
2328     hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1,
2329             hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2330     ok(hdata != NULL, "DdeClientTransaction failed\n");
2331     ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2332     err = DdeGetLastError(client_pid);
2333     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2334
2335     hdata = DdeClientTransaction((LPBYTE)test_cmd_w, lstrlenW(test_cmd_w) * sizeof(WCHAR) + 2,
2336             hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2337     ok(hdata != NULL, "DdeClientTransaction failed\n");
2338     ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2339     err = DdeGetLastError(client_pid);
2340     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2341
2342     DdeFreeStringHandle(client_pid, topic);
2343     ret = DdeDisconnect(hconv);
2344     ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2345
2346     ret = DdeUninitialize(client_pid);
2347     ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2348
2349 }
2350
2351 static void test_end_to_end_server(HANDLE hproc, HANDLE hthread, BOOL type_a)
2352 {
2353     MSG msg;
2354     HSZ server;
2355     BOOL ret;
2356     DWORD res;
2357     HDDEDATA hdata;
2358     static CHAR test_service[] = "TestDDEService";
2359
2360     trace("start end to end server %d\n", type_a);
2361     server_pid = 0;
2362
2363     if (type_a)
2364         res = DdeInitializeA(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2365     else
2366         res = DdeInitializeW(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2367     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
2368
2369     server = DdeCreateStringHandleA(server_pid, test_service, CP_WINANSI);
2370     ok(server != NULL, "Expected non-NULL string handle\n");
2371
2372     hdata = DdeNameService(server_pid, server, 0, DNS_REGISTER);
2373     ok(hdata == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", hdata);
2374     ResumeThread( hthread );
2375
2376
2377     while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
2378     {
2379         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2380     }
2381
2382     ret = DdeUninitialize(server_pid);
2383     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2384     GetExitCodeProcess( hproc, &res );
2385     ok( !res, "client failed with %u error(s)\n", res );
2386 }
2387
2388 START_TEST(dde)
2389 {
2390     int argc;
2391     char **argv;
2392     char buffer[MAX_PATH];
2393     STARTUPINFO startup;
2394     PROCESS_INFORMATION proc;
2395
2396     argc = winetest_get_mainargs(&argv);
2397     if (argc == 3)
2398     {
2399         if (!lstrcmpA(argv[2], "ddeml"))
2400             test_ddeml_client();
2401         else if (!lstrcmpA(argv[2], "msg"))
2402             test_msg_client();
2403         else if (!lstrcmpA(argv[2], "enda"))
2404             test_end_to_end_client(TRUE);
2405         else if (!lstrcmpA(argv[2], "endw"))
2406             test_end_to_end_client(FALSE);
2407
2408         return;
2409     }
2410
2411     test_initialisation();
2412
2413     ZeroMemory(&startup, sizeof(STARTUPINFO));
2414     sprintf(buffer, "%s dde ddeml", argv[0]);
2415     startup.cb = sizeof(startup);
2416     startup.dwFlags = STARTF_USESHOWWINDOW;
2417     startup.wShowWindow = SW_SHOWNORMAL;
2418
2419     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2420                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2421
2422     test_msg_server(proc.hProcess, proc.hThread);
2423
2424     sprintf(buffer, "%s dde msg", argv[0]);
2425     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2426                    0, NULL, NULL, &startup, &proc);
2427
2428     test_ddeml_server(proc.hProcess);
2429
2430     /* Test the combinations of A and W interfaces with A and W data
2431        end to end to ensure that data conversions are accurate */
2432     sprintf(buffer, "%s dde enda", argv[0]);
2433     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2434                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2435
2436     test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2437
2438     sprintf(buffer, "%s dde endw", argv[0]);
2439     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2440                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2441
2442     test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2443
2444     sprintf(buffer, "%s dde enda", argv[0]);
2445     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2446                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2447
2448     test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2449
2450     sprintf(buffer, "%s dde endw", argv[0]);
2451     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2452                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2453
2454     test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2455
2456     test_dde_aw_transaction();
2457
2458     test_DdeCreateDataHandle();
2459     test_DdeCreateStringHandle();
2460     test_FreeDDElParam();
2461     test_PackDDElParam();
2462     test_UnpackDDElParam();
2463 }