sane.ds: Add simplified and traditional Chinese resources.
[wine] / dlls / rpcrt4 / cproxy.c
1 /*
2  * COM proxy implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  *
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.
10  *
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.
15  *
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
19  * 
20  * TODO: Handle non-i386 architectures
21  */
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30
31 #include "objbase.h"
32 #include "rpcproxy.h"
33
34 #include "cpsf.h"
35 #include "ndr_misc.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 struct StublessThunk;
41
42 /* I don't know what MS's std proxy structure looks like,
43    so this probably doesn't match, but that shouldn't matter */
44 typedef struct {
45   const IRpcProxyBufferVtbl *lpVtbl;
46   LPVOID *PVtbl;
47   LONG RefCount;
48   const MIDL_STUBLESS_PROXY_INFO *stubless;
49   const IID* piid;
50   LPUNKNOWN pUnkOuter;
51   PCInterfaceName name;
52   LPPSFACTORYBUFFER pPSFactory;
53   LPRPCCHANNELBUFFER pChannel;
54   struct StublessThunk *thunks;
55 } StdProxyImpl;
56
57 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
58
59 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
60
61 #if defined(__i386__)
62
63 #include "pshpack1.h"
64
65 struct StublessThunk {
66   BYTE push;
67   DWORD index;
68   BYTE call;
69   LONG handler;
70   BYTE ret;
71   WORD bytes;
72   BYTE pad[3];
73 };
74
75 #include "poppack.h"
76
77 /* adjust the stack size since we don't use Windows's method */
78 #define STACK_ADJUST sizeof(DWORD)
79
80 #define FILL_STUBLESS(x,idx,stk) \
81  x->push = 0x68; /* pushl [immediate] */ \
82  x->index = (idx); \
83  x->call = 0xe8; /* call [near] */ \
84  x->handler = (char*)ObjectStubless - (char*)&x->ret; \
85  x->ret = 0xc2; /* ret [immediate] */ \
86  x->bytes = stk; \
87  x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
88  x->pad[1] = 0x76; \
89  x->pad[2] = 0x00;
90
91 static HRESULT WINAPI ObjectStubless(DWORD index)
92 {
93   char *args = (char*)(&index + 2);
94   LPVOID iface = *(LPVOID*)args;
95
96   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
97
98   PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
99   unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
100   TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, bytes, *(DWORD*)(args+bytes));
101
102   return NdrClientCall2(This->stubless->pStubDesc, fs, args);
103 }
104
105 #else  /* __i386__ */
106
107 /* can't do that on this arch */
108 struct StublessThunk { int dummy; };
109 #define FILL_STUBLESS(x,idx,stk) \
110  ERR("stubless proxies are not supported on this architecture\n");
111 #define STACK_ADJUST 0
112
113 #endif  /* __i386__ */
114
115 HRESULT WINAPI StdProxy_Construct(REFIID riid,
116                                  LPUNKNOWN pUnkOuter,
117                                  const ProxyFileInfo *ProxyInfo,
118                                  int Index,
119                                  LPPSFACTORYBUFFER pPSFactory,
120                                  LPRPCPROXYBUFFER *ppProxy,
121                                  LPVOID *ppvObj)
122 {
123   StdProxyImpl *This;
124   const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
125   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
126   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
127
128   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
129
130   /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
131   if (ProxyInfo->TableVersion > 1) {
132     stubless = *(const void **)vtbl;
133     vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
134     TRACE("stubless=%p\n", stubless);
135   }
136
137   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
138   TRACE("vtbl=%p\n", vtbl->Vtbl);
139
140   if (!IsEqualGUID(vtbl->header.piid, riid)) {
141     ERR("IID mismatch during proxy creation\n");
142     return RPC_E_UNEXPECTED;
143   }
144
145   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
146   if (!This) return E_OUTOFMEMORY;
147
148   if (stubless) {
149     CInterfaceStubVtbl *svtbl = ProxyInfo->pStubVtblList[Index];
150     unsigned long i, count = svtbl->header.DispatchTableCount;
151     /* Maybe the original vtbl is just modified directly to point at
152      * ObjectStublessClientXXX thunks in real Windows, but I don't like it
153      */
154     TRACE("stubless thunks: count=%ld\n", count);
155     This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
156     This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
157     for (i=0; i<count; i++) {
158       struct StublessThunk *thunk = &This->thunks[i];
159       if (vtbl->Vtbl[i] == (LPVOID)-1) {
160         PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
161         unsigned bytes = *(const WORD*)(fs+8) - STACK_ADJUST;
162         TRACE("method %ld: stacksize=%d\n", i, bytes);
163         FILL_STUBLESS(thunk, i, bytes)
164         This->PVtbl[i] = thunk;
165       }
166       else {
167         memset(thunk, 0, sizeof(struct StublessThunk));
168         This->PVtbl[i] = vtbl->Vtbl[i];
169       }
170     }
171   }
172   else 
173     This->PVtbl = vtbl->Vtbl;
174
175   This->lpVtbl = &StdProxy_Vtbl;
176   /* one reference for the proxy */
177   This->RefCount = 1;
178   This->stubless = stubless;
179   This->piid = vtbl->header.piid;
180   This->pUnkOuter = pUnkOuter;
181   This->name = name;
182   This->pPSFactory = pPSFactory;
183   This->pChannel = NULL;
184   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
185   *ppvObj = &This->PVtbl;
186   /* if there is no outer unknown then the caller will control the lifetime
187    * of the proxy object through the proxy buffer, so no need to increment the
188    * ref count of the proxy object */
189   if (pUnkOuter)
190     IUnknown_AddRef((IUnknown *)*ppvObj);
191   IPSFactoryBuffer_AddRef(pPSFactory);
192
193   return S_OK;
194 }
195
196 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
197 {
198   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
199
200   if (This->pChannel)
201     IRpcProxyBuffer_Disconnect(iface);
202
203   IPSFactoryBuffer_Release(This->pPSFactory);
204   if (This->thunks) {
205     HeapFree(GetProcessHeap(),0,This->PVtbl);
206     HeapFree(GetProcessHeap(),0,This->thunks);
207   }
208   HeapFree(GetProcessHeap(),0,This);
209 }
210
211 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
212                                              REFIID riid,
213                                              LPVOID *obj)
214 {
215   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
216   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
217
218   if (IsEqualGUID(&IID_IUnknown,riid) ||
219       IsEqualGUID(This->piid,riid)) {
220     *obj = &This->PVtbl;
221     InterlockedIncrement(&This->RefCount);
222     return S_OK;
223   }
224
225   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
226     *obj = &This->lpVtbl;
227     InterlockedIncrement(&This->RefCount);
228     return S_OK;
229   }
230
231   return E_NOINTERFACE;
232 }
233
234 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
235 {
236   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
237   TRACE("(%p)->AddRef()\n",This);
238
239   return InterlockedIncrement(&This->RefCount);
240 }
241
242 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
243 {
244   ULONG refs;
245   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
246   TRACE("(%p)->Release()\n",This);
247
248   refs = InterlockedDecrement(&This->RefCount);
249   if (!refs)
250     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
251   return refs;
252 }
253
254 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
255                                       LPRPCCHANNELBUFFER pChannel)
256 {
257   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
258   TRACE("(%p)->Connect(%p)\n",This,pChannel);
259
260   This->pChannel = pChannel;
261   IRpcChannelBuffer_AddRef(pChannel);
262   return S_OK;
263 }
264
265 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
266 {
267   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
268   TRACE("(%p)->Disconnect()\n",This);
269
270   IRpcChannelBuffer_Release(This->pChannel);
271   This->pChannel = NULL;
272 }
273
274 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
275 {
276   StdProxy_QueryInterface,
277   StdProxy_AddRef,
278   StdProxy_Release,
279   StdProxy_Connect,
280   StdProxy_Disconnect
281 };
282
283 static void StdProxy_GetChannel(LPVOID iface,
284                                    LPRPCCHANNELBUFFER *ppChannel)
285 {
286   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
287   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
288
289   *ppChannel = This->pChannel;
290 }
291
292 static void StdProxy_GetIID(LPVOID iface,
293                                const IID **ppiid)
294 {
295   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
296   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
297
298   *ppiid = This->piid;
299 }
300
301 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
302                                             REFIID riid,
303                                             LPVOID *ppvObj)
304 {
305   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
306   TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
307   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
308 }
309
310 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
311 {
312   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
313   TRACE("(%p)->AddRef() %s\n",This,This->name);
314   return IUnknown_AddRef(This->pUnkOuter);
315 }
316
317 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
318 {
319   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
320   TRACE("(%p)->Release() %s\n",This,This->name);
321   return IUnknown_Release(This->pUnkOuter);
322 }
323
324 /***********************************************************************
325  *           NdrProxyInitialize [RPCRT4.@]
326  */
327 void WINAPI NdrProxyInitialize(void *This,
328                               PRPC_MESSAGE pRpcMsg,
329                               PMIDL_STUB_MESSAGE pStubMsg,
330                               PMIDL_STUB_DESC pStubDescriptor,
331                               unsigned int ProcNum)
332 {
333   TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
334   NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
335   StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
336   IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
337                                &pStubMsg->dwDestContext,
338                                &pStubMsg->pvDestContext);
339   TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
340 }
341
342 /***********************************************************************
343  *           NdrProxyGetBuffer [RPCRT4.@]
344  */
345 void WINAPI NdrProxyGetBuffer(void *This,
346                              PMIDL_STUB_MESSAGE pStubMsg)
347 {
348   HRESULT hr;
349   const IID *riid = NULL;
350
351   TRACE("(%p,%p)\n", This, pStubMsg);
352   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
353   pStubMsg->dwStubPhase = PROXY_GETBUFFER;
354   StdProxy_GetIID(This, &riid);
355   hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
356                                   (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
357                                   riid);
358   if (FAILED(hr))
359   {
360     RpcRaiseException(hr);
361     return;
362   }
363   pStubMsg->fBufferValid = TRUE;
364   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
365   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
366   pStubMsg->Buffer = pStubMsg->BufferStart;
367   pStubMsg->dwStubPhase = PROXY_MARSHAL;
368 }
369
370 /***********************************************************************
371  *           NdrProxySendReceive [RPCRT4.@]
372  */
373 void WINAPI NdrProxySendReceive(void *This,
374                                PMIDL_STUB_MESSAGE pStubMsg)
375 {
376   ULONG Status = 0;
377   HRESULT hr;
378
379   TRACE("(%p,%p)\n", This, pStubMsg);
380
381   if (!pStubMsg->pRpcChannelBuffer)
382   {
383     WARN("Trying to use disconnected proxy %p\n", This);
384     RpcRaiseException(RPC_E_DISCONNECTED);
385   }
386
387   pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
388   /* avoid sending uninitialised parts of the buffer on the wire */
389   pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
390   hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
391                                     (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
392                                     &Status);
393   pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
394   pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
395   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
396   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
397   pStubMsg->Buffer = pStubMsg->BufferStart;
398
399   /* raise exception if call failed */
400   if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
401   else if (FAILED(hr)) RpcRaiseException(hr);
402 }
403
404 /***********************************************************************
405  *           NdrProxyFreeBuffer [RPCRT4.@]
406  */
407 void WINAPI NdrProxyFreeBuffer(void *This,
408                               PMIDL_STUB_MESSAGE pStubMsg)
409 {
410   TRACE("(%p,%p)\n", This, pStubMsg);
411
412   if (pStubMsg->fBufferValid)
413   {
414     IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
415                                  (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
416     pStubMsg->fBufferValid = TRUE;
417   }
418 }
419
420 /***********************************************************************
421  *           NdrProxyErrorHandler [RPCRT4.@]
422  */
423 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
424 {
425   WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
426
427   if (FAILED(dwExceptionCode))
428     return dwExceptionCode;
429   else
430     return HRESULT_FROM_WIN32(dwExceptionCode);
431 }
432
433 HRESULT WINAPI
434 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
435                          LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
436 {
437     typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
438     HMODULE hUser32 = LoadLibraryA("user32");
439     MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
440
441     FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
442     if (pMessageBoxA)
443     {
444         pMessageBoxA(NULL,
445             "The native implementation of OLEAUT32.DLL cannot be used "
446             "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
447             "Wine: Unimplemented CreateProxyFromTypeInfo",
448             0x10);
449         ExitProcess(1);
450     }
451     return E_NOTIMPL;
452 }