Fixed some dependencies.
[wine/wine-kai.git] / dlls / rpcrt4 / ndr_ole.c
blob0bd1725aa983a7d077f02786e4d404eb79203543
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 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winreg.h"
35 #include "objbase.h"
37 #include "rpcndr.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 static HMODULE hOLE;
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");
58 if (!hOLE) return 0;
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");
67 return hOLE;
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
75 ICOM_VFIELD(IStream);
76 DWORD RefCount;
77 PMIDL_STUB_MESSAGE pMsg;
78 LPDWORD size;
79 char *data;
80 DWORD pos;
81 } RpcStreamImpl;
83 static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
84 REFIID riid,
85 LPVOID *obj)
87 ICOM_THIS(RpcStreamImpl, iface);
88 if (IsEqualGUID(&IID_IUnknown, riid) ||
89 IsEqualGUID(&IID_ISequentialStream, riid) ||
90 IsEqualGUID(&IID_IStream, riid)) {
91 *obj = This;
92 This->RefCount++;
93 return S_OK;
95 return E_NOINTERFACE;
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);
111 return 0;
113 return This->RefCount;
116 static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
117 void *pv,
118 ULONG cb,
119 ULONG *pcbRead)
121 ICOM_THIS(RpcStreamImpl, iface);
122 if (This->pos + cb > *This->size) cb = *This->size - This->pos;
123 if (cb) {
124 memcpy(pv, This->data + This->pos, cb);
125 This->pos += cb;
127 if (pcbRead) *pcbRead = cb;
128 return S_OK;
131 static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
132 const void *pv,
133 ULONG cb,
134 ULONG *pcbWritten)
136 ICOM_THIS(RpcStreamImpl, iface);
137 memcpy(This->data + This->pos, pv, cb);
138 This->pos += cb;
139 if (This->pos > *This->size) *This->size = This->pos;
140 if (pcbWritten) *pcbWritten = cb;
141 return S_OK;
144 static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
145 LARGE_INTEGER move,
146 DWORD origin,
147 ULARGE_INTEGER *newPos)
149 ICOM_THIS(RpcStreamImpl, iface);
150 switch (origin) {
151 case STREAM_SEEK_SET:
152 This->pos = move.s.LowPart;
153 break;
154 case STREAM_SEEK_CUR:
155 This->pos = This->pos + move.s.LowPart;
156 break;
157 case STREAM_SEEK_END:
158 This->pos = *This->size + move.s.LowPart;
159 break;
160 default:
161 return STG_E_INVALIDFUNCTION;
163 if (newPos) {
164 newPos->s.LowPart = This->pos;
165 newPos->s.HighPart = 0;
167 return S_OK;
170 static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
171 ULARGE_INTEGER newSize)
173 ICOM_THIS(RpcStreamImpl, iface);
174 *This->size = newSize.s.LowPart;
175 return S_OK;
178 static ICOM_VTABLE(IStream) RpcStream_Vtbl =
180 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
181 RpcStream_QueryInterface,
182 RpcStream_AddRef,
183 RpcStream_Release,
184 RpcStream_Read,
185 RpcStream_Write,
186 RpcStream_Seek,
187 RpcStream_SetSize,
188 NULL, /* CopyTo */
189 NULL, /* Commit */
190 NULL, /* Revert */
191 NULL, /* LockRegion */
192 NULL, /* UnlockRegion */
193 NULL, /* Stat */
194 NULL /* Clone */
197 static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
199 RpcStreamImpl *This;
200 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
201 if (!This) return NULL;
202 This->lpVtbl = &RpcStream_Vtbl;
203 This->RefCount = 1;
204 This->pMsg = pStubMsg;
205 This->size = (LPDWORD)pStubMsg->Buffer;
206 This->data = (char*)(This->size + 1);
207 This->pos = 0;
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;
221 LPSTREAM stream;
222 HRESULT hr;
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,
231 MSHLFLAGS_NORMAL);
232 IStream_Release(stream);
233 return NULL;
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)
244 LPSTREAM stream;
245 HRESULT hr;
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);
253 return NULL;
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;
264 ULONG size = 0;
265 HRESULT hr;
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,
272 MSHLFLAGS_NORMAL);
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);
284 return 0;
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);
317 /* internal */
318 HRESULT RPCRT4_GetPSFactory(REFIID riid, LPPSFACTORYBUFFER *pPS)
320 HRESULT hr;
321 CLSID clsid;
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);
328 return hr;