dmscript: Merge the IClassFactory implementations.
[wine/testsucceed.git] / dlls / ole32 / tests / usrmarshal.c
blobed09558ed9bc0c419e61ab9be2d60c5c81bdbe51
1 /*
2 * User Marshaling Tests
4 * Copyright 2004-2006 Robert Shearman for CodeWeavers
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
21 #define COBJMACROS
22 #define CONST_VTABLE
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "objidl.h"
30 #include "wine/test.h"
32 ULONG __RPC_USER HMETAFILE_UserSize(ULONG *, ULONG, HMETAFILE *);
33 unsigned char * __RPC_USER HMETAFILE_UserMarshal(ULONG *, unsigned char *, HMETAFILE *);
34 unsigned char * __RPC_USER HMETAFILE_UserUnmarshal(ULONG *, unsigned char *, HMETAFILE *);
35 void __RPC_USER HMETAFILE_UserFree(ULONG *, HMETAFILE *);
37 ULONG __RPC_USER HENHMETAFILE_UserSize(ULONG *, ULONG, HENHMETAFILE *);
38 unsigned char * __RPC_USER HENHMETAFILE_UserMarshal (ULONG *, unsigned char *, HENHMETAFILE *);
39 unsigned char * __RPC_USER HENHMETAFILE_UserUnmarshal(ULONG *, unsigned char *, HENHMETAFILE *);
40 void __RPC_USER HENHMETAFILE_UserFree(ULONG *, HENHMETAFILE *);
42 ULONG __RPC_USER HMETAFILEPICT_UserSize(ULONG *, ULONG, HMETAFILEPICT *);
43 unsigned char * __RPC_USER HMETAFILEPICT_UserMarshal (ULONG *, unsigned char *, HMETAFILEPICT *);
44 unsigned char * __RPC_USER HMETAFILEPICT_UserUnmarshal(ULONG *, unsigned char *, HMETAFILEPICT *);
45 void __RPC_USER HMETAFILEPICT_UserFree(ULONG *, HMETAFILEPICT *);
47 static void * WINAPI user_allocate(SIZE_T size)
49 return CoTaskMemAlloc(size);
52 static void WINAPI user_free(void *p)
54 CoTaskMemFree(p);
57 static void init_user_marshal_cb(USER_MARSHAL_CB *umcb,
58 PMIDL_STUB_MESSAGE stub_msg,
59 PRPC_MESSAGE rpc_msg, unsigned char *buffer,
60 unsigned int size, MSHCTX context)
62 memset(rpc_msg, 0, sizeof(*rpc_msg));
63 rpc_msg->Buffer = buffer;
64 rpc_msg->BufferLength = size;
66 memset(stub_msg, 0, sizeof(*stub_msg));
67 stub_msg->RpcMsg = rpc_msg;
68 stub_msg->Buffer = buffer;
69 stub_msg->pfnAllocate = user_allocate;
70 stub_msg->pfnFree = user_free;
72 memset(umcb, 0, sizeof(*umcb));
73 umcb->Flags = MAKELONG(context, NDR_LOCAL_DATA_REPRESENTATION);
74 umcb->pStubMsg = stub_msg;
75 umcb->Signature = USER_MARSHAL_CB_SIGNATURE;
76 umcb->CBType = buffer ? USER_MARSHAL_CB_UNMARSHALL : USER_MARSHAL_CB_BUFFER_SIZE;
79 static const char cf_marshaled[] =
81 0x9, 0x0, 0x0, 0x0,
82 0x0, 0x0, 0x0, 0x0,
83 0x9, 0x0, 0x0, 0x0,
84 'M', 0x0, 'y', 0x0,
85 'F', 0x0, 'o', 0x0,
86 'r', 0x0, 'm', 0x0,
87 'a', 0x0, 't', 0x0,
88 0x0, 0x0
91 static void test_marshal_CLIPFORMAT(void)
93 USER_MARSHAL_CB umcb;
94 MIDL_STUB_MESSAGE stub_msg;
95 RPC_MESSAGE rpc_msg;
96 unsigned char *buffer;
97 ULONG i, size;
98 CLIPFORMAT cf = RegisterClipboardFormatA("MyFormat");
99 CLIPFORMAT cf2;
101 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
102 size = CLIPFORMAT_UserSize(&umcb.Flags, 0, &cf);
103 ok(size == 8 + sizeof(cf_marshaled) ||
104 broken(size == 12 + sizeof(cf_marshaled)) || /* win64 adds 4 extra (unused) bytes */
105 broken(size == 8 + sizeof(cf_marshaled) - 2), /* win9x and winnt don't include the '\0' */
106 "CLIPFORMAT: Wrong size %d\n", size);
108 buffer = HeapAlloc(GetProcessHeap(), 0, size);
109 memset( buffer, 0xcc, size );
110 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
111 CLIPFORMAT_UserMarshal(&umcb.Flags, buffer, &cf);
112 ok(*(LONG *)(buffer + 0) == WDT_REMOTE_CALL, "CLIPFORMAT: Context should be WDT_REMOTE_CALL instead of 0x%08x\n", *(LONG *)(buffer + 0));
113 ok(*(DWORD *)(buffer + 4) == cf, "CLIPFORMAT: Marshaled value should be 0x%04x instead of 0x%04x\n", cf, *(DWORD *)(buffer + 4));
114 ok(!memcmp(buffer + 8, cf_marshaled, min( sizeof(cf_marshaled), size-8 )), "Marshaled data differs\n");
115 if (size > sizeof(cf_marshaled) + 8) /* make sure the extra bytes are not used */
116 for (i = sizeof(cf_marshaled) + 8; i < size; i++)
117 ok( buffer[i] == 0xcc, "buffer offset %u has been set to %x\n", i, buffer[i] );
119 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
120 CLIPFORMAT_UserUnmarshal(&umcb.Flags, buffer, &cf2);
121 ok(cf == cf2, "CLIPFORMAT: Didn't unmarshal properly\n");
122 HeapFree(GetProcessHeap(), 0, buffer);
124 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
125 CLIPFORMAT_UserFree(&umcb.Flags, &cf2);
128 static void test_marshal_HWND(void)
130 USER_MARSHAL_CB umcb;
131 MIDL_STUB_MESSAGE stub_msg;
132 RPC_MESSAGE rpc_msg;
133 unsigned char *buffer;
134 ULONG size;
135 HWND hwnd = GetDesktopWindow();
136 HWND hwnd2;
137 wireHWND wirehwnd;
139 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
140 size = HWND_UserSize(&umcb.Flags, 0, &hwnd);
141 ok(size == sizeof(*wirehwnd), "Wrong size %d\n", size);
143 buffer = HeapAlloc(GetProcessHeap(), 0, size);
144 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
145 HWND_UserMarshal(&umcb.Flags, buffer, &hwnd);
146 wirehwnd = (wireHWND)buffer;
147 ok(wirehwnd->fContext == WDT_INPROC_CALL, "Context should be WDT_INPROC_CALL instead of 0x%08x\n", wirehwnd->fContext);
148 ok(wirehwnd->u.hInproc == (LONG_PTR)hwnd, "Marshaled value should be %p instead of %x\n", hwnd, wirehwnd->u.hRemote);
150 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
151 HWND_UserUnmarshal(&umcb.Flags, buffer, &hwnd2);
152 ok(hwnd == hwnd2, "Didn't unmarshal properly\n");
153 HeapFree(GetProcessHeap(), 0, buffer);
155 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
156 HWND_UserFree(&umcb.Flags, &hwnd2);
159 static void test_marshal_HGLOBAL(void)
161 USER_MARSHAL_CB umcb;
162 MIDL_STUB_MESSAGE stub_msg;
163 RPC_MESSAGE rpc_msg;
164 unsigned char *buffer;
165 ULONG size, block_size;
166 HGLOBAL hglobal;
167 HGLOBAL hglobal2;
168 unsigned char *wirehglobal;
169 int i;
171 hglobal = NULL;
172 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
173 size = HGLOBAL_UserSize(&umcb.Flags, 0, &hglobal);
174 /* native is poorly programmed and allocates 4/8 bytes more than it needs to
175 * here - Wine doesn't have to emulate that */
176 ok((size == 8) || broken(size == 12) || broken(size == 16), "Size should be 8, instead of %d\n", size);
177 buffer = HeapAlloc(GetProcessHeap(), 0, size);
178 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
179 HGLOBAL_UserMarshal(&umcb.Flags, buffer, &hglobal);
180 wirehglobal = buffer;
181 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08x\n", *(ULONG *)wirehglobal);
182 wirehglobal += sizeof(ULONG);
183 ok(*(ULONG *)wirehglobal == 0, "buffer+4 should be HGLOBAL\n");
184 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
185 hglobal2 = NULL;
186 HGLOBAL_UserUnmarshal(&umcb.Flags, buffer, &hglobal2);
187 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
188 HeapFree(GetProcessHeap(), 0, buffer);
189 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
190 HGLOBAL_UserFree(&umcb.Flags, &hglobal2);
193 for(block_size = 0; block_size <= 17; block_size++)
195 ULONG actual_size, expected_size;
197 hglobal = GlobalAlloc(0, block_size);
198 buffer = GlobalLock(hglobal);
199 for (i = 0; i < block_size; i++)
200 buffer[i] = i;
201 GlobalUnlock(hglobal);
202 actual_size = GlobalSize(hglobal);
203 expected_size = actual_size + 5 * sizeof(DWORD);
204 trace("%d: actual size %d\n", block_size, actual_size);
205 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
206 size = HGLOBAL_UserSize(&umcb.Flags, 0, &hglobal);
207 /* native is poorly programmed and allocates 4/8 bytes more than it needs to
208 * here - Wine doesn't have to emulate that */
209 ok(size == expected_size ||
210 broken(size == expected_size + 4) ||
211 broken(size == expected_size + 8),
212 "%d: got size %d\n", block_size, size);
213 buffer = HeapAlloc(GetProcessHeap(), 0, size);
214 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
215 HGLOBAL_UserMarshal(&umcb.Flags, buffer, &hglobal);
216 wirehglobal = buffer;
217 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08x\n", *(ULONG *)wirehglobal);
218 wirehglobal += sizeof(ULONG);
219 ok(*(ULONG *)wirehglobal == (ULONG)(ULONG_PTR)hglobal, "buffer+0x4 should be HGLOBAL\n");
220 wirehglobal += sizeof(ULONG);
221 ok(*(ULONG *)wirehglobal == actual_size, "%d: buffer+0x8 %08x\n", block_size, *(ULONG *)wirehglobal);
222 wirehglobal += sizeof(ULONG);
223 ok(*(ULONG *)wirehglobal == (ULONG)(ULONG_PTR)hglobal, "buffer+0xc should be HGLOBAL\n");
224 wirehglobal += sizeof(ULONG);
225 ok(*(ULONG *)wirehglobal == actual_size, "%d: buffer+0x10 %08x\n", block_size, *(ULONG *)wirehglobal);
226 wirehglobal += sizeof(ULONG);
227 for (i = 0; i < block_size; i++)
228 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
230 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
231 hglobal2 = NULL;
232 HGLOBAL_UserUnmarshal(&umcb.Flags, buffer, &hglobal2);
233 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
234 HeapFree(GetProcessHeap(), 0, buffer);
235 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
236 HGLOBAL_UserFree(&umcb.Flags, &hglobal2);
237 GlobalFree(hglobal);
241 static HENHMETAFILE create_emf(void)
243 const RECT rect = {0, 0, 100, 100};
244 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
245 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
246 return CloseEnhMetaFile(hdc);
249 static void test_marshal_HENHMETAFILE(void)
251 USER_MARSHAL_CB umcb;
252 MIDL_STUB_MESSAGE stub_msg;
253 RPC_MESSAGE rpc_msg;
254 unsigned char *buffer;
255 ULONG size;
256 HENHMETAFILE hemf;
257 HENHMETAFILE hemf2 = NULL;
258 unsigned char *wirehemf;
260 hemf = create_emf();
262 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
263 size = HENHMETAFILE_UserSize(&umcb.Flags, 0, &hemf);
264 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
265 buffer = HeapAlloc(GetProcessHeap(), 0, size);
266 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
267 HENHMETAFILE_UserMarshal(&umcb.Flags, buffer, &hemf);
268 wirehemf = buffer;
269 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehemf);
270 wirehemf += sizeof(DWORD);
271 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08x\n", *(DWORD *)wirehemf);
272 wirehemf += sizeof(DWORD);
273 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehemf);
274 wirehemf += sizeof(DWORD);
275 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehemf);
276 wirehemf += sizeof(DWORD);
277 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %d\n", *(DWORD *)wirehemf);
278 wirehemf += sizeof(DWORD);
279 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
280 * at this point */
282 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
283 HENHMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hemf2);
284 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
285 HeapFree(GetProcessHeap(), 0, buffer);
286 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
287 HENHMETAFILE_UserFree(&umcb.Flags, &hemf2);
288 DeleteEnhMetaFile(hemf);
290 /* test NULL emf */
291 hemf = NULL;
293 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
294 size = HENHMETAFILE_UserSize(&umcb.Flags, 0, &hemf);
295 ok(size == 8, "size should be 8 bytes, not %d\n", size);
296 buffer = HeapAlloc(GetProcessHeap(), 0, size);
297 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
298 HENHMETAFILE_UserMarshal(&umcb.Flags, buffer, &hemf);
299 wirehemf = buffer;
300 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehemf);
301 wirehemf += sizeof(DWORD);
302 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08x\n", *(DWORD *)wirehemf);
303 wirehemf += sizeof(DWORD);
305 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
306 HENHMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hemf2);
307 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
308 HeapFree(GetProcessHeap(), 0, buffer);
309 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
310 HENHMETAFILE_UserFree(&umcb.Flags, &hemf2);
313 static HMETAFILE create_mf(void)
315 RECT rect = {0, 0, 100, 100};
316 HDC hdc = CreateMetaFile(NULL);
317 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
318 return CloseMetaFile(hdc);
321 static void test_marshal_HMETAFILE(void)
323 USER_MARSHAL_CB umcb;
324 MIDL_STUB_MESSAGE stub_msg;
325 RPC_MESSAGE rpc_msg;
326 unsigned char *buffer;
327 ULONG size;
328 HMETAFILE hmf;
329 HMETAFILE hmf2 = NULL;
330 unsigned char *wirehmf;
332 hmf = create_mf();
334 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
335 size = HMETAFILE_UserSize(&umcb.Flags, 0, &hmf);
336 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
337 buffer = HeapAlloc(GetProcessHeap(), 0, size);
338 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
339 HMETAFILE_UserMarshal(&umcb.Flags, buffer, &hmf);
340 wirehmf = buffer;
341 ok(*(DWORD *)wirehmf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmf);
342 wirehmf += sizeof(DWORD);
343 ok(*(DWORD *)wirehmf == (DWORD)(DWORD_PTR)hmf, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmf);
344 wirehmf += sizeof(DWORD);
345 ok(*(DWORD *)wirehmf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehmf);
346 wirehmf += sizeof(DWORD);
347 ok(*(DWORD *)wirehmf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehmf);
348 wirehmf += sizeof(DWORD);
349 ok(*(WORD *)wirehmf == 1, "wirestgm + 0x10 should be 1 instead of 0x%08x\n", *(DWORD *)wirehmf);
350 wirehmf += sizeof(DWORD);
351 /* ... rest of data not tested - refer to tests for GetMetaFileBits
352 * at this point */
354 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
355 HMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hmf2);
356 ok(hmf2 != NULL, "HMETAFILE didn't unmarshal\n");
357 HeapFree(GetProcessHeap(), 0, buffer);
358 HMETAFILE_UserFree(&umcb.Flags, &hmf2);
359 DeleteMetaFile(hmf);
361 /* test NULL emf */
362 hmf = NULL;
364 size = HMETAFILE_UserSize(&umcb.Flags, 0, &hmf);
365 ok(size == 8, "size should be 8 bytes, not %d\n", size);
366 buffer = HeapAlloc(GetProcessHeap(), 0, size);
367 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
368 HMETAFILE_UserMarshal(&umcb.Flags, buffer, &hmf);
369 wirehmf = buffer;
370 ok(*(DWORD *)wirehmf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmf);
371 wirehmf += sizeof(DWORD);
372 ok(*(DWORD *)wirehmf == (DWORD)(DWORD_PTR)hmf, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmf);
373 wirehmf += sizeof(DWORD);
375 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
376 HMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hmf2);
377 ok(hmf2 == NULL, "NULL HMETAFILE didn't unmarshal\n");
378 HeapFree(GetProcessHeap(), 0, buffer);
379 HMETAFILE_UserFree(&umcb.Flags, &hmf2);
382 #define USER_MARSHAL_PTR_PREFIX \
383 ( (DWORD)'U' | ( (DWORD)'s' << 8 ) | \
384 ( (DWORD)'e' << 16 ) | ( (DWORD)'r' << 24 ) )
386 static void test_marshal_HMETAFILEPICT(void)
388 USER_MARSHAL_CB umcb;
389 MIDL_STUB_MESSAGE stub_msg;
390 RPC_MESSAGE rpc_msg;
391 unsigned char *buffer, *buffer_end;
392 ULONG size;
393 HMETAFILEPICT hmfp;
394 HMETAFILEPICT hmfp2 = NULL;
395 METAFILEPICT *pmfp;
396 unsigned char *wirehmfp;
398 hmfp = GlobalAlloc(GMEM_MOVEABLE, sizeof(*pmfp));
399 pmfp = GlobalLock(hmfp);
400 pmfp->mm = MM_ISOTROPIC;
401 pmfp->xExt = 1;
402 pmfp->yExt = 2;
403 pmfp->hMF = create_mf();
404 GlobalUnlock(hmfp);
406 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
407 size = HMETAFILEPICT_UserSize(&umcb.Flags, 0, &hmfp);
408 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
409 trace("size is %d\n", size);
410 buffer = HeapAlloc(GetProcessHeap(), 0, size);
411 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
412 buffer_end = HMETAFILEPICT_UserMarshal(&umcb.Flags, buffer, &hmfp);
413 wirehmfp = buffer;
414 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
415 wirehmfp += sizeof(DWORD);
416 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)hmfp, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmfp);
417 wirehmfp += sizeof(DWORD);
418 ok(*(DWORD *)wirehmfp == MM_ISOTROPIC, "wirestgm + 0x8 should be MM_ISOTROPIC instead of 0x%08x\n", *(DWORD *)wirehmfp);
419 wirehmfp += sizeof(DWORD);
420 ok(*(DWORD *)wirehmfp == 1, "wirestgm + 0xc should be 1 instead of 0x%08x\n", *(DWORD *)wirehmfp);
421 wirehmfp += sizeof(DWORD);
422 ok(*(DWORD *)wirehmfp == 2, "wirestgm + 0x10 should be 2 instead of 0x%08x\n", *(DWORD *)wirehmfp);
423 wirehmfp += sizeof(DWORD);
424 ok(*(DWORD *)wirehmfp == USER_MARSHAL_PTR_PREFIX, "wirestgm + 0x14 should be \"User\" instead of 0x%08x\n", *(DWORD *)wirehmfp);
425 wirehmfp += sizeof(DWORD);
426 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x18 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
427 wirehmfp += sizeof(DWORD);
428 pmfp = GlobalLock(hmfp);
429 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)pmfp->hMF, "wirestgm + 0x1c should be pmfp->hMF instead of 0x%08x\n", *(DWORD *)wirehmfp);
430 GlobalUnlock(hmfp);
431 wirehmfp += sizeof(DWORD);
432 /* Note use (buffer_end - buffer) instead of size here, because size is an
433 * overestimate with native */
434 ok(*(DWORD *)wirehmfp == (buffer_end - buffer - 0x28), "wirestgm + 0x20 should be size - 0x34 instead of 0x%08x\n", *(DWORD *)wirehmfp);
435 wirehmfp += sizeof(DWORD);
436 ok(*(DWORD *)wirehmfp == (buffer_end - buffer - 0x28), "wirestgm + 0x24 should be size - 0x34 instead of 0x%08x\n", *(DWORD *)wirehmfp);
437 wirehmfp += sizeof(DWORD);
438 ok(*(WORD *)wirehmfp == 1, "wirehmfp + 0x28 should be 1 instead of 0x%08x\n", *(DWORD *)wirehmfp);
439 wirehmfp += sizeof(DWORD);
440 /* ... rest of data not tested - refer to tests for GetMetaFileBits
441 * at this point */
443 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
444 HMETAFILEPICT_UserUnmarshal(&umcb.Flags, buffer, &hmfp2);
445 ok(hmfp2 != NULL, "HMETAFILEPICT didn't unmarshal\n");
446 HeapFree(GetProcessHeap(), 0, buffer);
447 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
448 HMETAFILEPICT_UserFree(&umcb.Flags, &hmfp2);
449 pmfp = GlobalLock(hmfp);
450 DeleteMetaFile(pmfp->hMF);
451 GlobalUnlock(hmfp);
452 GlobalFree(hmfp);
454 /* test NULL emf */
455 hmfp = NULL;
457 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
458 size = HMETAFILEPICT_UserSize(&umcb.Flags, 0, &hmfp);
459 ok(size == 8, "size should be 8 bytes, not %d\n", size);
460 buffer = HeapAlloc(GetProcessHeap(), 0, size);
461 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
462 HMETAFILEPICT_UserMarshal(&umcb.Flags, buffer, &hmfp);
463 wirehmfp = buffer;
464 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
465 wirehmfp += sizeof(DWORD);
466 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)hmfp, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmfp);
467 wirehmfp += sizeof(DWORD);
469 hmfp2 = NULL;
470 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
471 HMETAFILEPICT_UserUnmarshal(&umcb.Flags, buffer, &hmfp2);
472 ok(hmfp2 == NULL, "NULL HMETAFILE didn't unmarshal\n");
473 HeapFree(GetProcessHeap(), 0, buffer);
474 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
475 HMETAFILEPICT_UserFree(&umcb.Flags, &hmfp2);
478 static HRESULT WINAPI Test_IUnknown_QueryInterface(
479 LPUNKNOWN iface,
480 REFIID riid,
481 LPVOID *ppvObj)
483 if (ppvObj == NULL) return E_POINTER;
485 if (IsEqualGUID(riid, &IID_IUnknown))
487 *ppvObj = iface;
488 IUnknown_AddRef(iface);
489 return S_OK;
492 *ppvObj = NULL;
493 return E_NOINTERFACE;
496 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
498 return 2; /* non-heap-based object */
501 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
503 return 1; /* non-heap-based object */
506 static const IUnknownVtbl TestUnknown_Vtbl =
508 Test_IUnknown_QueryInterface,
509 Test_IUnknown_AddRef,
510 Test_IUnknown_Release,
513 static HRESULT WINAPI Test_IStream_QueryInterface(IStream *iface,
514 REFIID riid, LPVOID *ppvObj)
516 if (ppvObj == NULL) return E_POINTER;
518 if (IsEqualIID(riid, &IID_IUnknown) ||
519 IsEqualIID(riid, &IID_IStream))
521 *ppvObj = iface;
522 IStream_AddRef(iface);
523 return S_OK;
526 *ppvObj = NULL;
527 return E_NOINTERFACE;
530 static ULONG WINAPI Test_IStream_AddRef(IStream *iface)
532 return 2; /* non-heap-based object */
535 static ULONG WINAPI Test_IStream_Release(IStream *iface)
537 return 1; /* non-heap-based object */
540 static const IStreamVtbl TestStream_Vtbl =
542 Test_IStream_QueryInterface,
543 Test_IStream_AddRef,
544 Test_IStream_Release
545 /* the rest can be NULLs */
548 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
549 static IStream Test_Stream = { &TestStream_Vtbl };
551 ULONG __RPC_USER WdtpInterfacePointer_UserSize(ULONG *, ULONG, ULONG, IUnknown *, REFIID);
552 unsigned char * __RPC_USER WdtpInterfacePointer_UserMarshal(ULONG *, ULONG, unsigned char *, IUnknown *, REFIID);
553 unsigned char * __RPC_USER WdtpInterfacePointer_UserUnmarshal(ULONG *, unsigned char *, IUnknown **, REFIID);
554 void __RPC_USER WdtpInterfacePointer_UserFree(IUnknown *);
556 static void marshal_WdtpInterfacePointer(DWORD umcb_ctx, DWORD ctx)
558 USER_MARSHAL_CB umcb;
559 MIDL_STUB_MESSAGE stub_msg;
560 RPC_MESSAGE rpc_msg;
561 unsigned char *buffer, *buffer_end;
562 ULONG size;
563 IUnknown *unk;
564 IUnknown *unk2;
565 unsigned char *wireip;
566 HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, 0);
567 IStream *stm;
568 void *marshal_data;
569 LARGE_INTEGER zero;
570 ULARGE_INTEGER pos;
571 DWORD marshal_size;
573 /* shows that the WdtpInterfacePointer functions don't marshal anything for
574 * NULL pointers, so code using these functions must handle that case
575 * itself */
577 unk = NULL;
578 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, umcb_ctx);
579 size = WdtpInterfacePointer_UserSize(&umcb.Flags, ctx, 0, unk, &IID_IUnknown);
580 ok(size == 0, "size should be 0 bytes, not %d\n", size);
581 buffer = HeapAlloc(GetProcessHeap(), 0, size);
582 buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, ctx, buffer, unk, &IID_IUnknown);
583 HeapFree(GetProcessHeap(), 0, buffer);
585 /* Now for a non-NULL pointer. The marshalled data are two size DWORDS and then
586 the result of CoMarshalInterface called with the LOWORD of the ctx */
588 unk = &Test_Unknown;
590 CreateStreamOnHGlobal(h, TRUE, &stm);
591 CoMarshalInterface(stm, &IID_IUnknown, unk, LOWORD(ctx), NULL, MSHLFLAGS_NORMAL);
592 zero.QuadPart = 0;
593 IStream_Seek(stm, zero, STREAM_SEEK_CUR, &pos);
594 marshal_size = pos.u.LowPart;
595 marshal_data = GlobalLock(h);
596 trace("marshal_size %x\n", marshal_size);
598 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, umcb_ctx);
599 size = WdtpInterfacePointer_UserSize(&umcb.Flags, ctx, 0, unk, &IID_IUnknown);
600 ok(size >= marshal_size + 2 * sizeof(DWORD), "marshal size %x got %x\n", marshal_size, size);
601 trace("WdtpInterfacePointer_UserSize returned %x\n", size);
602 buffer = HeapAlloc(GetProcessHeap(), 0, size);
603 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, umcb_ctx);
604 buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, ctx, buffer, unk, &IID_IUnknown);
605 wireip = buffer;
607 ok(buffer_end == buffer + marshal_size + 2 * sizeof(DWORD), "buffer_end %p buffer %p\n", buffer_end, buffer);
609 ok(*(DWORD *)wireip == marshal_size, "wireip + 0x0 should be %x instead of %x\n", marshal_size, *(DWORD *)wireip);
610 wireip += sizeof(DWORD);
611 ok(*(DWORD *)wireip == marshal_size, "wireip + 0x4 should be %x instead of %x\n", marshal_size, *(DWORD *)wireip);
612 wireip += sizeof(DWORD);
614 ok(!memcmp(marshal_data, wireip, marshal_size), "buffer mismatch\n");
615 GlobalUnlock(h);
616 zero.QuadPart = 0;
617 IStream_Seek(stm, zero, STREAM_SEEK_SET, NULL);
618 CoReleaseMarshalData(stm);
619 IStream_Release(stm);
621 unk2 = NULL;
622 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, umcb_ctx);
623 WdtpInterfacePointer_UserUnmarshal(&umcb.Flags, buffer, &unk2, &IID_IUnknown);
624 ok(unk2 != NULL, "IUnknown object didn't unmarshal properly\n");
625 HeapFree(GetProcessHeap(), 0, buffer);
626 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_INPROC);
627 WdtpInterfacePointer_UserFree(unk2);
630 static void test_marshal_WdtpInterfacePointer(void)
633 * There are two places where we can pass the marshalling ctx: as
634 * part of the umcb and as a separate flag. The loword of that
635 * separate flag field is what matters.
638 /* All three are marshalled as inproc */
639 marshal_WdtpInterfacePointer(MSHCTX_INPROC, MSHCTX_INPROC);
640 marshal_WdtpInterfacePointer(MSHCTX_DIFFERENTMACHINE, MSHCTX_INPROC);
641 marshal_WdtpInterfacePointer(MSHCTX_INPROC, MAKELONG(MSHCTX_INPROC, 0xffff));
643 /* All three are marshalled as remote */
644 marshal_WdtpInterfacePointer(MSHCTX_INPROC, MSHCTX_DIFFERENTMACHINE);
645 marshal_WdtpInterfacePointer(MSHCTX_DIFFERENTMACHINE, MSHCTX_DIFFERENTMACHINE);
646 marshal_WdtpInterfacePointer(MSHCTX_INPROC, MAKELONG(MSHCTX_DIFFERENTMACHINE, 0xffff));
649 static void test_marshal_STGMEDIUM(void)
651 USER_MARSHAL_CB umcb;
652 MIDL_STUB_MESSAGE stub_msg;
653 RPC_MESSAGE rpc_msg;
654 unsigned char *buffer, *buffer_end, *expect_buffer, *expect_buffer_end;
655 ULONG size, expect_size;
656 STGMEDIUM med, med2;
657 IUnknown *unk = &Test_Unknown;
658 IStream *stm = &Test_Stream;
660 /* TYMED_NULL with pUnkForRelease */
662 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
663 expect_size = WdtpInterfacePointer_UserSize(&umcb.Flags, umcb.Flags, 2 * sizeof(DWORD), unk, &IID_IUnknown);
664 expect_buffer = HeapAlloc(GetProcessHeap(), 0, expect_size);
665 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, expect_buffer, expect_size, MSHCTX_DIFFERENTMACHINE);
666 expect_buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, umcb.Flags, expect_buffer + 2 * sizeof(DWORD), unk, &IID_IUnknown);
668 med.tymed = TYMED_NULL;
669 U(med).pstg = NULL;
670 med.pUnkForRelease = unk;
672 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
673 size = STGMEDIUM_UserSize(&umcb.Flags, 0, &med);
674 ok(size == expect_size, "size %d should be %d bytes\n", size, expect_size);
676 buffer = HeapAlloc(GetProcessHeap(), 0, size);
677 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
678 buffer_end = STGMEDIUM_UserMarshal(&umcb.Flags, buffer, &med);
679 ok(buffer_end - buffer == expect_buffer_end - expect_buffer, "buffer size mismatch\n");
680 ok(*(DWORD*)buffer == TYMED_NULL, "got %08x\n", *(DWORD*)buffer);
681 ok(*((DWORD*)buffer+1) != 0, "got %08x\n", *((DWORD*)buffer+1));
682 ok(!memcmp(buffer+8, expect_buffer + 8, expect_buffer_end - expect_buffer - 8), "buffer mismatch\n");
684 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
686 /* native crashes if this is uninitialised, presumably because it
687 tries to release it */
688 med2.tymed = TYMED_NULL;
689 U(med2).pstm = NULL;
690 med2.pUnkForRelease = NULL;
692 STGMEDIUM_UserUnmarshal(&umcb.Flags, buffer, &med2);
694 ok(med2.tymed == TYMED_NULL, "got tymed %x\n", med2.tymed);
695 ok(med2.pUnkForRelease != NULL, "Incorrectly unmarshalled\n");
697 HeapFree(GetProcessHeap(), 0, buffer);
698 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
699 STGMEDIUM_UserFree(&umcb.Flags, &med2);
701 HeapFree(GetProcessHeap(), 0, expect_buffer);
703 /* TYMED_ISTREAM with pUnkForRelease */
705 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
706 expect_size = WdtpInterfacePointer_UserSize(&umcb.Flags, umcb.Flags, 3 * sizeof(DWORD), (IUnknown*)stm, &IID_IStream);
707 expect_size = WdtpInterfacePointer_UserSize(&umcb.Flags, umcb.Flags, expect_size, unk, &IID_IUnknown);
709 expect_buffer = HeapAlloc(GetProcessHeap(), 0, expect_size);
710 /* There may be a hole between the two interfaces so init the buffer to something */
711 memset(expect_buffer, 0xcc, expect_size);
712 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, expect_buffer, expect_size, MSHCTX_DIFFERENTMACHINE);
713 expect_buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, umcb.Flags, expect_buffer + 3 * sizeof(DWORD), (IUnknown*)stm, &IID_IStream);
714 expect_buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, umcb.Flags, expect_buffer_end, unk, &IID_IUnknown);
716 med.tymed = TYMED_ISTREAM;
717 U(med).pstm = stm;
718 med.pUnkForRelease = unk;
720 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
721 size = STGMEDIUM_UserSize(&umcb.Flags, 0, &med);
722 ok(size == expect_size, "size %d should be %d bytes\n", size, expect_size);
724 buffer = HeapAlloc(GetProcessHeap(), 0, size);
725 memset(buffer, 0xcc, size);
726 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
727 buffer_end = STGMEDIUM_UserMarshal(&umcb.Flags, buffer, &med);
728 ok(buffer_end - buffer == expect_buffer_end - expect_buffer, "buffer size mismatch\n");
729 ok(*(DWORD*)buffer == TYMED_ISTREAM, "got %08x\n", *(DWORD*)buffer);
730 ok(*((DWORD*)buffer+1) != 0, "got %08x\n", *((DWORD*)buffer+1));
731 ok(*((DWORD*)buffer+2) != 0, "got %08x\n", *((DWORD*)buffer+2));
732 ok(!memcmp(buffer + 12, expect_buffer + 12, (buffer_end - buffer) - 12), "buffer mismatch\n");
734 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
736 /* native crashes if this is uninitialised, presumably because it
737 tries to release it */
738 med2.tymed = TYMED_NULL;
739 U(med2).pstm = NULL;
740 med2.pUnkForRelease = NULL;
742 STGMEDIUM_UserUnmarshal(&umcb.Flags, buffer, &med2);
744 ok(med2.tymed == TYMED_ISTREAM, "got tymed %x\n", med2.tymed);
745 ok(U(med2).pstm != NULL, "Incorrectly unmarshalled\n");
746 ok(med2.pUnkForRelease != NULL, "Incorrectly unmarshalled\n");
748 HeapFree(GetProcessHeap(), 0, buffer);
749 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
750 STGMEDIUM_UserFree(&umcb.Flags, &med2);
752 HeapFree(GetProcessHeap(), 0, expect_buffer);
755 START_TEST(usrmarshal)
757 CoInitialize(NULL);
759 test_marshal_CLIPFORMAT();
760 test_marshal_HWND();
761 test_marshal_HGLOBAL();
762 test_marshal_HENHMETAFILE();
763 test_marshal_HMETAFILE();
764 test_marshal_HMETAFILEPICT();
765 test_marshal_WdtpInterfacePointer();
766 test_marshal_STGMEDIUM();
768 CoUninitialize();