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