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