push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / ole32 / tests / usrmarshal.c
blobb6e37fa089c9433a01af034c6c58f470461a5363
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 *, unsigned long, 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 %p\n", hwnd, (HANDLE)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;
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 HGLOBAL_UserUnmarshal(&umcb.Flags, buffer, &hglobal2);
186 ok(hglobal2 == hglobal, "Didn't unmarshal properly\n");
187 HeapFree(GetProcessHeap(), 0, buffer);
188 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
189 HGLOBAL_UserFree(&umcb.Flags, &hglobal2);
191 hglobal = GlobalAlloc(0, 4);
192 buffer = GlobalLock(hglobal);
193 for (i = 0; i < 4; i++)
194 buffer[i] = i;
195 GlobalUnlock(hglobal);
196 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
197 size = HGLOBAL_UserSize(&umcb.Flags, 0, &hglobal);
198 /* native is poorly programmed and allocates 4/8 bytes more than it needs to
199 * here - Wine doesn't have to emulate that */
200 ok((size == 24) || broken(size == 28) || broken(size == 32), "Size should be 24, instead of %d\n", size);
201 buffer = HeapAlloc(GetProcessHeap(), 0, size);
202 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
203 HGLOBAL_UserMarshal(&umcb.Flags, buffer, &hglobal);
204 wirehglobal = buffer;
205 ok(*(ULONG *)wirehglobal == WDT_REMOTE_CALL, "Context should be WDT_REMOTE_CALL instead of 0x%08x\n", *(ULONG *)wirehglobal);
206 wirehglobal += sizeof(ULONG);
207 ok(*(ULONG *)wirehglobal == (ULONG)(ULONG_PTR)hglobal, "buffer+0x4 should be HGLOBAL\n");
208 wirehglobal += sizeof(ULONG);
209 ok(*(ULONG *)wirehglobal == 4, "buffer+0x8 should be size of HGLOBAL instead of %d\n", *(ULONG *)wirehglobal);
210 wirehglobal += sizeof(ULONG);
211 ok(*(ULONG *)wirehglobal == (ULONG)(ULONG_PTR)hglobal, "buffer+0xc should be HGLOBAL\n");
212 wirehglobal += sizeof(ULONG);
213 ok(*(ULONG *)wirehglobal == 4, "buffer+0x10 should be size of HGLOBAL instead of %d\n", *(ULONG *)wirehglobal);
214 wirehglobal += sizeof(ULONG);
215 for (i = 0; i < 4; i++)
216 ok(wirehglobal[i] == i, "buffer+0x%x should be %d\n", 0x10 + i, i);
217 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_LOCAL);
218 HGLOBAL_UserUnmarshal(&umcb.Flags, buffer, &hglobal2);
219 ok(hglobal2 != NULL, "Didn't unmarshal properly\n");
220 HeapFree(GetProcessHeap(), 0, buffer);
221 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_LOCAL);
222 HGLOBAL_UserFree(&umcb.Flags, &hglobal2);
223 GlobalFree(hglobal);
226 static HENHMETAFILE create_emf(void)
228 const RECT rect = {0, 0, 100, 100};
229 HDC hdc = CreateEnhMetaFile(NULL, NULL, &rect, "HENHMETAFILE Marshaling Test\0Test\0\0");
230 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
231 return CloseEnhMetaFile(hdc);
234 static void test_marshal_HENHMETAFILE(void)
236 USER_MARSHAL_CB umcb;
237 MIDL_STUB_MESSAGE stub_msg;
238 RPC_MESSAGE rpc_msg;
239 unsigned char *buffer;
240 ULONG size;
241 HENHMETAFILE hemf;
242 HENHMETAFILE hemf2 = NULL;
243 unsigned char *wirehemf;
245 hemf = create_emf();
247 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
248 size = HENHMETAFILE_UserSize(&umcb.Flags, 0, &hemf);
249 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
250 buffer = HeapAlloc(GetProcessHeap(), 0, size);
251 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
252 HENHMETAFILE_UserMarshal(&umcb.Flags, buffer, &hemf);
253 wirehemf = buffer;
254 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehemf);
255 wirehemf += sizeof(DWORD);
256 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08x\n", *(DWORD *)wirehemf);
257 wirehemf += sizeof(DWORD);
258 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehemf);
259 wirehemf += sizeof(DWORD);
260 ok(*(DWORD *)wirehemf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehemf);
261 wirehemf += sizeof(DWORD);
262 ok(*(DWORD *)wirehemf == EMR_HEADER, "wirestgm + 0x10 should be EMR_HEADER instead of %d\n", *(DWORD *)wirehemf);
263 wirehemf += sizeof(DWORD);
264 /* ... rest of data not tested - refer to tests for GetEnhMetaFileBits
265 * at this point */
267 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
268 HENHMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hemf2);
269 ok(hemf2 != NULL, "HENHMETAFILE didn't unmarshal\n");
270 HeapFree(GetProcessHeap(), 0, buffer);
271 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
272 HENHMETAFILE_UserFree(&umcb.Flags, &hemf2);
273 DeleteEnhMetaFile(hemf);
275 /* test NULL emf */
276 hemf = NULL;
278 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
279 size = HENHMETAFILE_UserSize(&umcb.Flags, 0, &hemf);
280 ok(size == 8, "size should be 8 bytes, not %d\n", size);
281 buffer = HeapAlloc(GetProcessHeap(), 0, size);
282 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
283 HENHMETAFILE_UserMarshal(&umcb.Flags, buffer, &hemf);
284 wirehemf = buffer;
285 ok(*(DWORD *)wirehemf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehemf);
286 wirehemf += sizeof(DWORD);
287 ok(*(DWORD *)wirehemf == (DWORD)(DWORD_PTR)hemf, "wirestgm + 0x4 should be hemf instead of 0x%08x\n", *(DWORD *)wirehemf);
288 wirehemf += sizeof(DWORD);
290 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
291 HENHMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hemf2);
292 ok(hemf2 == NULL, "NULL HENHMETAFILE didn't unmarshal\n");
293 HeapFree(GetProcessHeap(), 0, buffer);
294 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
295 HENHMETAFILE_UserFree(&umcb.Flags, &hemf2);
298 static HMETAFILE create_mf(void)
300 RECT rect = {0, 0, 100, 100};
301 HDC hdc = CreateMetaFile(NULL);
302 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
303 return CloseMetaFile(hdc);
306 static void test_marshal_HMETAFILE(void)
308 USER_MARSHAL_CB umcb;
309 MIDL_STUB_MESSAGE stub_msg;
310 RPC_MESSAGE rpc_msg;
311 unsigned char *buffer;
312 ULONG size;
313 HMETAFILE hmf;
314 HMETAFILE hmf2 = NULL;
315 unsigned char *wirehmf;
317 hmf = create_mf();
319 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
320 size = HMETAFILE_UserSize(&umcb.Flags, 0, &hmf);
321 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
322 buffer = HeapAlloc(GetProcessHeap(), 0, size);
323 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
324 HMETAFILE_UserMarshal(&umcb.Flags, buffer, &hmf);
325 wirehmf = buffer;
326 ok(*(DWORD *)wirehmf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmf);
327 wirehmf += sizeof(DWORD);
328 ok(*(DWORD *)wirehmf == (DWORD)(DWORD_PTR)hmf, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmf);
329 wirehmf += sizeof(DWORD);
330 ok(*(DWORD *)wirehmf == (size - 0x10), "wirestgm + 0x8 should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehmf);
331 wirehmf += sizeof(DWORD);
332 ok(*(DWORD *)wirehmf == (size - 0x10), "wirestgm + 0xc should be size - 0x10 instead of 0x%08x\n", *(DWORD *)wirehmf);
333 wirehmf += sizeof(DWORD);
334 ok(*(WORD *)wirehmf == 1, "wirestgm + 0x10 should be 1 instead of 0x%08x\n", *(DWORD *)wirehmf);
335 wirehmf += sizeof(DWORD);
336 /* ... rest of data not tested - refer to tests for GetMetaFileBits
337 * at this point */
339 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
340 HMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hmf2);
341 ok(hmf2 != NULL, "HMETAFILE didn't unmarshal\n");
342 HeapFree(GetProcessHeap(), 0, buffer);
343 HMETAFILE_UserFree(&umcb.Flags, &hmf2);
344 DeleteMetaFile(hmf);
346 /* test NULL emf */
347 hmf = NULL;
349 size = HMETAFILE_UserSize(&umcb.Flags, 0, &hmf);
350 ok(size == 8, "size should be 8 bytes, not %d\n", size);
351 buffer = HeapAlloc(GetProcessHeap(), 0, size);
352 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
353 HMETAFILE_UserMarshal(&umcb.Flags, buffer, &hmf);
354 wirehmf = buffer;
355 ok(*(DWORD *)wirehmf == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmf);
356 wirehmf += sizeof(DWORD);
357 ok(*(DWORD *)wirehmf == (DWORD)(DWORD_PTR)hmf, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmf);
358 wirehmf += sizeof(DWORD);
360 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
361 HMETAFILE_UserUnmarshal(&umcb.Flags, buffer, &hmf2);
362 ok(hmf2 == NULL, "NULL HMETAFILE didn't unmarshal\n");
363 HeapFree(GetProcessHeap(), 0, buffer);
364 HMETAFILE_UserFree(&umcb.Flags, &hmf2);
367 #define USER_MARSHAL_PTR_PREFIX \
368 ( (DWORD)'U' | ( (DWORD)'s' << 8 ) | \
369 ( (DWORD)'e' << 16 ) | ( (DWORD)'r' << 24 ) )
371 static void test_marshal_HMETAFILEPICT(void)
373 USER_MARSHAL_CB umcb;
374 MIDL_STUB_MESSAGE stub_msg;
375 RPC_MESSAGE rpc_msg;
376 unsigned char *buffer, *buffer_end;
377 ULONG size;
378 HMETAFILEPICT hmfp;
379 HMETAFILEPICT hmfp2 = NULL;
380 METAFILEPICT *pmfp;
381 unsigned char *wirehmfp;
383 hmfp = GlobalAlloc(GMEM_MOVEABLE, sizeof(*pmfp));
384 pmfp = GlobalLock(hmfp);
385 pmfp->mm = MM_ISOTROPIC;
386 pmfp->xExt = 1;
387 pmfp->yExt = 2;
388 pmfp->hMF = create_mf();
389 GlobalUnlock(hmfp);
391 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
392 size = HMETAFILEPICT_UserSize(&umcb.Flags, 0, &hmfp);
393 ok(size > 20, "size should be at least 20 bytes, not %d\n", size);
394 trace("size is %d\n", size);
395 buffer = HeapAlloc(GetProcessHeap(), 0, size);
396 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
397 buffer_end = HMETAFILEPICT_UserMarshal(&umcb.Flags, buffer, &hmfp);
398 wirehmfp = buffer;
399 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
400 wirehmfp += sizeof(DWORD);
401 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)hmfp, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmfp);
402 wirehmfp += sizeof(DWORD);
403 ok(*(DWORD *)wirehmfp == MM_ISOTROPIC, "wirestgm + 0x8 should be MM_ISOTROPIC instead of 0x%08x\n", *(DWORD *)wirehmfp);
404 wirehmfp += sizeof(DWORD);
405 ok(*(DWORD *)wirehmfp == 1, "wirestgm + 0xc should be 1 instead of 0x%08x\n", *(DWORD *)wirehmfp);
406 wirehmfp += sizeof(DWORD);
407 ok(*(DWORD *)wirehmfp == 2, "wirestgm + 0x10 should be 2 instead of 0x%08x\n", *(DWORD *)wirehmfp);
408 wirehmfp += sizeof(DWORD);
409 ok(*(DWORD *)wirehmfp == USER_MARSHAL_PTR_PREFIX, "wirestgm + 0x14 should be \"User\" instead of 0x%08x\n", *(DWORD *)wirehmfp);
410 wirehmfp += sizeof(DWORD);
411 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x18 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
412 wirehmfp += sizeof(DWORD);
413 pmfp = GlobalLock(hmfp);
414 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)pmfp->hMF, "wirestgm + 0x1c should be pmfp->hMF instead of 0x%08x\n", *(DWORD *)wirehmfp);
415 GlobalUnlock(hmfp);
416 wirehmfp += sizeof(DWORD);
417 /* Note use (buffer_end - buffer) instead of size here, because size is an
418 * overestimate with native */
419 ok(*(DWORD *)wirehmfp == (buffer_end - buffer - 0x28), "wirestgm + 0x20 should be size - 0x34 instead of 0x%08x\n", *(DWORD *)wirehmfp);
420 wirehmfp += sizeof(DWORD);
421 ok(*(DWORD *)wirehmfp == (buffer_end - buffer - 0x28), "wirestgm + 0x24 should be size - 0x34 instead of 0x%08x\n", *(DWORD *)wirehmfp);
422 wirehmfp += sizeof(DWORD);
423 ok(*(WORD *)wirehmfp == 1, "wirehmfp + 0x28 should be 1 instead of 0x%08x\n", *(DWORD *)wirehmfp);
424 wirehmfp += sizeof(DWORD);
425 /* ... rest of data not tested - refer to tests for GetMetaFileBits
426 * at this point */
428 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
429 HMETAFILEPICT_UserUnmarshal(&umcb.Flags, buffer, &hmfp2);
430 ok(hmfp2 != NULL, "HMETAFILEPICT didn't unmarshal\n");
431 HeapFree(GetProcessHeap(), 0, buffer);
432 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
433 HMETAFILEPICT_UserFree(&umcb.Flags, &hmfp2);
434 pmfp = GlobalLock(hmfp);
435 DeleteMetaFile(pmfp->hMF);
436 GlobalUnlock(hmfp);
437 GlobalFree(hmfp);
439 /* test NULL emf */
440 hmfp = NULL;
442 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
443 size = HMETAFILEPICT_UserSize(&umcb.Flags, 0, &hmfp);
444 ok(size == 8, "size should be 8 bytes, not %d\n", size);
445 buffer = HeapAlloc(GetProcessHeap(), 0, size);
446 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
447 HMETAFILEPICT_UserMarshal(&umcb.Flags, buffer, &hmfp);
448 wirehmfp = buffer;
449 ok(*(DWORD *)wirehmfp == WDT_REMOTE_CALL, "wirestgm + 0x0 should be WDT_REMOTE_CALL instead of 0x%08x\n", *(DWORD *)wirehmfp);
450 wirehmfp += sizeof(DWORD);
451 ok(*(DWORD *)wirehmfp == (DWORD)(DWORD_PTR)hmfp, "wirestgm + 0x4 should be hmf instead of 0x%08x\n", *(DWORD *)wirehmfp);
452 wirehmfp += sizeof(DWORD);
454 hmfp2 = NULL;
455 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_DIFFERENTMACHINE);
456 HMETAFILEPICT_UserUnmarshal(&umcb.Flags, buffer, &hmfp2);
457 ok(hmfp2 == NULL, "NULL HMETAFILE didn't unmarshal\n");
458 HeapFree(GetProcessHeap(), 0, buffer);
459 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_DIFFERENTMACHINE);
460 HMETAFILEPICT_UserFree(&umcb.Flags, &hmfp2);
463 static HRESULT WINAPI Test_IUnknown_QueryInterface(
464 LPUNKNOWN iface,
465 REFIID riid,
466 LPVOID *ppvObj)
468 if (ppvObj == NULL) return E_POINTER;
470 if (IsEqualGUID(riid, &IID_IUnknown))
472 *ppvObj = iface;
473 IUnknown_AddRef(iface);
474 return S_OK;
477 *ppvObj = NULL;
478 return E_NOINTERFACE;
481 static ULONG WINAPI Test_IUnknown_AddRef(LPUNKNOWN iface)
483 return 2; /* non-heap-based object */
486 static ULONG WINAPI Test_IUnknown_Release(LPUNKNOWN iface)
488 return 1; /* non-heap-based object */
491 static const IUnknownVtbl TestUnknown_Vtbl =
493 Test_IUnknown_QueryInterface,
494 Test_IUnknown_AddRef,
495 Test_IUnknown_Release,
498 static IUnknown Test_Unknown = { &TestUnknown_Vtbl };
500 ULONG __RPC_USER WdtpInterfacePointer_UserSize(ULONG *, ULONG, ULONG, IUnknown *, REFIID);
501 unsigned char * __RPC_USER WdtpInterfacePointer_UserMarshal(ULONG *, ULONG, unsigned char *, IUnknown *, REFIID);
502 unsigned char * __RPC_USER WdtpInterfacePointer_UserUnmarshal(ULONG *, unsigned char *, IUnknown **, REFIID);
503 void __RPC_USER WdtpInterfacePointer_UserFree(IUnknown *);
505 static void test_marshal_WdtpInterfacePointer(void)
507 USER_MARSHAL_CB umcb;
508 MIDL_STUB_MESSAGE stub_msg;
509 RPC_MESSAGE rpc_msg;
510 unsigned char *buffer, *buffer_end;
511 ULONG size;
512 IUnknown *unk;
513 IUnknown *unk2;
514 unsigned char *wireip;
516 /* shows that the WdtpInterfacePointer functions don't marshal anything for
517 * NULL pointers, so code using these functions must handle that case
518 * itself */
519 unk = NULL;
520 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_INPROC);
521 size = WdtpInterfacePointer_UserSize(&umcb.Flags, umcb.Flags, 0, unk, &IID_IUnknown);
522 ok(size == 0, "size should be 0 bytes, not %d\n", size);
523 buffer = HeapAlloc(GetProcessHeap(), 0, size);
524 buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, umcb.Flags, buffer, unk, &IID_IUnknown);
525 wireip = buffer;
526 HeapFree(GetProcessHeap(), 0, buffer);
528 unk = &Test_Unknown;
529 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_INPROC);
530 size = WdtpInterfacePointer_UserSize(&umcb.Flags, umcb.Flags, 0, unk, &IID_IUnknown);
531 todo_wine
532 ok(size >= 0x4c, "size should be >= 0x4c bytes, not %d\n", size);
533 trace("WdtpInterfacePointer_UserSize returned %d\n", size);
534 buffer = HeapAlloc(GetProcessHeap(), 0, size);
535 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_INPROC);
536 buffer_end = WdtpInterfacePointer_UserMarshal(&umcb.Flags, umcb.Flags, buffer, unk, &IID_IUnknown);
537 wireip = buffer;
538 if (size >= 0x4c)
540 HGLOBAL h = GlobalAlloc(GMEM_MOVEABLE, 0);
541 IStream *stm;
542 void *ptr;
543 LARGE_INTEGER pos;
544 DWORD h_size;
546 ok(buffer_end == buffer + 0x4c, "buffer_end %p buffer %p\n", buffer_end, buffer);
547 ok(*(DWORD *)wireip == 0x44, "wireip + 0x0 should be 0x44 instead of 0x%08x\n", *(DWORD *)wireip);
548 wireip += sizeof(DWORD);
549 ok(*(DWORD *)wireip == 0x44, "wireip + 0x4 should be 0x44 instead of 0x%08x\n", *(DWORD *)wireip);
550 wireip += sizeof(DWORD);
552 /* The remaining 0x44 bytes are the result of CoMarshalInterface */
554 CreateStreamOnHGlobal(h, TRUE, &stm);
555 CoMarshalInterface(stm, &IID_IUnknown, unk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
556 h_size = GlobalSize(h);
557 ok(h_size == 0x44, "size %x\n", h_size);
559 ptr = GlobalLock(h);
560 ok(!memcmp(ptr, wireip, 0x44), "buffer mismatch\n");
561 GlobalUnlock(h);
562 pos.QuadPart = 0;
563 IStream_Seek(stm, pos, STREAM_SEEK_SET, NULL);
564 CoReleaseMarshalData(stm);
565 IStream_Release(stm);
568 unk2 = NULL;
569 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, buffer, size, MSHCTX_INPROC);
570 WdtpInterfacePointer_UserUnmarshal(&umcb.Flags, buffer, &unk2, &IID_IUnknown);
571 todo_wine
572 ok(unk2 != NULL, "IUnknown object didn't unmarshal properly\n");
573 HeapFree(GetProcessHeap(), 0, buffer);
574 init_user_marshal_cb(&umcb, &stub_msg, &rpc_msg, NULL, 0, MSHCTX_INPROC);
575 WdtpInterfacePointer_UserFree(unk2);
578 START_TEST(usrmarshal)
580 CoInitialize(NULL);
582 test_marshal_CLIPFORMAT();
583 test_marshal_HWND();
584 test_marshal_HGLOBAL();
585 test_marshal_HENHMETAFILE();
586 test_marshal_HMETAFILE();
587 test_marshal_HMETAFILEPICT();
588 test_marshal_WdtpInterfacePointer();
590 CoUninitialize();