Added wglGetExtensionsStringARB.
[wine/wine-gecko.git] / dlls / rpcrt4 / ndr_ole.c
blob3e05275e82bcd74225aac66d93afdac487ab5644
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * TODO:
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
27 #include <stdio.h>
28 #include <string.h>
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winreg.h"
37 #include "objbase.h"
39 #include "ndr_misc.h"
40 #include "rpcndr.h"
41 #include "wine/rpcfc.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ole);
47 static HMODULE hOLE;
49 static HRESULT WINAPI (*COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
50 static HRESULT WINAPI (*COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
51 static HRESULT WINAPI (*COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
52 static HRESULT WINAPI (*COM_ReleaseMarshalData)(LPSTREAM);
53 static HRESULT WINAPI (*COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
54 static HRESULT WINAPI (*COM_GetPSClsid)(REFIID,CLSID *);
55 static LPVOID WINAPI (*COM_MemAlloc)(ULONG);
56 static void WINAPI (*COM_MemFree)(LPVOID);
58 static HMODULE LoadCOM(void)
60 if (hOLE) return hOLE;
61 hOLE = LoadLibraryA("OLE32.DLL");
62 if (!hOLE) return 0;
63 COM_GetMarshalSizeMax = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
64 COM_MarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
65 COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
66 COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
67 COM_GetClassObject = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
68 COM_GetPSClsid = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
69 COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
70 COM_MemFree = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
71 return hOLE;
74 /* CoMarshalInterface/CoUnmarshalInterface works on streams,
75 * so implement a simple stream on top of the RPC buffer
76 * (which also implements the MInterfacePointer structure) */
77 typedef struct RpcStreamImpl
79 ICOM_VFIELD(IStream);
80 DWORD RefCount;
81 PMIDL_STUB_MESSAGE pMsg;
82 LPDWORD size;
83 char *data;
84 DWORD pos;
85 } RpcStreamImpl;
87 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
88 REFIID riid,
89 LPVOID *obj)
91 ICOM_THIS(RpcStreamImpl, iface);
92 if (IsEqualGUID(&IID_IUnknown, riid) ||
93 IsEqualGUID(&IID_ISequentialStream, riid) ||
94 IsEqualGUID(&IID_IStream, riid)) {
95 *obj = This;
96 This->RefCount++;
97 return S_OK;
99 return E_NOINTERFACE;
102 static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
104 ICOM_THIS(RpcStreamImpl, iface);
105 return ++(This->RefCount);
108 static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
110 ICOM_THIS(RpcStreamImpl, iface);
111 if (!--(This->RefCount)) {
112 TRACE("size=%ld\n", *This->size);
113 This->pMsg->Buffer = This->data + *This->size;
114 HeapFree(GetProcessHeap(),0,This);
115 return 0;
117 return This->RefCount;
120 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
121 void *pv,
122 ULONG cb,
123 ULONG *pcbRead)
125 ICOM_THIS(RpcStreamImpl, iface);
126 if (This->pos + cb > *This->size) cb = *This->size - This->pos;
127 if (cb) {
128 memcpy(pv, This->data + This->pos, cb);
129 This->pos += cb;
131 if (pcbRead) *pcbRead = cb;
132 return S_OK;
135 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
136 const void *pv,
137 ULONG cb,
138 ULONG *pcbWritten)
140 ICOM_THIS(RpcStreamImpl, iface);
141 memcpy(This->data + This->pos, pv, cb);
142 This->pos += cb;
143 if (This->pos > *This->size) *This->size = This->pos;
144 if (pcbWritten) *pcbWritten = cb;
145 return S_OK;
148 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
149 LARGE_INTEGER move,
150 DWORD origin,
151 ULARGE_INTEGER *newPos)
153 ICOM_THIS(RpcStreamImpl, iface);
154 switch (origin) {
155 case STREAM_SEEK_SET:
156 This->pos = move.s.LowPart;
157 break;
158 case STREAM_SEEK_CUR:
159 This->pos = This->pos + move.s.LowPart;
160 break;
161 case STREAM_SEEK_END:
162 This->pos = *This->size + move.s.LowPart;
163 break;
164 default:
165 return STG_E_INVALIDFUNCTION;
167 if (newPos) {
168 newPos->s.LowPart = This->pos;
169 newPos->s.HighPart = 0;
171 return S_OK;
174 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
175 ULARGE_INTEGER newSize)
177 ICOM_THIS(RpcStreamImpl, iface);
178 *This->size = newSize.s.LowPart;
179 return S_OK;
182 static ICOM_VTABLE(IStream) RpcStream_Vtbl =
184 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
185 RpcStream_QueryInterface,
186 RpcStream_AddRef,
187 RpcStream_Release,
188 RpcStream_Read,
189 RpcStream_Write,
190 RpcStream_Seek,
191 RpcStream_SetSize,
192 NULL, /* CopyTo */
193 NULL, /* Commit */
194 NULL, /* Revert */
195 NULL, /* LockRegion */
196 NULL, /* UnlockRegion */
197 NULL, /* Stat */
198 NULL /* Clone */
201 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
203 RpcStreamImpl *This;
204 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
205 if (!This) return NULL;
206 This->lpVtbl = &RpcStream_Vtbl;
207 This->RefCount = 1;
208 This->pMsg = pStubMsg;
209 This->size = (LPDWORD)pStubMsg->Buffer;
210 This->data = (char*)(This->size + 1);
211 This->pos = 0;
212 if (init) *This->size = 0;
213 TRACE("init size=%ld\n", *This->size);
214 return (LPSTREAM)This;
217 const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
219 const IID *riid;
220 if (!pFormat) return &IID_IUnknown;
221 TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
222 if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
223 if (pFormat[1] == RPC_FC_CONSTANT_IID)
224 return (const IID*)&pFormat[2];
226 ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
227 riid = (const IID *)pStubMsg->MaxCount;
228 if (!riid) riid = &IID_IUnknown;
229 return riid;
232 /***********************************************************************
233 * NdrInterfacePointerMarshall [RPCRT4.@]
235 unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
236 unsigned char *pMemory,
237 PFORMAT_STRING pFormat)
239 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
240 LPSTREAM stream;
241 HRESULT hr;
243 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
244 pStubMsg->MaxCount = 0;
245 if (!LoadCOM()) return NULL;
246 stream = RpcStream_Create(pStubMsg, TRUE);
247 hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
248 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
249 MSHLFLAGS_NORMAL);
250 IStream_Release(stream);
251 return NULL;
254 /***********************************************************************
255 * NdrInterfacePointerUnmarshall [RPCRT4.@]
257 unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
258 unsigned char **ppMemory,
259 PFORMAT_STRING pFormat,
260 unsigned char fMustAlloc)
262 LPSTREAM stream;
263 HRESULT hr;
265 TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
266 if (!LoadCOM()) return NULL;
267 *(LPVOID*)ppMemory = NULL;
268 stream = RpcStream_Create(pStubMsg, FALSE);
269 hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
270 IStream_Release(stream);
271 return NULL;
274 /***********************************************************************
275 * NdrInterfacePointerBufferSize [RPCRT4.@]
277 void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
278 unsigned char *pMemory,
279 PFORMAT_STRING pFormat)
281 const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
282 ULONG size = 0;
283 HRESULT hr;
285 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
286 if (!LoadCOM()) return;
287 hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
288 pStubMsg->dwDestContext, pStubMsg->pvDestContext,
289 MSHLFLAGS_NORMAL);
290 TRACE("size=%ld\n", size);
291 pStubMsg->BufferLength += sizeof(DWORD) + size;
294 /***********************************************************************
295 * NdrInterfacePointerMemorySize [RPCRT4.@]
297 unsigned long WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
298 PFORMAT_STRING pFormat)
300 FIXME("(%p,%p): stub\n", pStubMsg, pFormat);
301 return 0;
304 /***********************************************************************
305 * NdrInterfacePointerFree [RPCRT4.@]
307 void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
308 unsigned char *pMemory,
309 PFORMAT_STRING pFormat)
311 LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
312 TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
313 if (pUnk) IUnknown_Release(pUnk);
316 /***********************************************************************
317 * NdrOleAllocate [RPCRT4.@]
319 void * WINAPI NdrOleAllocate(size_t Size)
321 if (!LoadCOM()) return NULL;
322 return COM_MemAlloc(Size);
325 /***********************************************************************
326 * NdrOleFree [RPCRT4.@]
328 void WINAPI NdrOleFree(void *NodeToFree)
330 if (!LoadCOM()) return;
331 COM_MemFree(NodeToFree);
334 /* internal */
335 HRESULT RPCRT4_GetPSFactory(REFIID riid, LPPSFACTORYBUFFER *pPS)
337 HRESULT hr;
338 CLSID clsid;
340 if (!LoadCOM()) return RPC_E_UNEXPECTED;
341 hr = COM_GetPSClsid(riid, &clsid);
342 if (FAILED(hr)) return hr;
343 hr = COM_GetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL,
344 &IID_IPSFactoryBuffer, (LPVOID *)pPS);
345 return hr;