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