hlink: Added HlinkGetSpecialReference implementation.
[wine] / dlls / rpcrt4 / cpsf.c
1 /*
2  * COM proxy/stub factory (CStdPSFactory) 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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winreg.h"
31
32 #include "objbase.h"
33
34 #include "rpcproxy.h"
35
36 #include "wine/debug.h"
37
38 #include "cpsf.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
43 {
44   while (*pProxyFileList) {
45     if ((*pProxyFileList)->pIIDLookupRtn(riid, pIndex)) {
46       *pProxyInfo = *pProxyFileList;
47       TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo, *pIndex);
48       return TRUE;
49     }
50     pProxyFileList++;
51   }
52   TRACE("not found\n");
53   return FALSE;
54 }
55
56 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
57                                                   REFIID riid,
58                                                   LPVOID *obj)
59 {
60   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
61   TRACE("(%p)->QueryInterface(%s,%p)\n",iface,debugstr_guid(riid),obj);
62   if (IsEqualGUID(&IID_IUnknown,riid) ||
63       IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
64     *obj = This;
65     This->RefCount++;
66     return S_OK;
67   }
68   return E_NOINTERFACE;
69 }
70
71 static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
72 {
73   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
74   TRACE("(%p)->AddRef()\n",iface);
75   return ++(This->RefCount);
76 }
77
78 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
79 {
80   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
81   TRACE("(%p)->Release()\n",iface);
82   return --(This->RefCount);
83 }
84
85 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
86                                                LPUNKNOWN pUnkOuter,
87                                                REFIID riid,
88                                                LPRPCPROXYBUFFER *ppProxy,
89                                                LPVOID *ppv)
90 {
91   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
92   const ProxyFileInfo *ProxyInfo;
93   int Index;
94   TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface,pUnkOuter,
95        debugstr_guid(riid),ppProxy,ppv);
96   if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
97     return E_NOINTERFACE;
98   return StdProxy_Construct(riid, pUnkOuter, ProxyInfo, Index, iface, ppProxy, ppv);
99 }
100
101 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
102                                               REFIID riid,
103                                               LPUNKNOWN pUnkServer,
104                                               LPRPCSTUBBUFFER *ppStub)
105 {
106   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
107   const ProxyFileInfo *ProxyInfo;
108   int Index;
109   TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
110        pUnkServer,ppStub);
111   if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
112     return E_NOINTERFACE;
113
114   if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
115     return  CStdStubBuffer_Delegating_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
116                                                 ProxyInfo->pStubVtblList[Index], ProxyInfo->pDelegatedIIDs[Index],
117                                                 iface, ppStub);
118
119   return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
120                                   ProxyInfo->pStubVtblList[Index], iface, ppStub);
121 }
122
123 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
124 {
125   CStdPSFactory_QueryInterface,
126   CStdPSFactory_AddRef,
127   CStdPSFactory_Release,
128   CStdPSFactory_CreateProxy,
129   CStdPSFactory_CreateStub
130 };
131
132 /***********************************************************************
133  *           NdrDllGetClassObject [RPCRT4.@]
134  */
135 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
136                                    const ProxyFileInfo **pProxyFileList,
137                                    const CLSID *pclsid,
138                                    CStdPSFactoryBuffer *pPSFactoryBuffer)
139 {
140   TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
141     debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
142     pPSFactoryBuffer);
143
144   *ppv = NULL;
145   if (!pPSFactoryBuffer->lpVtbl) {
146     const ProxyFileInfo **pProxyFileList2;
147     int max_delegating_vtbl_size = 0;
148     pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
149     pPSFactoryBuffer->RefCount = 0;
150     pPSFactoryBuffer->pProxyFileList = pProxyFileList;
151     for (pProxyFileList2 = pProxyFileList; *pProxyFileList2; pProxyFileList2++) {
152       int i;
153       for (i = 0; i < (*pProxyFileList2)->TableSize; i++) {
154         /* FIXME: i think that different vtables should be copied for
155          * async interfaces */
156         void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
157         void **pRpcStubVtbl = (void **)&(*pProxyFileList2)->pStubVtblList[i]->Vtbl;
158         int j;
159
160         if ((*pProxyFileList2)->pDelegatedIIDs && (*pProxyFileList2)->pDelegatedIIDs[i]) {
161           pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
162           if ((*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount > max_delegating_vtbl_size)
163             max_delegating_vtbl_size = (*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount;
164         }
165
166         for (j = 0; j < sizeof(IRpcStubBufferVtbl)/sizeof(void *); j++)
167           if (!pRpcStubVtbl[j])
168             pRpcStubVtbl[j] = pSrcRpcStubVtbl[j];
169       }
170     }
171     if(max_delegating_vtbl_size > 0)
172       create_delegating_vtbl(max_delegating_vtbl_size);
173   }
174   if (IsEqualGUID(rclsid, pclsid))
175     return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
176   else {
177     const ProxyFileInfo *info;
178     int index;
179     /* otherwise, the dll may be using the iid as the clsid, so
180      * search for it in the proxy file list */
181     if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
182       return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
183
184     WARN("class %s not available\n", debugstr_guid(rclsid));
185     return CLASS_E_CLASSNOTAVAILABLE;
186   }
187 }
188
189 /***********************************************************************
190  *           NdrDllCanUnloadNow [RPCRT4.@]
191  */
192 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
193 {
194   return !(pPSFactoryBuffer->RefCount);
195 }
196
197 /***********************************************************************
198  *           NdrDllRegisterProxy [RPCRT4.@]
199  */
200 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
201                                   const ProxyFileInfo **pProxyFileList,
202                                   const CLSID *pclsid)
203 {
204   LPSTR clsid;
205   char keyname[120], module[MAX_PATH];
206   HKEY key, subkey;
207   DWORD len;
208
209   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
210   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
211
212   /* register interfaces to point to clsid */
213   while (*pProxyFileList) {
214     unsigned u;
215     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
216       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
217       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
218       LPSTR iid;
219
220       TRACE("registering %s %s => %s\n", name, debugstr_guid(proxy->header.piid), clsid);
221
222       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
223       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
224       RpcStringFreeA((unsigned char**)&iid);
225       if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
226                           KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
227         if (name)
228           RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name));
229         if (RegCreateKeyExA(key, "ProxyStubClsid32", 0, NULL, 0,
230                             KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
231           snprintf(module, sizeof(module), "{%s}", clsid);
232           RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
233           RegCloseKey(subkey);
234         }
235         RegCloseKey(key);
236       }
237     }
238     pProxyFileList++;
239   }
240
241   /* register clsid to point to module */
242   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
243   len = GetModuleFileNameA(hDll, module, sizeof(module));
244   if (len && len < sizeof(module)) {
245     TRACE("registering CLSID %s => %s\n", clsid, module);
246     if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
247                         KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
248       RegSetValueExA(subkey, NULL, 0, REG_SZ, (const BYTE *)"PSFactoryBuffer", strlen("PSFactoryBuffer"));
249       if (RegCreateKeyExA(key, "InProcServer32", 0, NULL, 0,
250                           KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
251         RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
252         RegSetValueExA(subkey, "ThreadingModel", 0, REG_SZ, (const BYTE *)"Both", strlen("Both"));
253         RegCloseKey(subkey);
254       }
255       RegCloseKey(key);
256     }
257   }
258
259   /* done */
260   RpcStringFreeA((unsigned char**)&clsid);
261   return S_OK;
262 }
263
264 /***********************************************************************
265  *           NdrDllUnregisterProxy [RPCRT4.@]
266  */
267 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
268                                     const ProxyFileInfo **pProxyFileList,
269                                     const CLSID *pclsid)
270 {
271   LPSTR clsid;
272   char keyname[120], module[MAX_PATH];
273   DWORD len;
274
275   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
276   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
277
278   /* unregister interfaces */
279   while (*pProxyFileList) {
280     unsigned u;
281     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
282       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
283       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
284       LPSTR iid;
285
286       TRACE("unregistering %s %s <= %s\n", name, debugstr_guid(proxy->header.piid), clsid);
287
288       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
289       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
290       RpcStringFreeA((unsigned char**)&iid);
291       RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
292     }
293     pProxyFileList++;
294   }
295
296   /* unregister clsid */
297   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
298   len = GetModuleFileNameA(hDll, module, sizeof(module));
299   if (len && len < sizeof(module)) {
300     TRACE("unregistering CLSID %s <= %s\n", clsid, module);
301     RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
302   }
303
304   /* done */
305   RpcStringFreeA((unsigned char**)&clsid);
306   return S_OK;
307 }