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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * - figure out whether we *really* got this right
22 * - check for errors and throw exceptions
23 * - what are the marshalling functions supposed to return?
24 * - finish RpcStream_Vtbl
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
45 static HRESULT
WINAPI (*COM_GetMarshalSizeMax
)(ULONG
*,REFIID
,LPUNKNOWN
,DWORD
,LPVOID
,DWORD
);
46 static HRESULT
WINAPI (*COM_MarshalInterface
)(LPSTREAM
,REFIID
,LPUNKNOWN
,DWORD
,LPVOID
,DWORD
);
47 static HRESULT
WINAPI (*COM_UnmarshalInterface
)(LPSTREAM
,REFIID
,LPVOID
*);
48 static HRESULT
WINAPI (*COM_ReleaseMarshalData
)(LPSTREAM
);
49 static HRESULT
WINAPI (*COM_GetClassObject
)(REFCLSID
,DWORD
,COSERVERINFO
*,REFIID
,LPVOID
*);
50 static HRESULT
WINAPI (*COM_GetPSClsid
)(REFIID
,CLSID
*);
51 static LPVOID
WINAPI (*COM_MemAlloc
)(ULONG
);
52 static void WINAPI (*COM_MemFree
)(LPVOID
);
54 static HMODULE
LoadCOM(void)
56 if (hOLE
) return hOLE
;
57 hOLE
= LoadLibraryA("OLE32.DLL");
59 COM_GetMarshalSizeMax
= (LPVOID
)GetProcAddress(hOLE
, "CoGetMarshalSizeMax");
60 COM_MarshalInterface
= (LPVOID
)GetProcAddress(hOLE
, "CoMarshalInterface");
61 COM_UnmarshalInterface
= (LPVOID
)GetProcAddress(hOLE
, "CoUnmarshalInterface");
62 COM_ReleaseMarshalData
= (LPVOID
)GetProcAddress(hOLE
, "CoReleaseMarshalData");
63 COM_GetClassObject
= (LPVOID
)GetProcAddress(hOLE
, "CoGetClassObject");
64 COM_GetPSClsid
= (LPVOID
)GetProcAddress(hOLE
, "CoGetPSClsid");
65 COM_MemAlloc
= (LPVOID
)GetProcAddress(hOLE
, "CoTaskMemAlloc");
66 COM_MemFree
= (LPVOID
)GetProcAddress(hOLE
, "CoTaskMemFree");
70 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
71 * so implement a simple stream on top of the RPC buffer
72 * (which also implements the MInterfacePointer structure) */
73 typedef struct RpcStreamImpl
77 PMIDL_STUB_MESSAGE pMsg
;
83 static HRESULT WINAPI
RpcStream_QueryInterface(LPSTREAM iface
,
87 ICOM_THIS(RpcStreamImpl
, iface
);
88 if (IsEqualGUID(&IID_IUnknown
, riid
) ||
89 IsEqualGUID(&IID_ISequentialStream
, riid
) ||
90 IsEqualGUID(&IID_IStream
, riid
)) {
98 static ULONG WINAPI
RpcStream_AddRef(LPSTREAM iface
)
100 ICOM_THIS(RpcStreamImpl
, iface
);
101 return ++(This
->RefCount
);
104 static ULONG WINAPI
RpcStream_Release(LPSTREAM iface
)
106 ICOM_THIS(RpcStreamImpl
, iface
);
107 if (!--(This
->RefCount
)) {
108 TRACE("size=%ld\n", *This
->size
);
109 This
->pMsg
->Buffer
= This
->data
+ *This
->size
;
110 HeapFree(GetProcessHeap(),0,This
);
113 return This
->RefCount
;
116 static HRESULT WINAPI
RpcStream_Read(LPSTREAM iface
,
121 ICOM_THIS(RpcStreamImpl
, iface
);
122 if (This
->pos
+ cb
> *This
->size
) cb
= *This
->size
- This
->pos
;
124 memcpy(pv
, This
->data
+ This
->pos
, cb
);
127 if (pcbRead
) *pcbRead
= cb
;
131 static HRESULT WINAPI
RpcStream_Write(LPSTREAM iface
,
136 ICOM_THIS(RpcStreamImpl
, iface
);
137 memcpy(This
->data
+ This
->pos
, pv
, cb
);
139 if (This
->pos
> *This
->size
) *This
->size
= This
->pos
;
140 if (pcbWritten
) *pcbWritten
= cb
;
144 static HRESULT WINAPI
RpcStream_Seek(LPSTREAM iface
,
147 ULARGE_INTEGER
*newPos
)
149 ICOM_THIS(RpcStreamImpl
, iface
);
151 case STREAM_SEEK_SET
:
152 This
->pos
= move
.s
.LowPart
;
154 case STREAM_SEEK_CUR
:
155 This
->pos
= This
->pos
+ move
.s
.LowPart
;
157 case STREAM_SEEK_END
:
158 This
->pos
= *This
->size
+ move
.s
.LowPart
;
161 return STG_E_INVALIDFUNCTION
;
164 newPos
->s
.LowPart
= This
->pos
;
165 newPos
->s
.HighPart
= 0;
170 static HRESULT WINAPI
RpcStream_SetSize(LPSTREAM iface
,
171 ULARGE_INTEGER newSize
)
173 ICOM_THIS(RpcStreamImpl
, iface
);
174 *This
->size
= newSize
.s
.LowPart
;
178 static ICOM_VTABLE(IStream
) RpcStream_Vtbl
=
180 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
181 RpcStream_QueryInterface
,
191 NULL
, /* LockRegion */
192 NULL
, /* UnlockRegion */
197 static LPSTREAM
RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg
, BOOL init
)
200 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(RpcStreamImpl
));
201 if (!This
) return NULL
;
202 This
->lpVtbl
= &RpcStream_Vtbl
;
204 This
->pMsg
= pStubMsg
;
205 This
->size
= (LPDWORD
)pStubMsg
->Buffer
;
206 This
->data
= (char*)(This
->size
+ 1);
208 if (init
) *This
->size
= 0;
209 TRACE("init size=%ld\n", *This
->size
);
210 return (LPSTREAM
)This
;
213 /***********************************************************************
214 * NdrInterfacePointerMarshall [RPCRT4.@]
216 unsigned char * WINAPI
NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg
,
217 unsigned char *pMemory
,
218 PFORMAT_STRING pFormat
)
220 const IID
*riid
= (const IID
*)pStubMsg
->MaxCount
;
224 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
225 if (!riid
) riid
= &IID_IUnknown
;
226 pStubMsg
->MaxCount
= 0;
227 if (!LoadCOM()) return NULL
;
228 stream
= RpcStream_Create(pStubMsg
, TRUE
);
229 hr
= COM_MarshalInterface(stream
, riid
, (LPUNKNOWN
)pMemory
,
230 pStubMsg
->dwDestContext
, pStubMsg
->pvDestContext
,
232 IStream_Release(stream
);
236 /***********************************************************************
237 * NdrInterfacePointerUnmarshall [RPCRT4.@]
239 unsigned char * WINAPI
NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg
,
240 unsigned char **ppMemory
,
241 PFORMAT_STRING pFormat
,
242 unsigned char fMustAlloc
)
247 TRACE("(%p,%p,%p,%d)\n", pStubMsg
, ppMemory
, pFormat
, fMustAlloc
);
248 if (!LoadCOM()) return NULL
;
249 *(LPVOID
*)ppMemory
= NULL
;
250 stream
= RpcStream_Create(pStubMsg
, FALSE
);
251 hr
= COM_UnmarshalInterface(stream
, &IID_NULL
, (LPVOID
*)ppMemory
);
252 IStream_Release(stream
);
256 /***********************************************************************
257 * NdrInterfacePointerBufferSize [RPCRT4.@]
259 void WINAPI
NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg
,
260 unsigned char *pMemory
,
261 PFORMAT_STRING pFormat
)
263 const IID
*riid
= (const IID
*)pStubMsg
->MaxCount
;
267 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
268 if (!riid
) riid
= &IID_IUnknown
;
269 if (!LoadCOM()) return;
270 hr
= COM_GetMarshalSizeMax(&size
, riid
, (LPUNKNOWN
)pMemory
,
271 pStubMsg
->dwDestContext
, pStubMsg
->pvDestContext
,
273 TRACE("size=%ld\n", size
);
274 pStubMsg
->BufferLength
+= sizeof(DWORD
) + size
;
277 /***********************************************************************
278 * NdrInterfacePointerMemorySize [RPCRT4.@]
280 unsigned long WINAPI
NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg
,
281 PFORMAT_STRING pFormat
)
283 FIXME("(%p,%p): stub\n", pStubMsg
, pFormat
);
287 /***********************************************************************
288 * NdrInterfacePointerFree [RPCRT4.@]
290 void WINAPI
NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg
,
291 unsigned char *pMemory
,
292 PFORMAT_STRING pFormat
)
294 LPUNKNOWN pUnk
= (LPUNKNOWN
)pMemory
;
295 TRACE("(%p,%p,%p)\n", pStubMsg
, pMemory
, pFormat
);
296 if (pUnk
) IUnknown_Release(pUnk
);
299 /***********************************************************************
300 * NdrOleAllocate [RPCRT4.@]
302 void * WINAPI
NdrOleAllocate(size_t Size
)
304 if (!LoadCOM()) return NULL
;
305 return COM_MemAlloc(Size
);
308 /***********************************************************************
309 * NdrOleFree [RPCRT4.@]
311 void WINAPI
NdrOleFree(void *NodeToFree
)
313 if (!LoadCOM()) return;
314 COM_MemFree(NodeToFree
);
318 HRESULT
RPCRT4_GetPSFactory(REFIID riid
, LPPSFACTORYBUFFER
*pPS
)
323 if (!LoadCOM()) return RPC_E_UNEXPECTED
;
324 hr
= COM_GetPSClsid(riid
, &clsid
);
325 if (FAILED(hr
)) return hr
;
326 hr
= COM_GetClassObject(&clsid
, CLSCTX_INPROC_SERVER
, NULL
,
327 &IID_IPSFactoryBuffer
, (LPVOID
*)pPS
);