2 * OLE32 callouts, COM interface marshalling
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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 * - fix the wire-protocol to match MS/RPC
22 * - finish RpcStream_Vtbl
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
42 #include "wine/rpcfc.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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);
60 static HMODULE LoadCOM(void)
62 if (hOLE) return hOLE;
63 hOLE = LoadLibraryA("OLE32.DLL");
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");
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
81 IStream IStream_iface;
83 PMIDL_STUB_MESSAGE pMsg;
89 static inline RpcStreamImpl *impl_from_IStream(IStream *iface)
91 return CONTAINING_RECORD(iface, RpcStreamImpl, IStream_iface);
94 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
98 RpcStreamImpl *This = impl_from_IStream(iface);
99 if (IsEqualGUID(&IID_IUnknown, riid) ||
100 IsEqualGUID(&IID_ISequentialStream, riid) ||
101 IsEqualGUID(&IID_IStream, riid)) {
103 InterlockedIncrement( &This->RefCount );
106 return E_NOINTERFACE;
109 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
111 RpcStreamImpl *This = impl_from_IStream(iface);
112 return InterlockedIncrement( &This->RefCount );
115 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
117 RpcStreamImpl *This = impl_from_IStream(iface);
118 ULONG ref = InterlockedDecrement( &This->RefCount );
120 TRACE("size=%d\n", *This->size);
121 This->pMsg->Buffer = This->data + *This->size;
122 HeapFree(GetProcessHeap(),0,This);
128 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
133 RpcStreamImpl *This = impl_from_IStream(iface);
135 if (This->pos + cb > *This->size)
137 cb = *This->size - This->pos;
141 memcpy(pv, This->data + This->pos, cb);
144 if (pcbRead) *pcbRead = cb;
148 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
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);
158 if (This->pos > *This->size) *This->size = This->pos;
159 if (pcbWritten) *pcbWritten = cb;
163 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
166 ULARGE_INTEGER *newPos)
168 RpcStreamImpl *This = impl_from_IStream(iface);
170 case STREAM_SEEK_SET:
171 This->pos = move.u.LowPart;
173 case STREAM_SEEK_CUR:
174 This->pos = This->pos + move.u.LowPart;
176 case STREAM_SEEK_END:
177 This->pos = *This->size + move.u.LowPart;
180 return STG_E_INVALIDFUNCTION;
183 newPos->u.LowPart = This->pos;
184 newPos->u.HighPart = 0;
189 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
190 ULARGE_INTEGER newSize)
192 RpcStreamImpl *This = impl_from_IStream(iface);
193 *This->size = newSize.u.LowPart;
197 static const IStreamVtbl RpcStream_Vtbl =
199 RpcStream_QueryInterface,
209 NULL, /* LockRegion */
210 NULL, /* UnlockRegion */
215 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
218 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
219 if (!This) return NULL;
220 This->IStream_iface.lpVtbl = &RpcStream_Vtbl;
222 This->pMsg = pStubMsg;
223 This->size = (LPDWORD)pStubMsg->Buffer;
224 This->data = (unsigned char*)(This->size + 1);
226 if (init) *This->size = 0;
227 TRACE("init size=%d\n", *This->size);
228 return (LPSTREAM)This;
231 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
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];
240 ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
241 riid = (const IID *)pStubMsg->MaxCount;
243 if (!riid) riid = &IID_IUnknown;
244 TRACE("got %s\n", debugstr_guid(riid));
248 /***********************************************************************
249 * NdrInterfacePointerMarshall [RPCRT4.@]
251 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
252 unsigned char *pMemory,
253 PFORMAT_STRING pFormat)
255 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
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);
266 hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
267 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
272 IStream_Release(stream);
274 RpcRaiseException(hr);
280 /***********************************************************************
281 * NdrInterfacePointerUnmarshall [RPCRT4.@]
283 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
284 unsigned char **ppMemory,
285 PFORMAT_STRING pFormat,
286 unsigned char fMustAlloc)
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);
301 IStream_Release(stream);
303 RpcRaiseException(hr);
308 /***********************************************************************
309 * NdrInterfacePointerBufferSize [RPCRT4.@]
311 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
312 unsigned char *pMemory,
313 PFORMAT_STRING pFormat)
315 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
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,
323 TRACE("size=%d\n", size);
324 pStubMsg->BufferLength += sizeof(DWORD) + size;
327 /***********************************************************************
328 * NdrInterfacePointerMemorySize [RPCRT4.@]
330 ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
331 PFORMAT_STRING pFormat)
335 TRACE("(%p,%p)\n", pStubMsg, pFormat);
337 size = *(ULONG *)pStubMsg->Buffer;
338 pStubMsg->Buffer += 4;
339 pStubMsg->MemorySize += 4;
341 pStubMsg->Buffer += size;
343 return pStubMsg->MemorySize;
346 /***********************************************************************
347 * NdrInterfacePointerFree [RPCRT4.@]
349 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
350 unsigned char *pMemory,
351 PFORMAT_STRING pFormat)
353 LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
354 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
355 if (pUnk) IUnknown_Release(pUnk);
358 /***********************************************************************
359 * NdrOleAllocate [RPCRT4.@]
361 void * WINAPI NdrOleAllocate(SIZE_T Size)
363 if (!LoadCOM()) return NULL;
364 return COM_MemAlloc(Size);
367 /***********************************************************************
368 * NdrOleFree [RPCRT4.@]
370 void WINAPI NdrOleFree(void *NodeToFree)
372 if (!LoadCOM()) return;
373 COM_MemFree(NodeToFree);
376 /***********************************************************************
377 * Helper function to create a proxy.
378 * Probably similar to NdrpCreateProxy.
380 HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv)
383 IPSFactoryBuffer *psfac;
386 if(!LoadCOM()) return E_FAIL;
388 r = COM_GetPSClsid( iid, &clsid );
389 if(FAILED(r)) return r;
391 r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
392 if(FAILED(r)) return r;
394 r = IPSFactoryBuffer_CreateProxy(psfac, pUnkOuter, iid, pproxy, ppv);
396 IPSFactoryBuffer_Release(psfac);
400 /***********************************************************************
401 * Helper function to create a stub.
402 * This probably looks very much like NdrpCreateStub.
404 HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
407 IPSFactoryBuffer *psfac;
410 if(!LoadCOM()) return E_FAIL;
412 r = COM_GetPSClsid( iid, &clsid );
413 if(FAILED(r)) return r;
415 r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
416 if(FAILED(r)) return r;
418 r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);
420 IPSFactoryBuffer_Release(psfac);