msvcp90: Added _Locinfo::_Gettnames implementation.
[wine/multimedia.git] / dlls / rpcrt4 / ndr_ole.c
blobc3ab5297df33677e9c60578d570b8f2c223bb997
1 /*
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
20 * TODO:
21 * - fix the wire-protocol to match MS/RPC
22 * - finish RpcStream_Vtbl
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
37 #include "objbase.h"
39 #include "ndr_misc.h"
40 #include "rpcndr.h"
41 #include "rpcproxy.h"
42 #include "wine/rpcfc.h"
43 #include "cpsf.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 static HMODULE hOLE;
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");
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;
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;
82 LONG RefCount;
83 PMIDL_STUB_MESSAGE pMsg;
84 LPDWORD size;
85 unsigned char *data;
86 DWORD pos;
87 } RpcStreamImpl;
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,
95 REFIID riid,
96 LPVOID *obj)
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;
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 );
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;
125 return ref;
128 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
129 void *pv,
130 ULONG cb,
131 ULONG *pcbRead)
133 RpcStreamImpl *This = impl_from_IStream(iface);
134 HRESULT hr = S_OK;
135 if (This->pos + cb > *This->size)
137 cb = *This->size - This->pos;
138 hr = S_FALSE;
140 if (cb) {
141 memcpy(pv, This->data + This->pos, cb);
142 This->pos += cb;
144 if (pcbRead) *pcbRead = cb;
145 return hr;
148 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
149 const void *pv,
150 ULONG cb,
151 ULONG *pcbWritten)
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;
163 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
164 LARGE_INTEGER move,
165 DWORD origin,
166 ULARGE_INTEGER *newPos)
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;
182 if (newPos) {
183 newPos->u.LowPart = This->pos;
184 newPos->u.HighPart = 0;
186 return S_OK;
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;
194 return S_OK;
197 static const IStreamVtbl RpcStream_Vtbl =
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 */
215 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
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;
231 static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
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;
243 if (!riid) riid = &IID_IUnknown;
244 TRACE("got %s\n", debugstr_guid(riid));
245 return 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);
256 LPSTREAM stream;
257 HRESULT hr;
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;
272 IStream_Release(stream);
273 if (FAILED(hr))
274 RpcRaiseException(hr);
277 return NULL;
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)
288 LPSTREAM stream;
289 HRESULT hr;
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);
305 return NULL;
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);
316 ULONG size = 0;
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;
327 /***********************************************************************
328 * NdrInterfacePointerMemorySize [RPCRT4.@]
330 ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
331 PFORMAT_STRING pFormat)
333 ULONG size;
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)
382 CLSID clsid;
383 IPSFactoryBuffer *psfac;
384 HRESULT r;
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);
397 return r;
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)
406 CLSID clsid;
407 IPSFactoryBuffer *psfac;
408 HRESULT r;
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);
421 return r;