comctl32: listview: When shrinking the last column clear the now unused field.
[wine] / dlls / ole32 / usrmarshal.c
1 /*
2  * Miscellaneous Marshaling Routines
3  *
4  * Copyright 2005 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winerror.h"
34
35 #include "ole2.h"
36 #include "oleauto.h"
37 #include "rpcproxy.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 #define ALIGNED_LENGTH(_Len, _Align) (((_Len)+(_Align))&~(_Align))
43 #define ALIGNED_POINTER(_Ptr, _Align) ((LPVOID)ALIGNED_LENGTH((ULONG_PTR)(_Ptr), _Align))
44 #define ALIGN_LENGTH(_Len, _Align) _Len = ALIGNED_LENGTH(_Len, _Align)
45 #define ALIGN_POINTER(_Ptr, _Align) _Ptr = ALIGNED_POINTER(_Ptr, _Align)
46
47 #define USER_MARSHAL_PTR_PREFIX \
48   ( (DWORD)'U'         | ( (DWORD)'s' << 8 ) | \
49   ( (DWORD)'e' << 16 ) | ( (DWORD)'r' << 24 ) )
50
51 static const char* debugstr_user_flags(ULONG *pFlags)
52 {
53     char buf[12];
54     const char* loword;
55     switch (LOWORD(*pFlags))
56     {
57     case MSHCTX_LOCAL:
58         loword="MSHCTX_LOCAL";
59         break;
60     case MSHCTX_NOSHAREDMEM:
61         loword="MSHCTX_NOSHAREDMEM";
62         break;
63     case MSHCTX_DIFFERENTMACHINE:
64         loword="MSHCTX_DIFFERENTMACHINE";
65         break;
66     case MSHCTX_INPROC:
67         loword="MSHCTX_INPROC";
68         break;
69     default:
70         sprintf(buf, "%d", LOWORD(*pFlags));
71         loword=buf;
72     }
73
74     if (HIWORD(*pFlags) == NDR_LOCAL_DATA_REPRESENTATION)
75         return wine_dbg_sprintf("MAKELONG(NDR_LOCAL_REPRESENTATION, %s)", loword);
76     else
77         return wine_dbg_sprintf("MAKELONG(0x%04x, %s)", HIWORD(*pFlags), loword);
78 }
79
80 /******************************************************************************
81  *           CLIPFORMAT_UserSize [OLE32.@]
82  *
83  * Calculates the buffer size required to marshal a clip format.
84  *
85  * PARAMS
86  *  pFlags       [I] Flags. See notes.
87  *  StartingSize [I] Starting size of the buffer. This value is added on to
88  *                   the buffer size required for the clip format.
89  *  pCF          [I] Clip format to size.
90  *
91  * RETURNS
92  *  The buffer size required to marshal a clip format plus the starting size.
93  *
94  * NOTES
95  *  Even though the function is documented to take a pointer to an unsigned
96  *  long in pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
97  *  the first parameter is an unsigned long.
98  *  This function is only intended to be called by the RPC runtime.
99  */
100 ULONG __RPC_USER CLIPFORMAT_UserSize(ULONG *pFlags, ULONG StartingSize, CLIPFORMAT *pCF)
101 {
102     ULONG size = StartingSize;
103
104     TRACE("(%s, %d, %p\n", debugstr_user_flags(pFlags), StartingSize, pCF);
105
106     size += sizeof(userCLIPFORMAT);
107
108     /* only need to marshal the name if it is not a pre-defined type and
109      * we are going remote */
110     if ((*pCF >= 0xc000) && (LOWORD(*pFlags) == MSHCTX_DIFFERENTMACHINE))
111     {
112         WCHAR format[255];
113         INT ret;
114         size += 3 * sizeof(INT);
115         /* urg! this function is badly designed because it won't tell us how
116          * much space is needed without doing a dummy run of storing the
117          * name into a buffer */
118         ret = GetClipboardFormatNameW(*pCF, format, sizeof(format)/sizeof(format[0])-1);
119         if (!ret)
120             RaiseException(DV_E_CLIPFORMAT, 0, 0, NULL);
121         size += (ret + 1) * sizeof(WCHAR);
122     }
123     return size;
124 }
125
126 /******************************************************************************
127  *           CLIPFORMAT_UserMarshal [OLE32.@]
128  *
129  * Marshals a clip format into a buffer.
130  *
131  * PARAMS
132  *  pFlags  [I] Flags. See notes.
133  *  pBuffer [I] Buffer to marshal the clip format into.
134  *  pCF     [I] Clip format to marshal.
135  *
136  * RETURNS
137  *  The end of the marshaled data in the buffer.
138  *
139  * NOTES
140  *  Even though the function is documented to take a pointer to an unsigned
141  *  long in pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
142  *  the first parameter is an unsigned long.
143  *  This function is only intended to be called by the RPC runtime.
144  */
145 unsigned char * __RPC_USER CLIPFORMAT_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, CLIPFORMAT *pCF)
146 {
147     wireCLIPFORMAT wirecf = (wireCLIPFORMAT)pBuffer;
148
149     TRACE("(%s, %p, &0x%04x\n", debugstr_user_flags(pFlags), pBuffer, *pCF);
150
151     wirecf->u.dwValue = *pCF;
152     pBuffer += sizeof(*wirecf);
153
154     /* only need to marshal the name if it is not a pre-defined type and
155      * we are going remote */
156     if ((*pCF >= 0xc000) && (LOWORD(*pFlags) == MSHCTX_DIFFERENTMACHINE))
157     {
158         WCHAR format[255];
159         INT len;
160         wirecf->fContext = WDT_REMOTE_CALL;
161         len = GetClipboardFormatNameW(*pCF, format, sizeof(format)/sizeof(format[0])-1);
162         if (!len)
163             RaiseException(DV_E_CLIPFORMAT, 0, 0, NULL);
164         len += 1;
165         *(INT *)pBuffer = len;
166         pBuffer += sizeof(INT);
167         *(INT *)pBuffer = 0;
168         pBuffer += sizeof(INT);
169         *(INT *)pBuffer = len;
170         pBuffer += sizeof(INT);
171         TRACE("marshaling format name %s\n", debugstr_wn(format, len-1));
172         lstrcpynW((LPWSTR)pBuffer, format, len);
173         pBuffer += len * sizeof(WCHAR);
174         *(WCHAR *)pBuffer = '\0';
175         pBuffer += sizeof(WCHAR);
176     }
177     else
178         wirecf->fContext = WDT_INPROC_CALL;
179
180     return pBuffer;
181 }
182
183 /******************************************************************************
184  *           CLIPFORMAT_UserUnmarshal [OLE32.@]
185  *
186  * Unmarshals a clip format from a buffer.
187  *
188  * PARAMS
189  *  pFlags  [I] Flags. See notes.
190  *  pBuffer [I] Buffer to marshal the clip format from.
191  *  pCF     [O] Address that receive the unmarshaled clip format.
192  *
193  * RETURNS
194  *  The end of the marshaled data in the buffer.
195  *
196  * NOTES
197  *  Even though the function is documented to take a pointer to an unsigned
198  *  long in pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
199  *  the first parameter is an unsigned long.
200  *  This function is only intended to be called by the RPC runtime.
201  */
202 unsigned char * __RPC_USER CLIPFORMAT_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, CLIPFORMAT *pCF)
203 {
204     wireCLIPFORMAT wirecf = (wireCLIPFORMAT)pBuffer;
205
206     TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, pCF);
207
208     pBuffer += sizeof(*wirecf);
209     if (wirecf->fContext == WDT_INPROC_CALL)
210         *pCF = (CLIPFORMAT)wirecf->u.dwValue;
211     else if (wirecf->fContext == WDT_REMOTE_CALL)
212     {
213         CLIPFORMAT cf;
214         INT len = *(INT *)pBuffer;
215         pBuffer += sizeof(INT);
216         if (*(INT *)pBuffer != 0)
217             RaiseException(RPC_S_INVALID_BOUND, 0, 0, NULL);
218         pBuffer += sizeof(INT);
219         if (*(INT *)pBuffer != len)
220             RaiseException(RPC_S_INVALID_BOUND, 0, 0, NULL);
221         pBuffer += sizeof(INT);
222         if (((WCHAR *)pBuffer)[len] != '\0')
223             RaiseException(RPC_S_INVALID_BOUND, 0, 0, NULL);
224         TRACE("unmarshaling clip format %s\n", debugstr_w((LPCWSTR)pBuffer));
225         cf = RegisterClipboardFormatW((LPCWSTR)pBuffer);
226         pBuffer += (len + 1) * sizeof(WCHAR);
227         if (!cf)
228             RaiseException(DV_E_CLIPFORMAT, 0, 0, NULL);
229         *pCF = cf;
230     }
231     else
232         /* code not really appropriate, but nearest I can find */
233         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
234     return pBuffer;
235 }
236
237 /******************************************************************************
238  *           CLIPFORMAT_UserFree [OLE32.@]
239  *
240  * Frees an unmarshaled clip format.
241  *
242  * PARAMS
243  *  pFlags  [I] Flags. See notes.
244  *  pCF     [I] Clip format to free.
245  *
246  * RETURNS
247  *  The end of the marshaled data in the buffer.
248  *
249  * NOTES
250  *  Even though the function is documented to take a pointer to an unsigned
251  *  long in pFlags, it actually takes a pointer to a USER_MARSHAL_CB
252  *  structure, of which the first parameter is an unsigned long.
253  *  This function is only intended to be called by the RPC runtime.
254  */
255 void __RPC_USER CLIPFORMAT_UserFree(ULONG *pFlags, CLIPFORMAT *pCF)
256 {
257     /* there is no inverse of the RegisterClipboardFormat function,
258      * so nothing to do */
259 }
260
261 static ULONG __RPC_USER handle_UserSize(ULONG *pFlags, ULONG StartingSize, HANDLE *handle)
262 {
263     if (LOWORD(*pFlags) == MSHCTX_DIFFERENTMACHINE)
264     {
265         ERR("can't remote a local handle\n");
266         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
267         return StartingSize;
268     }
269     return StartingSize + sizeof(RemotableHandle);
270 }
271
272 static unsigned char * __RPC_USER handle_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HANDLE *handle)
273 {
274     RemotableHandle *remhandle = (RemotableHandle *)pBuffer;
275     if (LOWORD(*pFlags) == MSHCTX_DIFFERENTMACHINE)
276     {
277         ERR("can't remote a local handle\n");
278         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
279         return pBuffer;
280     }
281     remhandle->fContext = WDT_INPROC_CALL;
282     remhandle->u.hInproc = (LONG_PTR)*handle;
283     return pBuffer + sizeof(RemotableHandle);
284 }
285
286 static unsigned char * __RPC_USER handle_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HANDLE *handle)
287 {
288     RemotableHandle *remhandle = (RemotableHandle *)pBuffer;
289     if (remhandle->fContext != WDT_INPROC_CALL)
290         RaiseException(RPC_X_BAD_STUB_DATA, 0, 0, NULL);
291     *handle = (HANDLE)remhandle->u.hInproc;
292     return pBuffer + sizeof(RemotableHandle);
293 }
294
295 static void __RPC_USER handle_UserFree(ULONG *pFlags, HANDLE *phMenu)
296 {
297     /* nothing to do */
298 }
299
300 #define IMPL_WIREM_HANDLE(type) \
301     ULONG __RPC_USER type##_UserSize(ULONG *pFlags, ULONG StartingSize, type *handle) \
302     { \
303         TRACE("(%s, %d, %p\n", debugstr_user_flags(pFlags), StartingSize, handle); \
304         return handle_UserSize(pFlags, StartingSize, (HANDLE *)handle); \
305     } \
306     \
307     unsigned char * __RPC_USER type##_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, type *handle) \
308     { \
309         TRACE("(%s, %p, &%p\n", debugstr_user_flags(pFlags), pBuffer, *handle); \
310         return handle_UserMarshal(pFlags, pBuffer, (HANDLE *)handle); \
311     } \
312     \
313     unsigned char * __RPC_USER type##_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, type *handle) \
314     { \
315         TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, handle); \
316         return handle_UserUnmarshal(pFlags, pBuffer, (HANDLE *)handle); \
317     } \
318     \
319     void __RPC_USER type##_UserFree(ULONG *pFlags, type *handle) \
320     { \
321         TRACE("(%s, &%p\n", debugstr_user_flags(pFlags), *handle); \
322         return handle_UserFree(pFlags, (HANDLE *)handle); \
323     }
324
325 IMPL_WIREM_HANDLE(HACCEL)
326 IMPL_WIREM_HANDLE(HMENU)
327 IMPL_WIREM_HANDLE(HWND)
328
329 /******************************************************************************
330  *           HGLOBAL_UserSize [OLE32.@]
331  *
332  * Calculates the buffer size required to marshal an HGLOBAL.
333  *
334  * PARAMS
335  *  pFlags       [I] Flags. See notes.
336  *  StartingSize [I] Starting size of the buffer. This value is added on to
337  *                   the buffer size required for the clip format.
338  *  phGlobal     [I] HGLOBAL to size.
339  *
340  * RETURNS
341  *  The buffer size required to marshal an HGLOBAL plus the starting size.
342  *
343  * NOTES
344  *  Even though the function is documented to take a pointer to a ULONG in
345  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
346  *  the first parameter is a ULONG.
347  *  This function is only intended to be called by the RPC runtime.
348  */
349 ULONG __RPC_USER HGLOBAL_UserSize(ULONG *pFlags, ULONG StartingSize, HGLOBAL *phGlobal)
350 {
351     ULONG size = StartingSize;
352
353     TRACE("(%s, %d, %p\n", debugstr_user_flags(pFlags), StartingSize, phGlobal);
354
355     ALIGN_LENGTH(size, 3);
356
357     size += sizeof(ULONG);
358
359     if (LOWORD(*pFlags == MSHCTX_INPROC))
360         size += sizeof(HGLOBAL);
361     else
362     {
363         size += sizeof(ULONG);
364         if (*phGlobal)
365         {
366             SIZE_T ret;
367             size += 3 * sizeof(ULONG);
368             ret = GlobalSize(*phGlobal);
369             size += (ULONG)ret;
370         }
371     }
372     
373     return size;
374 }
375
376 /******************************************************************************
377  *           HGLOBAL_UserMarshal [OLE32.@]
378  *
379  * Marshals an HGLOBAL into a buffer.
380  *
381  * PARAMS
382  *  pFlags   [I] Flags. See notes.
383  *  pBuffer  [I] Buffer to marshal the clip format into.
384  *  phGlobal [I] HGLOBAL to marshal.
385  *
386  * RETURNS
387  *  The end of the marshaled data in the buffer.
388  *
389  * NOTES
390  *  Even though the function is documented to take a pointer to a ULONG in
391  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
392  *  the first parameter is a ULONG.
393  *  This function is only intended to be called by the RPC runtime.
394  */
395 unsigned char * __RPC_USER HGLOBAL_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HGLOBAL *phGlobal)
396 {
397     TRACE("(%s, %p, &%p\n", debugstr_user_flags(pFlags), pBuffer, *phGlobal);
398
399     ALIGN_POINTER(pBuffer, 3);
400
401     if (LOWORD(*pFlags == MSHCTX_INPROC))
402     {
403         if (sizeof(*phGlobal) == 8)
404             *(ULONG *)pBuffer = WDT_INPROC64_CALL;
405         else
406             *(ULONG *)pBuffer = WDT_INPROC_CALL;
407         pBuffer += sizeof(ULONG);
408         *(HGLOBAL *)pBuffer = *phGlobal;
409         pBuffer += sizeof(HGLOBAL);
410     }
411     else
412     {
413         *(ULONG *)pBuffer = WDT_REMOTE_CALL;
414         pBuffer += sizeof(ULONG);
415         *(ULONG *)pBuffer = (ULONG)*phGlobal;
416         pBuffer += sizeof(ULONG);
417         if (*phGlobal)
418         {
419             const unsigned char *memory;
420             SIZE_T size = GlobalSize(*phGlobal);
421             *(ULONG *)pBuffer = (ULONG)size;
422             pBuffer += sizeof(ULONG);
423             *(ULONG *)pBuffer = (ULONG)*phGlobal;
424             pBuffer += sizeof(ULONG);
425             *(ULONG *)pBuffer = (ULONG)size;
426             pBuffer += sizeof(ULONG);
427
428             memory = GlobalLock(*phGlobal);
429             memcpy(pBuffer, memory, size);
430             pBuffer += size;
431             GlobalUnlock(*phGlobal);
432         }
433     }
434
435     return pBuffer;
436 }
437
438 /******************************************************************************
439  *           HGLOBAL_UserUnmarshal [OLE32.@]
440  *
441  * Unmarshals an HGLOBAL from a buffer.
442  *
443  * PARAMS
444  *  pFlags   [I] Flags. See notes.
445  *  pBuffer  [I] Buffer to marshal the clip format from.
446  *  phGlobal [O] Address that receive the unmarshaled HGLOBAL.
447  *
448  * RETURNS
449  *  The end of the marshaled data in the buffer.
450  *
451  * NOTES
452  *  Even though the function is documented to take a pointer to an ULONG in
453  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
454  *  the first parameter is an ULONG.
455  *  This function is only intended to be called by the RPC runtime.
456  */
457 unsigned char * __RPC_USER HGLOBAL_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HGLOBAL *phGlobal)
458 {
459     ULONG fContext;
460
461     TRACE("(%s, %p, &%p\n", debugstr_user_flags(pFlags), pBuffer, *phGlobal);
462
463     ALIGN_POINTER(pBuffer, 3);
464
465     fContext = *(ULONG *)pBuffer;
466     pBuffer += sizeof(ULONG);
467
468     if (((fContext == WDT_INPROC_CALL) && (sizeof(*phGlobal) < 8)) ||
469         ((fContext == WDT_INPROC64_CALL) && (sizeof(*phGlobal) == 8)))
470     {
471         *phGlobal = *(HGLOBAL *)pBuffer;
472         pBuffer += sizeof(*phGlobal);
473     }
474     else if (fContext == WDT_REMOTE_CALL)
475     {
476         ULONG handle;
477
478         handle = *(ULONG *)pBuffer;
479         pBuffer += sizeof(ULONG);
480
481         if (handle)
482         {
483             ULONG size;
484             void *memory;
485
486             size = *(ULONG *)pBuffer;
487             pBuffer += sizeof(ULONG);
488             /* redundancy is bad - it means you have to check consistency like
489              * this: */
490             if (*(ULONG *)pBuffer != handle)
491             {
492                 RaiseException(RPC_X_BAD_STUB_DATA, 0, 0, NULL);
493                 return pBuffer;
494             }
495             pBuffer += sizeof(ULONG);
496             /* redundancy is bad - it means you have to check consistency like
497              * this: */
498             if (*(ULONG *)pBuffer != size)
499             {
500                 RaiseException(RPC_X_BAD_STUB_DATA, 0, 0, NULL);
501                 return pBuffer;
502             }
503             pBuffer += sizeof(ULONG);
504
505             /* FIXME: check size is not too big */
506
507             *phGlobal = GlobalAlloc(GMEM_MOVEABLE, size);
508             memory = GlobalLock(*phGlobal);
509             memcpy(memory, pBuffer, size);
510             pBuffer += size;
511             GlobalUnlock(*phGlobal);
512         }
513         else
514             *phGlobal = NULL;
515     }
516     else
517         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
518
519     return pBuffer;
520 }
521
522 /******************************************************************************
523  *           HGLOBAL_UserFree [OLE32.@]
524  *
525  * Frees an unmarshaled HGLOBAL.
526  *
527  * PARAMS
528  *  pFlags   [I] Flags. See notes.
529  *  phGlobal [I] HGLOBAL to free.
530  *
531  * RETURNS
532  *  The end of the marshaled data in the buffer.
533  *
534  * NOTES
535  *  Even though the function is documented to take a pointer to a ULONG in
536  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
537  *  which the first parameter is a ULONG.
538  *  This function is only intended to be called by the RPC runtime.
539  */
540 void __RPC_USER HGLOBAL_UserFree(ULONG *pFlags, HGLOBAL *phGlobal)
541 {
542     TRACE("(%s, &%p\n", debugstr_user_flags(pFlags), *phGlobal);
543
544     if (LOWORD(*pFlags != MSHCTX_INPROC) && *phGlobal)
545         GlobalFree(*phGlobal);
546 }
547
548 /******************************************************************************
549  *           HBITMAP_UserSize [OLE32.@]
550  *
551  * Calculates the buffer size required to marshal a bitmap.
552  *
553  * PARAMS
554  *  pFlags       [I] Flags. See notes.
555  *  StartingSize [I] Starting size of the buffer. This value is added on to
556  *                   the buffer size required for the clip format.
557  *  phBmp        [I] Bitmap to size.
558  *
559  * RETURNS
560  *  The buffer size required to marshal an bitmap plus the starting size.
561  *
562  * NOTES
563  *  Even though the function is documented to take a pointer to a ULONG in
564  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
565  *  the first parameter is a ULONG.
566  *  This function is only intended to be called by the RPC runtime.
567  */
568 ULONG __RPC_USER HBITMAP_UserSize(ULONG *pFlags, ULONG StartingSize, HBITMAP *phBmp)
569 {
570     FIXME(":stub\n");
571     return StartingSize;
572 }
573
574 /******************************************************************************
575 *           HBITMAP_UserMarshal [OLE32.@]
576 *
577 * Marshals a bitmap into a buffer.
578 *
579 * PARAMS
580 *  pFlags  [I] Flags. See notes.
581 *  pBuffer [I] Buffer to marshal the clip format into.
582 *  phBmp   [I] Bitmap to marshal.
583 *
584 * RETURNS
585 *  The end of the marshaled data in the buffer.
586 *
587 * NOTES
588 *  Even though the function is documented to take a pointer to a ULONG in
589 *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
590 *  the first parameter is a ULONG.
591 *  This function is only intended to be called by the RPC runtime.
592 */
593 unsigned char * __RPC_USER HBITMAP_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HBITMAP *phBmp)
594 {
595     FIXME(":stub\n");
596     return pBuffer;
597 }
598
599 /******************************************************************************
600  *           HBITMAP_UserUnmarshal [OLE32.@]
601  *
602  * Unmarshals a bitmap from a buffer.
603  *
604  * PARAMS
605  *  pFlags   [I] Flags. See notes.
606  *  pBuffer  [I] Buffer to marshal the clip format from.
607  *  phBmp    [O] Address that receive the unmarshaled bitmap.
608  *
609  * RETURNS
610  *  The end of the marshaled data in the buffer.
611  *
612  * NOTES
613  *  Even though the function is documented to take a pointer to an ULONG in
614  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
615  *  the first parameter is an ULONG.
616  *  This function is only intended to be called by the RPC runtime.
617  */
618 unsigned char * __RPC_USER HBITMAP_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HBITMAP *phBmp)
619 {
620     FIXME(":stub\n");
621     return pBuffer;
622 }
623
624 /******************************************************************************
625  *           HBITMAP_UserFree [OLE32.@]
626  *
627  * Frees an unmarshaled bitmap.
628  *
629  * PARAMS
630  *  pFlags   [I] Flags. See notes.
631  *  phBmp    [I] Bitmap to free.
632  *
633  * RETURNS
634  *  The end of the marshaled data in the buffer.
635  *
636  * NOTES
637  *  Even though the function is documented to take a pointer to a ULONG in
638  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
639  *  which the first parameter is a ULONG.
640  *  This function is only intended to be called by the RPC runtime.
641  */
642 void __RPC_USER HBITMAP_UserFree(ULONG *pFlags, HBITMAP *phBmp)
643 {
644     FIXME(":stub\n");
645 }
646
647 /******************************************************************************
648  *           HDC_UserSize [OLE32.@]
649  *
650  * Calculates the buffer size required to marshal an HDC.
651  *
652  * PARAMS
653  *  pFlags       [I] Flags. See notes.
654  *  StartingSize [I] Starting size of the buffer. This value is added on to
655  *                   the buffer size required for the clip format.
656  *  phGlobal     [I] HDC to size.
657  *
658  * RETURNS
659  *  The buffer size required to marshal an HDC plus the starting size.
660  *
661  * NOTES
662  *  Even though the function is documented to take a pointer to a ULONG in
663  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
664  *  the first parameter is a ULONG.
665  *  This function is only intended to be called by the RPC runtime.
666  */
667 ULONG __RPC_USER HDC_UserSize(ULONG *pFlags, ULONG StartingSize, HDC *phdc)
668 {
669     FIXME(":stub\n");
670     return StartingSize;
671 }
672
673 /******************************************************************************
674  *           HDC_UserMarshal [OLE32.@]
675  *
676  * Marshals an HDC into a buffer.
677  *
678  * PARAMS
679  *  pFlags  [I] Flags. See notes.
680  *  pBuffer [I] Buffer to marshal the clip format into.
681  *  phdc    [I] HDC to marshal.
682  *
683  * RETURNS
684  *  The end of the marshaled data in the buffer.
685  *
686  * NOTES
687  *  Even though the function is documented to take a pointer to a ULONG in
688  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
689  *  the first parameter is a ULONG.
690  *  This function is only intended to be called by the RPC runtime.
691  */
692 unsigned char * __RPC_USER HDC_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HDC *phdc)
693 {
694     FIXME(":stub\n");
695     return pBuffer;
696 }
697
698 /******************************************************************************
699  *           HDC_UserUnmarshal [OLE32.@]
700  *
701  * Unmarshals an HDC from a buffer.
702  *
703  * PARAMS
704  *  pFlags   [I] Flags. See notes.
705  *  pBuffer  [I] Buffer to marshal the clip format from.
706  *  phdc     [O] Address that receive the unmarshaled HDC.
707  *
708  * RETURNS
709  *  The end of the marshaled data in the buffer.
710  *
711  * NOTES
712  *  Even though the function is documented to take a pointer to an ULONG in
713  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
714  *  the first parameter is an ULONG.
715  *  This function is only intended to be called by the RPC runtime.
716  */
717 unsigned char * __RPC_USER HDC_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HDC *phdc)
718 {
719     FIXME(":stub\n");
720     return pBuffer;
721 }
722
723 /******************************************************************************
724  *           HDC_UserFree [OLE32.@]
725  *
726  * Frees an unmarshaled HDC.
727  *
728  * PARAMS
729  *  pFlags   [I] Flags. See notes.
730  *  phdc     [I] HDC to free.
731  *
732  * RETURNS
733  *  The end of the marshaled data in the buffer.
734  *
735  * NOTES
736  *  Even though the function is documented to take a pointer to a ULONG in
737  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
738  *  which the first parameter is a ULONG.
739  *  This function is only intended to be called by the RPC runtime.
740  */
741 void __RPC_USER HDC_UserFree(ULONG *pFlags, HDC *phdc)
742 {
743     FIXME(":stub\n");
744 }
745
746 /******************************************************************************
747  *           HPALETTE_UserSize [OLE32.@]
748  *
749  * Calculates the buffer size required to marshal a palette.
750  *
751  * PARAMS
752  *  pFlags       [I] Flags. See notes.
753  *  StartingSize [I] Starting size of the buffer. This value is added on to
754  *                   the buffer size required for the clip format.
755  *  phPal        [I] Palette to size.
756  *
757  * RETURNS
758  *  The buffer size required to marshal a palette plus the starting size.
759  *
760  * NOTES
761  *  Even though the function is documented to take a pointer to a ULONG in
762  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
763  *  the first parameter is a ULONG.
764  *  This function is only intended to be called by the RPC runtime.
765  */
766 ULONG __RPC_USER HPALETTE_UserSize(ULONG *pFlags, ULONG StartingSize, HPALETTE *phPal)
767 {
768     FIXME(":stub\n");
769     return StartingSize;
770 }
771
772 /******************************************************************************
773  *           HPALETTE_UserMarshal [OLE32.@]
774  *
775  * Marshals a palette into a buffer.
776  *
777  * PARAMS
778  *  pFlags  [I] Flags. See notes.
779  *  pBuffer [I] Buffer to marshal the clip format into.
780  *  phPal   [I] Palette to marshal.
781  *
782  * RETURNS
783  *  The end of the marshaled data in the buffer.
784  *
785  * NOTES
786  *  Even though the function is documented to take a pointer to a ULONG in
787  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
788  *  the first parameter is a ULONG.
789  *  This function is only intended to be called by the RPC runtime.
790  */
791 unsigned char * __RPC_USER HPALETTE_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HPALETTE *phPal)
792 {
793     FIXME(":stub\n");
794     return pBuffer;
795 }
796
797 /******************************************************************************
798  *           HPALETTE_UserUnmarshal [OLE32.@]
799  *
800  * Unmarshals a palette from a buffer.
801  *
802  * PARAMS
803  *  pFlags   [I] Flags. See notes.
804  *  pBuffer  [I] Buffer to marshal the clip format from.
805  *  phPal    [O] Address that receive the unmarshaled palette.
806  *
807  * RETURNS
808  *  The end of the marshaled data in the buffer.
809  *
810  * NOTES
811  *  Even though the function is documented to take a pointer to an ULONG in
812  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
813  *  the first parameter is an ULONG.
814  *  This function is only intended to be called by the RPC runtime.
815  */
816 unsigned char * __RPC_USER HPALETTE_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HPALETTE *phPal)
817 {
818     FIXME(":stub\n");
819     return pBuffer;
820 }
821
822 /******************************************************************************
823  *           HPALETTE_UserFree [OLE32.@]
824  *
825  * Frees an unmarshaled palette.
826  *
827  * PARAMS
828  *  pFlags   [I] Flags. See notes.
829  *  phPal    [I] Palette to free.
830  *
831  * RETURNS
832  *  The end of the marshaled data in the buffer.
833  *
834  * NOTES
835  *  Even though the function is documented to take a pointer to a ULONG in
836  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
837  *  which the first parameter is a ULONG.
838  *  This function is only intended to be called by the RPC runtime.
839  */
840 void __RPC_USER HPALETTE_UserFree(ULONG *pFlags, HPALETTE *phPal)
841 {
842     FIXME(":stub\n");
843 }
844
845
846 /******************************************************************************
847  *           HMETAFILE_UserSize [OLE32.@]
848  *
849  * Calculates the buffer size required to marshal a metafile.
850  *
851  * PARAMS
852  *  pFlags       [I] Flags. See notes.
853  *  StartingSize [I] Starting size of the buffer. This value is added on to
854  *                   the buffer size required for the clip format.
855  *  phmf         [I] Metafile to size.
856  *
857  * RETURNS
858  *  The buffer size required to marshal a metafile plus the starting size.
859  *
860  * NOTES
861  *  Even though the function is documented to take a pointer to a ULONG in
862  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
863  *  the first parameter is a ULONG.
864  *  This function is only intended to be called by the RPC runtime.
865  */
866 ULONG __RPC_USER HMETAFILE_UserSize(ULONG *pFlags, ULONG StartingSize, HMETAFILE *phmf)
867 {
868     ULONG size = StartingSize;
869
870     TRACE("(%s, %d, &%p\n", debugstr_user_flags(pFlags), StartingSize, *phmf);
871
872     ALIGN_LENGTH(size, 3);
873
874     size += sizeof(ULONG);
875     if (LOWORD(*pFlags) == MSHCTX_INPROC)
876         size += sizeof(ULONG_PTR);
877     else
878     {
879         size += sizeof(ULONG);
880
881         if (*phmf)
882         {
883             UINT mfsize;
884
885             size += 2 * sizeof(ULONG);
886             mfsize = GetMetaFileBitsEx(*phmf, 0, NULL);
887             size += mfsize;
888         }
889     }
890
891     return size;
892 }
893
894 /******************************************************************************
895  *           HMETAFILE_UserMarshal [OLE32.@]
896  *
897  * Marshals a metafile into a buffer.
898  *
899  * PARAMS
900  *  pFlags  [I] Flags. See notes.
901  *  pBuffer [I] Buffer to marshal the clip format into.
902  *  phEmf   [I] Metafile to marshal.
903  *
904  * RETURNS
905  *  The end of the marshaled data in the buffer.
906  *
907  * NOTES
908  *  Even though the function is documented to take a pointer to a ULONG in
909  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
910  *  the first parameter is a ULONG.
911  *  This function is only intended to be called by the RPC runtime.
912  */
913 unsigned char * __RPC_USER HMETAFILE_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HMETAFILE *phmf)
914 {
915     TRACE("(%s, %p, &%p\n", debugstr_user_flags(pFlags), pBuffer, *phmf);
916
917     ALIGN_POINTER(pBuffer, 3);
918
919     if (LOWORD(*pFlags) == MSHCTX_INPROC)
920     {
921         if (sizeof(*phmf) == 8)
922             *(ULONG *)pBuffer = WDT_INPROC64_CALL;
923         else
924             *(ULONG *)pBuffer = WDT_INPROC_CALL;
925         pBuffer += sizeof(ULONG);
926         *(HMETAFILE *)pBuffer = *phmf;
927         pBuffer += sizeof(HMETAFILE);
928     }
929     else
930     {
931         *(ULONG *)pBuffer = WDT_REMOTE_CALL;
932         pBuffer += sizeof(ULONG);
933         *(ULONG *)pBuffer = (ULONG)(ULONG_PTR)*phmf;
934         pBuffer += sizeof(ULONG);
935
936         if (*phmf)
937         {
938             UINT mfsize = GetMetaFileBitsEx(*phmf, 0, NULL);
939
940             *(ULONG *)pBuffer = mfsize;
941             pBuffer += sizeof(ULONG);
942             *(ULONG *)pBuffer = mfsize;
943             pBuffer += sizeof(ULONG);
944             GetMetaFileBitsEx(*phmf, mfsize, pBuffer);
945             pBuffer += mfsize;
946         }
947     }
948
949     return pBuffer;
950 }
951
952 /******************************************************************************
953  *           HMETAFILE_UserUnmarshal [OLE32.@]
954  *
955  * Unmarshals a metafile from a buffer.
956  *
957  * PARAMS
958  *  pFlags   [I] Flags. See notes.
959  *  pBuffer  [I] Buffer to marshal the clip format from.
960  *  phmf     [O] Address that receive the unmarshaled metafile.
961  *
962  * RETURNS
963  *  The end of the marshaled data in the buffer.
964  *
965  * NOTES
966  *  Even though the function is documented to take a pointer to an ULONG in
967  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
968  *  the first parameter is an ULONG.
969  *  This function is only intended to be called by the RPC runtime.
970  */
971 unsigned char * __RPC_USER HMETAFILE_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HMETAFILE *phmf)
972 {
973     ULONG fContext;
974
975     TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, phmf);
976
977     ALIGN_POINTER(pBuffer, 3);
978
979     fContext = *(ULONG *)pBuffer;
980     pBuffer += sizeof(ULONG);
981
982     if (((fContext == WDT_INPROC_CALL) && (sizeof(*phmf) < 8)) ||
983         ((fContext == WDT_INPROC64_CALL) && (sizeof(*phmf) == 8)))
984     {
985         *phmf = *(HMETAFILE *)pBuffer;
986         pBuffer += sizeof(*phmf);
987     }
988     else if (fContext == WDT_REMOTE_CALL)
989     {
990         ULONG handle;
991
992         handle = *(ULONG *)pBuffer;
993         pBuffer += sizeof(ULONG);
994
995         if (handle)
996         {
997             ULONG size;
998             size = *(ULONG *)pBuffer;
999             pBuffer += sizeof(ULONG);
1000             if (size != *(ULONG *)pBuffer)
1001             {
1002                 RaiseException(RPC_X_BAD_STUB_DATA, 0, 0, NULL);
1003                 return pBuffer;
1004             }
1005             pBuffer += sizeof(ULONG);
1006             *phmf = SetMetaFileBitsEx(size, pBuffer);
1007             pBuffer += size;
1008         }
1009         else
1010             *phmf = NULL;
1011     }
1012     else
1013         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
1014
1015     return pBuffer;
1016 }
1017
1018 /******************************************************************************
1019  *           HMETAFILE_UserFree [OLE32.@]
1020  *
1021  * Frees an unmarshaled metafile.
1022  *
1023  * PARAMS
1024  *  pFlags   [I] Flags. See notes.
1025  *  phmf     [I] Metafile to free.
1026  *
1027  * RETURNS
1028  *  The end of the marshaled data in the buffer.
1029  *
1030  * NOTES
1031  *  Even though the function is documented to take a pointer to a ULONG in
1032  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
1033  *  which the first parameter is a ULONG.
1034  *  This function is only intended to be called by the RPC runtime.
1035  */
1036 void __RPC_USER HMETAFILE_UserFree(ULONG *pFlags, HMETAFILE *phmf)
1037 {
1038     TRACE("(%s, &%p\n", debugstr_user_flags(pFlags), *phmf);
1039
1040     if (LOWORD(*pFlags) != MSHCTX_INPROC)
1041         DeleteMetaFile(*phmf);
1042 }
1043
1044 /******************************************************************************
1045 *           HENHMETAFILE_UserSize [OLE32.@]
1046 *
1047 * Calculates the buffer size required to marshal an enhanced metafile.
1048 *
1049 * PARAMS
1050 *  pFlags       [I] Flags. See notes.
1051 *  StartingSize [I] Starting size of the buffer. This value is added on to
1052 *                   the buffer size required for the clip format.
1053 *  phEmf        [I] Enhanced metafile to size.
1054 *
1055 * RETURNS
1056 *  The buffer size required to marshal an enhanced metafile plus the starting size.
1057 *
1058 * NOTES
1059 *  Even though the function is documented to take a pointer to a ULONG in
1060 *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1061 *  the first parameter is a ULONG.
1062 *  This function is only intended to be called by the RPC runtime.
1063 */
1064 ULONG __RPC_USER HENHMETAFILE_UserSize(ULONG *pFlags, ULONG StartingSize, HENHMETAFILE *phEmf)
1065 {
1066     ULONG size = StartingSize;
1067
1068     TRACE("(%s, %d, %p\n", debugstr_user_flags(pFlags), StartingSize, *phEmf);
1069
1070     size += sizeof(ULONG);
1071     if (LOWORD(*pFlags) == MSHCTX_INPROC)
1072         size += sizeof(ULONG_PTR);
1073     else
1074     {
1075         size += sizeof(ULONG);
1076
1077         if (*phEmf)
1078         {
1079             UINT emfsize;
1080     
1081             size += 2 * sizeof(ULONG);
1082             emfsize = GetEnhMetaFileBits(*phEmf, 0, NULL);
1083             size += emfsize;
1084         }
1085     }
1086
1087     return size;
1088 }
1089
1090 /******************************************************************************
1091  *           HENHMETAFILE_UserMarshal [OLE32.@]
1092  *
1093  * Marshals an enhance metafile into a buffer.
1094  *
1095  * PARAMS
1096  *  pFlags  [I] Flags. See notes.
1097  *  pBuffer [I] Buffer to marshal the clip format into.
1098  *  phEmf   [I] Enhanced metafile to marshal.
1099  *
1100  * RETURNS
1101  *  The end of the marshaled data in the buffer.
1102  *
1103  * NOTES
1104  *  Even though the function is documented to take a pointer to a ULONG in
1105  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1106  *  the first parameter is a ULONG.
1107  *  This function is only intended to be called by the RPC runtime.
1108  */
1109 unsigned char * __RPC_USER HENHMETAFILE_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HENHMETAFILE *phEmf)
1110 {
1111     TRACE("(%s, %p, &%p\n", debugstr_user_flags(pFlags), pBuffer, *phEmf);
1112
1113     if (LOWORD(*pFlags) == MSHCTX_INPROC)
1114     {
1115         if (sizeof(*phEmf) == 8)
1116             *(ULONG *)pBuffer = WDT_INPROC64_CALL;
1117         else
1118             *(ULONG *)pBuffer = WDT_INPROC_CALL;
1119         pBuffer += sizeof(ULONG);
1120         *(HENHMETAFILE *)pBuffer = *phEmf;
1121         pBuffer += sizeof(HENHMETAFILE);
1122     }
1123     else
1124     {
1125         *(ULONG *)pBuffer = WDT_REMOTE_CALL;
1126         pBuffer += sizeof(ULONG);
1127         *(ULONG *)pBuffer = (ULONG)(ULONG_PTR)*phEmf;
1128         pBuffer += sizeof(ULONG);
1129     
1130         if (*phEmf)
1131         {
1132             UINT emfsize = GetEnhMetaFileBits(*phEmf, 0, NULL);
1133     
1134             *(ULONG *)pBuffer = emfsize;
1135             pBuffer += sizeof(ULONG);
1136             *(ULONG *)pBuffer = emfsize;
1137             pBuffer += sizeof(ULONG);
1138             GetEnhMetaFileBits(*phEmf, emfsize, pBuffer);
1139             pBuffer += emfsize;
1140         }
1141     }
1142
1143     return pBuffer;
1144 }
1145
1146 /******************************************************************************
1147  *           HENHMETAFILE_UserUnmarshal [OLE32.@]
1148  *
1149  * Unmarshals an enhanced metafile from a buffer.
1150  *
1151  * PARAMS
1152  *  pFlags   [I] Flags. See notes.
1153  *  pBuffer  [I] Buffer to marshal the clip format from.
1154  *  phEmf    [O] Address that receive the unmarshaled enhanced metafile.
1155  *
1156  * RETURNS
1157  *  The end of the marshaled data in the buffer.
1158  *
1159  * NOTES
1160  *  Even though the function is documented to take a pointer to an ULONG in
1161  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1162  *  the first parameter is an ULONG.
1163  *  This function is only intended to be called by the RPC runtime.
1164  */
1165 unsigned char * __RPC_USER HENHMETAFILE_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HENHMETAFILE *phEmf)
1166 {
1167     ULONG fContext;
1168
1169     TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, phEmf);
1170
1171     fContext = *(ULONG *)pBuffer;
1172     pBuffer += sizeof(ULONG);
1173
1174     if (((fContext == WDT_INPROC_CALL) && (sizeof(*phEmf) < 8)) ||
1175         ((fContext == WDT_INPROC64_CALL) && (sizeof(*phEmf) == 8)))
1176     {
1177         *phEmf = *(HENHMETAFILE *)pBuffer;
1178         pBuffer += sizeof(*phEmf);
1179     }
1180     else if (fContext == WDT_REMOTE_CALL)
1181     {
1182         ULONG handle;
1183
1184         handle = *(ULONG *)pBuffer;
1185         pBuffer += sizeof(ULONG);
1186
1187         if (handle)
1188         {
1189             ULONG size;
1190             size = *(ULONG *)pBuffer;
1191             pBuffer += sizeof(ULONG);
1192             if (size != *(ULONG *)pBuffer)
1193             {
1194                 RaiseException(RPC_X_BAD_STUB_DATA, 0, 0, NULL);
1195                 return pBuffer;
1196             }
1197             pBuffer += sizeof(ULONG);
1198             *phEmf = SetEnhMetaFileBits(size, pBuffer);
1199             pBuffer += size;
1200         }
1201         else 
1202             *phEmf = NULL;
1203     }
1204     else
1205         RaiseException(RPC_S_INVALID_TAG, 0, 0, NULL);
1206
1207     return pBuffer;
1208 }
1209
1210 /******************************************************************************
1211  *           HENHMETAFILE_UserFree [OLE32.@]
1212  *
1213  * Frees an unmarshaled enhanced metafile.
1214  *
1215  * PARAMS
1216  *  pFlags   [I] Flags. See notes.
1217  *  phEmf    [I] Enhanced metafile to free.
1218  *
1219  * RETURNS
1220  *  The end of the marshaled data in the buffer.
1221  *
1222  * NOTES
1223  *  Even though the function is documented to take a pointer to a ULONG in
1224  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
1225  *  which the first parameter is a ULONG.
1226  *  This function is only intended to be called by the RPC runtime.
1227  */
1228 void __RPC_USER HENHMETAFILE_UserFree(ULONG *pFlags, HENHMETAFILE *phEmf)
1229 {
1230     TRACE("(%s, &%p\n", debugstr_user_flags(pFlags), *phEmf);
1231
1232     if (LOWORD(*pFlags) != MSHCTX_INPROC)
1233         DeleteEnhMetaFile(*phEmf);
1234 }
1235
1236 /******************************************************************************
1237  *           HMETAFILEPICT_UserSize [OLE32.@]
1238  *
1239  * Calculates the buffer size required to marshal an metafile pict.
1240  *
1241  * PARAMS
1242  *  pFlags       [I] Flags. See notes.
1243  *  StartingSize [I] Starting size of the buffer. This value is added on to
1244  *                   the buffer size required for the clip format.
1245  *  phMfp        [I] Metafile pict to size.
1246  *
1247  * RETURNS
1248  *  The buffer size required to marshal a metafile pict plus the starting size.
1249  *
1250  * NOTES
1251  *  Even though the function is documented to take a pointer to a ULONG in
1252  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1253  *  the first parameter is a ULONG.
1254  *  This function is only intended to be called by the RPC runtime.
1255  */
1256 ULONG __RPC_USER HMETAFILEPICT_UserSize(ULONG *pFlags, ULONG StartingSize, HMETAFILEPICT *phMfp)
1257 {
1258     ULONG size = StartingSize;
1259
1260     TRACE("(%s, %d, &%p)\n", debugstr_user_flags(pFlags), StartingSize, *phMfp);
1261
1262     size += sizeof(ULONG);
1263     size += sizeof(HMETAFILEPICT);
1264
1265     if ((LOWORD(*pFlags) != MSHCTX_INPROC) && *phMfp)
1266     {
1267         METAFILEPICT *mfpict = GlobalLock(*phMfp);
1268
1269         /* FIXME: raise an exception if mfpict is NULL? */
1270         size += FIELD_OFFSET(remoteMETAFILEPICT, hMF);
1271         size += sizeof(ULONG);
1272
1273         size = HMETAFILE_UserSize(pFlags, size, &mfpict->hMF);
1274
1275         GlobalUnlock(*phMfp);
1276     }
1277
1278     return size;
1279 }
1280
1281 /******************************************************************************
1282  *           HMETAFILEPICT_UserMarshal [OLE32.@]
1283  *
1284  * Marshals a metafile pict into a buffer.
1285  *
1286  * PARAMS
1287  *  pFlags  [I] Flags. See notes.
1288  *  pBuffer [I] Buffer to marshal the clip format into.
1289  *  phMfp   [I] Metafile pict to marshal.
1290  *
1291  * RETURNS
1292  *  The end of the marshaled data in the buffer.
1293  *
1294  * NOTES
1295  *  Even though the function is documented to take a pointer to a ULONG in
1296  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1297  *  the first parameter is a ULONG.
1298  *  This function is only intended to be called by the RPC runtime.
1299  */
1300 unsigned char * __RPC_USER HMETAFILEPICT_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, HMETAFILEPICT *phMfp)
1301 {
1302     TRACE("(%s, %p, &%p)\n", debugstr_user_flags(pFlags), pBuffer, *phMfp);
1303
1304     if (LOWORD(*pFlags) == MSHCTX_INPROC)
1305         *(ULONG *)pBuffer = WDT_INPROC_CALL;
1306     else
1307         *(ULONG *)pBuffer = WDT_REMOTE_CALL;
1308     pBuffer += sizeof(ULONG);
1309
1310     *(HMETAFILEPICT *)pBuffer = *phMfp;
1311     pBuffer += sizeof(HMETAFILEPICT);
1312
1313     if ((LOWORD(*pFlags) != MSHCTX_INPROC) && *phMfp)
1314     {
1315         METAFILEPICT *mfpict = GlobalLock(*phMfp);
1316         remoteMETAFILEPICT * remmfpict = (remoteMETAFILEPICT *)pBuffer;
1317
1318         /* FIXME: raise an exception if mfpict is NULL? */
1319         remmfpict->mm = mfpict->mm;
1320         remmfpict->xExt = mfpict->xExt;
1321         remmfpict->yExt = mfpict->yExt;
1322         pBuffer += FIELD_OFFSET(remoteMETAFILEPICT, hMF);
1323         *(ULONG *)pBuffer = USER_MARSHAL_PTR_PREFIX;
1324         pBuffer += sizeof(ULONG);
1325
1326         pBuffer = HMETAFILE_UserMarshal(pFlags, pBuffer, &mfpict->hMF);
1327
1328         GlobalUnlock(*phMfp);
1329     }
1330
1331     return pBuffer;
1332 }
1333
1334 /******************************************************************************
1335  *           HMETAFILEPICT_UserUnmarshal [OLE32.@]
1336  *
1337  * Unmarshals an metafile pict from a buffer.
1338  *
1339  * PARAMS
1340  *  pFlags   [I] Flags. See notes.
1341  *  pBuffer  [I] Buffer to marshal the clip format from.
1342  *  phMfp    [O] Address that receive the unmarshaled metafile pict.
1343  *
1344  * RETURNS
1345  *  The end of the marshaled data in the buffer.
1346  *
1347  * NOTES
1348  *  Even though the function is documented to take a pointer to an ULONG in
1349  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1350  *  the first parameter is an ULONG.
1351  *  This function is only intended to be called by the RPC runtime.
1352  */
1353 unsigned char * __RPC_USER HMETAFILEPICT_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, HMETAFILEPICT *phMfp)
1354 {
1355     ULONG fContext;
1356
1357     TRACE("(%s, %p, %p)\n", debugstr_user_flags(pFlags), pBuffer, phMfp);
1358
1359     fContext = *(ULONG *)pBuffer;
1360     pBuffer += sizeof(ULONG);
1361
1362     if ((fContext == WDT_INPROC_CALL) || !*(HMETAFILEPICT *)pBuffer)
1363     {
1364         *phMfp = *(HMETAFILEPICT *)pBuffer;
1365         pBuffer += sizeof(HMETAFILEPICT);
1366     }
1367     else
1368     {
1369         METAFILEPICT *mfpict;
1370         const remoteMETAFILEPICT *remmfpict;
1371         ULONG user_marshal_prefix;
1372
1373         pBuffer += sizeof(HMETAFILEPICT);
1374         remmfpict = (const remoteMETAFILEPICT *)pBuffer;
1375
1376         *phMfp = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
1377         if (!*phMfp)
1378             RpcRaiseException(E_OUTOFMEMORY);
1379
1380         mfpict = GlobalLock(*phMfp);
1381         mfpict->mm = remmfpict->mm;
1382         mfpict->xExt = remmfpict->xExt;
1383         mfpict->yExt = remmfpict->yExt;
1384         pBuffer += FIELD_OFFSET(remoteMETAFILEPICT, hMF);
1385         user_marshal_prefix = *(ULONG *)pBuffer;
1386         pBuffer += sizeof(ULONG);
1387
1388         if (user_marshal_prefix != USER_MARSHAL_PTR_PREFIX)
1389             RpcRaiseException(RPC_X_INVALID_TAG);
1390
1391         pBuffer = HMETAFILE_UserUnmarshal(pFlags, pBuffer, &mfpict->hMF);
1392
1393         GlobalUnlock(*phMfp);
1394     }
1395
1396     return pBuffer;
1397 }
1398
1399 /******************************************************************************
1400  *           HMETAFILEPICT_UserFree [OLE32.@]
1401  *
1402  * Frees an unmarshaled metafile pict.
1403  *
1404  * PARAMS
1405  *  pFlags   [I] Flags. See notes.
1406  *  phMfp    [I] Metafile pict to free.
1407  *
1408  * RETURNS
1409  *  The end of the marshaled data in the buffer.
1410  *
1411  * NOTES
1412  *  Even though the function is documented to take a pointer to a ULONG in
1413  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
1414  *  which the first parameter is a ULONG.
1415  *  This function is only intended to be called by the RPC runtime.
1416  */
1417 void __RPC_USER HMETAFILEPICT_UserFree(ULONG *pFlags, HMETAFILEPICT *phMfp)
1418 {
1419     TRACE("(%s, &%p)\n", debugstr_user_flags(pFlags), *phMfp);
1420
1421     if ((LOWORD(*pFlags) == MSHCTX_INPROC) && *phMfp)
1422     {
1423         METAFILEPICT *mfpict;
1424
1425         mfpict = GlobalLock(*phMfp);
1426         /* FIXME: raise an exception if mfpict is NULL? */
1427
1428         GlobalUnlock(*phMfp);
1429     }
1430 }
1431
1432 /******************************************************************************
1433 *           STGMEDIUM_UserSize [OLE32.@]
1434 *
1435 * Calculates the buffer size required to marshal an STGMEDIUM.
1436 *
1437 * PARAMS
1438 *  pFlags       [I] Flags. See notes.
1439 *  StartingSize [I] Starting size of the buffer. This value is added on to
1440 *                   the buffer size required for the clip format.
1441 *  pStgMedium   [I] STGMEDIUM to size.
1442 *
1443 * RETURNS
1444 *  The buffer size required to marshal an STGMEDIUM plus the starting size.
1445 *
1446 * NOTES
1447 *  Even though the function is documented to take a pointer to a ULONG in
1448 *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1449 *  the first parameter is a ULONG.
1450 *  This function is only intended to be called by the RPC runtime.
1451 */
1452 ULONG __RPC_USER STGMEDIUM_UserSize(ULONG *pFlags, ULONG StartingSize, STGMEDIUM *pStgMedium)
1453 {
1454     ULONG size = StartingSize;
1455
1456     TRACE("(%s, %d, %p\n", debugstr_user_flags(pFlags), StartingSize, pStgMedium);
1457
1458     ALIGN_LENGTH(size, 3);
1459
1460     size += 2 * sizeof(DWORD);
1461     if (pStgMedium->tymed != TYMED_NULL)
1462         size += sizeof(DWORD);
1463
1464     switch (pStgMedium->tymed)
1465     {
1466     case TYMED_NULL:
1467         TRACE("TYMED_NULL\n");
1468         break;
1469     case TYMED_HGLOBAL:
1470         TRACE("TYMED_HGLOBAL\n");
1471         size = HGLOBAL_UserSize(pFlags, size, &pStgMedium->u.hGlobal);
1472         break;
1473     case TYMED_FILE:
1474         FIXME("TYMED_FILE\n");
1475         break;
1476     case TYMED_ISTREAM:
1477         FIXME("TYMED_ISTREAM\n");
1478         break;
1479     case TYMED_ISTORAGE:
1480         FIXME("TYMED_ISTORAGE\n");
1481         break;
1482     case TYMED_GDI:
1483         FIXME("TYMED_GDI\n");
1484         break;
1485     case TYMED_MFPICT:
1486         TRACE("TYMED_MFPICT\n");
1487         size = HMETAFILEPICT_UserSize(pFlags, size, &pStgMedium->u.hMetaFilePict);
1488         break;
1489     case TYMED_ENHMF:
1490         TRACE("TYMED_ENHMF\n");
1491         size = HENHMETAFILE_UserSize(pFlags, size, &pStgMedium->u.hEnhMetaFile);
1492         break;
1493     default:
1494         RaiseException(DV_E_TYMED, 0, 0, NULL);
1495     }
1496
1497     if (pStgMedium->pUnkForRelease)
1498         FIXME("buffer size pUnkForRelease\n");
1499
1500     return size;
1501 }
1502
1503 /******************************************************************************
1504  *           STGMEDIUM_UserMarshal [OLE32.@]
1505  *
1506  * Marshals a STGMEDIUM into a buffer.
1507  *
1508  * PARAMS
1509  *  pFlags  [I] Flags. See notes.
1510  *  pBuffer [I] Buffer to marshal the clip format into.
1511  *  pCF     [I] STGMEDIUM to marshal.
1512  *
1513  * RETURNS
1514  *  The end of the marshaled data in the buffer.
1515  *
1516  * NOTES
1517  *  Even though the function is documented to take a pointer to a ULONG in
1518  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1519  *  the first parameter is a ULONG.
1520  *  This function is only intended to be called by the RPC runtime.
1521  */
1522 unsigned char * __RPC_USER STGMEDIUM_UserMarshal(ULONG *pFlags, unsigned char *pBuffer, STGMEDIUM *pStgMedium)
1523 {
1524     TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, pStgMedium);
1525
1526     ALIGN_POINTER(pBuffer, 3);
1527
1528     *(DWORD *)pBuffer = pStgMedium->tymed;
1529     pBuffer += sizeof(DWORD);
1530     if (pStgMedium->tymed != TYMED_NULL)
1531     {
1532         *(DWORD *)pBuffer = (DWORD)(DWORD_PTR)pStgMedium->u.pstg;
1533         pBuffer += sizeof(DWORD);
1534     }
1535     *(DWORD *)pBuffer = (DWORD)(DWORD_PTR)pStgMedium->pUnkForRelease;
1536     pBuffer += sizeof(DWORD);
1537
1538     switch (pStgMedium->tymed)
1539     {
1540     case TYMED_NULL:
1541         TRACE("TYMED_NULL\n");
1542         break;
1543     case TYMED_HGLOBAL:
1544         TRACE("TYMED_HGLOBAL\n");
1545         pBuffer = HGLOBAL_UserMarshal(pFlags, pBuffer, &pStgMedium->u.hGlobal);
1546         break;
1547     case TYMED_FILE:
1548         FIXME("TYMED_FILE\n");
1549         break;
1550     case TYMED_ISTREAM:
1551         FIXME("TYMED_ISTREAM\n");
1552         break;
1553     case TYMED_ISTORAGE:
1554         FIXME("TYMED_ISTORAGE\n");
1555         break;
1556     case TYMED_GDI:
1557         FIXME("TYMED_GDI\n");
1558         break;
1559     case TYMED_MFPICT:
1560         TRACE("TYMED_MFPICT\n");
1561         pBuffer = HMETAFILEPICT_UserMarshal(pFlags, pBuffer, &pStgMedium->u.hMetaFilePict);
1562         break;
1563     case TYMED_ENHMF:
1564         TRACE("TYMED_ENHMF\n");
1565         pBuffer = HENHMETAFILE_UserMarshal(pFlags, pBuffer, &pStgMedium->u.hEnhMetaFile);
1566         break;
1567     default:
1568         RaiseException(DV_E_TYMED, 0, 0, NULL);
1569     }
1570
1571     if (pStgMedium->pUnkForRelease)
1572         FIXME("marshal pUnkForRelease\n");
1573
1574     return pBuffer;
1575 }
1576
1577 /******************************************************************************
1578  *           STGMEDIUM_UserUnmarshal [OLE32.@]
1579  *
1580  * Unmarshals a STGMEDIUM from a buffer.
1581  *
1582  * PARAMS
1583  *  pFlags     [I] Flags. See notes.
1584  *  pBuffer    [I] Buffer to marshal the clip format from.
1585  *  pStgMedium [O] Address that receive the unmarshaled STGMEDIUM.
1586  *
1587  * RETURNS
1588  *  The end of the marshaled data in the buffer.
1589  *
1590  * NOTES
1591  *  Even though the function is documented to take a pointer to an ULONG in
1592  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of which
1593  *  the first parameter is an ULONG.
1594  *  This function is only intended to be called by the RPC runtime.
1595  */
1596 unsigned char * __RPC_USER STGMEDIUM_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, STGMEDIUM *pStgMedium)
1597 {
1598     DWORD content;
1599     DWORD releaseunk;
1600
1601     ALIGN_POINTER(pBuffer, 3);
1602
1603     TRACE("(%s, %p, %p\n", debugstr_user_flags(pFlags), pBuffer, pStgMedium);
1604
1605     pStgMedium->tymed = *(DWORD *)pBuffer;
1606     pBuffer += sizeof(DWORD);
1607     if (pStgMedium->tymed != TYMED_NULL)
1608     {
1609         content = *(DWORD *)pBuffer;
1610         pBuffer += sizeof(DWORD);
1611     }
1612     releaseunk = *(DWORD *)pBuffer;
1613     pBuffer += sizeof(DWORD);
1614
1615     switch (pStgMedium->tymed)
1616     {
1617     case TYMED_NULL:
1618         TRACE("TYMED_NULL\n");
1619         break;
1620     case TYMED_HGLOBAL:
1621         TRACE("TYMED_HGLOBAL\n");
1622         pBuffer = HGLOBAL_UserUnmarshal(pFlags, pBuffer, &pStgMedium->u.hGlobal);
1623         break;
1624     case TYMED_FILE:
1625         FIXME("TYMED_FILE\n");
1626         break;
1627     case TYMED_ISTREAM:
1628         FIXME("TYMED_ISTREAM\n");
1629         break;
1630     case TYMED_ISTORAGE:
1631         FIXME("TYMED_ISTORAGE\n");
1632         break;
1633     case TYMED_GDI:
1634         FIXME("TYMED_GDI\n");
1635         break;
1636     case TYMED_MFPICT:
1637         TRACE("TYMED_MFPICT\n");
1638         pBuffer = HMETAFILEPICT_UserUnmarshal(pFlags, pBuffer, &pStgMedium->u.hMetaFilePict);
1639         break;
1640     case TYMED_ENHMF:
1641         TRACE("TYMED_ENHMF\n");
1642         pBuffer = HENHMETAFILE_UserUnmarshal(pFlags, pBuffer, &pStgMedium->u.hEnhMetaFile);
1643         break;
1644     default:
1645         RaiseException(DV_E_TYMED, 0, 0, NULL);
1646     }
1647
1648     pStgMedium->pUnkForRelease = NULL;
1649     if (releaseunk)
1650         FIXME("unmarshal pUnkForRelease\n");
1651
1652     return pBuffer;
1653 }
1654
1655 /******************************************************************************
1656  *           STGMEDIUM_UserFree [OLE32.@]
1657  *
1658  * Frees an unmarshaled STGMEDIUM.
1659  *
1660  * PARAMS
1661  *  pFlags     [I] Flags. See notes.
1662  *  pStgmedium [I] STGMEDIUM to free.
1663  *
1664  * RETURNS
1665  *  The end of the marshaled data in the buffer.
1666  *
1667  * NOTES
1668  *  Even though the function is documented to take a pointer to a ULONG in
1669  *  pFlags, it actually takes a pointer to a USER_MARSHAL_CB structure, of
1670  *  which the first parameter is a ULONG.
1671  *  This function is only intended to be called by the RPC runtime.
1672  */
1673 void __RPC_USER STGMEDIUM_UserFree(ULONG *pFlags, STGMEDIUM *pStgMedium)
1674 {
1675     TRACE("(%s, %p\n", debugstr_user_flags(pFlags), pStgMedium);
1676
1677     ReleaseStgMedium(pStgMedium);
1678 }
1679
1680 ULONG __RPC_USER ASYNC_STGMEDIUM_UserSize(ULONG *pFlags, ULONG StartingSize, ASYNC_STGMEDIUM *pStgMedium)
1681 {
1682     FIXME(":stub\n");
1683     return StartingSize;
1684 }
1685
1686 unsigned char * __RPC_USER ASYNC_STGMEDIUM_UserMarshal(  ULONG *pFlags, unsigned char *pBuffer, ASYNC_STGMEDIUM *pStgMedium)
1687 {
1688     FIXME(":stub\n");
1689     return pBuffer;
1690 }
1691
1692 unsigned char * __RPC_USER ASYNC_STGMEDIUM_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, ASYNC_STGMEDIUM *pStgMedium)
1693 {
1694     FIXME(":stub\n");
1695     return pBuffer;
1696 }
1697
1698 void __RPC_USER ASYNC_STGMEDIUM_UserFree(ULONG *pFlags, ASYNC_STGMEDIUM *pStgMedium)
1699 {
1700     FIXME(":stub\n");
1701 }
1702
1703 ULONG __RPC_USER FLAG_STGMEDIUM_UserSize(ULONG *pFlags, ULONG StartingSize, FLAG_STGMEDIUM *pStgMedium)
1704 {
1705     FIXME(":stub\n");
1706     return StartingSize;
1707 }
1708
1709 unsigned char * __RPC_USER FLAG_STGMEDIUM_UserMarshal(  ULONG *pFlags, unsigned char *pBuffer, FLAG_STGMEDIUM *pStgMedium)
1710 {
1711     FIXME(":stub\n");
1712     return pBuffer;
1713 }
1714
1715 unsigned char * __RPC_USER FLAG_STGMEDIUM_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, FLAG_STGMEDIUM *pStgMedium)
1716 {
1717     FIXME(":stub\n");
1718     return pBuffer;
1719 }
1720
1721 void __RPC_USER FLAG_STGMEDIUM_UserFree(ULONG *pFlags, FLAG_STGMEDIUM *pStgMedium)
1722 {
1723     FIXME(":stub\n");
1724 }
1725
1726 ULONG __RPC_USER SNB_UserSize(ULONG *pFlags, ULONG StartingSize, SNB *pSnb)
1727 {
1728     FIXME(":stub\n");
1729     return StartingSize;
1730 }
1731
1732 unsigned char * __RPC_USER SNB_UserMarshal(  ULONG *pFlags, unsigned char *pBuffer, SNB *pSnb)
1733 {
1734     FIXME(":stub\n");
1735     return pBuffer;
1736 }
1737
1738 unsigned char * __RPC_USER SNB_UserUnmarshal(ULONG *pFlags, unsigned char *pBuffer, SNB *pSnb)
1739 {
1740     FIXME(":stub\n");
1741     return pBuffer;
1742 }
1743
1744 void __RPC_USER SNB_UserFree(ULONG *pFlags, SNB *pSnb)
1745 {
1746     FIXME(":stub\n");
1747 }