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