comctl32: Shed unused parameter from TOOLTIPS_NCCreate.
[wine] / dlls / rpcrt4 / ndr_ole.c
1 /*
2  * OLE32 callouts, COM interface marshalling
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
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  * TODO:
21  *  - fix the wire-protocol to match MS/RPC
22  *  - finish RpcStream_Vtbl
23  */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36
37 #include "objbase.h"
38
39 #include "ndr_misc.h"
40 #include "rpcndr.h"
41 #include "rpcproxy.h"
42 #include "wine/rpcfc.h"
43 #include "cpsf.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48
49 static HMODULE hOLE;
50
51 static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
52 static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
53 static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
54 static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
55 static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
56 static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
57 static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
58 static void (WINAPI *COM_MemFree)(LPVOID);
59
60 static HMODULE LoadCOM(void)
61 {
62   if (hOLE) return hOLE;
63   hOLE = LoadLibraryA("OLE32.DLL");
64   if (!hOLE) return 0;
65   COM_GetMarshalSizeMax  = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
66   COM_MarshalInterface   = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
67   COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
68   COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
69   COM_GetClassObject     = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
70   COM_GetPSClsid         = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
71   COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
72   COM_MemFree  = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
73   return hOLE;
74 }
75
76 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
77  * so implement a simple stream on top of the RPC buffer
78  * (which also implements the MInterfacePointer structure) */
79 typedef struct RpcStreamImpl
80 {
81   IStream IStream_iface;
82   LONG RefCount;
83   PMIDL_STUB_MESSAGE pMsg;
84   LPDWORD size;
85   unsigned char *data;
86   DWORD pos;
87 } RpcStreamImpl;
88
89 static inline RpcStreamImpl *impl_from_IStream(IStream *iface)
90 {
91   return CONTAINING_RECORD(iface, RpcStreamImpl, IStream_iface);
92 }
93
94 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
95                                               REFIID riid,
96                                               LPVOID *obj)
97 {
98   RpcStreamImpl *This = impl_from_IStream(iface);
99   if (IsEqualGUID(&IID_IUnknown, riid) ||
100       IsEqualGUID(&IID_ISequentialStream, riid) ||
101       IsEqualGUID(&IID_IStream, riid)) {
102     *obj = This;
103     InterlockedIncrement( &This->RefCount );
104     return S_OK;
105   }
106   return E_NOINTERFACE;
107 }
108
109 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
110 {
111   RpcStreamImpl *This = impl_from_IStream(iface);
112   return InterlockedIncrement( &This->RefCount );
113 }
114
115 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
116 {
117   RpcStreamImpl *This = impl_from_IStream(iface);
118   ULONG ref = InterlockedDecrement( &This->RefCount );
119   if (!ref) {
120     TRACE("size=%d\n", *This->size);
121     This->pMsg->Buffer = This->data + *This->size;
122     HeapFree(GetProcessHeap(),0,This);
123     return 0;
124   }
125   return ref;
126 }
127
128 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
129                                     void *pv,
130                                     ULONG cb,
131                                     ULONG *pcbRead)
132 {
133   RpcStreamImpl *This = impl_from_IStream(iface);
134   HRESULT hr = S_OK;
135   if (This->pos + cb > *This->size)
136   {
137     cb = *This->size - This->pos;
138     hr = S_FALSE;
139   }
140   if (cb) {
141     memcpy(pv, This->data + This->pos, cb);
142     This->pos += cb;
143   }
144   if (pcbRead) *pcbRead = cb;
145   return hr;
146 }
147
148 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
149                                      const void *pv,
150                                      ULONG cb,
151                                      ULONG *pcbWritten)
152 {
153   RpcStreamImpl *This = impl_from_IStream(iface);
154   if (This->data + cb > (unsigned char *)This->pMsg->RpcMsg->Buffer + This->pMsg->BufferLength)
155     return STG_E_MEDIUMFULL;
156   memcpy(This->data + This->pos, pv, cb);
157   This->pos += cb;
158   if (This->pos > *This->size) *This->size = This->pos;
159   if (pcbWritten) *pcbWritten = cb;
160   return S_OK;
161 }
162
163 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
164                                     LARGE_INTEGER move,
165                                     DWORD origin,
166                                     ULARGE_INTEGER *newPos)
167 {
168   RpcStreamImpl *This = impl_from_IStream(iface);
169   switch (origin) {
170   case STREAM_SEEK_SET:
171     This->pos = move.u.LowPart;
172     break;
173   case STREAM_SEEK_CUR:
174     This->pos = This->pos + move.u.LowPart;
175     break;
176   case STREAM_SEEK_END:
177     This->pos = *This->size + move.u.LowPart;
178     break;
179   default:
180     return STG_E_INVALIDFUNCTION;
181   }
182   if (newPos) {
183     newPos->u.LowPart = This->pos;
184     newPos->u.HighPart = 0;
185   }
186   return S_OK;
187 }
188
189 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
190                                        ULARGE_INTEGER newSize)
191 {
192   RpcStreamImpl *This = impl_from_IStream(iface);
193   *This->size = newSize.u.LowPart;
194   return S_OK;
195 }
196
197 static const IStreamVtbl RpcStream_Vtbl =
198 {
199   RpcStream_QueryInterface,
200   RpcStream_AddRef,
201   RpcStream_Release,
202   RpcStream_Read,
203   RpcStream_Write,
204   RpcStream_Seek,
205   RpcStream_SetSize,
206   NULL, /* CopyTo */
207   NULL, /* Commit */
208   NULL, /* Revert */
209   NULL, /* LockRegion */
210   NULL, /* UnlockRegion */
211   NULL, /* Stat */
212   NULL  /* Clone */
213 };
214
215 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
216 {
217   RpcStreamImpl *This;
218   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
219   if (!This) return NULL;
220   This->IStream_iface.lpVtbl = &RpcStream_Vtbl;
221   This->RefCount = 1;
222   This->pMsg = pStubMsg;
223   This->size = (LPDWORD)pStubMsg->Buffer;
224   This->data = (unsigned char*)(This->size + 1);
225   This->pos = 0;
226   if (init) *This->size = 0;
227   TRACE("init size=%d\n", *This->size);
228   return (LPSTREAM)This;
229 }
230
231 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
232 {
233   const IID *riid;
234   if (!pFormat) return &IID_IUnknown;
235   TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
236   if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
237   if (pFormat[1] == RPC_FC_CONSTANT_IID) {
238     riid = (const IID *)&pFormat[2];
239   } else {
240     ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
241     riid = (const IID *)pStubMsg->MaxCount;
242   }
243   if (!riid) riid = &IID_IUnknown;
244   TRACE("got %s\n", debugstr_guid(riid));
245   return riid;
246 }
247
248 /***********************************************************************
249  *           NdrInterfacePointerMarshall [RPCRT4.@]
250  */
251 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
252                                                   unsigned char *pMemory,
253                                                   PFORMAT_STRING pFormat)
254 {
255   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
256   LPSTREAM stream;
257   HRESULT hr;
258
259   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
260   pStubMsg->MaxCount = 0;
261   if (!LoadCOM()) return NULL;
262   if (pStubMsg->Buffer + sizeof(DWORD) <= (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
263     stream = RpcStream_Create(pStubMsg, TRUE);
264     if (stream) {
265       if (pMemory)
266         hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
267                                   pStubMsg->dwDestContext, pStubMsg->pvDestContext,
268                                   MSHLFLAGS_NORMAL);
269       else
270         hr = S_OK;
271
272       IStream_Release(stream);
273       if (FAILED(hr))
274         RpcRaiseException(hr);
275     }
276   }
277   return NULL;
278 }
279
280 /***********************************************************************
281  *           NdrInterfacePointerUnmarshall [RPCRT4.@]
282  */
283 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
284                                                     unsigned char **ppMemory,
285                                                     PFORMAT_STRING pFormat,
286                                                     unsigned char fMustAlloc)
287 {
288   LPSTREAM stream;
289   HRESULT hr;
290
291   TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
292   if (!LoadCOM()) return NULL;
293   *(LPVOID*)ppMemory = NULL;
294   if (pStubMsg->Buffer + sizeof(DWORD) < (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
295     stream = RpcStream_Create(pStubMsg, FALSE);
296     if (!stream) RpcRaiseException(E_OUTOFMEMORY);
297     if (*((RpcStreamImpl *)stream)->size != 0)
298       hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
299     else
300       hr = S_OK;
301     IStream_Release(stream);
302     if (FAILED(hr))
303         RpcRaiseException(hr);
304   }
305   return NULL;
306 }
307
308 /***********************************************************************
309  *           NdrInterfacePointerBufferSize [RPCRT4.@]
310  */
311 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
312                                          unsigned char *pMemory,
313                                          PFORMAT_STRING pFormat)
314 {
315   const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
316   ULONG size = 0;
317
318   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
319   if (!LoadCOM()) return;
320   COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
321                         pStubMsg->dwDestContext, pStubMsg->pvDestContext,
322                         MSHLFLAGS_NORMAL);
323   TRACE("size=%d\n", size);
324   pStubMsg->BufferLength += sizeof(DWORD) + size;
325 }
326
327 /***********************************************************************
328  *           NdrInterfacePointerMemorySize [RPCRT4.@]
329  */
330 ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
331                                            PFORMAT_STRING pFormat)
332 {
333   ULONG size;
334
335   TRACE("(%p,%p)\n", pStubMsg, pFormat);
336
337   size = *(ULONG *)pStubMsg->Buffer;
338   pStubMsg->Buffer += 4;
339   pStubMsg->MemorySize += 4;
340
341   pStubMsg->Buffer += size;
342
343   return pStubMsg->MemorySize;
344 }
345
346 /***********************************************************************
347  *           NdrInterfacePointerFree [RPCRT4.@]
348  */
349 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
350                                    unsigned char *pMemory,
351                                    PFORMAT_STRING pFormat)
352 {
353   LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
354   TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
355   if (pUnk) IUnknown_Release(pUnk);
356 }
357
358 /***********************************************************************
359  *           NdrOleAllocate [RPCRT4.@]
360  */
361 void * WINAPI NdrOleAllocate(SIZE_T Size)
362 {
363   if (!LoadCOM()) return NULL;
364   return COM_MemAlloc(Size);
365 }
366
367 /***********************************************************************
368  *           NdrOleFree [RPCRT4.@]
369  */
370 void WINAPI NdrOleFree(void *NodeToFree)
371 {
372   if (!LoadCOM()) return;
373   COM_MemFree(NodeToFree);
374 }
375
376 /***********************************************************************
377  * Helper function to create a proxy.
378  * Probably similar to NdrpCreateProxy.
379  */
380 HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv)
381 {
382     CLSID clsid;
383     IPSFactoryBuffer *psfac;
384     HRESULT r;
385
386     if(!LoadCOM()) return E_FAIL;
387
388     r = COM_GetPSClsid( iid, &clsid );
389     if(FAILED(r)) return r;
390
391     r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
392     if(FAILED(r)) return r;
393
394     r = IPSFactoryBuffer_CreateProxy(psfac, pUnkOuter, iid, pproxy, ppv);
395
396     IPSFactoryBuffer_Release(psfac);
397     return r;
398 }
399
400 /***********************************************************************
401  * Helper function to create a stub.
402  * This probably looks very much like NdrpCreateStub.
403  */
404 HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
405 {
406     CLSID clsid;
407     IPSFactoryBuffer *psfac;
408     HRESULT r;
409
410     if(!LoadCOM()) return E_FAIL;
411
412     r = COM_GetPSClsid( iid, &clsid );
413     if(FAILED(r)) return r;
414
415     r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
416     if(FAILED(r)) return r;
417
418     r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
419
420     IPSFactoryBuffer_Release(psfac);
421     return r;
422 }