user32: Check for NULL pData in DdeClientTransAction expecting to be passed a handle.
[wine/hacks.git] / dlls / user32 / tests / dde.c
blobe1ccf662ba5dfcce0c946d4936f10d8460e026f4
1 /*
2 * Unit tests for DDE functions
4 * Copyright (c) 2004 Dmitry Timoshkov
5 * Copyright (c) 2007 James Hawkins
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.
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.
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
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "dde.h"
30 #include "ddeml.h"
31 #include "winerror.h"
33 #include "wine/test.h"
35 static const WCHAR TEST_DDE_SERVICE[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
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};
40 static WNDPROC old_dde_client_wndproc;
42 static const DWORD default_timeout = 200;
44 static void flush_events(void)
46 MSG msg;
47 int diff = default_timeout;
48 int min_timeout = 50;
49 DWORD time = GetTickCount() + diff;
51 while (diff > 0)
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;
60 static void create_dde_window(HWND *hwnd, LPCSTR name, WNDPROC wndproc)
62 WNDCLASSA wcA;
64 memset(&wcA, 0, sizeof(wcA));
65 wcA.lpfnWndProc = wndproc;
66 wcA.lpszClassName = name;
67 wcA.hInstance = GetModuleHandleA(0);
68 assert(RegisterClassA(&wcA));
70 *hwnd = CreateWindowExA(0, name, NULL, WS_POPUP,
71 500, 500, CW_USEDEFAULT, CW_USEDEFAULT,
72 GetDesktopWindow(), 0, GetModuleHandleA(0), NULL);
73 assert(*hwnd);
76 static void destroy_dde_window(HWND *hwnd, LPCSTR name)
78 DestroyWindow(*hwnd);
79 UnregisterClass(name, GetModuleHandleA(0));
82 static LRESULT WINAPI dde_server_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
84 UINT_PTR lo, hi;
85 char str[MAX_PATH], *ptr;
86 HGLOBAL hglobal;
87 DDEDATA *data;
88 DDEPOKE *poke;
89 DWORD size;
91 static int msg_index = 0;
92 static HWND client = 0;
93 static BOOL executed = FALSE;
95 if (msg < WM_DDE_FIRST || msg > WM_DDE_LAST)
96 return DefWindowProcA(hwnd, msg, wparam, lparam);
98 msg_index++;
100 switch (msg)
102 case WM_DDE_INITIATE:
104 client = (HWND)wparam;
105 ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
107 GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
108 ok(!lstrcmpA(str, "TestDDEService"), "Expected TestDDEService, got %s\n", str);
110 GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
111 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
113 SendMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
115 break;
118 case WM_DDE_REQUEST:
120 ok((msg_index >= 2 && msg_index <= 4) ||
121 (msg_index >= 7 && msg_index <= 8),
122 "Expected 2, 3, 4, 7 or 8, got %d\n", msg_index);
123 ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
124 ok(LOWORD(lparam) == CF_TEXT, "Expected CF_TEXT, got %d\n", LOWORD(lparam));
126 GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
127 if (msg_index < 8)
128 ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
129 else
130 ok(!lstrcmpA(str, "executed"), "Expected executed, got %s\n", str);
132 if (msg_index == 8)
134 if (executed)
135 lstrcpyA(str, "command executed\r\n");
136 else
137 lstrcpyA(str, "command not executed\r\n");
139 else
140 lstrcpyA(str, "requested data\r\n");
142 size = sizeof(DDEDATA) + lstrlenA(str) + 1;
143 hglobal = GlobalAlloc(GMEM_MOVEABLE, size);
144 ok(hglobal != NULL, "Expected non-NULL hglobal\n");
146 data = GlobalLock(hglobal);
147 ZeroMemory(data, size);
149 /* setting fResponse to FALSE at this point destroys
150 * the internal messaging state of native dde
152 data->fResponse = TRUE;
154 if (msg_index == 2)
155 data->fRelease = TRUE;
156 else if (msg_index == 3)
157 data->fAckReq = TRUE;
159 data->cfFormat = CF_TEXT;
160 lstrcpyA((LPSTR)data->Value, str);
161 GlobalUnlock(hglobal);
163 lparam = PackDDElParam(WM_DDE_ACK, (UINT_PTR)hglobal, HIWORD(lparam));
164 PostMessageA(client, WM_DDE_DATA, (WPARAM)hwnd, lparam);
166 break;
169 case WM_DDE_POKE:
171 ok(msg_index == 5 || msg_index == 6, "Expected 5 or 6, got %d\n", msg_index);
172 ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
174 UnpackDDElParam(WM_DDE_POKE, lparam, &lo, &hi);
176 GlobalGetAtomNameA(hi, str, MAX_PATH);
177 ok(!lstrcmpA(str, "poker"), "Expected poker, got %s\n", str);
179 poke = GlobalLock((HGLOBAL)lo);
180 ok(poke != NULL, "Expected non-NULL poke\n");
181 ok(poke->fReserved == 0, "Expected 0, got %d\n", poke->fReserved);
182 ok(poke->unused == 0, "Expected 0, got %d\n", poke->unused);
183 ok(poke->fRelease == TRUE, "Expected TRUE, got %d\n", poke->fRelease);
184 ok(poke->cfFormat == CF_TEXT, "Expected CF_TEXT, got %d\n", poke->cfFormat);
186 if (msg_index == 5)
188 size = GlobalSize((HGLOBAL)lo);
189 ok(size == 4, "got %d\n", size);
191 else
192 ok(!lstrcmpA((LPSTR)poke->Value, "poke data\r\n"),
193 "Expected 'poke data\\r\\n', got %s\n", poke->Value);
195 GlobalUnlock((HGLOBAL)lo);
197 lparam = PackDDElParam(WM_DDE_ACK, DDE_FACK, hi);
198 PostMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
200 break;
203 case WM_DDE_EXECUTE:
205 ok(msg_index == 7, "Expected 7, got %d\n", msg_index);
206 ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
208 ptr = GlobalLock((HGLOBAL)lparam);
209 ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected [Command(Var)], got %s\n", ptr);
210 GlobalUnlock((HGLOBAL)lparam);
212 executed = TRUE;
214 lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, DDE_FACK, HIWORD(lparam));
215 PostMessageA(client, WM_DDE_ACK, (WPARAM)hwnd, lparam);
217 break;
220 case WM_DDE_TERMINATE:
222 ok(msg_index == 9, "Expected 9, got %d\n", msg_index);
223 ok(wparam == (WPARAM)client, "Expected client hwnd, got %08lx\n", wparam);
224 ok(lparam == 0, "Expected 0, got %08lx\n", lparam);
226 PostMessageA(client, WM_DDE_TERMINATE, (WPARAM)hwnd, 0);
228 break;
231 default:
232 ok(FALSE, "Unhandled msg: %08x\n", msg);
235 return DefWindowProcA(hwnd, msg, wparam, lparam);
238 static void test_msg_server(HANDLE hproc, HANDLE hthread)
240 MSG msg;
241 HWND hwnd;
242 DWORD res;
244 create_dde_window(&hwnd, "dde_server", dde_server_wndproc);
245 ResumeThread( hthread );
247 while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
249 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
252 destroy_dde_window(&hwnd, "dde_server");
253 GetExitCodeProcess( hproc, &res );
254 ok( !res, "client failed with %u error(s)\n", res );
257 static HDDEDATA CALLBACK client_ddeml_callback(UINT uType, UINT uFmt, HCONV hconv,
258 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
259 ULONG_PTR dwData1, ULONG_PTR dwData2)
261 ok(FALSE, "Unhandled msg: %08x\n", uType);
262 return 0;
265 static void test_ddeml_client(void)
267 UINT ret;
268 char buffer[32];
269 LPSTR str;
270 DWORD size, res;
271 HDDEDATA hdata, op;
272 HSZ server, topic, item;
273 DWORD client_pid;
274 HCONV conversation;
276 client_pid = 0;
277 ret = DdeInitializeA(&client_pid, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
278 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
280 /* FIXME: make these atoms global and check them in the server */
282 server = DdeCreateStringHandleA(client_pid, "TestDDEService", CP_WINANSI);
283 topic = DdeCreateStringHandleA(client_pid, "TestDDETopic", CP_WINANSI);
285 DdeGetLastError(client_pid);
286 conversation = DdeConnect(client_pid, server, topic, NULL);
287 ok(conversation != NULL, "Expected non-NULL conversation\n");
288 ret = DdeGetLastError(client_pid);
289 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
291 DdeFreeStringHandle(client_pid, server);
293 item = DdeCreateStringHandleA(client_pid, "request", CP_WINANSI);
295 /* XTYP_REQUEST, fRelease = TRUE */
296 res = 0xdeadbeef;
297 DdeGetLastError(client_pid);
298 hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
299 ret = DdeGetLastError(client_pid);
300 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
301 ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %08x\n", res);
302 if (hdata == NULL)
303 ok(FALSE, "hdata is NULL\n");
304 else
306 str = (LPSTR)DdeAccessData(hdata, &size);
307 ok(!lstrcmpA(str, "requested data\r\n"), "Expected 'requested data\\r\\n', got %s\n", str);
308 ok(size == 19, "Expected 19, got %d\n", size);
310 ret = DdeUnaccessData(hdata);
311 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
314 /* XTYP_REQUEST, fAckReq = TRUE */
315 res = 0xdeadbeef;
316 DdeGetLastError(client_pid);
317 hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
318 ret = DdeGetLastError(client_pid);
319 ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
320 todo_wine
321 ok(ret == DMLERR_MEMORY_ERROR, "Expected DMLERR_MEMORY_ERROR, got %d\n", ret);
322 if (hdata == NULL)
323 ok(FALSE, "hdata is NULL\n");
324 else
326 str = (LPSTR)DdeAccessData(hdata, &size);
327 ok(!lstrcmpA(str, "requested data\r\n"), "Expected 'requested data\\r\\n', got %s\n", str);
328 ok(size == 19, "Expected 19, got %d\n", size);
330 ret = DdeUnaccessData(hdata);
331 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
334 /* XTYP_REQUEST, all params normal */
335 res = 0xdeadbeef;
336 DdeGetLastError(client_pid);
337 hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
338 ret = DdeGetLastError(client_pid);
339 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
340 ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
341 if (hdata == NULL)
342 ok(FALSE, "hdata is NULL\n");
343 else
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);
349 ret = DdeUnaccessData(hdata);
350 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
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);
362 DdeFreeStringHandle(client_pid, item);
364 item = DdeCreateStringHandleA(client_pid, "poker", CP_WINANSI);
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");
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);
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);
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);
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);
407 DdeFreeDataHandle(hdata);
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");
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);
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 ok(ret == DMLERR_MEMORY_ERROR, "Expected DMLERR_MEMORY_ERROR, got %d\n", ret);
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 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
441 DdeFreeStringHandle(client_pid, topic);
442 DdeFreeDataHandle(hdata);
444 item = DdeCreateStringHandleA(client_pid, "executed", CP_WINANSI);
446 /* verify the execute */
447 res = 0xdeadbeef;
448 DdeGetLastError(client_pid);
449 hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
450 ret = DdeGetLastError(client_pid);
451 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
452 ok(res == DDE_FNOTPROCESSED, "Expected DDE_FNOTPROCESSED, got %d\n", res);
453 if (hdata == NULL)
454 ok(FALSE, "hdata is NULL\n");
455 else
457 str = (LPSTR)DdeAccessData(hdata, &size);
458 ok(!lstrcmpA(str, "command executed\r\n"), "Expected 'command executed\\r\\n', got %s\n", str);
459 ok(size == 21, "Expected 21, got %d\n", size);
461 ret = DdeUnaccessData(hdata);
462 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
465 /* invalid transactions */
466 res = 0xdeadbeef;
467 DdeGetLastError(client_pid);
468 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_ADVREQ, default_timeout, &res);
469 ret = DdeGetLastError(client_pid);
470 ok(op == NULL, "Expected NULL, got %p\n", op);
471 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
472 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
474 res = 0xdeadbeef;
475 DdeGetLastError(client_pid);
476 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_CONNECT, default_timeout, &res);
477 ret = DdeGetLastError(client_pid);
478 ok(op == NULL, "Expected NULL, got %p\n", op);
479 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
480 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
482 res = 0xdeadbeef;
483 DdeGetLastError(client_pid);
484 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_CONNECT_CONFIRM, default_timeout, &res);
485 ret = DdeGetLastError(client_pid);
486 ok(op == NULL, "Expected NULL, got %p\n", op);
487 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
488 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
490 res = 0xdeadbeef;
491 DdeGetLastError(client_pid);
492 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_DISCONNECT, default_timeout, &res);
493 ret = DdeGetLastError(client_pid);
494 ok(op == NULL, "Expected NULL, got %p\n", op);
495 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
496 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
498 res = 0xdeadbeef;
499 DdeGetLastError(client_pid);
500 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_ERROR, default_timeout, &res);
501 ret = DdeGetLastError(client_pid);
502 ok(op == NULL, "Expected NULL, got %p\n", op);
503 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
504 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
506 res = 0xdeadbeef;
507 DdeGetLastError(client_pid);
508 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_MONITOR, default_timeout, &res);
509 ret = DdeGetLastError(client_pid);
510 ok(op == NULL, "Expected NULL, got %p\n", op);
511 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
512 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
514 res = 0xdeadbeef;
515 DdeGetLastError(client_pid);
516 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REGISTER, default_timeout, &res);
517 ret = DdeGetLastError(client_pid);
518 ok(op == NULL, "Expected NULL, got %p\n", op);
519 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
520 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
522 res = 0xdeadbeef;
523 DdeGetLastError(client_pid);
524 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_UNREGISTER, default_timeout, &res);
525 ret = DdeGetLastError(client_pid);
526 ok(op == NULL, "Expected NULL, got %p\n", op);
527 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
528 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
530 res = 0xdeadbeef;
531 DdeGetLastError(client_pid);
532 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_WILDCONNECT, default_timeout, &res);
533 ret = DdeGetLastError(client_pid);
534 ok(op == NULL, "Expected NULL, got %p\n", op);
535 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
536 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
538 res = 0xdeadbeef;
539 DdeGetLastError(client_pid);
540 op = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_XACT_COMPLETE, default_timeout, &res);
541 ret = DdeGetLastError(client_pid);
542 ok(op == NULL, "Expected NULL, got %p\n", op);
543 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", res);
544 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
546 DdeFreeStringHandle(client_pid, item);
548 ret = DdeDisconnect(conversation);
549 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
551 ret = DdeUninitialize(client_pid);
552 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
555 static DWORD server_pid;
557 static HDDEDATA CALLBACK server_ddeml_callback(UINT uType, UINT uFmt, HCONV hconv,
558 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
559 ULONG_PTR dwData1, ULONG_PTR dwData2)
561 char str[MAX_PATH], *ptr;
562 HDDEDATA ret;
563 DWORD size;
565 static int msg_index = 0;
566 static HCONV conversation = 0;
568 msg_index++;
570 switch (uType)
572 case XTYP_REGISTER:
574 ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
575 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
576 ok(hconv == 0, "Expected 0, got %p\n", hconv);
577 ok(hdata == 0, "Expected 0, got %p\n", hdata);
578 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
579 ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
581 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
582 ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
583 ok(size == 13, "Expected 13, got %d\n", size);
585 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
586 ok(!strncmp(str, "TestDDEServer(", 14), "Expected TestDDEServer(, got %s\n", str);
587 ok(str[size - 1] == ')', "Expected ')', got %c\n", str[size - 1]);
588 ok(size == 25, "Expected 25, got %d\n", size);
590 return (HDDEDATA)TRUE;
593 case XTYP_CONNECT:
595 ok(msg_index == 2, "Expected 2, got %d\n", msg_index);
596 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
597 ok(hconv == 0, "Expected 0, got %p\n", hconv);
598 ok(hdata == 0, "Expected 0, got %p\n", hdata);
599 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
600 ok(dwData2 == FALSE, "Expected FALSE, got %08lx\n", dwData2);
602 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
603 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
604 ok(size == 12, "Expected 12, got %d\n", size);
606 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
607 ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
608 ok(size == 13, "Expected 13, got %d\n", size);
610 return (HDDEDATA)TRUE;
613 case XTYP_CONNECT_CONFIRM:
615 conversation = hconv;
617 ok(msg_index == 3, "Expected 3, got %d\n", msg_index);
618 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
619 ok(hconv != NULL, "Expected non-NULL hconv\n");
620 ok(hdata == 0, "Expected 0, got %p\n", hdata);
621 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
622 ok(dwData2 == FALSE, "Expected FALSE, got %08lx\n", dwData2);
624 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
625 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
626 ok(size == 12, "Expected 12, got %d\n", size);
628 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
629 ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
630 ok(size == 13, "Expected 13, got %d\n", size);
632 return (HDDEDATA)TRUE;
635 case XTYP_REQUEST:
637 ok(msg_index == 4 || msg_index == 5 || msg_index == 6,
638 "Expected 4, 5 or 6, got %d\n", msg_index);
639 ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
640 ok(hdata == 0, "Expected 0, got %p\n", hdata);
641 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
642 ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
644 if (msg_index == 4)
645 ok(uFmt == 0xbeef, "Expected 0xbeef, got %08x\n", uFmt);
646 else
647 ok(uFmt == CF_TEXT, "Expected CF_TEXT, got %08x\n", uFmt);
649 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
650 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
651 ok(size == 12, "Expected 12, got %d\n", size);
653 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
655 if (msg_index == 5)
658 ok(!lstrcmpA(str, ""), "Expected empty string, got %s\n", str);
659 ok(size == 1, "Expected 1, got %d\n", size);
662 else if (msg_index == 6)
664 ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
665 ok(size == 7, "Expected 7, got %d\n", size);
668 if (msg_index == 6)
670 lstrcpyA(str, "requested data\r\n");
671 return DdeCreateDataHandle(server_pid, (LPBYTE)str, lstrlenA(str) + 1,
672 0, hsz2, CF_TEXT, 0);
675 return NULL;
678 case XTYP_POKE:
680 ok(msg_index == 7 || msg_index == 8, "Expected 7 or 8, got %d\n", msg_index);
681 ok(uFmt == CF_TEXT, "Expected CF_TEXT, got %d\n", uFmt);
682 ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
683 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
684 ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
686 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
687 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
688 ok(size == 12, "Expected 12, got %d\n", size);
690 ptr = (LPSTR)DdeAccessData(hdata, &size);
691 ok(!lstrcmpA(ptr, "poke data\r\n"), "Expected 'poke data\\r\\n', got %s\n", ptr);
692 ok(size == 12, "Expected 12, got %d\n", size);
693 DdeUnaccessData(hdata);
695 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
696 if (msg_index == 7)
699 ok(!lstrcmpA(str, ""), "Expected empty string, got %s\n", str);
700 ok(size == 1, "Expected 1, got %d\n", size);
703 else
705 ok(!lstrcmpA(str, "poke"), "Expected poke, got %s\n", str);
706 ok(size == 4, "Expected 4, got %d\n", size);
709 return (HDDEDATA)DDE_FACK;
712 case XTYP_EXECUTE:
714 ok(msg_index == 9 || msg_index == 10, "Expected 9 or 10, got %d\n", msg_index);
715 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
716 ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
717 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
718 ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
719 ok(hsz2 == 0, "Expected 0, got %p\n", hsz2);
721 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
722 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
723 ok(size == 12, "Expected 12, got %d\n", size);
725 ptr = (LPSTR)DdeAccessData(hdata, &size);
727 if (msg_index == 9)
729 ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected '[Command(Var)]', got %s\n", ptr);
730 ok(size == 15, "Expected 15, got %d\n", size);
731 ret = (HDDEDATA)DDE_FACK;
733 else
735 ok(!lstrcmpA(ptr, "[BadCommand(Var)]"), "Expected '[BadCommand(Var)]', got %s\n", ptr);
736 ok(size == 18, "Expected 18, got %d\n", size);
737 ret = (HDDEDATA)DDE_FNOTPROCESSED;
740 DdeUnaccessData(hdata);
742 return ret;
745 case XTYP_DISCONNECT:
747 ok(msg_index == 11, "Expected 11, got %d\n", msg_index);
748 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
749 ok(hconv == conversation, "Expected conversation handle, got %p\n", hconv);
750 ok(dwData1 == 0, "Expected 0, got %08lx\n", dwData1);
751 ok(dwData2 == 0, "Expected 0, got %08lx\n", dwData2);
752 ok(hsz1 == 0, "Expected 0, got %p\n", hsz2);
753 ok(hsz2 == 0, "Expected 0, got %p\n", hsz2);
755 return 0;
758 default:
759 ok(FALSE, "Unhandled msg: %08x\n", uType);
762 return 0;
765 static void test_ddeml_server(HANDLE hproc)
767 MSG msg;
768 UINT res;
769 BOOL ret;
770 HSZ server;
771 HDDEDATA hdata;
773 /* set up DDE server */
774 server_pid = 0;
775 res = DdeInitialize(&server_pid, server_ddeml_callback, APPCLASS_STANDARD, 0);
776 ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
778 server = DdeCreateStringHandle(server_pid, "TestDDEServer", CP_WINANSI);
779 ok(server != NULL, "Expected non-NULL string handle\n");
781 hdata = DdeNameService(server_pid, server, 0, DNS_REGISTER);
782 ok(hdata == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", hdata);
784 while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
786 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
788 ret = DdeUninitialize(server_pid);
789 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
790 GetExitCodeProcess( hproc, &res );
791 ok( !res, "client failed with %u error(s)\n", res );
794 static HWND client_hwnd, server_hwnd;
795 static ATOM server, topic, item;
796 static HGLOBAL execute_hglobal;
798 static LRESULT WINAPI dde_msg_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
800 char str[MAX_PATH];
801 UINT_PTR lo, hi;
802 DDEDATA *data;
803 DDEACK *ack;
804 DWORD size;
805 LPSTR ptr;
807 static int msg_index = 0;
809 if (msg < WM_DDE_FIRST || msg > WM_DDE_LAST)
810 return DefWindowProcA(hwnd, msg, wparam, lparam);
812 msg_index++;
814 switch (msg)
816 case WM_DDE_INITIATE:
818 ok(msg_index == 1, "Expected 1, got %d\n", msg_index);
819 ok(wparam == (WPARAM)client_hwnd, "Expected client hwnd, got %08lx\n", wparam);
821 size = GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
822 ok(LOWORD(lparam) == server, "Expected server atom, got %08x\n", LOWORD(lparam));
823 ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
824 ok(size == 13, "Expected 13, got %d\n", size);
826 size = GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
827 ok(HIWORD(lparam) == topic, "Expected topic atom, got %08x\n", HIWORD(lparam));
828 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
829 ok(size == 12, "Expected 12, got %d\n", size);
831 break;
834 case WM_DDE_ACK:
836 ok((msg_index >= 2 && msg_index <= 4) || (msg_index >= 6 && msg_index <= 10),
837 "Expected 2, 3, 4, 6, 7, 8, 9 or 10, got %d\n", msg_index);
839 if (msg_index == 2)
841 server_hwnd = (HWND)wparam;
842 ok(wparam != 0, "Expected non-NULL wparam, got %08lx\n", wparam);
844 size = GlobalGetAtomNameA(LOWORD(lparam), str, MAX_PATH);
845 ok(LOWORD(lparam) == server, "Expected server atom, got %08x\n", LOWORD(lparam));
846 ok(!lstrcmpA(str, "TestDDEServer"), "Expected TestDDEServer, got %s\n", str);
847 ok(size == 13, "Expected 13, got %d\n", size);
849 size = GlobalGetAtomNameA(HIWORD(lparam), str, MAX_PATH);
850 ok(HIWORD(lparam) == topic, "Expected topic atom, got %08x\n", HIWORD(lparam));
851 ok(!lstrcmpA(str, "TestDDETopic"), "Expected TestDDETopic, got %s\n", str);
852 ok(size == 12, "Expected 12, got %d\n", size);
854 else if (msg_index == 9 || msg_index == 10)
856 ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
858 UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
860 ack = (DDEACK *)&lo;
861 ok(ack->bAppReturnCode == 0, "Expected 0, got %d\n", ack->bAppReturnCode);
862 ok(ack->reserved == 0, "Expected 0, got %d\n", ack->reserved);
863 ok(ack->fBusy == FALSE, "Expected FALSE, got %d\n", ack->fBusy);
865 ok(hi == (UINT_PTR)execute_hglobal, "Execpted execute hglobal, got %08lx\n", hi);
866 ptr = GlobalLock((HGLOBAL)hi);
868 if (msg_index == 9)
870 ok(ack->fAck == TRUE, "Expected TRUE, got %d\n", ack->fAck);
871 ok(!lstrcmpA(ptr, "[Command(Var)]"), "Expected '[Command(Var)]', got %s\n", ptr);
873 else
875 ok(ack->fAck == FALSE, "Expected FALSE, got %d\n", ack->fAck);
876 ok(!lstrcmpA(ptr, "[BadCommand(Var)]"), "Expected '[BadCommand(Var)]', got %s\n", ptr);
879 GlobalUnlock((HGLOBAL)hi);
881 else
883 ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
885 UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
887 ack = (DDEACK *)&lo;
888 ok(ack->bAppReturnCode == 0, "Expected 0, got %d\n", ack->bAppReturnCode);
889 ok(ack->reserved == 0, "Expected 0, got %d\n", ack->reserved);
890 ok(ack->fBusy == FALSE, "Expected FALSE, got %d\n", ack->fBusy);
892 if (msg_index >= 7)
893 ok(ack->fAck == TRUE, "Expected TRUE, got %d\n", ack->fAck);
894 else
896 if (msg_index == 6) todo_wine
897 ok(ack->fAck == FALSE, "Expected FALSE, got %d\n", ack->fAck);
900 size = GlobalGetAtomNameA(hi, str, MAX_PATH);
901 if (msg_index == 3)
903 ok(hi == item, "Expected item atom, got %08lx\n", hi);
904 ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
905 ok(size == 7, "Expected 7, got %d\n", size);
907 else if (msg_index == 4 || msg_index == 7)
909 ok(hi == 0, "Expected 0, got %08lx\n", hi);
910 ok(size == 0, "Expected empty string, got %d\n", size);
912 else
914 ok(hi == item, "Expected item atom, got %08lx\n", hi);
915 if (msg_index == 6) todo_wine
917 ok(!lstrcmpA(str, "poke"), "Expected poke, got %s\n", str);
918 ok(size == 4, "Expected 4, got %d\n", size);
923 break;
926 case WM_DDE_DATA:
928 ok(msg_index == 5, "Expected 5, got %d\n", msg_index);
929 ok(wparam == (WPARAM)server_hwnd, "Expected server hwnd, got %08lx\n", wparam);
931 UnpackDDElParam(WM_DDE_DATA, lparam, &lo, &hi);
933 data = GlobalLock((HGLOBAL)lo);
934 ok(data->unused == 0, "Expected 0, got %d\n", data->unused);
935 ok(data->fResponse == TRUE, "Expected TRUE, got %d\n", data->fResponse);
936 todo_wine
938 ok(data->fRelease == TRUE, "Expected TRUE, got %d\n", data->fRelease);
940 ok(data->fAckReq == 0, "Expected 0, got %d\n", data->fAckReq);
941 ok(data->cfFormat == CF_TEXT, "Expected CF_TEXT, got %d\n", data->cfFormat);
942 ok(!lstrcmpA((LPSTR)data->Value, "requested data\r\n"),
943 "Expeted 'requested data\\r\\n', got %s\n", data->Value);
944 GlobalUnlock((HGLOBAL)lo);
946 size = GlobalGetAtomNameA(hi, str, MAX_PATH);
947 ok(hi == item, "Expected item atom, got %08x\n", HIWORD(lparam));
948 ok(!lstrcmpA(str, "request"), "Expected request, got %s\n", str);
949 ok(size == 7, "Expected 7, got %d\n", size);
951 GlobalFree((HGLOBAL)lo);
952 GlobalDeleteAtom(hi);
954 break;
957 default:
958 ok(FALSE, "Unhandled msg: %08x\n", msg);
961 return DefWindowProcA(hwnd, msg, wparam, lparam);
964 static HGLOBAL create_poke()
966 HGLOBAL hglobal;
967 DDEPOKE *poke;
968 DWORD size;
970 size = FIELD_OFFSET(DDEPOKE, Value[sizeof("poke data\r\n")]);
971 hglobal = GlobalAlloc(GMEM_DDESHARE, size);
972 ok(hglobal != 0, "Expected non-NULL hglobal\n");
974 poke = GlobalLock(hglobal);
975 poke->unused = 0;
976 poke->fRelease = TRUE;
977 poke->fReserved = TRUE;
978 poke->cfFormat = CF_TEXT;
979 lstrcpyA((LPSTR)poke->Value, "poke data\r\n");
980 GlobalUnlock(hglobal);
982 return hglobal;
985 static HGLOBAL create_execute(LPCSTR command)
987 HGLOBAL hglobal;
988 LPSTR ptr;
990 hglobal = GlobalAlloc(GMEM_DDESHARE, lstrlenA(command) + 1);
991 ok(hglobal != 0, "Expected non-NULL hglobal\n");
993 ptr = GlobalLock(hglobal);
994 lstrcpyA(ptr, command);
995 GlobalUnlock(hglobal);
997 return hglobal;
1000 static void test_msg_client()
1002 HGLOBAL hglobal;
1003 LPARAM lparam;
1005 create_dde_window(&client_hwnd, "dde_client", dde_msg_client_wndproc);
1007 server = GlobalAddAtomA("TestDDEServer");
1008 ok(server != 0, "Expected non-NULL server\n");
1010 topic = GlobalAddAtomA("TestDDETopic");
1011 ok(topic != 0, "Expected non-NULL topic\n");
1013 SendMessageA(HWND_BROADCAST, WM_DDE_INITIATE, (WPARAM)client_hwnd, MAKELONG(server, topic));
1015 GlobalDeleteAtom(server);
1016 GlobalDeleteAtom(topic);
1018 flush_events();
1020 item = GlobalAddAtom("request");
1021 ok(item != 0, "Expected non-NULL item\n");
1023 /* WM_DDE_REQUEST, bad clipboard format */
1024 lparam = PackDDElParam(WM_DDE_REQUEST, 0xdeadbeef, item);
1025 PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1027 flush_events();
1029 /* WM_DDE_REQUEST, no item */
1030 lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, 0);
1031 PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1033 flush_events();
1035 /* WM_DDE_REQUEST, no client hwnd */
1036 lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, item);
1037 PostMessageA(server_hwnd, WM_DDE_REQUEST, 0, lparam);
1039 flush_events();
1041 /* WM_DDE_REQUEST, correct params */
1042 lparam = PackDDElParam(WM_DDE_REQUEST, CF_TEXT, item);
1043 PostMessageA(server_hwnd, WM_DDE_REQUEST, (WPARAM)client_hwnd, lparam);
1045 flush_events();
1047 GlobalDeleteAtom(item);
1048 item = GlobalAddAtomA("poke");
1049 ok(item != 0, "Expected non-NULL item\n");
1051 hglobal = create_poke();
1053 /* WM_DDE_POKE, no ddepoke */
1054 lparam = PackDDElParam(WM_DDE_POKE, 0, item);
1055 /* win9x returns 0 here and crashes in PostMessageA */
1056 if (lparam) {
1057 PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1058 flush_events();
1060 else
1061 win_skip("no lparam for WM_DDE_POKE\n");
1064 /* WM_DDE_POKE, no item */
1065 lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, 0);
1066 PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1068 flush_events();
1070 hglobal = create_poke();
1072 /* WM_DDE_POKE, no client hwnd */
1073 lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1074 PostMessageA(server_hwnd, WM_DDE_POKE, 0, lparam);
1076 flush_events();
1078 /* WM_DDE_POKE, all params correct */
1079 lparam = PackDDElParam(WM_DDE_POKE, (UINT_PTR)hglobal, item);
1080 PostMessageA(server_hwnd, WM_DDE_POKE, (WPARAM)client_hwnd, lparam);
1082 flush_events();
1084 execute_hglobal = create_execute("[Command(Var)]");
1086 /* WM_DDE_EXECUTE, no lparam */
1087 PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, 0);
1089 flush_events();
1091 /* WM_DDE_EXECUTE, no hglobal */
1092 lparam = PackDDElParam(WM_DDE_EXECUTE, 0, 0);
1093 PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1095 flush_events();
1097 /* WM_DDE_EXECUTE, no client hwnd */
1098 lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1099 PostMessageA(server_hwnd, WM_DDE_EXECUTE, 0, lparam);
1101 flush_events();
1103 /* WM_DDE_EXECUTE, all params correct */
1104 lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1105 PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1107 flush_events();
1109 GlobalFree(execute_hglobal);
1110 execute_hglobal = create_execute("[BadCommand(Var)]");
1112 /* WM_DDE_EXECUTE that will get rejected */
1113 lparam = PackDDElParam(WM_DDE_EXECUTE, 0, (UINT_PTR)execute_hglobal);
1114 PostMessageA(server_hwnd, WM_DDE_EXECUTE, (WPARAM)client_hwnd, lparam);
1116 flush_events();
1118 destroy_dde_window(&client_hwnd, "dde_client");
1121 static LRESULT WINAPI hook_dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1123 UINT_PTR lo, hi;
1125 trace("hook_dde_client_wndproc: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1127 switch (msg)
1129 case WM_DDE_ACK:
1130 UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1131 trace("WM_DDE_ACK: status %04lx hglobal %p\n", lo, (HGLOBAL)hi);
1132 break;
1134 default:
1135 break;
1137 return CallWindowProcA(old_dde_client_wndproc, hwnd, msg, wparam, lparam);
1140 static LRESULT WINAPI dde_server_wndprocW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1142 trace("dde_server_wndprocW: %p %04x %08lx %08lx\n", hwnd, msg, wparam, lparam);
1144 switch (msg)
1146 case WM_DDE_INITIATE:
1148 ATOM aService = GlobalAddAtomW(TEST_DDE_SERVICE);
1150 trace("server: got WM_DDE_INITIATE from %p with %08lx\n", (HWND)wparam, lparam);
1152 if (LOWORD(lparam) == aService)
1154 ok(!IsWindowUnicode((HWND)wparam), "client should be an ANSI window\n");
1155 old_dde_client_wndproc = (WNDPROC)SetWindowLongPtrA((HWND)wparam, GWLP_WNDPROC, (ULONG_PTR)hook_dde_client_wndproc);
1156 trace("server: sending WM_DDE_ACK to %p\n", (HWND)wparam);
1157 SendMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, MAKELPARAM(aService, 0));
1159 else
1160 GlobalDeleteAtom(aService);
1161 return 0;
1164 case WM_DDE_EXECUTE:
1166 DDEACK ack;
1167 WORD status;
1168 LPCSTR cmd;
1169 UINT_PTR lo, hi;
1171 trace("server: got WM_DDE_EXECUTE from %p with %08lx\n", (HWND)wparam, lparam);
1173 UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1174 trace("%08lx => lo %04lx hi %04lx\n", lparam, lo, hi);
1176 ack.bAppReturnCode = 0;
1177 ack.reserved = 0;
1178 ack.fBusy = 0;
1180 cmd = GlobalLock((HGLOBAL)hi);
1181 if (!cmd || (lstrcmpA(cmd, exec_cmdA) && lstrcmpW((LPCWSTR)cmd, exec_cmdW)))
1183 trace("ignoring unknown WM_DDE_EXECUTE command\n");
1184 /* We have to send a negative acknowledge even if we don't
1185 * accept the command, otherwise Windows goes mad and next time
1186 * we send an acknowledge DDEML drops the connection.
1187 * Not sure how to call it: a bug or a feature.
1189 ack.fAck = 0;
1191 else
1192 ack.fAck = 1;
1193 GlobalUnlock((HGLOBAL)hi);
1195 trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1197 status = *((WORD *)&ack);
1198 lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, status, hi);
1200 PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1201 return 0;
1204 case WM_DDE_TERMINATE:
1206 DDEACK ack;
1207 WORD status;
1209 trace("server: got WM_DDE_TERMINATE from %p with %08lx\n", (HWND)wparam, lparam);
1211 ack.bAppReturnCode = 0;
1212 ack.reserved = 0;
1213 ack.fBusy = 0;
1214 ack.fAck = 1;
1216 trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
1218 status = *((WORD *)&ack);
1219 lparam = PackDDElParam(WM_DDE_ACK, status, 0);
1221 PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
1222 return 0;
1225 default:
1226 break;
1229 return DefWindowProcW(hwnd, msg, wparam, lparam);
1232 static LRESULT WINAPI dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
1234 return DefWindowProcA(hwnd, msg, wparam, lparam);
1237 static BOOL create_dde_windows(HWND *client, HWND *server)
1239 WNDCLASSA wcA;
1240 WNDCLASSW wcW;
1241 static const WCHAR server_class_name[] = {'d','d','e','_','s','e','r','v','e','r','_','w','i','n','d','o','w',0};
1242 static const char client_class_name[] = "dde_client_window";
1244 memset(&wcW, 0, sizeof(wcW));
1245 wcW.lpfnWndProc = dde_server_wndprocW;
1246 wcW.lpszClassName = server_class_name;
1247 wcW.hInstance = GetModuleHandleA(0);
1248 if (!RegisterClassW(&wcW)) return FALSE;
1250 memset(&wcA, 0, sizeof(wcA));
1251 wcA.lpfnWndProc = dde_client_wndproc;
1252 wcA.lpszClassName = client_class_name;
1253 wcA.hInstance = GetModuleHandleA(0);
1254 assert(RegisterClassA(&wcA));
1256 *server = CreateWindowExW(0, server_class_name, NULL,
1257 WS_POPUP,
1258 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1259 GetDesktopWindow(), 0,
1260 GetModuleHandleA(0), NULL);
1261 assert(*server);
1263 *client = CreateWindowExA(0, client_class_name, NULL,
1264 WS_POPUP,
1265 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
1266 GetDesktopWindow(), 0,
1267 GetModuleHandleA(0), NULL);
1268 assert(*client);
1270 trace("server hwnd %p, client hwnd %p\n", *server, *client);
1272 ok(IsWindowUnicode(*server), "server has to be a unicode window\n");
1273 ok(!IsWindowUnicode(*client), "client has to be an ANSI window\n");
1275 return TRUE;
1278 static HDDEDATA CALLBACK client_dde_callback(UINT uType, UINT uFmt, HCONV hconv,
1279 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
1280 ULONG_PTR dwData1, ULONG_PTR dwData2)
1282 static const char * const cmd_type[15] = {
1283 "XTYP_ERROR", "XTYP_ADVDATA", "XTYP_ADVREQ", "XTYP_ADVSTART",
1284 "XTYP_ADVSTOP", "XTYP_EXECUTE", "XTYP_CONNECT", "XTYP_CONNECT_CONFIRM",
1285 "XTYP_XACT_COMPLETE", "XTYP_POKE", "XTYP_REGISTER", "XTYP_REQUEST",
1286 "XTYP_DISCONNECT", "XTYP_UNREGISTER", "XTYP_WILDCONNECT" };
1287 UINT type;
1288 const char *cmd_name;
1290 type = (uType & XTYP_MASK) >> XTYP_SHIFT;
1291 cmd_name = (type <= 14) ? cmd_type[type] : "unknown";
1293 trace("client_dde_callback: %04x (%s) %d %p %p %p %p %08lx %08lx\n",
1294 uType, cmd_name, uFmt, hconv, hsz1, hsz2, hdata, dwData1, dwData2);
1295 return 0;
1298 static void test_dde_aw_transaction(void)
1300 HSZ hsz_server;
1301 DWORD dde_inst, ret, err;
1302 HCONV hconv;
1303 HWND hwnd_client, hwnd_server;
1304 CONVINFO info;
1305 HDDEDATA hdata;
1306 static char test_cmd[] = "test dde command";
1308 /* server: unicode, client: ansi */
1309 if (!create_dde_windows(&hwnd_client, &hwnd_server)) return;
1311 dde_inst = 0;
1312 ret = DdeInitializeA(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
1313 ok(ret == DMLERR_NO_ERROR, "DdeInitializeA failed with error %04x (%x)\n",
1314 ret, DdeGetLastError(dde_inst));
1316 hsz_server = DdeCreateStringHandleW(dde_inst, TEST_DDE_SERVICE, CP_WINUNICODE);
1318 hconv = DdeConnect(dde_inst, hsz_server, 0, NULL);
1319 ok(hconv != 0, "DdeConnect error %x\n", DdeGetLastError(dde_inst));
1320 err = DdeGetLastError(dde_inst);
1321 ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1323 info.cb = sizeof(info);
1324 ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1325 ok(ret, "wrong info size %d, DdeQueryConvInfo error %x\n", ret, DdeGetLastError(dde_inst));
1326 /* should be CP_WINANSI since we used DdeInitializeA */
1327 ok(info.ConvCtxt.iCodePage == CP_WINANSI, "wrong iCodePage %d\n", info.ConvCtxt.iCodePage);
1328 ok(!info.hConvPartner, "unexpected info.hConvPartner: %p\n", info.hConvPartner);
1329 todo_wine {
1330 ok((info.wStatus & DDE_FACK), "unexpected info.wStatus: %04x\n", info.wStatus);
1332 ok((info.wStatus & (ST_CONNECTED | ST_CLIENT)) == (ST_CONNECTED | ST_CLIENT), "unexpected info.wStatus: %04x\n", info.wStatus);
1333 ok(info.wConvst == XST_CONNECTED, "unexpected info.wConvst: %04x\n", info.wConvst);
1334 ok(info.wType == 0, "unexpected info.wType: %04x\n", info.wType);
1336 trace("hwnd %p, hwndPartner %p\n", info.hwnd, info.hwndPartner);
1338 trace("sending test client transaction command\n");
1339 ret = 0xdeadbeef;
1340 hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1, hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
1341 ok(!hdata, "DdeClientTransaction succeeded\n");
1342 ok(ret == DDE_FNOTPROCESSED, "wrong status code %04x\n", ret);
1343 err = DdeGetLastError(dde_inst);
1344 ok(err == DMLERR_NOTPROCESSED, "wrong dde error %x\n", err);
1346 trace("sending ANSI client transaction command\n");
1347 ret = 0xdeadbeef;
1348 hdata = DdeClientTransaction((LPBYTE)exec_cmdA, lstrlenA(exec_cmdA) + 1, hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1349 ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1350 ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1352 err = DdeGetLastError(dde_inst);
1353 ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1355 trace("sending unicode client transaction command\n");
1356 ret = 0xdeadbeef;
1357 hdata = DdeClientTransaction((LPBYTE)exec_cmdW, (lstrlenW(exec_cmdW) + 1) * sizeof(WCHAR), hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
1358 ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
1359 ok(ret == DDE_FACK, "wrong status code %04x\n", ret);
1360 err = DdeGetLastError(dde_inst);
1361 ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
1363 ok(DdeDisconnect(hconv), "DdeDisconnect error %x\n", DdeGetLastError(dde_inst));
1365 info.cb = sizeof(info);
1366 ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
1367 ok(!ret, "DdeQueryConvInfo should fail\n");
1368 err = DdeGetLastError(dde_inst);
1369 todo_wine {
1370 ok(err == DMLERR_INVALIDPARAMETER, "wrong dde error %x\n", err);
1373 ok(DdeFreeStringHandle(dde_inst, hsz_server), "DdeFreeStringHandle error %x\n", DdeGetLastError(dde_inst));
1375 /* This call hangs on win2k SP4 and XP SP1.
1376 DdeUninitialize(dde_inst);*/
1378 DestroyWindow(hwnd_client);
1379 DestroyWindow(hwnd_server);
1382 static void test_initialisation(void)
1384 UINT ret;
1385 DWORD res;
1386 HDDEDATA hdata;
1387 HSZ server, topic, item;
1388 DWORD client_pid;
1389 HCONV conversation;
1391 /* Initialise without a valid server window. */
1392 client_pid = 0;
1393 ret = DdeInitializeA(&client_pid, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1394 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", ret);
1397 server = DdeCreateStringHandleA(client_pid, "TestDDEService", CP_WINANSI);
1398 topic = DdeCreateStringHandleA(client_pid, "TestDDETopic", CP_WINANSI);
1400 DdeGetLastError(client_pid);
1402 /* There is no server window so no conversation can be extracted */
1403 conversation = DdeConnect(client_pid, server, topic, NULL);
1404 ok(conversation == NULL, "Expected NULL conversation, %p\n", conversation);
1405 ret = DdeGetLastError(client_pid);
1406 ok(ret == DMLERR_NO_CONV_ESTABLISHED, "Expected DMLERR_NO_CONV_ESTABLISHED, got %d\n", ret);
1408 DdeFreeStringHandle(client_pid, server);
1410 item = DdeCreateStringHandleA(client_pid, "request", CP_WINANSI);
1412 /* There is no converstation so an invalild parameter results */
1413 res = 0xdeadbeef;
1414 DdeGetLastError(client_pid);
1415 hdata = DdeClientTransaction(NULL, 0, conversation, item, CF_TEXT, XTYP_REQUEST, default_timeout, &res);
1416 ret = DdeGetLastError(client_pid);
1417 todo_wine
1418 ok(ret == DMLERR_INVALIDPARAMETER, "Expected DMLERR_INVALIDPARAMETER, got %d\n", ret);
1419 ok(res == 0xdeadbeef, "Expected 0xdeadbeef, got %08x\n", res);
1421 DdeFreeStringHandle(client_pid, server);
1422 ret = DdeDisconnect(conversation);
1423 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1425 ret = DdeUninitialize(client_pid);
1426 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1429 static void test_DdeCreateStringHandleW(DWORD dde_inst, int codepage)
1431 static const WCHAR dde_string[] = {'D','D','E',' ','S','t','r','i','n','g',0};
1432 HSZ str_handle;
1433 WCHAR bufW[256];
1434 char buf[256];
1435 ATOM atom;
1436 int ret;
1438 str_handle = DdeCreateStringHandleW(dde_inst, dde_string, codepage);
1439 ok(str_handle != 0, "DdeCreateStringHandleW failed with error %08x\n",
1440 DdeGetLastError(dde_inst));
1442 ret = DdeQueryStringW(dde_inst, str_handle, NULL, 0, codepage);
1443 if (codepage == CP_WINANSI)
1444 ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1445 else
1446 ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1448 ret = DdeQueryStringW(dde_inst, str_handle, bufW, 256, codepage);
1449 if (codepage == CP_WINANSI)
1451 ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
1452 ok(!lstrcmpA("D", (LPCSTR)bufW), "DdeQueryStringW returned wrong string\n");
1454 else
1456 ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
1457 ok(!lstrcmpW(dde_string, bufW), "DdeQueryStringW returned wrong string\n");
1460 ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINANSI);
1461 if (codepage == CP_WINANSI)
1463 ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1464 ok(!lstrcmpA("D", buf), "DdeQueryStringW returned wrong string\n");
1466 else
1468 ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1469 ok(!lstrcmpA("DDE String", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1472 ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINUNICODE);
1473 if (codepage == CP_WINANSI)
1475 ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
1476 ok(!lstrcmpA("D", buf), "DdeQueryStringA returned wrong string %s\n", buf);
1478 else
1480 ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
1481 ok(!lstrcmpW(dde_string, (LPCWSTR)buf), "DdeQueryStringW returned wrong string\n");
1484 if (codepage == CP_WINANSI)
1486 atom = FindAtomA((LPSTR)dde_string);
1487 ok(atom != 0, "Expected a valid atom\n");
1489 SetLastError(0xdeadbeef);
1490 atom = GlobalFindAtomA((LPSTR)dde_string);
1491 ok(atom == 0, "Expected 0, got %d\n", atom);
1492 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1493 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1495 else
1497 atom = FindAtomW(dde_string);
1498 ok(atom != 0, "Expected a valid atom\n");
1500 SetLastError(0xdeadbeef);
1501 atom = GlobalFindAtomW(dde_string);
1502 ok(atom == 0, "Expected 0, got %d\n", atom);
1503 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
1504 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1507 ok(DdeFreeStringHandle(dde_inst, str_handle), "DdeFreeStringHandle failed\n");
1510 static void test_DdeCreateDataHandle(void)
1512 HDDEDATA hdata;
1513 DWORD dde_inst;
1514 DWORD size;
1515 UINT res, err;
1516 BOOL ret;
1517 HSZ item;
1518 LPBYTE ptr;
1520 dde_inst = 0;
1521 res = DdeInitializeA(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1522 ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1524 item = DdeCreateStringHandleA(dde_inst, "item", CP_WINANSI);
1525 ok(item != NULL, "Expected non-NULL hsz\n");
1527 if (0) {
1528 /* do not test with an invalid instance id: that crashes on win9x */
1529 hdata = DdeCreateDataHandle(0xdeadbeef, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1532 /* 0 instance id */
1533 DdeGetLastError(dde_inst);
1534 hdata = DdeCreateDataHandle(0, (LPBYTE)"data", MAX_PATH, 0, item, CF_TEXT, 0);
1535 err = DdeGetLastError(dde_inst);
1536 todo_wine
1538 ok(hdata == NULL, "Expected NULL, got %p\n", hdata);
1539 ok(err == DMLERR_INVALIDPARAMETER,
1540 "Expected DMLERR_INVALIDPARAMETER, got %d\n", err);
1543 /* NULL pSrc */
1544 DdeGetLastError(dde_inst);
1545 hdata = DdeCreateDataHandle(dde_inst, NULL, MAX_PATH, 0, item, CF_TEXT, 0);
1546 err = DdeGetLastError(dde_inst);
1547 ok(hdata != NULL, "Expected non-NULL hdata\n");
1548 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1550 ptr = GlobalLock(hdata);
1551 todo_wine
1553 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1556 ptr = DdeAccessData(hdata, &size);
1557 ok(ptr != NULL, "Expected non-NULL ptr\n");
1558 ok(size == 260, "Expected 260, got %d\n", size);
1560 ret = DdeUnaccessData(hdata);
1561 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1563 ret = DdeFreeDataHandle(hdata);
1564 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1566 /* cb is zero */
1567 DdeGetLastError(dde_inst);
1568 hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", 0, 0, item, CF_TEXT, 0);
1569 err = DdeGetLastError(dde_inst);
1570 ok(hdata != NULL, "Expected non-NULL hdata\n");
1571 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1573 ptr = GlobalLock(hdata);
1574 todo_wine
1576 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1579 ptr = DdeAccessData(hdata, &size);
1580 ok(ptr != NULL, "Expected non-NULL ptr\n");
1581 ok(size == 0, "Expected 0, got %d\n", size);
1583 ret = DdeUnaccessData(hdata);
1584 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1586 ret = DdeFreeDataHandle(hdata);
1587 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1589 /* cbOff is non-zero */
1590 DdeGetLastError(dde_inst);
1591 hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 2, item, CF_TEXT, 0);
1592 err = DdeGetLastError(dde_inst);
1593 ok(hdata != NULL, "Expected non-NULL hdata\n");
1594 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1596 ptr = GlobalLock(hdata);
1597 todo_wine
1599 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1602 ptr = DdeAccessData(hdata, &size);
1603 ok(ptr != NULL, "Expected non-NULL ptr\n");
1604 ok(size == 262, "Expected 262, got %d\n", size);
1605 todo_wine
1607 ok(lstrlenA((LPSTR)ptr) == 0, "Expected 0, got %d\n", lstrlenA((LPSTR)ptr));
1610 ret = DdeUnaccessData(hdata);
1611 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1613 ret = DdeFreeDataHandle(hdata);
1614 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1616 /* NULL item */
1617 DdeGetLastError(dde_inst);
1618 hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, 0, CF_TEXT, 0);
1619 err = DdeGetLastError(dde_inst);
1620 ok(hdata != NULL, "Expected non-NULL hdata\n");
1621 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1623 ptr = GlobalLock(hdata);
1624 todo_wine
1626 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1629 ptr = DdeAccessData(hdata, &size);
1630 ok(ptr != NULL, "Expected non-NULL ptr\n");
1631 ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1632 ok(size == 260, "Expected 260, got %d\n", size);
1634 ret = DdeUnaccessData(hdata);
1635 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1637 ret = DdeFreeDataHandle(hdata);
1638 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1640 /* NULL item */
1641 DdeGetLastError(dde_inst);
1642 hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, (HSZ)0xdeadbeef, CF_TEXT, 0);
1643 err = DdeGetLastError(dde_inst);
1644 ok(hdata != NULL, "Expected non-NULL hdata\n");
1645 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1647 ptr = GlobalLock(hdata);
1648 todo_wine
1650 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1653 ptr = DdeAccessData(hdata, &size);
1654 ok(ptr != NULL, "Expected non-NULL ptr\n");
1655 ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1656 ok(size == 260, "Expected 260, got %d\n", size);
1658 ret = DdeUnaccessData(hdata);
1659 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1661 ret = DdeFreeDataHandle(hdata);
1662 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1664 /* invalid clipboard format */
1665 DdeGetLastError(dde_inst);
1666 hdata = DdeCreateDataHandle(dde_inst, (LPBYTE)"data", MAX_PATH, 0, item, 0xdeadbeef, 0);
1667 err = DdeGetLastError(dde_inst);
1668 ok(hdata != NULL, "Expected non-NULL hdata\n");
1669 ok(err == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", err);
1671 ptr = GlobalLock(hdata);
1672 todo_wine
1674 ok(ptr == NULL, "Expected NULL, got %p\n", ptr);
1677 ptr = DdeAccessData(hdata, &size);
1678 ok(ptr != NULL, "Expected non-NULL ptr\n");
1679 ok(!lstrcmpA((LPSTR)ptr, "data"), "Expected data, got %s\n", ptr);
1680 ok(size == 260, "Expected 260, got %d\n", size);
1682 ret = DdeUnaccessData(hdata);
1683 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1685 ret = DdeFreeDataHandle(hdata);
1686 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1688 ret = DdeUninitialize(dde_inst);
1689 ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
1692 static void test_DdeCreateStringHandle(void)
1694 DWORD dde_inst, ret;
1696 dde_inst = 0xdeadbeef;
1697 SetLastError(0xdeadbeef);
1698 ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1699 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1701 win_skip("DdeInitializeW is unimplemented\n");
1702 return;
1705 ok(ret == DMLERR_INVALIDPARAMETER, "DdeInitializeW should fail, but got %04x instead\n", ret);
1706 ok(DdeGetLastError(dde_inst) == DMLERR_INVALIDPARAMETER, "expected DMLERR_INVALIDPARAMETER\n");
1708 dde_inst = 0;
1709 ret = DdeInitializeW(&dde_inst, client_ddeml_callback, APPCMD_CLIENTONLY, 0);
1710 ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04x (%08x)\n",
1711 ret, DdeGetLastError(dde_inst));
1713 test_DdeCreateStringHandleW(dde_inst, 0);
1714 test_DdeCreateStringHandleW(dde_inst, CP_WINUNICODE);
1715 test_DdeCreateStringHandleW(dde_inst, CP_WINANSI);
1717 ok(DdeUninitialize(dde_inst), "DdeUninitialize failed\n");
1720 static void test_FreeDDElParam(void)
1722 HGLOBAL val, hglobal;
1723 BOOL ret;
1725 ret = FreeDDElParam(WM_DDE_INITIATE, 0);
1726 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1728 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1729 ret = FreeDDElParam(WM_DDE_INITIATE, (LPARAM)hglobal);
1730 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1731 val = GlobalFree(hglobal);
1732 ok(val == NULL, "Expected NULL, got %p\n", val);
1734 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1735 ret = FreeDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal);
1736 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1737 val = GlobalFree(hglobal);
1738 ok(val == hglobal, "Expected hglobal, got %p\n", val);
1739 ok(GetLastError() == ERROR_INVALID_HANDLE,
1740 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1742 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1743 ret = FreeDDElParam(WM_DDE_UNADVISE, (LPARAM)hglobal);
1744 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1745 val = GlobalFree(hglobal);
1746 ok(val == NULL, "Expected NULL, got %p\n", val);
1748 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1749 ret = FreeDDElParam(WM_DDE_ACK, (LPARAM)hglobal);
1750 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1751 val = GlobalFree(hglobal);
1752 ok(val == hglobal, "Expected hglobal, got %p\n", val);
1753 ok(GetLastError() == ERROR_INVALID_HANDLE,
1754 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1756 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1757 ret = FreeDDElParam(WM_DDE_DATA, (LPARAM)hglobal);
1758 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1759 val = GlobalFree(hglobal);
1760 ok(val == hglobal, "Expected hglobal, got %p\n", val);
1761 ok(GetLastError() == ERROR_INVALID_HANDLE,
1762 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1764 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1765 ret = FreeDDElParam(WM_DDE_REQUEST, (LPARAM)hglobal);
1766 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1767 val = GlobalFree(hglobal);
1768 ok(val == NULL, "Expected NULL, got %p\n", val);
1770 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1771 ret = FreeDDElParam(WM_DDE_POKE, (LPARAM)hglobal);
1772 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1773 val = GlobalFree(hglobal);
1774 ok(val == hglobal, "Expected hglobal, got %p\n", val);
1775 ok(GetLastError() == ERROR_INVALID_HANDLE,
1776 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1778 hglobal = GlobalAlloc(GMEM_DDESHARE, 100);
1779 ret = FreeDDElParam(WM_DDE_EXECUTE, (LPARAM)hglobal);
1780 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1781 val = GlobalFree(hglobal);
1782 ok(val == NULL, "Expected NULL, got %p\n", val);
1785 static void test_PackDDElParam(void)
1787 UINT_PTR lo, hi, *ptr;
1788 HGLOBAL hglobal;
1789 LPARAM lparam;
1790 BOOL ret;
1792 lparam = PackDDElParam(WM_DDE_INITIATE, 0xcafe, 0xbeef);
1793 /* value gets sign-extended despite being an LPARAM */
1794 ok(lparam == (int)0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1795 ok(GlobalLock((HGLOBAL)lparam) == NULL,
1796 "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1797 ok(GetLastError() == ERROR_INVALID_HANDLE,
1798 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1800 lo = hi = 0;
1801 ret = UnpackDDElParam(WM_DDE_INITIATE, lparam, &lo, &hi);
1802 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1803 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1804 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1806 ret = FreeDDElParam(WM_DDE_INITIATE, lparam);
1807 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1809 lparam = PackDDElParam(WM_DDE_TERMINATE, 0xcafe, 0xbeef);
1810 ok(lparam == (int)0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1811 ok(GlobalLock((HGLOBAL)lparam) == NULL,
1812 "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1813 ok(GetLastError() == ERROR_INVALID_HANDLE,
1814 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1816 lo = hi = 0;
1817 ret = UnpackDDElParam(WM_DDE_TERMINATE, lparam, &lo, &hi);
1818 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1819 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1820 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1822 ret = FreeDDElParam(WM_DDE_TERMINATE, lparam);
1823 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1825 lparam = PackDDElParam(WM_DDE_ADVISE, 0xcafe, 0xbeef);
1826 /* win9x returns 0 here */
1827 if (lparam) {
1828 ptr = GlobalLock((HGLOBAL)lparam);
1829 ok(ptr != NULL, "Expected non-NULL ptr\n");
1830 ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1831 ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1833 ret = GlobalUnlock((HGLOBAL)lparam);
1834 ok(ret == 1, "Expected 1, got %d\n", ret);
1836 lo = hi = 0;
1837 ret = UnpackDDElParam(WM_DDE_ADVISE, lparam, &lo, &hi);
1838 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1839 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1840 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1842 else
1843 win_skip("no lparam for WM_DDE_ADVISE\n");
1845 ret = FreeDDElParam(WM_DDE_ADVISE, lparam);
1846 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1848 hglobal = GlobalFree((HGLOBAL)lparam);
1849 ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1850 ok(GetLastError() == ERROR_INVALID_HANDLE,
1851 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1853 lparam = PackDDElParam(WM_DDE_UNADVISE, 0xcafe, 0xbeef);
1854 ok(lparam == (int)0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1855 ok(GlobalLock((HGLOBAL)lparam) == NULL,
1856 "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1857 ok(GetLastError() == ERROR_INVALID_HANDLE,
1858 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1860 lo = hi = 0;
1861 ret = UnpackDDElParam(WM_DDE_UNADVISE, lparam, &lo, &hi);
1862 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1863 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1864 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1866 ret = FreeDDElParam(WM_DDE_UNADVISE, lparam);
1867 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1869 lparam = PackDDElParam(WM_DDE_ACK, 0xcafe, 0xbeef);
1870 /* win9x returns the input (0xbeef<<16 | 0xcafe) here */
1871 if (lparam != (int)0xbeefcafe) {
1872 ptr = GlobalLock((HGLOBAL)lparam);
1873 ok(ptr != NULL, "Expected non-NULL ptr\n");
1874 ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1875 ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1877 ret = GlobalUnlock((HGLOBAL)lparam);
1878 ok(ret == 1, "Expected 1, got %d\n", ret);
1880 lo = hi = 0;
1881 ret = UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
1882 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1883 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1884 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1886 ret = FreeDDElParam(WM_DDE_ACK, lparam);
1887 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1889 hglobal = GlobalFree((HGLOBAL)lparam);
1890 ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1891 ok(GetLastError() == ERROR_INVALID_HANDLE,
1892 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1894 else
1895 win_skip("got lparam 0x%lx for WM_DDE_ACK\n", lparam);
1897 lparam = PackDDElParam(WM_DDE_DATA, 0xcafe, 0xbeef);
1898 /* win9x returns 0 here */
1899 if (lparam) {
1900 ptr = GlobalLock((HGLOBAL)lparam);
1901 ok(ptr != NULL, "Expected non-NULL ptr\n");
1902 ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1903 ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1905 ret = GlobalUnlock((HGLOBAL)lparam);
1906 ok(ret == 1, "Expected 1, got %d\n", ret);
1908 lo = hi = 0;
1909 ret = UnpackDDElParam(WM_DDE_DATA, lparam, &lo, &hi);
1910 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1911 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1912 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1914 else
1915 win_skip("no lparam for WM_DDE_DATA\n");
1917 ret = FreeDDElParam(WM_DDE_DATA, lparam);
1918 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1920 hglobal = GlobalFree((HGLOBAL)lparam);
1921 ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1922 ok(GetLastError() == ERROR_INVALID_HANDLE,
1923 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1925 lparam = PackDDElParam(WM_DDE_REQUEST, 0xcafe, 0xbeef);
1926 ok(lparam == (int)0xbeefcafe, "Expected 0xbeefcafe, got %08lx\n", lparam);
1927 ok(GlobalLock((HGLOBAL)lparam) == NULL,
1928 "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1929 ok(GetLastError() == ERROR_INVALID_HANDLE,
1930 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1932 lo = hi = 0;
1933 ret = UnpackDDElParam(WM_DDE_REQUEST, lparam, &lo, &hi);
1934 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1935 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1936 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1938 ret = FreeDDElParam(WM_DDE_REQUEST, lparam);
1939 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1941 lparam = PackDDElParam(WM_DDE_POKE, 0xcafe, 0xbeef);
1942 /* win9x returns 0 here */
1943 if (lparam) {
1944 ptr = GlobalLock((HGLOBAL)lparam);
1945 ok(ptr != NULL, "Expected non-NULL ptr\n");
1946 ok(ptr[0] == 0xcafe, "Expected 0xcafe, got %08lx\n", ptr[0]);
1947 ok(ptr[1] == 0xbeef, "Expected 0xbeef, got %08lx\n", ptr[1]);
1949 ret = GlobalUnlock((HGLOBAL)lparam);
1950 ok(ret == 1, "Expected 1, got %d\n", ret);
1952 lo = hi = 0;
1953 ret = UnpackDDElParam(WM_DDE_POKE, lparam, &lo, &hi);
1954 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1955 ok(lo == 0xcafe, "Expected 0xcafe, got %08lx\n", lo);
1956 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1958 else
1959 win_skip("no lparam for WM_DDE_POKE\n");
1961 ret = FreeDDElParam(WM_DDE_POKE, lparam);
1962 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1964 hglobal = GlobalFree((HGLOBAL)lparam);
1965 ok(hglobal == (HGLOBAL)lparam, "Expected lparam, got %d\n", ret);
1966 ok(GetLastError() == ERROR_INVALID_HANDLE,
1967 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1969 lparam = PackDDElParam(WM_DDE_EXECUTE, 0xcafe, 0xbeef);
1970 ok(lparam == 0xbeef, "Expected 0xbeef, got %08lx\n", lparam);
1971 ok(GlobalLock((HGLOBAL)lparam) == NULL,
1972 "Expected NULL, got %p\n", GlobalLock((HGLOBAL)lparam));
1973 ok(GetLastError() == ERROR_INVALID_HANDLE,
1974 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1976 lo = hi = 0;
1977 ret = UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
1978 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1979 ok(lo == 0, "Expected 0, got %08lx\n", lo);
1980 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
1982 ret = FreeDDElParam(WM_DDE_EXECUTE, lparam);
1983 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1986 static void test_UnpackDDElParam(void)
1988 UINT_PTR lo, hi, *ptr;
1989 HGLOBAL hglobal;
1990 BOOL ret;
1992 /* NULL lParam */
1993 lo = 0xdead;
1994 hi = 0xbeef;
1995 ret = UnpackDDElParam(WM_DDE_INITIATE, 0, &lo, &hi);
1996 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1997 ok(lo == 0, "Expected 0, got %08lx\n", lo);
1998 ok(hi == 0, "Expected 0, got %08lx\n", hi);
2000 /* NULL lo */
2001 lo = 0xdead;
2002 hi = 0xbeef;
2003 ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, NULL, &hi);
2004 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2005 ok(lo == 0xdead, "Expected 0xdead, got %08lx\n", lo);
2006 ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2008 /* NULL hi */
2009 lo = 0xdead;
2010 hi = 0xbeef;
2011 ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, NULL);
2012 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2013 ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2014 ok(hi == 0xbeef, "Expected 0xbeef, got %08lx\n", hi);
2016 lo = 0xdead;
2017 hi = 0xbeef;
2018 ret = UnpackDDElParam(WM_DDE_INITIATE, 0xcafebabe, &lo, &hi);
2019 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2020 ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2021 ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2023 lo = 0xdead;
2024 hi = 0xbeef;
2025 ret = UnpackDDElParam(WM_DDE_TERMINATE, 0xcafebabe, &lo, &hi);
2026 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2027 ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2028 ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2030 lo = 0xdead;
2031 hi = 0xbeef;
2032 ret = UnpackDDElParam(WM_DDE_ADVISE, 0, &lo, &hi);
2033 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2034 ok(lo == 0 ||
2035 broken(lo == 0xdead), /* win2k */
2036 "Expected 0, got %08lx\n", lo);
2037 ok(hi == 0 ||
2038 broken(hi == 0xbeef), /* win2k */
2039 "Expected 0, got %08lx\n", hi);
2041 lo = 0xdead;
2042 hi = 0xbeef;
2043 ret = UnpackDDElParam(WM_DDE_ADVISE, 0xcafebabe, &lo, &hi);
2044 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2045 ok(lo == 0 ||
2046 broken(lo == 0xdead), /* win2k */
2047 "Expected 0, got %08lx\n", lo);
2048 ok(hi == 0 ||
2049 broken(hi == 0xbeef), /* win2k */
2050 "Expected 0, got %08lx\n", hi);
2052 hglobal = GlobalAlloc(GMEM_DDESHARE, 2);
2053 ptr = GlobalLock(hglobal);
2054 ptr[0] = 0xcafebabe;
2055 ptr[1] = 0xdeadbeef;
2056 GlobalUnlock(hglobal);
2058 lo = 0xdead;
2059 hi = 0xbeef;
2060 ret = UnpackDDElParam(WM_DDE_ADVISE, (LPARAM)hglobal, &lo, &hi);
2061 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2062 ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2063 ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2065 lo = 0xdead;
2066 hi = 0xbeef;
2067 ret = UnpackDDElParam(WM_DDE_UNADVISE, 0xcafebabe, &lo, &hi);
2068 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2069 ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2070 ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2072 lo = 0xdead;
2073 hi = 0xbeef;
2074 ret = UnpackDDElParam(WM_DDE_ACK, 0xcafebabe, &lo, &hi);
2075 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2076 ok(lo == 0 ||
2077 broken(lo == 0xdead), /* win2k */
2078 "Expected 0, got %08lx\n", lo);
2079 ok(hi == 0 ||
2080 broken(hi == 0xbeef), /* win2k */
2081 "Expected 0, got %08lx\n", hi);
2083 lo = 0xdead;
2084 hi = 0xbeef;
2085 ret = UnpackDDElParam(WM_DDE_ACK, (LPARAM)hglobal, &lo, &hi);
2086 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2087 ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2088 ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2090 lo = 0xdead;
2091 hi = 0xbeef;
2092 ret = UnpackDDElParam(WM_DDE_DATA, 0xcafebabe, &lo, &hi);
2093 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2094 ok(lo == 0 ||
2095 broken(lo == 0xdead), /* win2k */
2096 "Expected 0, got %08lx\n", lo);
2097 ok(hi == 0 ||
2098 broken(hi == 0xbeef), /* win2k */
2099 "Expected 0, got %08lx\n", hi);
2101 lo = 0xdead;
2102 hi = 0xbeef;
2103 ret = UnpackDDElParam(WM_DDE_DATA, (LPARAM)hglobal, &lo, &hi);
2104 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2105 ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2106 ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2108 lo = 0xdead;
2109 hi = 0xbeef;
2110 ret = UnpackDDElParam(WM_DDE_REQUEST, 0xcafebabe, &lo, &hi);
2111 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2112 ok(lo == 0xbabe, "Expected 0xbabe, got %08lx\n", lo);
2113 ok(hi == 0xcafe, "Expected 0xcafe, got %08lx\n", hi);
2115 lo = 0xdead;
2116 hi = 0xbeef;
2117 ret = UnpackDDElParam(WM_DDE_POKE, 0xcafebabe, &lo, &hi);
2118 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
2119 ok(lo == 0 ||
2120 broken(lo == 0xdead), /* win2k */
2121 "Expected 0, got %08lx\n", lo);
2122 ok(hi == 0 ||
2123 broken(hi == 0xbeef), /* win2k */
2124 "Expected 0, got %08lx\n", hi);
2126 lo = 0xdead;
2127 hi = 0xbeef;
2128 ret = UnpackDDElParam(WM_DDE_POKE, (LPARAM)hglobal, &lo, &hi);
2129 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2130 ok(lo == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", lo);
2131 ok(hi == 0xdeadbeef, "Expected 0xdeadbeef, got %08lx\n", hi);
2133 lo = 0xdead;
2134 hi = 0xbeef;
2135 ret = UnpackDDElParam(WM_DDE_EXECUTE, 0xcafebabe, &lo, &hi);
2136 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2137 ok(lo == 0, "Expected 0, got %08lx\n", lo);
2138 ok(hi == 0xcafebabe, "Expected 0xcafebabe, got %08lx\n", hi);
2141 static HDDEDATA CALLBACK server_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2142 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2143 ULONG_PTR dwData1, ULONG_PTR dwData2)
2145 DWORD size, rsize;
2146 char str[MAX_PATH];
2147 static int msg_index = 0;
2148 static HCONV conversation = 0;
2149 static char test_cmd[] = "test dde command";
2150 static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2151 static char test_service [] = "TestDDEService";
2152 static char test_topic [] = "TestDDETopic";
2154 msg_index++;
2156 switch (uType)
2158 case XTYP_REGISTER:
2160 ok(msg_index == 1 || msg_index == 7 || msg_index == 13 || msg_index == 19,
2161 "Expected 1, 7, 13 or 19, got %d\n", msg_index);
2162 return (HDDEDATA)TRUE;
2165 case XTYP_CONNECT:
2167 ok(msg_index == 2 || msg_index == 8 || msg_index == 14 || msg_index == 20,
2168 "Expected 2, 8, 14 or 20, got %d\n", msg_index);
2169 ok(uFmt == 0, "Expected 0, got %d, msg_index=%d\n", uFmt, msg_index);
2170 ok(hconv == 0, "Expected 0, got %p, msg_index=%d\n", hconv, msg_index);
2171 ok(hdata == 0, "Expected 0, got %p, msg_index=%d\n", hdata, msg_index);
2172 ok(dwData1 != 0, "Expected not 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2173 ok(dwData2 == FALSE, "Expected FALSE, got %08lx, msg_index=%d\n", dwData2, msg_index);
2175 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2176 ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2177 test_topic, str, msg_index);
2178 ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2180 size = DdeQueryStringA(server_pid, hsz2, str, MAX_PATH, CP_WINANSI);
2181 ok(!lstrcmpA(str, test_service), "Expected %s, got %s, msg_index=%d\n",
2182 test_service, str, msg_index);
2183 ok(size == 14, "Expected 14, got %d, msg_index=%d\n", size, msg_index);
2185 return (HDDEDATA) TRUE;
2187 case XTYP_CONNECT_CONFIRM:
2189 ok(msg_index == 3 || msg_index == 9 || msg_index == 15 || msg_index == 21,
2190 "Expected 3, 9, 15 or 21 got %d\n", msg_index);
2191 conversation = hconv;
2192 return (HDDEDATA) TRUE;
2194 case XTYP_EXECUTE:
2196 BYTE *buffer = NULL;
2198 ok(msg_index == 4 || msg_index == 5 || msg_index == 10 || msg_index == 11 ||
2199 msg_index == 16 || msg_index == 17 || msg_index == 22 || msg_index == 23,
2200 "Expected 4, 5, 10, 11, 16, 17, 22 or 23, got %d\n", msg_index);
2201 ok(uFmt == 0, "Expected 0, got %d\n", uFmt);
2202 ok(hconv == conversation, "Expected conversation handle, got %p, msg_index=%d\n",
2203 hconv, msg_index);
2204 ok(dwData1 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData1, msg_index);
2205 ok(dwData2 == 0, "Expected 0, got %08lx, msg_index=%d\n", dwData2, msg_index);
2206 ok(hsz2 == 0, "Expected 0, got %p, msg_index=%d\n", hsz2, msg_index);
2207 size = DdeQueryStringA(server_pid, hsz1, str, MAX_PATH, CP_WINANSI);
2208 ok(!lstrcmpA(str, test_topic), "Expected %s, got %s, msg_index=%d\n",
2209 test_topic, str, msg_index);
2210 ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2211 ok(size == 12, "Expected 12, got %d, msg_index=%d\n", size, msg_index);
2213 size = DdeGetData(hdata, NULL, 0, 0);
2214 if (msg_index == 10 || msg_index ==11 || msg_index == 16 || msg_index ==17)
2215 if (msg_index == 10 || msg_index == 16)
2216 todo_wine
2217 ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2218 size, msg_index);
2219 else
2220 ok(size == 34, "Expected that size should be 34 not %d, msg_index=%d\n",
2221 size, msg_index);
2222 else
2223 if (msg_index ==22)
2224 todo_wine
2225 ok(size == 9, "Expected that size should be 9 not %d, msg_index=%d\n",
2226 size, msg_index);
2227 else
2228 if (msg_index == 5)
2229 todo_wine
2230 ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2231 size, msg_index);
2232 else
2233 ok(size == 17, "Expected that size should be 17 not %d, msg_index=%d\n",
2234 size, msg_index);
2235 ok((buffer = HeapAlloc(GetProcessHeap(), 0, size)) != NULL, "should not be null\n");
2236 rsize = DdeGetData(hdata, buffer, size, 0);
2237 if (msg_index == 10 || msg_index == 11 || msg_index == 16 || msg_index ==17)
2239 ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2240 size, rsize, msg_index);
2241 if (msg_index == 10 || msg_index == 16)
2242 todo_wine {
2243 ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2244 "Expected \"Test dde command\", msg_index=%d\n",
2245 msg_index);
2246 ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2247 } else
2249 ok(!lstrcmpW((WCHAR*)buffer, test_cmd_w),
2250 "Expected \"Test dde command\", msg_index=%d\n",
2251 msg_index);
2252 ok(size == 34, "Expected 34, got %d, msg_index=%d\n", size, msg_index);
2254 }else if (msg_index == 22)
2256 ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2257 size, rsize, msg_index);
2258 } else
2260 ok(rsize == size, "Incorrect size returned, expected %d got %d, msg_index=%d\n",
2261 size, rsize, msg_index);
2262 if (msg_index == 5)
2263 todo_wine {
2264 ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2265 test_cmd, buffer, msg_index);
2266 ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2268 else
2270 ok(!lstrcmpA((CHAR*)buffer, test_cmd), "Expected %s, got %s, msg_index=%d\n",
2271 test_cmd, buffer, msg_index);
2272 ok(size == 17, "Expected size should be 17, got %d, msg_index=%d\n", size, msg_index);
2277 return (HDDEDATA) DDE_FACK;
2279 case XTYP_DISCONNECT:
2280 return (HDDEDATA) TRUE;
2282 default:
2283 ok(FALSE, "Unhandled msg: %08x, msg_index=%d\n", uType, msg_index);
2286 return NULL;
2289 static HDDEDATA CALLBACK client_end_to_end_callback(UINT uType, UINT uFmt, HCONV hconv,
2290 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
2291 ULONG_PTR dwData1, ULONG_PTR dwData2)
2293 switch (uType)
2295 case XTYP_DISCONNECT:
2296 return (HDDEDATA) TRUE;
2298 default:
2299 ok(FALSE, "Unhandled msg: %08x\n", uType);
2302 return NULL;
2305 static void test_end_to_end_client(BOOL type_a)
2307 DWORD ret, err;
2308 DWORD client_pid = 0;
2309 HSZ server, topic;
2310 HCONV hconv;
2311 HDDEDATA hdata;
2312 static char test_cmd[] = "test dde command";
2313 static WCHAR test_cmd_w[] = {'t','e','s','t',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
2314 static char test_service[] = "TestDDEService";
2315 static WCHAR test_service_w[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
2316 static char test_topic[] = "TestDDETopic";
2317 static WCHAR test_topic_w[] = {'T','e','s','t','D','D','E','T','o','p','i','c',0};
2319 trace("Start end to end client %d\n", type_a);
2321 if (type_a)
2322 ret = DdeInitializeA(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2323 else
2324 ret = DdeInitializeW(&client_pid, client_end_to_end_callback, APPCMD_CLIENTONLY, 0);
2325 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2327 if (type_a)
2329 server = DdeCreateStringHandleA(client_pid, test_service, CP_WINANSI);
2330 topic = DdeCreateStringHandleA(client_pid, test_topic, CP_WINANSI);
2332 else {
2333 server = DdeCreateStringHandleW(client_pid, test_service_w, CP_WINUNICODE);
2334 topic = DdeCreateStringHandleW(client_pid, test_topic_w, CP_WINUNICODE);
2337 DdeGetLastError(client_pid);
2338 hconv = DdeConnect(client_pid, server, topic, NULL);
2339 ok(hconv != NULL, "Expected non-NULL conversation\n");
2340 ret = DdeGetLastError(client_pid);
2341 ok(ret == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %x\n", ret);
2342 DdeFreeStringHandle(client_pid, server);
2344 /* Test both A and W data being passed to DdeClientTransaction */
2345 hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1,
2346 hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2347 ok(hdata != NULL, "DdeClientTransaction failed\n");
2348 ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2349 err = DdeGetLastError(client_pid);
2350 ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2352 hdata = DdeClientTransaction((LPBYTE)test_cmd_w, lstrlenW(test_cmd_w) * sizeof(WCHAR) + 2,
2353 hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
2354 ok(hdata != NULL, "DdeClientTransaction failed\n");
2355 ok(ret == DDE_FACK, "wrong status code %x\n", ret);
2356 err = DdeGetLastError(client_pid);
2357 ok(err == DMLERR_NO_ERROR, "wrong dde error %x\n", err);
2359 DdeFreeStringHandle(client_pid, topic);
2360 ret = DdeDisconnect(hconv);
2361 ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2363 ret = DdeUninitialize(client_pid);
2364 ok(ret == TRUE, "Expected TRUE, got %x\n", ret);
2368 static void test_end_to_end_server(HANDLE hproc, HANDLE hthread, BOOL type_a)
2370 MSG msg;
2371 HSZ server;
2372 BOOL ret;
2373 DWORD res;
2374 HDDEDATA hdata;
2375 static CHAR test_service[] = "TestDDEService";
2377 trace("start end to end server %d\n", type_a);
2378 server_pid = 0;
2380 if (type_a)
2381 res = DdeInitializeA(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2382 else
2383 res = DdeInitializeW(&server_pid, server_end_to_end_callback, APPCLASS_STANDARD, 0);
2384 ok(res == DMLERR_NO_ERROR, "Expected DMLERR_NO_ERROR, got %d\n", res);
2386 server = DdeCreateStringHandleA(server_pid, test_service, CP_WINANSI);
2387 ok(server != NULL, "Expected non-NULL string handle\n");
2389 hdata = DdeNameService(server_pid, server, 0, DNS_REGISTER);
2390 ok(hdata == (HDDEDATA)TRUE, "Expected TRUE, got %p\n", hdata);
2391 ResumeThread( hthread );
2394 while (MsgWaitForMultipleObjects( 1, &hproc, FALSE, INFINITE, QS_ALLINPUT ) != 0)
2396 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
2399 ret = DdeUninitialize(server_pid);
2400 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
2401 GetExitCodeProcess( hproc, &res );
2402 ok( !res, "client failed with %u error(s)\n", res );
2405 START_TEST(dde)
2407 int argc;
2408 char **argv;
2409 char buffer[MAX_PATH];
2410 STARTUPINFO startup;
2411 PROCESS_INFORMATION proc;
2413 argc = winetest_get_mainargs(&argv);
2414 if (argc == 3)
2416 if (!lstrcmpA(argv[2], "ddeml"))
2417 test_ddeml_client();
2418 else if (!lstrcmpA(argv[2], "msg"))
2419 test_msg_client();
2420 else if (!lstrcmpA(argv[2], "enda"))
2421 test_end_to_end_client(TRUE);
2422 else if (!lstrcmpA(argv[2], "endw"))
2423 test_end_to_end_client(FALSE);
2425 return;
2428 test_initialisation();
2430 ZeroMemory(&startup, sizeof(STARTUPINFO));
2431 sprintf(buffer, "%s dde ddeml", argv[0]);
2432 startup.cb = sizeof(startup);
2433 startup.dwFlags = STARTF_USESHOWWINDOW;
2434 startup.wShowWindow = SW_SHOWNORMAL;
2436 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2437 CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2439 test_msg_server(proc.hProcess, proc.hThread);
2441 sprintf(buffer, "%s dde msg", argv[0]);
2442 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2443 0, NULL, NULL, &startup, &proc);
2445 test_ddeml_server(proc.hProcess);
2447 /* Test the combinations of A and W interfaces with A and W data
2448 end to end to ensure that data conversions are accurate */
2449 sprintf(buffer, "%s dde enda", argv[0]);
2450 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2451 CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2453 test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2455 sprintf(buffer, "%s dde endw", argv[0]);
2456 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2457 CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2459 test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2461 sprintf(buffer, "%s dde enda", argv[0]);
2462 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2463 CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2465 test_end_to_end_server(proc.hProcess, proc.hThread, FALSE);
2467 sprintf(buffer, "%s dde endw", argv[0]);
2468 CreateProcessA(NULL, buffer, NULL, NULL, FALSE,
2469 CREATE_SUSPENDED, NULL, NULL, &startup, &proc);
2471 test_end_to_end_server(proc.hProcess, proc.hThread, TRUE);
2473 test_dde_aw_transaction();
2475 test_DdeCreateDataHandle();
2476 test_DdeCreateStringHandle();
2477 test_FreeDDElParam();
2478 test_PackDDElParam();
2479 test_UnpackDDElParam();