ntdll: Added Urals time.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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   return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
114                                   ProxyInfo->pStubVtblList[Index], iface, ppStub);
115 }
116
117 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
118 {
119   CStdPSFactory_QueryInterface,
120   CStdPSFactory_AddRef,
121   CStdPSFactory_Release,
122   CStdPSFactory_CreateProxy,
123   CStdPSFactory_CreateStub
124 };
125
126 /***********************************************************************
127  *           NdrDllGetClassObject [RPCRT4.@]
128  */
129 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
130                                    const ProxyFileInfo **pProxyFileList,
131                                    const CLSID *pclsid,
132                                    CStdPSFactoryBuffer *pPSFactoryBuffer)
133 {
134   TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
135     debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
136     pPSFactoryBuffer);
137
138   *ppv = NULL;
139   if (!pPSFactoryBuffer->lpVtbl) {
140     const ProxyFileInfo **pProxyFileList2;
141     pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
142     pPSFactoryBuffer->RefCount = 0;
143     pPSFactoryBuffer->pProxyFileList = pProxyFileList;
144     for (pProxyFileList2 = pProxyFileList; *pProxyFileList2; pProxyFileList2++) {
145       int i;
146       for (i = 0; i < (*pProxyFileList2)->TableSize; i++) {
147         /* FIXME: i think that different vtables should be copied for
148          * async interfaces */
149         void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
150         void **pRpcStubVtbl = (void **)&(*pProxyFileList2)->pStubVtblList[i]->Vtbl;
151         int j;
152
153         for (j = 0; j < sizeof(IRpcStubBufferVtbl)/sizeof(void *); j++)
154           if (!pRpcStubVtbl[j])
155             pRpcStubVtbl[j] = pSrcRpcStubVtbl[j];
156       }
157     }
158   }
159   if (IsEqualGUID(rclsid, pclsid))
160     return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
161   else {
162     const ProxyFileInfo *info;
163     int index;
164     /* otherwise, the dll may be using the iid as the clsid, so
165      * search for it in the proxy file list */
166     if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
167       return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
168
169     WARN("class %s not available\n", debugstr_guid(rclsid));
170     return CLASS_E_CLASSNOTAVAILABLE;
171   }
172 }
173
174 /***********************************************************************
175  *           NdrDllCanUnloadNow [RPCRT4.@]
176  */
177 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
178 {
179   return !(pPSFactoryBuffer->RefCount);
180 }
181
182 /***********************************************************************
183  *           NdrDllRegisterProxy [RPCRT4.@]
184  */
185 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
186                                   const ProxyFileInfo **pProxyFileList,
187                                   const CLSID *pclsid)
188 {
189   LPSTR clsid;
190   char keyname[120], module[MAX_PATH];
191   HKEY key, subkey;
192   DWORD len;
193
194   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
195   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
196
197   /* register interfaces to point to clsid */
198   while (*pProxyFileList) {
199     unsigned u;
200     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
201       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
202       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
203       LPSTR iid;
204
205       TRACE("registering %s %s => %s\n", name, debugstr_guid(proxy->header.piid), clsid);
206
207       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
208       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
209       RpcStringFreeA((unsigned char**)&iid);
210       if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
211                           KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
212         if (name)
213           RegSetValueExA(key, NULL, 0, REG_SZ, (LPBYTE)name, strlen(name));
214         if (RegCreateKeyExA(key, "ProxyStubClsid32", 0, NULL, 0,
215                             KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
216           snprintf(module, sizeof(module), "{%s}", clsid);
217           RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
218           RegCloseKey(subkey);
219         }
220         RegCloseKey(key);
221       }
222     }
223     pProxyFileList++;
224   }
225
226   /* register clsid to point to module */
227   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
228   len = GetModuleFileNameA(hDll, module, sizeof(module));
229   if (len && len < sizeof(module)) {
230     TRACE("registering CLSID %s => %s\n", clsid, module);
231     if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
232                         KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
233       if (RegCreateKeyExA(key, "InProcServer32", 0, NULL, 0,
234                           KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
235         RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
236         RegCloseKey(subkey);
237       }
238       RegCloseKey(key);
239     }
240   }
241
242   /* done */
243   RpcStringFreeA((unsigned char**)&clsid);
244   return S_OK;
245 }
246
247 /***********************************************************************
248  *           NdrDllUnregisterProxy [RPCRT4.@]
249  */
250 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
251                                     const ProxyFileInfo **pProxyFileList,
252                                     const CLSID *pclsid)
253 {
254   LPSTR clsid;
255   char keyname[120], module[MAX_PATH];
256   DWORD len;
257
258   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
259   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
260
261   /* unregister interfaces */
262   while (*pProxyFileList) {
263     unsigned u;
264     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
265       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
266       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
267       LPSTR iid;
268
269       TRACE("unregistering %s %s <= %s\n", name, debugstr_guid(proxy->header.piid), clsid);
270
271       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
272       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
273       RpcStringFreeA((unsigned char**)&iid);
274       RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
275     }
276     pProxyFileList++;
277   }
278
279   /* unregister clsid */
280   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
281   len = GetModuleFileNameA(hDll, module, sizeof(module));
282   if (len && len < sizeof(module)) {
283     TRACE("unregistering CLSID %s <= %s\n", clsid, module);
284     RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
285   }
286
287   /* done */
288   RpcStringFreeA((unsigned char**)&clsid);
289   return S_OK;
290 }