2 * Unit tests for DDE functions
4 * Copyright (c) 2004 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
30 static const WCHAR TEST_DDE_SERVICE[] = {'T','e','s','t','D','D','E','S','e','r','v','i','c','e',0};
32 static const char exec_cmdA[] = "ANSI dde command";
33 static const WCHAR exec_cmdW[] = {'u','n','i','c','o','d','e',' ','d','d','e',' ','c','o','m','m','a','n','d',0};
35 static WNDPROC old_dde_client_wndproc;
37 LRESULT WINAPI hook_dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
41 trace("hook_dde_client_wndproc: %p %04x %08x %08lx\n", hwnd, msg, wparam, lparam);
46 UnpackDDElParam(WM_DDE_ACK, lparam, &lo, &hi);
47 trace("WM_DDE_ACK: status %04x hglobal %p\n", lo, (HGLOBAL)hi);
53 return CallWindowProcA(old_dde_client_wndproc, hwnd, msg, wparam, lparam);
56 static LRESULT WINAPI dde_server_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
58 trace("dde_server_wndproc: %p %04x %08x %08lx\n", hwnd, msg, wparam, lparam);
64 ATOM aService = GlobalAddAtomW(TEST_DDE_SERVICE);
66 trace("server: got WM_DDE_INITIATE from %p with %08lx\n", (HWND)wparam, lparam);
68 if (LOWORD(lparam) == aService)
70 ok(!IsWindowUnicode((HWND)wparam), "client should be an ANSI window\n");
71 old_dde_client_wndproc = (WNDPROC)SetWindowLongPtrA((HWND)wparam, GWLP_WNDPROC, (ULONG_PTR)hook_dde_client_wndproc);
72 trace("server: sending WM_DDE_ACK to %p\n", (HWND)wparam);
73 SendMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, MAKELPARAM(aService, 0));
76 GlobalDeleteAtom(aService);
87 trace("server: got WM_DDE_EXECUTE from %p with %08lx\n", (HWND)wparam, lparam);
89 UnpackDDElParam(WM_DDE_EXECUTE, lparam, &lo, &hi);
90 trace("%08lx => lo %04x hi %04x\n", lparam, lo, hi);
92 ack.bAppReturnCode = 0;
96 cmd = GlobalLock((HGLOBAL)hi);
98 if (!cmd || (lstrcmpW((LPCWSTR)cmd, exec_cmdW) && lstrcmpA(cmd, exec_cmdA)))
100 trace("ignoring unknown WM_DDE_EXECUTE command\n");
101 /* We have to send a negative acknowledge even if we don't
102 * accept the command, otherwise Windows goes mad and next time
103 * we send an acknowledge DDEML drops the connection.
104 * Not sure how to call it: a bug or a feature.
110 GlobalUnlock((HGLOBAL)hi);
112 trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
114 status = *((WORD *)&ack);
115 lparam = ReuseDDElParam(lparam, WM_DDE_EXECUTE, WM_DDE_ACK, status, hi);
117 PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
121 case WM_DDE_TERMINATE:
126 trace("server: got WM_DDE_TERMINATE from %p with %08lx\n", (HWND)wparam, lparam);
128 ack.bAppReturnCode = 0;
133 trace("server: posting %s WM_DDE_ACK to %p\n", ack.fAck ? "POSITIVE" : "NEGATIVE", (HWND)wparam);
135 status = *((WORD *)&ack);
136 lparam = PackDDElParam(WM_DDE_ACK, status, 0);
138 PostMessageW((HWND)wparam, WM_DDE_ACK, (WPARAM)hwnd, lparam);
146 return DefWindowProcW(hwnd, msg, wparam, lparam);
149 static LRESULT WINAPI dde_client_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
151 return DefWindowProcA(hwnd, msg, wparam, lparam);
154 static BOOL create_dde_windows(HWND *hwnd_client, HWND *hwnd_server)
158 static const WCHAR server_class_name[] = {'d','d','e','_','s','e','r','v','e','r','_','w','i','n','d','o','w',0};
159 static const char client_class_name[] = "dde_client_window";
161 memset(&wcW, 0, sizeof(wcW));
162 wcW.lpfnWndProc = dde_server_wndproc;
163 wcW.lpszClassName = server_class_name;
164 wcW.hInstance = GetModuleHandleA(0);
165 if (!RegisterClassW(&wcW)) return FALSE;
167 memset(&wcA, 0, sizeof(wcA));
168 wcA.lpfnWndProc = dde_client_wndproc;
169 wcA.lpszClassName = client_class_name;
170 wcA.hInstance = GetModuleHandleA(0);
171 assert(RegisterClassA(&wcA));
173 *hwnd_server = CreateWindowExW(0, server_class_name, NULL,
175 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
176 GetDesktopWindow(), 0,
177 GetModuleHandleA(0), NULL);
178 assert(*hwnd_server);
180 *hwnd_client = CreateWindowExA(0, client_class_name, NULL,
182 100, 100, CW_USEDEFAULT, CW_USEDEFAULT,
183 GetDesktopWindow(), 0,
184 GetModuleHandleA(0), NULL);
185 assert(*hwnd_client);
187 trace("server hwnd %p, client hwnd %p\n", *hwnd_server, *hwnd_client);
189 ok(IsWindowUnicode(*hwnd_server), "server has to be a unicode window\n");
190 ok(!IsWindowUnicode(*hwnd_client), "client has to be an ANSI window\n");
195 static HDDEDATA CALLBACK client_dde_callback(UINT uType, UINT uFmt, HCONV hconv,
196 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
197 ULONG_PTR dwData1, ULONG_PTR dwData2)
199 static const char * const cmd_type[15] = {
200 "XTYP_ERROR", "XTYP_ADVDATA", "XTYP_ADVREQ", "XTYP_ADVSTART",
201 "XTYP_ADVSTOP", "XTYP_EXECUTE", "XTYP_CONNECT", "XTYP_CONNECT_CONFIRM",
202 "XTYP_XACT_COMPLETE", "XTYP_POKE", "XTYP_REGISTER", "XTYP_REQUEST",
203 "XTYP_DISCONNECT", "XTYP_UNREGISTER", "XTYP_WILDCONNECT" };
205 const char *cmd_name;
207 type = (uType & XTYP_MASK) >> XTYP_SHIFT;
208 cmd_name = (type >= 0 && type <= 14) ? cmd_type[type] : "unknown";
210 trace("client_dde_callback: %04x (%s) %d %p %p %p %p %08lx %08lx\n",
211 uType, cmd_name, uFmt, hconv, hsz1, hsz2, hdata, dwData1, dwData2);
215 static void test_dde_transaction(void)
218 DWORD dde_inst, ret, err;
220 HWND hwnd_client, hwnd_server;
223 static const char test_cmd[] = "test dde command";
225 /* server: unicode, client: ansi */
226 if (!create_dde_windows(&hwnd_client, &hwnd_server)) return;
229 ret = DdeInitializeA(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
230 ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04lx (%x)\n",
231 ret, DdeGetLastError(dde_inst));
233 hsz_server = DdeCreateStringHandleW(dde_inst, TEST_DDE_SERVICE, CP_WINUNICODE);
235 hconv = DdeConnect(dde_inst, hsz_server, 0, NULL);
236 ok(hconv != 0, "DdeConnect error %x\n", DdeGetLastError(dde_inst));
237 err = DdeGetLastError(dde_inst);
238 ok(err == DMLERR_NO_ERROR, "wrong dde error %lx\n", err);
240 info.cb = sizeof(info);
241 ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
242 ok(ret, "wrong info size %ld, DdeQueryConvInfo error %x\n", ret, DdeGetLastError(dde_inst));
243 /* should be CP_WINANSI since we used DdeInitializeA */
244 ok(info.ConvCtxt.iCodePage == CP_WINANSI, "wrong iCodePage %d\n", info.ConvCtxt.iCodePage);
245 ok(!info.hConvPartner, "unexpected info.hConvPartner: %p\n", info.hConvPartner);
247 ok((info.wStatus & DDE_FACK), "unexpected info.wStatus: %04x\n", info.wStatus);
249 ok((info.wStatus & (ST_CONNECTED | ST_CLIENT)) == (ST_CONNECTED | ST_CLIENT), "unexpected info.wStatus: %04x\n", info.wStatus);
250 ok(info.wConvst == XST_CONNECTED, "unexpected info.wConvst: %04x\n", info.wConvst);
251 ok(info.wType == 0, "unexpected info.wType: %04x\n", info.wType);
253 trace("hwnd %p, hwndPartner %p\n", info.hwnd, info.hwndPartner);
255 trace("sending test client transaction command\n");
257 hdata = DdeClientTransaction((LPBYTE)test_cmd, strlen(test_cmd) + 1, hconv, (HSZ)0xdead, 0xbeef, XTYP_EXECUTE, 1000, &ret);
258 ok(!hdata, "DdeClientTransaction succeeded\n");
259 ok(ret == DDE_FNOTPROCESSED, "wrong status code %04lx\n", ret);
260 err = DdeGetLastError(dde_inst);
261 ok(err == DMLERR_NOTPROCESSED, "wrong dde error %lx\n", err);
263 trace("sending ANSI client transaction command\n");
265 hdata = DdeClientTransaction((LPBYTE)exec_cmdA, lstrlenA(exec_cmdA) + 1, hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
266 ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
267 ok(ret == DDE_FACK, "wrong status code %04lx\n", ret);
269 err = DdeGetLastError(dde_inst);
270 ok(err == DMLERR_NO_ERROR, "wrong dde error %lx\n", err);
272 trace("sending unicode client transaction command\n");
274 hdata = DdeClientTransaction((LPBYTE)exec_cmdW, (lstrlenW(exec_cmdW) + 1) * sizeof(WCHAR), hconv, 0, 0, XTYP_EXECUTE, 1000, &ret);
275 ok(hdata != 0, "DdeClientTransaction returned %p, error %x\n", hdata, DdeGetLastError(dde_inst));
276 ok(ret == DDE_FACK, "wrong status code %04lx\n", ret);
277 err = DdeGetLastError(dde_inst);
278 ok(err == DMLERR_NO_ERROR, "wrong dde error %lx\n", err);
280 ok(DdeDisconnect(hconv), "DdeDisconnect error %x\n", DdeGetLastError(dde_inst));
282 info.cb = sizeof(info);
283 ret = DdeQueryConvInfo(hconv, QID_SYNC, &info);
284 ok(!ret, "DdeQueryConvInfo should fail\n");
285 err = DdeGetLastError(dde_inst);
287 ok(err == DMLERR_INVALIDPARAMETER, "wrong dde error %lx\n", err);
290 ok(DdeFreeStringHandle(dde_inst, hsz_server), "DdeFreeStringHandle error %x\n", DdeGetLastError(dde_inst));
292 /* This call hangs on win2k SP4 and XP SP1.
293 DdeUninitialize(dde_inst);*/
295 DestroyWindow(hwnd_client);
296 DestroyWindow(hwnd_server);
299 static void test_DdeCreateStringHandleW(DWORD dde_inst, int codepage)
301 static const WCHAR dde_string[] = {'D','D','E',' ','S','t','r','i','n','g',0};
307 str_handle = DdeCreateStringHandleW(dde_inst, dde_string, codepage);
308 ok(str_handle != 0, "DdeCreateStringHandleW failed with error %08x\n",
309 DdeGetLastError(dde_inst));
311 ret = DdeQueryStringW(dde_inst, str_handle, NULL, 0, codepage);
312 if (codepage == CP_WINANSI)
313 ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
315 ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
317 ret = DdeQueryStringW(dde_inst, str_handle, bufW, 256, codepage);
318 if (codepage == CP_WINANSI)
320 ok(ret == 1, "DdeQueryStringW returned wrong length %d\n", ret);
321 ok(!lstrcmpA("D", (LPCSTR)bufW), "DdeQueryStringW returned wrong string\n");
325 ok(ret == lstrlenW(dde_string), "DdeQueryStringW returned wrong length %d\n", ret);
326 ok(!lstrcmpW(dde_string, bufW), "DdeQueryStringW returned wrong string\n");
329 ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINANSI);
330 if (codepage == CP_WINANSI)
332 ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
333 ok(!lstrcmpA("D", buf), "DdeQueryStringW returned wrong string\n");
337 ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
338 ok(!lstrcmpA("DDE String", buf), "DdeQueryStringA returned wrong string %s\n", buf);
341 ret = DdeQueryStringA(dde_inst, str_handle, buf, 256, CP_WINUNICODE);
342 if (codepage == CP_WINANSI)
344 ok(ret == 1, "DdeQueryStringA returned wrong length %d\n", ret);
345 ok(!lstrcmpA("D", buf), "DdeQueryStringA returned wrong string %s\n", buf);
349 ok(ret == lstrlenA("DDE String"), "DdeQueryStringA returned wrong length %d\n", ret);
350 ok(!lstrcmpW(dde_string, (LPCWSTR)buf), "DdeQueryStringW returned wrong string\n");
353 ok(DdeFreeStringHandle(dde_inst, str_handle), "DdeFreeStringHandle failed\n");
356 static void test_DdeCreateStringHandle(void)
360 dde_inst = 0xdeadbeef;
361 SetLastError(0xdeadbeef);
362 ret = DdeInitializeW(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
363 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
365 trace("Skipping the DDE test on a Win9x platform\n");
369 ok(ret == DMLERR_INVALIDPARAMETER, "DdeInitializeW should fail, but got %04lx instead\n", ret);
370 ok(DdeGetLastError(dde_inst) == DMLERR_INVALIDPARAMETER, "expected DMLERR_INVALIDPARAMETER\n");
373 ret = DdeInitializeW(&dde_inst, client_dde_callback, APPCMD_CLIENTONLY, 0);
374 ok(ret == DMLERR_NO_ERROR, "DdeInitializeW failed with error %04lx (%08x)\n",
375 ret, DdeGetLastError(dde_inst));
377 test_DdeCreateStringHandleW(dde_inst, 0);
378 test_DdeCreateStringHandleW(dde_inst, CP_WINUNICODE);
379 test_DdeCreateStringHandleW(dde_inst, CP_WINANSI);
381 ok(DdeUninitialize(dde_inst), "DdeUninitialize failed\n");
386 test_DdeCreateStringHandle();
387 test_dde_transaction();