netstat: Initial implementation.
[wine] / dlls / dispex / usrmarshal.c
1 /*
2  *    Misc marshaling routines
3  *
4  * Copyright 2010 Huw Davies
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 #include <stdarg.h>
21 #include <string.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "objbase.h"
33 #include "oleauto.h"
34 #include "dispex.h"
35
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 #define NULL_RESULT    0x20000
41 #define NULL_EXCEPINFO 0x40000
42
43 HRESULT CALLBACK IDispatchEx_InvokeEx_Proxy(IDispatchEx* This, DISPID id, LCID lcid, WORD wFlags,
44                                             DISPPARAMS *pdp, VARIANT *pvarRes, EXCEPINFO *pei,
45                                             IServiceProvider *pspCaller)
46 {
47     HRESULT hr;
48     VARIANT result;
49     EXCEPINFO excep_info;
50     UINT byref_args, arg, dummy_idx;
51     VARIANT dummy_arg, *ref_arg = &dummy_arg, *copy_arg, *orig_arg = NULL;
52     UINT *ref_idx = &dummy_idx;
53     DWORD dword_flags = wFlags & 0xf;
54
55     TRACE("(%p)->(%08x, %04x, %04x, %p, %p, %p, %p)\n", This, id, lcid, wFlags,
56           pdp, pvarRes, pei, pspCaller);
57
58     if(!pvarRes)
59     {
60         pvarRes = &result;
61         dword_flags |= NULL_RESULT;
62     }
63
64     if(!pei)
65     {
66         pei = &excep_info;
67         dword_flags |= NULL_EXCEPINFO;
68     }
69
70     for(arg = 0, byref_args = 0; arg < pdp->cArgs; arg++)
71         if(V_ISBYREF(pdp->rgvarg + arg)) byref_args++;
72
73     if(byref_args)
74     {
75         DWORD size = pdp->cArgs * sizeof(VARIANT) +
76             byref_args * (sizeof(VARIANT) + sizeof(UINT));
77
78         copy_arg = CoTaskMemAlloc(size);
79         if(!copy_arg) return E_OUTOFMEMORY;
80
81         ref_arg = copy_arg + pdp->cArgs;
82         ref_idx = (UINT*)(ref_arg + byref_args);
83
84         /* copy the byref args to ref_arg[], the others go to copy_arg[] */
85         for(arg = 0, byref_args = 0; arg < pdp->cArgs; arg++)
86         {
87             if(V_ISBYREF(pdp->rgvarg + arg))
88             {
89                 ref_arg[byref_args] = pdp->rgvarg[arg];
90                 ref_idx[byref_args] = arg;
91                 VariantInit(copy_arg + arg);
92                 byref_args++;
93             }
94             else
95                 copy_arg[arg] = pdp->rgvarg[arg];
96         }
97
98         orig_arg = pdp->rgvarg;
99         pdp->rgvarg = copy_arg;
100     }
101
102     hr = IDispatchEx_RemoteInvokeEx_Proxy(This, id, lcid, dword_flags, pdp, pvarRes, pei, pspCaller,
103                                           byref_args, ref_idx, ref_arg);
104
105     if(byref_args)
106     {
107         CoTaskMemFree(pdp->rgvarg);
108         pdp->rgvarg = orig_arg;
109     }
110
111     return hr;
112 }
113
114 HRESULT __RPC_STUB IDispatchEx_InvokeEx_Stub(IDispatchEx* This, DISPID id, LCID lcid, DWORD dwFlags,
115                                              DISPPARAMS *pdp, VARIANT *result, EXCEPINFO *pei,
116                                              IServiceProvider *pspCaller, UINT byref_args,
117                                              UINT *ref_idx, VARIANT *ref_arg)
118 {
119     HRESULT hr;
120     UINT arg;
121     VARTYPE *vt_list = NULL;
122
123     TRACE("(%p)->(%08x, %04x, %08x, %p, %p, %p, %p, %d, %p, %p)\n", This, id, lcid, dwFlags,
124           pdp, result, pei, pspCaller, byref_args, ref_idx, ref_arg);
125
126     VariantInit(result);
127     memset(pei, 0, sizeof(*pei));
128
129     for(arg = 0; arg < byref_args; arg++)
130         pdp->rgvarg[ref_idx[arg]] = ref_arg[arg];
131
132     if(dwFlags & NULL_RESULT) result = NULL;
133     if(dwFlags & NULL_EXCEPINFO) pei = NULL;
134
135     /* Create an array of the original VTs to check that the function doesn't change
136        any on return. */
137     if(byref_args)
138     {
139         vt_list = HeapAlloc(GetProcessHeap(), 0, pdp->cArgs * sizeof(vt_list[0]));
140         if(!vt_list) return E_OUTOFMEMORY;
141         for(arg = 0; arg < pdp->cArgs; arg++)
142             vt_list[arg] = V_VT(pdp->rgvarg + arg);
143     }
144
145     hr = IDispatchEx_InvokeEx(This, id, lcid, dwFlags & 0xffff, pdp, result, pei, pspCaller);
146
147     if(SUCCEEDED(hr) && byref_args)
148     {
149         for(arg = 0; arg < pdp->cArgs; arg++)
150         {
151             if(vt_list[arg] != V_VT(pdp->rgvarg + arg))
152             {
153                 hr = DISP_E_BADCALLEE;
154                 break;
155             }
156         }
157     }
158
159     if(hr == DISP_E_EXCEPTION)
160     {
161         if(pei && pei->pfnDeferredFillIn)
162         {
163             pei->pfnDeferredFillIn(pei);
164             pei->pfnDeferredFillIn = NULL;
165         }
166     }
167
168     for(arg = 0; arg < byref_args; arg++)
169         VariantInit(pdp->rgvarg + ref_idx[arg]);
170
171     HeapFree(GetProcessHeap(), 0, vt_list);
172     return hr;
173 }