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
27 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
31 static HRESULT (WINAPI
*COM_GetMarshalSizeMax
)(ULONG
*,REFIID
,LPUNKNOWN
,DWORD
,LPVOID
,DWORD
);
32 static HRESULT (WINAPI
*COM_MarshalInterface
)(LPSTREAM
,REFIID
,LPUNKNOWN
,DWORD
,LPVOID
,DWORD
);
33 static HRESULT (WINAPI
*COM_UnmarshalInterface
)(LPSTREAM
,REFIID
,LPVOID
*);
34 static HRESULT (WINAPI
*COM_ReleaseMarshalData
)(LPSTREAM
);
35 static HRESULT (WINAPI
*COM_GetClassObject
)(REFCLSID
,DWORD
,COSERVERINFO
*,REFIID
,LPVOID
*);
36 static HRESULT (WINAPI
*COM_GetPSClsid
)(REFIID
,CLSID
*);
37 static LPVOID (WINAPI
*COM_MemAlloc
)(ULONG
);
38 static void (WINAPI
*COM_MemFree
)(LPVOID
);
40 static HMODULE
LoadCOM(void)
42 if (hOLE
) return hOLE
;
43 hOLE
= LoadLibraryA("OLE32.DLL");
45 COM_GetMarshalSizeMax
= (LPVOID
)GetProcAddress(hOLE
, "CoGetMarshalSizeMax");
46 COM_MarshalInterface
= (LPVOID
)GetProcAddress(hOLE
, "CoMarshalInterface");
47 COM_UnmarshalInterface
= (LPVOID
)GetProcAddress(hOLE
, "CoUnmarshalInterface");
48 COM_ReleaseMarshalData
= (LPVOID
)GetProcAddress(hOLE
, "CoReleaseMarshalData");
49 COM_GetClassObject
= (LPVOID
)GetProcAddress(hOLE
, "CoGetClassObject");
50 COM_GetPSClsid
= (LPVOID
)GetProcAddress(hOLE
, "CoGetPSClsid");
51 COM_MemAlloc
= (LPVOID
)GetProcAddress(hOLE
, "CoTaskMemAlloc");
52 COM_MemFree
= (LPVOID
)GetProcAddress(hOLE
, "CoTaskMemFree");
56 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
57 * so implement a simple stream on top of the RPC buffer
58 * (which also implements the MInterfacePointer structure) */
59 typedef struct RpcStreamImpl
61 IStream IStream_iface
;
63 PMIDL_STUB_MESSAGE pMsg
;
69 static inline RpcStreamImpl
*impl_from_IStream(IStream
*iface
)
71 return CONTAINING_RECORD(iface
, RpcStreamImpl
, IStream_iface
);
74 static HRESULT WINAPI
RpcStream_QueryInterface(LPSTREAM iface
,
78 RpcStreamImpl
*This
= impl_from_IStream(iface
);
79 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
80 IsEqualGUID(&IID_ISequentialStream
, riid
) ||
81 IsEqualGUID(&IID_IStream
, riid
)) {
83 InterlockedIncrement( &This
->RefCount
);
89 static ULONG WINAPI
RpcStream_AddRef(LPSTREAM iface
)
91 RpcStreamImpl
*This
= impl_from_IStream(iface
);
92 return InterlockedIncrement( &This
->RefCount
);
95 static ULONG WINAPI
RpcStream_Release(LPSTREAM iface
)
97 RpcStreamImpl
*This
= impl_from_IStream(iface
);
98 ULONG ref
= InterlockedDecrement( &This
->RefCount
);
100 TRACE("size=%d\n", *This
->size
);
101 This
->pMsg
->Buffer
= This
->data
+ *This
->size
;
102 HeapFree(GetProcessHeap(),0,This
);
108 static HRESULT WINAPI
RpcStream_Read(LPSTREAM iface
,
113 RpcStreamImpl
*This
= impl_from_IStream(iface
);
115 if (This
->pos
+ cb
> *This
->size
)
117 cb
= *This
->size
- This
->pos
;
121 memcpy(pv
, This
->data
+ This
->pos
, cb
);
124 if (pcbRead
) *pcbRead
= cb
;
128 static HRESULT WINAPI
RpcStream_Write(LPSTREAM iface
,
133 RpcStreamImpl
*This
= impl_from_IStream(iface
);
134 if (This
->data
+ cb
> (unsigned char *)This
->pMsg
->RpcMsg
->Buffer
+ This
->pMsg
->BufferLength
)
135 return STG_E_MEDIUMFULL
;
136 memcpy(This
->data
+ This
->pos
, pv
, cb
);
138 if (This
->pos
> *This
->size
) *This
->size
= This
->pos
;
139 if (pcbWritten
) *pcbWritten
= cb
;
143 static HRESULT WINAPI
RpcStream_Seek(LPSTREAM iface
,
146 ULARGE_INTEGER
*newPos
)
148 RpcStreamImpl
*This
= impl_from_IStream(iface
);
150 case STREAM_SEEK_SET
:
151 This
->pos
= move
.u
.LowPart
;
153 case STREAM_SEEK_CUR
:
154 This
->pos
= This
->pos
+ move
.u
.LowPart
;
156 case STREAM_SEEK_END
:
157 This
->pos
= *This
->size
+ move
.u
.LowPart
;
160 return STG_E_INVALIDFUNCTION
;
163 newPos
->u
.LowPart
= This
->pos
;
164 newPos
->u
.HighPart
= 0;
169 static HRESULT WINAPI
RpcStream_SetSize(LPSTREAM iface
,
170 ULARGE_INTEGER newSize
)
172 RpcStreamImpl
*This
= impl_from_IStream(iface
);
173 *This
->size
= newSize
.u
.LowPart
;
177 static const IStreamVtbl RpcStream_Vtbl
=
179 RpcStream_QueryInterface
,
189 NULL
, /* LockRegion */
190 NULL
, /* UnlockRegion */
195 static LPSTREAM
RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg
, BOOL init
)
198 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(RpcStreamImpl
));
199 if (!This
) return NULL
;
200 This
->IStream_iface
.lpVtbl
= &RpcStream_Vtbl
;
202 This
->pMsg
= pStubMsg
;
203 This
->size
= (LPDWORD
)pStubMsg
->Buffer
;
204 This
->data
= (unsigned char*)(This
->size
+ 1);
206 if (init
) *This
->size
= 0;
207 TRACE("init size=%d\n", *This
->size
);
208 return (LPSTREAM
)This
;
211 static const IID
* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg
, unsigned char *pMemory
, PFORMAT_STRING pFormat
)
214 if (!pFormat
) return &IID_IUnknown
;
215 TRACE("format=%02x %02x\n", pFormat
[0], pFormat
[1]);
216 if (pFormat
[0] != RPC_FC_IP
) FIXME("format=%d\n", pFormat
[0]);
217 if (pFormat
[1] == RPC_FC_CONSTANT_IID
) {
218 riid
= (const IID
*)&pFormat
[2];
220 ComputeConformance(pStubMsg
, pMemory
, pFormat
+2, 0);
221 riid
= (const IID
*)pStubMsg
->MaxCount
;
223 if (!riid
) riid
= &IID_IUnknown
;
224 TRACE("got %s\n", debugstr_guid(riid
));
228 /***********************************************************************
229 * NdrInterfacePointerMarshall [RPCRT4.@]
231 unsigned char * WINAPI
NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg
,
232 unsigned char *pMemory
,
233 PFORMAT_STRING pFormat
)
235 const IID
*riid
= get_ip_iid(pStubMsg
, pMemory
, pFormat
);
239 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
240 pStubMsg
->MaxCount
= 0;
241 if (!LoadCOM()) return NULL
;
242 if (pStubMsg
->Buffer
+ sizeof(DWORD
) <= (unsigned char *)pStubMsg
->RpcMsg
->Buffer
+ pStubMsg
->BufferLength
) {
243 stream
= RpcStream_Create(pStubMsg
, TRUE
);
246 hr
= COM_MarshalInterface(stream
, riid
, (LPUNKNOWN
)pMemory
,
247 pStubMsg
->dwDestContext
, pStubMsg
->pvDestContext
,
252 IStream_Release(stream
);
254 RpcRaiseException(hr
);
260 /***********************************************************************
261 * NdrInterfacePointerUnmarshall [RPCRT4.@]
263 unsigned char * WINAPI
NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg
,
264 unsigned char **ppMemory
,
265 PFORMAT_STRING pFormat
,
266 unsigned char fMustAlloc
)
271 TRACE("(%p,%p,%p,%d)\n", pStubMsg
, ppMemory
, pFormat
, fMustAlloc
);
272 if (!LoadCOM()) return NULL
;
273 *(LPVOID
*)ppMemory
= NULL
;
274 if (pStubMsg
->Buffer
+ sizeof(DWORD
) < (unsigned char *)pStubMsg
->RpcMsg
->Buffer
+ pStubMsg
->BufferLength
) {
275 stream
= RpcStream_Create(pStubMsg
, FALSE
);
276 if (!stream
) RpcRaiseException(E_OUTOFMEMORY
);
277 if (*((RpcStreamImpl
*)stream
)->size
!= 0)
278 hr
= COM_UnmarshalInterface(stream
, &IID_NULL
, (LPVOID
*)ppMemory
);
281 IStream_Release(stream
);
283 RpcRaiseException(hr
);
288 /***********************************************************************
289 * NdrInterfacePointerBufferSize [RPCRT4.@]
291 void WINAPI
NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg
,
292 unsigned char *pMemory
,
293 PFORMAT_STRING pFormat
)
295 const IID
*riid
= get_ip_iid(pStubMsg
, pMemory
, pFormat
);
298 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
299 if (!LoadCOM()) return;
300 COM_GetMarshalSizeMax(&size
, riid
, (LPUNKNOWN
)pMemory
,
301 pStubMsg
->dwDestContext
, pStubMsg
->pvDestContext
,
303 TRACE("size=%d\n", size
);
304 pStubMsg
->BufferLength
+= sizeof(DWORD
) + size
;
307 /***********************************************************************
308 * NdrInterfacePointerMemorySize [RPCRT4.@]
310 ULONG WINAPI
NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg
,
311 PFORMAT_STRING pFormat
)
315 TRACE("(%p,%p)\n", pStubMsg
, pFormat
);
317 size
= *(ULONG
*)pStubMsg
->Buffer
;
318 pStubMsg
->Buffer
+= 4;
319 pStubMsg
->MemorySize
+= 4;
321 pStubMsg
->Buffer
+= size
;
323 return pStubMsg
->MemorySize
;
326 /***********************************************************************
327 * NdrInterfacePointerFree [RPCRT4.@]
329 void WINAPI
NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg
,
330 unsigned char *pMemory
,
331 PFORMAT_STRING pFormat
)
333 LPUNKNOWN pUnk
= (LPUNKNOWN
)pMemory
;
334 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
335 if (pUnk
) IUnknown_Release(pUnk
);
338 /***********************************************************************
339 * NdrOleAllocate [RPCRT4.@]
341 void * WINAPI
NdrOleAllocate(SIZE_T Size
)
343 if (!LoadCOM()) return NULL
;
344 return COM_MemAlloc(Size
);
347 /***********************************************************************
348 * NdrOleFree [RPCRT4.@]
350 void WINAPI
NdrOleFree(void *NodeToFree
)
352 if (!LoadCOM()) return;
353 COM_MemFree(NodeToFree
);
356 /***********************************************************************
357 * Helper function to create a proxy.
358 * Probably similar to NdrpCreateProxy.
360 HRESULT
create_proxy(REFIID iid
, IUnknown
*pUnkOuter
, IRpcProxyBuffer
**pproxy
, void **ppv
)
363 IPSFactoryBuffer
*psfac
;
366 if(!LoadCOM()) return E_FAIL
;
368 r
= COM_GetPSClsid( iid
, &clsid
);
369 if(FAILED(r
)) return r
;
371 r
= COM_GetClassObject( &clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (void**)&psfac
);
372 if(FAILED(r
)) return r
;
374 r
= IPSFactoryBuffer_CreateProxy(psfac
, pUnkOuter
, iid
, pproxy
, ppv
);
376 IPSFactoryBuffer_Release(psfac
);
380 /***********************************************************************
381 * Helper function to create a stub.
382 * This probably looks very much like NdrpCreateStub.
384 HRESULT
create_stub(REFIID iid
, IUnknown
*pUnk
, IRpcStubBuffer
**ppstub
)
387 IPSFactoryBuffer
*psfac
;
390 if(!LoadCOM()) return E_FAIL
;
392 r
= COM_GetPSClsid( iid
, &clsid
);
393 if(FAILED(r
)) return r
;
395 r
= COM_GetClassObject( &clsid
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IPSFactoryBuffer
, (void**)&psfac
);
396 if(FAILED(r
)) return r
;
398 r
= IPSFactoryBuffer_CreateStub(psfac
, iid
, pUnk
, ppstub
);
400 IPSFactoryBuffer_Release(psfac
);