user32/tests: Make sure the message test doesn't hang if the window doesn't have...
[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     /* win9x returns 0 here and crashes in PostMessageA */
1059     if (lparam) {
1060         PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1061         flush_events();
1062     }
1063     else
1064         win_skip("no lparam for WM_DDE_POKE\n");
1065
1066
1067     /* WM_DDE_POKE, no item */
1068     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, 0);
1069     PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1070
1071     flush_events();
1072
1073     hglobal = create_poke();
1074
1075     /* WM_DDE_POKE, no client hwnd */
1076     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1077     PostMessageA(server_hwnd, WM_DDE_POKE, 0, lparam);
1078
1079     flush_events();
1080
1081     /* WM_DDE_POKE, all params correct */
1082     lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1083     PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1084
1085     flush_events();
1086
1087     execute_hglobal = create_execute("[Command(Var)]");
1088
1089     /* WM_DDE_EXECUTE, no lparam */
1090     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, 0);
1091
1092     flush_events();
1093
1094     /* WM_DDE_EXECUTE, no hglobal */
1095     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, 0);
1096     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1097
1098     flush_events();
1099
1100     /* WM_DDE_EXECUTE, no client hwnd */
1101     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1102     PostMessageA(server_hwnd, WM_DDE_EXECUTE, 0, lparam);
1103
1104     flush_events();
1105
1106     /* WM_DDE_EXECUTE, all params correct */
1107     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1108     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1109
1110     flush_events();
1111
1112     GlobalFree(execute_hglobal);
1113     execute_hglobal = create_execute("[BadCommand(Var)]");
1114
1115     /* WM_DDE_EXECUTE that will get rejected */
1116     lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1117     PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1118
1119     flush_events();
1120
1121     destroy_dde_window(&client_hwnd, "dde_client");
1122 }
1123
1124 static LRESULT WINAPI hook_dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1125 {
1126     UINT_PTR lo, hi;
1127
1128     trace("hook_dde_client_wndproc: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1129
1130     switch (msg)
1131     {
1132     case WM_DDE_ACK:
1133         UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1134         trace("WM_DDE_ACK: status %04lx hglobal %p\n", lo, (HGLOBAL)hi);
1135         break;
1136
1137     default:
1138         break;
1139     }
1140     return CallWindowProcA(old_dde_client_wndproc, hwnd, msg, wparam, lparam);
1141 }
1142
1143 static LRESULT WINAPI dde_server_wndprocW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1144 {
1145     trace("dde_server_wndprocW: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1146
1147     switch (msg)
1148     {
1149     case WM_DDE_INITIATE:
1150     {
1151         ATOM aService = GlobalAddAtomW(TEST_DDE_SERVICE);
1152
1153         trace("server: got WM_DDE_INITIATE from %p with %08lx\n", (HWND)wparam, lparam);
1154
1155         if (LOWORD(lparam) == aService)
1156         {
1157             ok(!IsWindowUnicode((HWND)wparam), "client should be an ANSI window\n");
1158             old_dde_client_wndproc = (WNDPROC)SetWindowLongPtrA((HWND)wparam, GWLP_WNDPROC, (ULONG_PTR)hook_dde_client_wndproc);
1159             trace("server: sending WM_DDE_ACK to %p\n", (HWND)wparam);
1160             SendMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, MAKELPARAM(aService, 0));
1161         }
1162         else
1163             GlobalDeleteAtom(aService);
1164         return 0;
1165     }
1166
1167     case WM_DDE_EXECUTE:
1168     {
1169         DDEACK ack;
1170         WORD status;
1171         LPCSTR cmd;
1172         UINT_PTR lo, hi;
1173
1174         trace("server: got WM_DDE_EXECUTE from %p with %08lx\n", (HWND)wparam, lparam);
1175
1176         UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1177         trace("%08lx => lo %04lx hi %04lx\n", lparam, lo, hi);
1178
1179         ack.bAppReturnCode = 0;
1180         ack.reserved = 0;
1181         ack.fBusy = 0;
1182
1183         cmd = GlobalLock((HGLOBAL)hi);
1184         if (!cmd || (lstrcmpA(cmd, exec_cmdA) && lstrcmpW((LPCWSTR)cmd, exec_cmdW)))
1185         {
1186             trace("ignoring unknown WM_DDE_EXECUTE command\n");
1187             /* We have to send a negative acknowledge even if we don't
1188              * accept the command, otherwise Windows goes mad and next time
1189              * we send an acknowledge DDEML drops the connection.
1190              * Not sure how to call it: a bug or a feature.
1191              */
1192             ack.fAck = 0;
1193         }
1194         else
1195             ack.fAck = 1;
1196         GlobalUnlock((HGLOBAL)hi);
1197
1198         trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1199
1200         status = *((WORD *)&ack);
1201         lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, status, hi);
1202
1203         PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1204         return 0;
1205     }
1206
1207     case WM_DDE_TERMINATE:
1208     {
1209         DDEACK ack;
1210         WORD status;
1211
1212         trace("server: got WM_DDE_TERMINATE from %p with %08lx\n", (HWND)wparam, lparam);
1213
1214         ack.bAppReturnCode = 0;
1215         ack.reserved = 0;
1216         ack.fBusy = 0;
1217         ack.fAck = 1;
1218
1219         trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1220
1221         status = *((WORD *)&ack);
1222         lparam = PackDDElParam(WM_DDE_ACK, status, 0);
1223
1224         PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1225         return 0;
1226     }
1227
1228     default:
1229         break;
1230     }
1231
1232     return DefWindowProcW(hwnd, msg, wparam, lparam);
1233 }
1234
1235 static LRESULT WINAPI dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1236 {
1237     return DefWindowProcA(hwnd, msg, wparam, lparam);
1238 }
1239
1240 static BOOL create_dde_windows(HWND *client, HWND *server)
1241 {
1242     WNDCLASSA wcA;
1243     WNDCLASSW wcW;
1244     static const WCHAR server_class_name[] = {'d','d','e','_','s','e','r','v','e','r','_','w','i','n','d','o','w',0};
1245     static const char client_class_name[] = "dde_client_window";
1246
1247     memset(&wcW, 0, sizeof(wcW));
1248     wcW.lpfnWndProc = dde_server_wndprocW;
1249     wcW.lpszClassName = server_class_name;
1250     wcW.hInstance = GetModuleHandleA(0);
1251     if (!RegisterClassW(&wcW)) return FALSE;
1252
1253     memset(&wcA, 0, sizeof(wcA));
1254     wcA.lpfnWndProc = dde_client_wndproc;
1255     wcA.lpszClassName = client_class_name;
1256     wcA.hInstance = GetModuleHandleA(0);
1257     assert(RegisterClassA(&wcA));
1258
1259     *server = CreateWindowExW(0, server_class_name, NULL,
1260                               WS_POPUP,
1261                               100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1262                               GetDesktopWindow(), 0,
1263                               GetModuleHandleA(0), NULL);
1264     assert(*server);
1265
1266     *client = CreateWindowExA(0, client_class_name, NULL,
1267                               WS_POPUP,
1268                               100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1269                               GetDesktopWindow(), 0,
1270                               GetModuleHandleA(0), NULL);
1271     assert(*client);
1272
1273     trace("server hwnd %p, client hwnd %p\n", *server, *client);
1274
1275     ok(IsWindowUnicode(*server), "server has to be a unicode window\n");
1276     ok(!IsWindowUnicode(*client), "client has to be an ANSI window\n");
1277
1278     return TRUE;
1279 }
1280
1281 static HDDEDATA CALLBACK client_dde_callback(UINT uType, UINT uFmt, HCONV hconv,
1282                                      HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
1283                                      ULONG_PTR dwData1, ULONG_PTR dwData2)
1284 {
1285     static const char * const cmd_type[15] = {
1286         "XTYP_ERROR", "XTYP_ADVDATA", "XTYP_ADVREQ", "XTYP_ADVSTART",
1287         "XTYP_ADVSTOP", "XTYP_EXECUTE", "XTYP_CONNECT", "XTYP_CONNECT_CONFIRM",
1288         "XTYP_XACT_COMPLETE", "XTYP_POKE", "XTYP_REGISTER", "XTYP_REQUEST",
1289         "XTYP_DISCONNECT", "XTYP_UNREGISTER", "XTYP_WILDCONNECT" };
1290     UINT type;
1291     const char *cmd_name;
1292
1293     type = (uType & XTYP_MASK) >> XTYP_SHIFT;
1294     cmd_name = (type <= 14) ? cmd_type[type] : "unknown";
1295
1296     trace("client_dde_callback: %04x (%s) %d %p %p %p %p %08lx %08lx\n",
1297           uType, cmd_name, uFmt, hconv, hsz1, hsz2, hdata, dwData1, dwData2);
1298     return 0;
1299 }
1300
1301 static void test_dde_aw_transaction(void)
1302 {
1303     HSZ hsz_server;
1304     DWORD dde_inst, ret, err;
1305     HCONV hconv;
1306     HWND hwnd_client, hwnd_server;
1307     CONVINFO info;
1308     HDDEDATA hdata;
1309     static char test_cmd[] = "test dde command";
1310
1311     /* server: unicode, client: ansi */
1312     if (!create_dde_windows(&hwnd_client, &hwnd_server)) return;
1313
1314     dde_inst = 0;
1315     ret = DdeInitializeA(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
1316     ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04x (%x)\n",
1317        ret, DdeGetLastError(dde_inst));
1318
1319     hsz_server = DdeCreateStringHandleW(dde_inst, TEST_DDE_SERVICE, CP_WINUNICODE);
1320
1321     hconv = DdeConnect(dde_inst, hsz_server, 0, NULL);
1322     ok(hconv != 0, "DdeConnect error %x\n", DdeGetLastError(dde_inst));
1323     err = DdeGetLastError(dde_inst);
1324     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1325
1326     info.cb = sizeof(info);
1327     ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1328     ok(ret, "wrong info size %d, DdeQueryConvInfo error %x\n", ret, DdeGetLastError(dde_inst));
1329     /* should be CP_WINANSI since we used DdeInitializeA */
1330     ok(info.ConvCtxt.iCodePage == CP_WINANSI, "wrong iCodePage %d\n", info.ConvCtxt.iCodePage);
1331     ok(!info.hConvPartner, "unexpected info.hConvPartner: %p\n", info.hConvPartner);
1332 todo_wine {
1333     ok((info.wStatus & DDE_FACK), "unexpected info.wStatus: %04x\n", info.wStatus);
1334 }
1335     ok((info.wStatus & (ST_CONNECTED | ST_CLIENT)) == (ST_CONNECTED | ST_CLIENT), "unexpected info.wStatus: %04x\n", info.wStatus);
1336     ok(info.wConvst == XST_CONNECTED, "unexpected info.wConvst: %04x\n", info.wConvst);
1337     ok(info.wType == 0, "unexpected info.wType: %04x\n", info.wType);
1338
1339     trace("hwnd %p, hwndPartner %p\n", info.hwnd, info.hwndPartner);
1340
1341     trace("sending test client transaction command\n");
1342     ret = 0xdeadbeef;
1343     hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1, hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
1344     ok(!hdata, "DdeClientTransaction succeeded\n");
1345     ok(ret == DDE_FNOTPROCESSED, "wrong status code %04x\n", ret);
1346     err = DdeGetLastError(dde_inst);
1347     ok(err == DMLERR_NOTPROCESSED, "wrong dde error %x\n", err);
1348
1349     trace("sending ANSI client transaction command\n");
1350     ret = 0xdeadbeef;
1351     hdata = DdeClientTransaction((LPBYTE)exec_cmdA, lstrlenA(exec_cmdA) + 1, hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1352     ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1353     ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1354
1355     err = DdeGetLastError(dde_inst);
1356     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1357
1358     trace("sending unicode client transaction command\n");
1359     ret = 0xdeadbeef;
1360     hdata = DdeClientTransaction((LPBYTE)exec_cmdW, (lstrlenW(exec_cmdW) + 1) * sizeof(WCHAR), hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1361     ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1362     ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1363     err = DdeGetLastError(dde_inst);
1364     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1365
1366     ok(DdeDisconnect(hconv), "DdeDisconnect error %x\n", DdeGetLastError(dde_inst));
1367
1368     info.cb = sizeof(info);
1369     ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1370     ok(!ret, "DdeQueryConvInfo should fail\n");
1371     err = DdeGetLastError(dde_inst);
1372 todo_wine {
1373     ok(err == DMLERR_INVALIDPARAMETER, "wrong dde error %x\n", err);
1374 }
1375
1376     ok(DdeFreeStringHandle(dde_inst, hsz_server), "DdeFreeStringHandle error %x\n", DdeGetLastError(dde_inst));
1377
1378     /* This call hangs on win2k SP4 and XP SP1.
1379     DdeUninitialize(dde_inst);*/
1380
1381     DestroyWindow(hwnd_client);
1382     DestroyWindow(hwnd_server);
1383 }
1384
1385 static void test_initialisation(void)
1386 {
1387     UINT ret;
1388     DWORD res;
1389     HDDEDATA hdata;
1390     HSZ server, topic, item;
1391     DWORD client_pid;
1392     HCONV conversation;
1393
1394     /* Initialise without a valid server window. */
1395     client_pid = 0;
1396     ret = DdeInitializeA(&client_pid, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1397     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
1398
1399
1400     server = DdeCreateStringHandleA(client_pid, "TestDDEService", CP_WINANSI);
1401     topic = DdeCreateStringHandleA(client_pid, "TestDDETopic", CP_WINANSI);
1402
1403     DdeGetLastError(client_pid);
1404
1405     /* There is no server window so no conversation can be extracted */
1406     conversation = DdeConnect(client_pid, server, topic, NULL);
1407     ok(conversation == NULL, "Expected NULL conversation, %p\n", conversation);
1408     ret = DdeGetLastError(client_pid);
1409     ok(ret == DMLERR_NO_CONV_ESTABLISHED, "Expected DMLERR_NO_CONV_ESTABLISHED, got %d\n", ret);
1410
1411     DdeFreeStringHandle(client_pid, server);
1412
1413     item = DdeCreateStringHandleA(client_pid, "request", CP_WINANSI);
1414
1415     /* There is no converstation so an invalild parameter results */
1416     res = 0xdeadbeef;
1417     DdeGetLastError(client_pid);
1418     hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
1419     ret = DdeGetLastError(client_pid);
1420 todo_wine
1421     ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
1422     ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %08x\n", res);
1423
1424     DdeFreeStringHandle(client_pid, server);
1425     ret = DdeDisconnect(conversation);
1426     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1427
1428     ret = DdeUninitialize(client_pid);
1429     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1430 }
1431
1432 static void test_DdeCreateStringHandleW(DWORD dde_inst, int codepage)
1433 {
1434     static const WCHAR dde_string[] = {'D','D','E',' ','S','t','r','i','n','g',0};
1435     HSZ str_handle;
1436     WCHAR bufW[256];
1437     char buf[256];
1438     ATOM atom;
1439     int ret;
1440
1441     str_handle = DdeCreateStringHandleW(dde_inst, dde_string, codepage);
1442     ok(str_handle != 0, "DdeCreateStringHandleW failed with error %08x\n",
1443        DdeGetLastError(dde_inst));
1444
1445     ret = DdeQueryStringW(dde_inst, str_handle, NULL, 0, codepage);
1446     if (codepage == CP_WINANSI)
1447         ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1448     else
1449         ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1450
1451     ret = DdeQueryStringW(dde_inst, str_handle, bufW, 256, codepage);
1452     if (codepage == CP_WINANSI)
1453     {
1454         ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1455         ok(!lstrcmpA("D", (LPCSTR)bufW), "DdeQueryStringW returned wrong string\n");
1456     }
1457     else
1458     {
1459         ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1460         ok(!lstrcmpW(dde_string, bufW), "DdeQueryStringW returned wrong string\n");
1461     }
1462
1463     ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINANSI);
1464     if (codepage == CP_WINANSI)
1465     {
1466         ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1467         ok(!lstrcmpA("D", buf), "DdeQueryStringW returned wrong string\n");
1468     }
1469     else
1470     {
1471         ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1472         ok(!lstrcmpA("DDE String", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1473     }
1474
1475     ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINUNICODE);
1476     if (codepage == CP_WINANSI)
1477     {
1478         ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1479         ok(!lstrcmpA("D", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1480     }
1481     else
1482     {
1483         ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1484         ok(!lstrcmpW(dde_string, (LPCWSTR)buf), "DdeQueryStringW returned wrong string\n");
1485     }
1486
1487     if (codepage == CP_WINANSI)
1488     {
1489         atom = FindAtomA((LPSTR)dde_string);
1490         ok(atom != 0, "Expected a valid atom\n");
1491
1492         SetLastError(0xdeadbeef);
1493         atom = GlobalFindAtomA((LPSTR)dde_string);
1494         ok(atom == 0, "Expected 0, got %d\n", atom);
1495         ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1496            "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1497     }
1498     else
1499     {
1500         atom = FindAtomW(dde_string);
1501         ok(atom != 0, "Expected a valid atom\n");
1502
1503         SetLastError(0xdeadbeef);
1504         atom = GlobalFindAtomW(dde_string);
1505         ok(atom == 0, "Expected 0, got %d\n", atom);
1506         ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1507            "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1508     }
1509
1510     ok(DdeFreeStringHandle(dde_inst, str_handle), "DdeFreeStringHandle failed\n");
1511 }
1512
1513 static void test_DdeCreateDataHandle(void)
1514 {
1515     HDDEDATA hdata;
1516     DWORD dde_inst;
1517     DWORD size;
1518     UINT res, err;
1519     BOOL ret;
1520     HSZ item;
1521     LPBYTE ptr;
1522
1523     dde_inst = 0;
1524     res = DdeInitializeA(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1525     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1526
1527     item = DdeCreateStringHandleA(dde_inst, "item", CP_WINANSI);
1528     ok(item != NULL, "Expected non-NULL hsz\n");
1529
1530     if (0) {
1531         /* do not test with an invalid instance id: that crashes on win9x */
1532         hdata = DdeCreateDataHandle(0xdeadbeef, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1533     }
1534
1535     /* 0 instance id */
1536     DdeGetLastError(dde_inst);
1537     hdata = DdeCreateDataHandle(0, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1538     err = DdeGetLastError(dde_inst);
1539     todo_wine
1540     {
1541         ok(hdata == NULL, "Expected NULL, got %p\n", hdata);
1542         ok(err == DMLERR_INVALIDPARAMETER,
1543            "Expected DMLERR_INVALIDPARAMETER, got %d\n", err);
1544     }
1545
1546     /* NULL pSrc */
1547     DdeGetLastError(dde_inst);
1548     hdata = DdeCreateDataHandle(dde_inst, NULL, MAX_PATH, 0, item, CF_TEXT, 0);
1549     err = DdeGetLastError(dde_inst);
1550     ok(hdata != NULL, "Expected non-NULL hdata\n");
1551     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1552
1553     ptr = GlobalLock(hdata);
1554     todo_wine
1555     {
1556         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1557     }
1558
1559     ptr = DdeAccessData(hdata, &size);
1560     ok(ptr != NULL, "Expected non-NULL ptr\n");
1561     ok(size == 260, "Expected 260, got %d\n", size);
1562
1563     ret = DdeUnaccessData(hdata);
1564     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1565
1566     ret = DdeFreeDataHandle(hdata);
1567     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1568
1569     /* cb is zero */
1570     DdeGetLastError(dde_inst);
1571     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", 0, 0, item, CF_TEXT, 0);
1572     err = DdeGetLastError(dde_inst);
1573     ok(hdata != NULL, "Expected non-NULL hdata\n");
1574     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1575
1576     ptr = GlobalLock(hdata);
1577     todo_wine
1578     {
1579         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1580     }
1581
1582     ptr = DdeAccessData(hdata, &size);
1583     ok(ptr != NULL, "Expected non-NULL ptr\n");
1584     ok(size == 0, "Expected 0, got %d\n", size);
1585
1586     ret = DdeUnaccessData(hdata);
1587     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1588
1589     ret = DdeFreeDataHandle(hdata);
1590     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1591
1592     /* cbOff is non-zero */
1593     DdeGetLastError(dde_inst);
1594     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 2, item, CF_TEXT, 0);
1595     err = DdeGetLastError(dde_inst);
1596     ok(hdata != NULL, "Expected non-NULL hdata\n");
1597     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1598
1599     ptr = GlobalLock(hdata);
1600     todo_wine
1601     {
1602         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1603     }
1604
1605     ptr = DdeAccessData(hdata, &size);
1606     ok(ptr != NULL, "Expected non-NULL ptr\n");
1607     ok(size == 262, "Expected 262, got %d\n", size);
1608     todo_wine
1609     {
1610         ok(lstrlenA((LPSTR)ptr) == 0, "Expected 0, got %d\n", lstrlenA((LPSTR)ptr));
1611     }
1612
1613     ret = DdeUnaccessData(hdata);
1614     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1615
1616     ret = DdeFreeDataHandle(hdata);
1617     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1618
1619     /* NULL item */
1620     DdeGetLastError(dde_inst);
1621     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, 0, CF_TEXT, 0);
1622     err = DdeGetLastError(dde_inst);
1623     ok(hdata != NULL, "Expected non-NULL hdata\n");
1624     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1625
1626     ptr = GlobalLock(hdata);
1627     todo_wine
1628     {
1629         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1630     }
1631
1632     ptr = DdeAccessData(hdata, &size);
1633     ok(ptr != NULL, "Expected non-NULL ptr\n");
1634     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1635     ok(size == 260, "Expected 260, got %d\n", size);
1636
1637     ret = DdeUnaccessData(hdata);
1638     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1639
1640     ret = DdeFreeDataHandle(hdata);
1641     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1642
1643     /* NULL item */
1644     DdeGetLastError(dde_inst);
1645     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, (HSZ)0xdeadbeef, CF_TEXT, 0);
1646     err = DdeGetLastError(dde_inst);
1647     ok(hdata != NULL, "Expected non-NULL hdata\n");
1648     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1649
1650     ptr = GlobalLock(hdata);
1651     todo_wine
1652     {
1653         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1654     }
1655
1656     ptr = DdeAccessData(hdata, &size);
1657     ok(ptr != NULL, "Expected non-NULL ptr\n");
1658     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1659     ok(size == 260, "Expected 260, got %d\n", size);
1660
1661     ret = DdeUnaccessData(hdata);
1662     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1663
1664     ret = DdeFreeDataHandle(hdata);
1665     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1666
1667     /* invalid clipboard format */
1668     DdeGetLastError(dde_inst);
1669     hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, item, 0xdeadbeef, 0);
1670     err = DdeGetLastError(dde_inst);
1671     ok(hdata != NULL, "Expected non-NULL hdata\n");
1672     ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1673
1674     ptr = GlobalLock(hdata);
1675     todo_wine
1676     {
1677         ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1678     }
1679
1680     ptr = DdeAccessData(hdata, &size);
1681     ok(ptr != NULL, "Expected non-NULL ptr\n");
1682     ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1683     ok(size == 260, "Expected 260, got %d\n", size);
1684
1685     ret = DdeUnaccessData(hdata);
1686     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1687
1688     ret = DdeFreeDataHandle(hdata);
1689     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1690
1691     ret = DdeUninitialize(dde_inst);
1692     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1693 }
1694
1695 static void test_DdeCreateStringHandle(void)
1696 {
1697     DWORD dde_inst, ret;
1698
1699     dde_inst = 0xdeadbeef;
1700     SetLastError(0xdeadbeef);
1701     ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1702     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1703     {
1704         skip("DdeInitialize is unimplemented\n");
1705         return;
1706     }
1707
1708     ok(ret == DMLERR_INVALIDPARAMETER, "DdeInitializeW should fail, but got %04x instead\n", ret);
1709     ok(DdeGetLastError(dde_inst) == DMLERR_INVALIDPARAMETER, "expected DMLERR_INVALIDPARAMETER\n");
1710
1711     dde_inst = 0;
1712     ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1713     ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04x (%08x)\n",
1714        ret, DdeGetLastError(dde_inst));
1715
1716     test_DdeCreateStringHandleW(dde_inst, 0);
1717     test_DdeCreateStringHandleW(dde_inst, CP_WINUNICODE);
1718     test_DdeCreateStringHandleW(dde_inst, CP_WINANSI);
1719
1720     ok(DdeUninitialize(dde_inst), "DdeUninitialize failed\n");
1721 }
1722
1723 static void test_FreeDDElParam(void)
1724 {
1725     HGLOBAL val, hglobal;
1726     BOOL ret;
1727
1728     ret = FreeDDElParam(WM_DDE_INITIATE, 0);
1729     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1730
1731     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1732     ret = FreeDDElParam(WM_DDE_INITIATE, (LPARAM)hglobal);
1733     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1734     val = GlobalFree(hglobal);
1735     ok(val == NULL, "Expected NULL, got %p\n", val);
1736
1737     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1738     ret = FreeDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal);
1739     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1740     val = GlobalFree(hglobal);
1741     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1742     ok(GetLastError() == ERROR_INVALID_HANDLE,
1743        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1744
1745     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1746     ret = FreeDDElParam(WM_DDE_UNADVISE, (LPARAM)hglobal);
1747     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1748     val = GlobalFree(hglobal);
1749     ok(val == NULL, "Expected NULL, got %p\n", val);
1750
1751     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1752     ret = FreeDDElParam(WM_DDE_ACK, (LPARAM)hglobal);
1753     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1754     val = GlobalFree(hglobal);
1755     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1756     ok(GetLastError() == ERROR_INVALID_HANDLE,
1757        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1758
1759     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1760     ret = FreeDDElParam(WM_DDE_DATA, (LPARAM)hglobal);
1761     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1762     val = GlobalFree(hglobal);
1763     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1764     ok(GetLastError() == ERROR_INVALID_HANDLE,
1765        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1766
1767     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1768     ret = FreeDDElParam(WM_DDE_REQUEST, (LPARAM)hglobal);
1769     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1770     val = GlobalFree(hglobal);
1771     ok(val == NULL, "Expected NULL, got %p\n", val);
1772
1773     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1774     ret = FreeDDElParam(WM_DDE_POKE, (LPARAM)hglobal);
1775     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1776     val = GlobalFree(hglobal);
1777     ok(val == hglobal, "Expected hglobal, got %p\n", val);
1778     ok(GetLastError() == ERROR_INVALID_HANDLE,
1779        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1780
1781     hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1782     ret = FreeDDElParam(WM_DDE_EXECUTE, (LPARAM)hglobal);
1783     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1784     val = GlobalFree(hglobal);
1785     ok(val == NULL, "Expected NULL, got %p\n", val);
1786 }
1787
1788 static void test_PackDDElParam(void)
1789 {
1790     UINT_PTR lo, hi, *ptr;
1791     HGLOBAL hglobal;
1792     LPARAM lparam;
1793     BOOL ret;
1794
1795     lparam = PackDDElParam(WM_DDE_INITIATE, 0xcafe, 0xbeef);
1796     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1797     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1798        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1799     ok(GetLastError() == ERROR_INVALID_HANDLE,
1800        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1801
1802     lo = hi = 0;
1803     ret = UnpackDDElParam(WM_DDE_INITIATE, lparam, &lo, &hi);
1804     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1805     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1806     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1807
1808     ret = FreeDDElParam(WM_DDE_INITIATE, lparam);
1809     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1810
1811     lparam = PackDDElParam(WM_DDE_TERMINATE, 0xcafe, 0xbeef);
1812     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1813     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1814        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1815     ok(GetLastError() == ERROR_INVALID_HANDLE,
1816        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1817
1818     lo = hi = 0;
1819     ret = UnpackDDElParam(WM_DDE_TERMINATE, lparam, &lo, &hi);
1820     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1821     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1822     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1823
1824     ret = FreeDDElParam(WM_DDE_TERMINATE, lparam);
1825     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1826
1827     lparam = PackDDElParam(WM_DDE_ADVISE, 0xcafe, 0xbeef);
1828     /* win9x returns 0 here */
1829     if (lparam) {
1830         ptr = GlobalLock((HGLOBAL)lparam);
1831         ok(ptr != NULL, "Expected non-NULL ptr\n");
1832         ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1833         ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1834
1835         ret = GlobalUnlock((HGLOBAL)lparam);
1836         ok(ret == 1, "Expected 1, got %d\n", ret);
1837
1838         lo = hi = 0;
1839         ret = UnpackDDElParam(WM_DDE_ADVISE, lparam, &lo, &hi);
1840         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1841         ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1842         ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1843     }
1844     else
1845         win_skip("no lparam for WM_DDE_ADVISE\n");
1846
1847     ret = FreeDDElParam(WM_DDE_ADVISE, lparam);
1848     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1849
1850     hglobal = GlobalFree((HGLOBAL)lparam);
1851     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1852     ok(GetLastError() == ERROR_INVALID_HANDLE,
1853        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1854
1855     lparam = PackDDElParam(WM_DDE_UNADVISE, 0xcafe, 0xbeef);
1856     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1857     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1858        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1859     ok(GetLastError() == ERROR_INVALID_HANDLE,
1860        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1861
1862     lo = hi = 0;
1863     ret = UnpackDDElParam(WM_DDE_UNADVISE, lparam, &lo, &hi);
1864     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1865     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1866     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1867
1868     ret = FreeDDElParam(WM_DDE_UNADVISE, lparam);
1869     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1870
1871     lparam = PackDDElParam(WM_DDE_ACK, 0xcafe, 0xbeef);
1872     /* win9x returns the input (0xbeef<<16 | 0xcafe) here */
1873     if (lparam != 0xbeefcafe) {
1874         ptr = GlobalLock((HGLOBAL)lparam);
1875         ok(ptr != NULL, "Expected non-NULL ptr\n");
1876         ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1877         ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1878
1879         ret = GlobalUnlock((HGLOBAL)lparam);
1880         ok(ret == 1, "Expected 1, got %d\n", ret);
1881
1882         lo = hi = 0;
1883         ret = UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1884         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1885         ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1886         ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1887
1888         ret = FreeDDElParam(WM_DDE_ACK, lparam);
1889         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1890
1891         hglobal = GlobalFree((HGLOBAL)lparam);
1892         ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1893         ok(GetLastError() == ERROR_INVALID_HANDLE,
1894            "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1895     }
1896     else
1897         win_skip("got lparam 0x%lx for WM_DDE_ACK\n", lparam);
1898
1899     lparam = PackDDElParam(WM_DDE_DATA, 0xcafe, 0xbeef);
1900     /* win9x returns 0 here */
1901     if (lparam) {
1902         ptr = GlobalLock((HGLOBAL)lparam);
1903         ok(ptr != NULL, "Expected non-NULL ptr\n");
1904         ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1905         ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1906
1907         ret = GlobalUnlock((HGLOBAL)lparam);
1908         ok(ret == 1, "Expected 1, got %d\n", ret);
1909
1910         lo = hi = 0;
1911         ret = UnpackDDElParam(WM_DDE_DATA, lparam, &lo, &hi);
1912         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1913         ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1914         ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1915     }
1916     else
1917         win_skip("no lparam for WM_DDE_DATA\n");
1918
1919     ret = FreeDDElParam(WM_DDE_DATA, lparam);
1920     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1921
1922     hglobal = GlobalFree((HGLOBAL)lparam);
1923     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1924     ok(GetLastError() == ERROR_INVALID_HANDLE,
1925        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1926
1927     lparam = PackDDElParam(WM_DDE_REQUEST, 0xcafe, 0xbeef);
1928     ok(lparam == 0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1929     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1930        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1931     ok(GetLastError() == ERROR_INVALID_HANDLE,
1932        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1933
1934     lo = hi = 0;
1935     ret = UnpackDDElParam(WM_DDE_REQUEST, lparam, &lo, &hi);
1936     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1937     ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1938     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1939
1940     ret = FreeDDElParam(WM_DDE_REQUEST, lparam);
1941     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1942
1943     lparam = PackDDElParam(WM_DDE_POKE, 0xcafe, 0xbeef);
1944     /* win9x returns 0 here */
1945     if (lparam) {
1946         ptr = GlobalLock((HGLOBAL)lparam);
1947         ok(ptr != NULL, "Expected non-NULL ptr\n");
1948         ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1949         ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1950
1951         ret = GlobalUnlock((HGLOBAL)lparam);
1952         ok(ret == 1, "Expected 1, got %d\n", ret);
1953
1954         lo = hi = 0;
1955         ret = UnpackDDElParam(WM_DDE_POKE, lparam, &lo, &hi);
1956         ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1957         ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1958         ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1959     }
1960     else
1961         win_skip("no lparam for WM_DDE_POKE\n");
1962
1963     ret = FreeDDElParam(WM_DDE_POKE, lparam);
1964     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1965
1966     hglobal = GlobalFree((HGLOBAL)lparam);
1967     ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1968     ok(GetLastError() == ERROR_INVALID_HANDLE,
1969        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1970
1971     lparam = PackDDElParam(WM_DDE_EXECUTE, 0xcafe, 0xbeef);
1972     ok(lparam == 0xbeef, "Expected 0xbeef, got %08lx\n", lparam);
1973     ok(GlobalLock((HGLOBAL)lparam) == NULL,
1974        "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1975     ok(GetLastError() == ERROR_INVALID_HANDLE,
1976        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1977
1978     lo = hi = 0;
1979     ret = UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1980     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1981     ok(lo == 0, "Expected 0, got %08lx\n", lo);
1982     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1983
1984     ret = FreeDDElParam(WM_DDE_EXECUTE, lparam);
1985     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1986 }
1987
1988 static void test_UnpackDDElParam(void)
1989 {
1990     UINT_PTR lo, hi, *ptr;
1991     HGLOBAL hglobal;
1992     BOOL ret;
1993
1994     /* NULL lParam */
1995     lo = 0xdead;
1996     hi = 0xbeef;
1997     ret = UnpackDDElParam(WM_DDE_INITIATE, 0, &lo, &hi);
1998     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1999     ok(lo == 0, "Expected 0, got %08lx\n", lo);
2000     ok(hi == 0, "Expected 0, got %08lx\n", hi);
2001
2002     /* NULL lo */
2003     lo = 0xdead;
2004     hi = 0xbeef;
2005     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, NULL, &hi);
2006     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2007     ok(lo == 0xdead, "Expected 0xdead, got %08lx\n", lo);
2008     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2009
2010     /* NULL hi */
2011     lo = 0xdead;
2012     hi = 0xbeef;
2013     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, NULL);
2014     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2015     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2016     ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
2017
2018     lo = 0xdead;
2019     hi = 0xbeef;
2020     ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, &hi);
2021     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2022     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2023     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2024
2025     lo = 0xdead;
2026     hi = 0xbeef;
2027     ret = UnpackDDElParam(WM_DDE_TERMINATE, 0xcafebabe, &lo, &hi);
2028     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2029     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2030     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2031
2032     lo = 0xdead;
2033     hi = 0xbeef;
2034     ret = UnpackDDElParam(WM_DDE_ADVISE, 0, &lo, &hi);
2035     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2036     ok(lo == 0 ||
2037        broken(lo == 0xdead), /* win2k */
2038        "Expected 0, got %08lx\n", lo);
2039     ok(hi == 0 ||
2040        broken(hi == 0xbeef), /* win2k */
2041        "Expected 0, got %08lx\n", hi);
2042
2043     lo = 0xdead;
2044     hi = 0xbeef;
2045     ret = UnpackDDElParam(WM_DDE_ADVISE, 0xcafebabe, &lo, &hi);
2046     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2047     ok(lo == 0 ||
2048        broken(lo == 0xdead), /* win2k */
2049        "Expected 0, got %08lx\n", lo);
2050     ok(hi == 0 ||
2051        broken(hi == 0xbeef), /* win2k */
2052        "Expected 0, got %08lx\n", hi);
2053
2054     hglobal = GlobalAlloc(GMEM_DDESHARE, 2);
2055     ptr = GlobalLock(hglobal);
2056     ptr[0] = 0xcafebabe;
2057     ptr[1] = 0xdeadbeef;
2058     GlobalUnlock(hglobal);
2059
2060     lo = 0xdead;
2061     hi = 0xbeef;
2062     ret = UnpackDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal, &lo, &hi);
2063     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2064     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2065     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2066
2067     lo = 0xdead;
2068     hi = 0xbeef;
2069     ret = UnpackDDElParam(WM_DDE_UNADVISE, 0xcafebabe, &lo, &hi);
2070     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2071     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2072     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2073
2074     lo = 0xdead;
2075     hi = 0xbeef;
2076     ret = UnpackDDElParam(WM_DDE_ACK, 0xcafebabe, &lo, &hi);
2077     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2078     ok(lo == 0 ||
2079        broken(lo == 0xdead), /* win2k */
2080        "Expected 0, got %08lx\n", lo);
2081     ok(hi == 0 ||
2082        broken(hi == 0xbeef), /* win2k */
2083        "Expected 0, got %08lx\n", hi);
2084
2085     lo = 0xdead;
2086     hi = 0xbeef;
2087     ret = UnpackDDElParam(WM_DDE_ACK, (LPARAM)hglobal, &lo, &hi);
2088     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2089     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2090     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2091
2092     lo = 0xdead;
2093     hi = 0xbeef;
2094     ret = UnpackDDElParam(WM_DDE_DATA, 0xcafebabe, &lo, &hi);
2095     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2096     ok(lo == 0 ||
2097        broken(lo == 0xdead), /* win2k */
2098        "Expected 0, got %08lx\n", lo);
2099     ok(hi == 0 ||
2100        broken(hi == 0xbeef), /* win2k */
2101        "Expected 0, got %08lx\n", hi);
2102
2103     lo = 0xdead;
2104     hi = 0xbeef;
2105     ret = UnpackDDElParam(WM_DDE_DATA, (LPARAM)hglobal, &lo, &hi);
2106     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2107     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2108     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2109
2110     lo = 0xdead;
2111     hi = 0xbeef;
2112     ret = UnpackDDElParam(WM_DDE_REQUEST, 0xcafebabe, &lo, &hi);
2113     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2114     ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2115     ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2116
2117     lo = 0xdead;
2118     hi = 0xbeef;
2119     ret = UnpackDDElParam(WM_DDE_POKE, 0xcafebabe, &lo, &hi);
2120     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2121     ok(lo == 0 ||
2122        broken(lo == 0xdead), /* win2k */
2123        "Expected 0, got %08lx\n", lo);
2124     ok(hi == 0 ||
2125        broken(hi == 0xbeef), /* win2k */
2126        "Expected 0, got %08lx\n", hi);
2127
2128     lo = 0xdead;
2129     hi = 0xbeef;
2130     ret = UnpackDDElParam(WM_DDE_POKE, (LPARAM)hglobal, &lo, &hi);
2131     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2132     ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2133     ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2134
2135     lo = 0xdead;
2136     hi = 0xbeef;
2137     ret = UnpackDDElParam(WM_DDE_EXECUTE, 0xcafebabe, &lo, &hi);
2138     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2139     ok(lo == 0, "Expected 0, got %08lx\n", lo);
2140     ok(hi == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", hi);
2141 }
2142
2143 static HDDEDATA CALLBACK server_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2144                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2145                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
2146 {
2147     DWORD size, rsize;
2148     char str[MAX_PATH];
2149     static int msg_index = 0;
2150     static HCONV conversation = 0;
2151     static char test_cmd[] = "test dde command";
2152     static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2153     static char test_service [] = "TestDDEService";
2154     static char test_topic [] = "TestDDETopic";
2155
2156     msg_index++;
2157
2158     switch (uType)
2159     {
2160     case XTYP_REGISTER:
2161     {
2162         ok(msg_index == 1 || msg_index == 7 || msg_index == 13 || msg_index == 19,
2163                              "Expected 1, 7, 13 or 19, got %d\n", msg_index);
2164         return (HDDEDATA)TRUE;
2165     }
2166
2167     case XTYP_CONNECT:
2168     {
2169         ok(msg_index == 2 || msg_index == 8 || msg_index == 14 || msg_index == 20,
2170                              "Expected 2, 8, 14 or 20, got %d\n", msg_index);
2171         ok(uFmt == 0, "Expected 0, got %d, msg_index=%d\n", uFmt, msg_index);
2172         ok(hconv == 0, "Expected 0, got %p, msg_index=%d\n", hconv, msg_index);
2173         ok(hdata == 0, "Expected 0, got %p, msg_index=%d\n", hdata, msg_index);
2174         ok(dwData1 != 0, "Expected not 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2175         ok(dwData2 == FALSE, "Expected FALSE, got %08lx, msg_index=%d\n", dwData2, msg_index);
2176
2177         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2178         ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2179                              test_topic, str, msg_index);
2180         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2181
2182         size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
2183         ok(!lstrcmpA(str, test_service), "Expected %s, got %s, msg_index=%d\n",
2184                              test_service, str, msg_index);
2185         ok(size == 14, "Expected 14, got %d, msg_index=%d\n", size, msg_index);
2186
2187         return (HDDEDATA) TRUE;
2188     }
2189     case XTYP_CONNECT_CONFIRM:
2190     {
2191         ok(msg_index == 3 || msg_index == 9  ||  msg_index == 15 ||  msg_index == 21,
2192                              "Expected 3, 9, 15 or 21 got %d\n", msg_index);
2193         conversation = hconv;
2194         return (HDDEDATA) TRUE;
2195     }
2196     case XTYP_EXECUTE:
2197     {
2198         BYTE *buffer = NULL;
2199
2200         ok(msg_index == 4 || msg_index == 5 || msg_index == 10 || msg_index == 11 ||
2201            msg_index == 16 || msg_index == 17 || msg_index == 22 || msg_index == 23,
2202            "Expected 4, 5, 10, 11, 16, 17, 22 or 23, got %d\n", msg_index);
2203         ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
2204         ok(hconv == conversation, "Expected conversation handle, got %p, msg_index=%d\n",
2205                              hconv, msg_index);
2206         ok(dwData1 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2207         ok(dwData2 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData2, msg_index);
2208         ok(hsz2 == 0, "Expected 0, got %p, msg_index=%d\n", hsz2, msg_index);
2209         size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2210         ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2211                              test_topic, str, msg_index);
2212         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2213         ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2214
2215         size = DdeGetData(hdata, NULL, 0, 0);
2216         if (msg_index == 10 || msg_index ==11 || msg_index == 16 || msg_index ==17)
2217           if (msg_index == 10 || msg_index == 16)
2218           todo_wine
2219             ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2220                              size, msg_index);
2221           else
2222             ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2223                              size, msg_index);
2224         else
2225         if (msg_index ==22)
2226         todo_wine
2227             ok(size == 9, "Expected that size should be 9 not %d, msg_index=%d\n",
2228                              size, msg_index);
2229         else
2230           if (msg_index == 5)
2231           todo_wine
2232             ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2233                              size, msg_index);
2234           else
2235             ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2236                              size, msg_index);
2237         ok((buffer = HeapAlloc(GetProcessHeap(), 0, size)) != NULL, "should not be null\n");
2238         rsize = DdeGetData(hdata, buffer, size, 0);
2239         if (msg_index == 10 || msg_index == 11 || msg_index == 16 || msg_index ==17)
2240         {
2241             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2242                              size, rsize, msg_index);
2243           if (msg_index == 10 || msg_index == 16)
2244           todo_wine {
2245             ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2246                              "Expected \"Test dde command\", msg_index=%d\n",
2247                              msg_index);
2248             ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2249           } else
2250           {
2251             ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2252                              "Expected \"Test dde command\", msg_index=%d\n",
2253                              msg_index);
2254             ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2255           }
2256         }else if (msg_index == 22)
2257         {
2258             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2259                              size, rsize, msg_index);
2260         } else
2261         {
2262             ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2263                              size, rsize, msg_index);
2264           if (msg_index == 5)
2265           todo_wine {
2266             ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2267                              test_cmd, buffer, msg_index);
2268             ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2269           }
2270           else
2271           {
2272             ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2273                              test_cmd, buffer, msg_index);
2274             ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2275           }
2276
2277         }
2278
2279         return (HDDEDATA) DDE_FACK;
2280     }
2281     case XTYP_DISCONNECT:
2282         return (HDDEDATA) TRUE;
2283
2284     default:
2285         ok(FALSE, "Unhandled msg: %08x, msg_index=%d\n", uType, msg_index);
2286     }
2287
2288     return NULL;
2289 }
2290
2291 static HDDEDATA CALLBACK client_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2292                                                HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2293                                                ULONG_PTR dwData1, ULONG_PTR dwData2)
2294 {
2295     switch (uType)
2296     {
2297     case XTYP_DISCONNECT:
2298         return (HDDEDATA) TRUE;
2299
2300     default:
2301         ok(FALSE, "Unhandled msg: %08x\n", uType);
2302     }
2303
2304     return NULL;
2305 }
2306
2307 static void test_end_to_end_client(BOOL type_a)
2308 {
2309     DWORD  ret, err;
2310     DWORD client_pid = 0;
2311     HSZ server, topic;
2312     HCONV hconv;
2313     HDDEDATA hdata;
2314     static char test_cmd[] = "test dde command";
2315     static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2316     static char test_service[] = "TestDDEService";
2317     static WCHAR test_service_w[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
2318     static char test_topic[] = "TestDDETopic";
2319     static WCHAR test_topic_w[] = {'T','e','s','t','D','D','E','T','o','p','i','c',0};
2320
2321     trace("Start end to end client %d\n", type_a);
2322
2323     if (type_a)
2324         ret = DdeInitializeA(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2325     else
2326         ret = DdeInitializeW(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2327     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2328
2329     if (type_a)
2330     {
2331         server = DdeCreateStringHandleA(client_pid, test_service, CP_WINANSI);
2332         topic = DdeCreateStringHandleA(client_pid, test_topic, CP_WINANSI);
2333     }
2334     else {
2335         server = DdeCreateStringHandleW(client_pid, test_service_w, CP_WINUNICODE);
2336         topic = DdeCreateStringHandleW(client_pid, test_topic_w, CP_WINUNICODE);
2337     }
2338
2339     DdeGetLastError(client_pid);
2340     hconv = DdeConnect(client_pid, server, topic, NULL);
2341     ok(hconv != NULL, "Expected non-NULL conversation\n");
2342     ret = DdeGetLastError(client_pid);
2343     ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2344     DdeFreeStringHandle(client_pid, server);
2345
2346     /* Test both A and W data being passed to DdeClientTransaction */
2347     hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1,
2348             hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2349     ok(hdata != NULL, "DdeClientTransaction failed\n");
2350     ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2351     err = DdeGetLastError(client_pid);
2352     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2353
2354     hdata = DdeClientTransaction((LPBYTE)test_cmd_w, lstrlenW(test_cmd_w) * sizeof(WCHAR) + 2,
2355             hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2356     ok(hdata != NULL, "DdeClientTransaction failed\n");
2357     ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2358     err = DdeGetLastError(client_pid);
2359     ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2360
2361     DdeFreeStringHandle(client_pid, topic);
2362     ret = DdeDisconnect(hconv);
2363     ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2364
2365     ret = DdeUninitialize(client_pid);
2366     ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2367
2368 }
2369
2370 static void test_end_to_end_server(HANDLE hproc, HANDLE hthread, BOOL type_a)
2371 {
2372     MSG msg;
2373     HSZ server;
2374     BOOL ret;
2375     DWORD res;
2376     HDDEDATA hdata;
2377     static CHAR test_service[] = "TestDDEService";
2378
2379     trace("start end to end server %d\n", type_a);
2380     server_pid = 0;
2381
2382     if (type_a)
2383         res = DdeInitializeA(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2384     else
2385         res = DdeInitializeW(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2386     ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
2387
2388     server = DdeCreateStringHandleA(server_pid, test_service, CP_WINANSI);
2389     ok(server != NULL, "Expected non-NULL string handle\n");
2390
2391     hdata = DdeNameService(server_pid, server, 0, DNS_REGISTER);
2392     ok(hdata == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", hdata);
2393     ResumeThread( hthread );
2394
2395
2396     while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
2397     {
2398         while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2399     }
2400
2401     ret = DdeUninitialize(server_pid);
2402     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2403     GetExitCodeProcess( hproc, &res );
2404     ok( !res, "client failed with %u error(s)\n", res );
2405 }
2406
2407 START_TEST(dde)
2408 {
2409     int argc;
2410     char **argv;
2411     char buffer[MAX_PATH];
2412     STARTUPINFO startup;
2413     PROCESS_INFORMATION proc;
2414
2415     argc = winetest_get_mainargs(&argv);
2416     if (argc == 3)
2417     {
2418         if (!lstrcmpA(argv[2], "ddeml"))
2419             test_ddeml_client();
2420         else if (!lstrcmpA(argv[2], "msg"))
2421             test_msg_client();
2422         else if (!lstrcmpA(argv[2], "enda"))
2423             test_end_to_end_client(TRUE);
2424         else if (!lstrcmpA(argv[2], "endw"))
2425             test_end_to_end_client(FALSE);
2426
2427         return;
2428     }
2429
2430     test_initialisation();
2431
2432     ZeroMemory(&startup, sizeof(STARTUPINFO));
2433     sprintf(buffer, "%s dde ddeml", argv[0]);
2434     startup.cb = sizeof(startup);
2435     startup.dwFlags = STARTF_USESHOWWINDOW;
2436     startup.wShowWindow = SW_SHOWNORMAL;
2437
2438     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2439                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2440
2441     test_msg_server(proc.hProcess, proc.hThread);
2442
2443     sprintf(buffer, "%s dde msg", argv[0]);
2444     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2445                    0, NULL, NULL, &startup, &proc);
2446
2447     test_ddeml_server(proc.hProcess);
2448
2449     /* Test the combinations of A and W interfaces with A and W data
2450        end to end to ensure that data conversions are accurate */
2451     sprintf(buffer, "%s dde enda", argv[0]);
2452     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2453                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2454
2455     test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2456
2457     sprintf(buffer, "%s dde endw", argv[0]);
2458     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2459                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2460
2461     test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2462
2463     sprintf(buffer, "%s dde enda", argv[0]);
2464     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2465                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2466
2467     test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2468
2469     sprintf(buffer, "%s dde endw", argv[0]);
2470     CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2471                    CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2472
2473     test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2474
2475     test_dde_aw_transaction();
2476
2477     test_DdeCreateDataHandle();
2478     test_DdeCreateStringHandle();
2479     test_FreeDDElParam();
2480     test_PackDDElParam();
2481     test_UnpackDDElParam();
2482 }