* Sync up to trunk HEAD (r62502).
[reactos.git] / dll / win32 / 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 "precomp.h"
22
23 #include <winreg.h>
24
25 WINE_DEFAULT_DEBUG_CHANNEL(ole);
26
27 static void format_clsid( WCHAR *buffer, const CLSID *clsid )
28 {
29 static const WCHAR clsid_formatW[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
30 '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X',
31 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
32
33 sprintfW( buffer, clsid_formatW, clsid->Data1, clsid->Data2, clsid->Data3,
34 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
35 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
36
37 }
38
39 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
40 {
41 while (*pProxyFileList) {
42 if ((*pProxyFileList)->pIIDLookupRtn(riid, pIndex)) {
43 *pProxyInfo = *pProxyFileList;
44 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo, *pIndex);
45 return TRUE;
46 }
47 pProxyFileList++;
48 }
49 TRACE("not found\n");
50 return FALSE;
51 }
52
53 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
54 REFIID riid,
55 LPVOID *obj)
56 {
57 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
58 TRACE("(%p)->QueryInterface(%s,%p)\n",iface,debugstr_guid(riid),obj);
59 if (IsEqualGUID(&IID_IUnknown,riid) ||
60 IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
61 *obj = This;
62 InterlockedIncrement( &This->RefCount );
63 return S_OK;
64 }
65 return E_NOINTERFACE;
66 }
67
68 static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
69 {
70 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
71 TRACE("(%p)->AddRef()\n",iface);
72 return InterlockedIncrement( &This->RefCount );
73 }
74
75 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
76 {
77 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
78 TRACE("(%p)->Release()\n",iface);
79 return InterlockedDecrement( &This->RefCount );
80 }
81
82 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
83 LPUNKNOWN pUnkOuter,
84 REFIID riid,
85 LPRPCPROXYBUFFER *ppProxy,
86 LPVOID *ppv)
87 {
88 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
89 const ProxyFileInfo *ProxyInfo;
90 int Index;
91 TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface,pUnkOuter,
92 debugstr_guid(riid),ppProxy,ppv);
93 if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
94 return E_NOINTERFACE;
95 return StdProxy_Construct(riid, pUnkOuter, ProxyInfo, Index, iface, ppProxy, ppv);
96 }
97
98 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
99 REFIID riid,
100 LPUNKNOWN pUnkServer,
101 LPRPCSTUBBUFFER *ppStub)
102 {
103 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
104 const ProxyFileInfo *ProxyInfo;
105 int Index;
106 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
107 pUnkServer,ppStub);
108 if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
109 return E_NOINTERFACE;
110
111 if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
112 return CStdStubBuffer_Delegating_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
113 ProxyInfo->pStubVtblList[Index], ProxyInfo->pDelegatedIIDs[Index],
114 iface, ppStub);
115
116 return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
117 ProxyInfo->pStubVtblList[Index], iface, ppStub);
118 }
119
120 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
121 {
122 CStdPSFactory_QueryInterface,
123 CStdPSFactory_AddRef,
124 CStdPSFactory_Release,
125 CStdPSFactory_CreateProxy,
126 CStdPSFactory_CreateStub
127 };
128
129
130 static void init_psfactory( CStdPSFactoryBuffer *psfac, const ProxyFileInfo **file_list )
131 {
132 DWORD i, j, k;
133
134 psfac->lpVtbl = &CStdPSFactory_Vtbl;
135 psfac->RefCount = 0;
136 psfac->pProxyFileList = file_list;
137 for (i = 0; file_list[i]; i++)
138 {
139 const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList;
140 const PCInterfaceStubVtblList *stubs = file_list[i]->pStubVtblList;
141
142 for (j = 0; j < file_list[i]->TableSize; j++)
143 {
144 /* FIXME: i think that different vtables should be copied for
145 * async interfaces */
146 void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
147 void **pRpcStubVtbl = (void **)&stubs[j]->Vtbl;
148
149 if (file_list[i]->pDelegatedIIDs && file_list[i]->pDelegatedIIDs[j])
150 {
151 void **vtbl = proxies[j]->Vtbl;
152 if (file_list[i]->TableVersion > 1) vtbl++;
153 fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount );
154 pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
155 }
156
157 for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++)
158 if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k];
159 }
160 }
161 }
162
163
164 /***********************************************************************
165 * NdrDllGetClassObject [RPCRT4.@]
166 */
167 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
168 const ProxyFileInfo **pProxyFileList,
169 const CLSID *pclsid,
170 CStdPSFactoryBuffer *pPSFactoryBuffer)
171 {
172 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
173 debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
174 pPSFactoryBuffer);
175
176 *ppv = NULL;
177 if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList );
178
179 if (pclsid && IsEqualGUID(rclsid, pclsid))
180 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
181 else {
182 const ProxyFileInfo *info;
183 int index;
184 /* otherwise, the dll may be using the iid as the clsid, so
185 * search for it in the proxy file list */
186 if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
187 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
188
189 WARN("class %s not available\n", debugstr_guid(rclsid));
190 return CLASS_E_CLASSNOTAVAILABLE;
191 }
192 }
193
194 /***********************************************************************
195 * NdrDllCanUnloadNow [RPCRT4.@]
196 */
197 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
198 {
199 return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK;
200 }
201
202
203 /***********************************************************************
204 * NdrDllRegisterProxy [RPCRT4.@]
205 */
206 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
207 const ProxyFileInfo **pProxyFileList,
208 const CLSID *pclsid)
209 {
210 static const WCHAR bothW[] = {'B','o','t','h',0};
211 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
212 static const WCHAR clsid32W[] = {'P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
213 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
214 static const WCHAR psfactoryW[] = {'P','S','F','a','c','t','o','r','y','B','u','f','f','e','r',0};
215 static const WCHAR numformatW[] = {'%','u',0};
216 static const WCHAR nummethodsW[] = {'N','u','m','M','e','t','h','o','d','s',0};
217 static const WCHAR inprocserverW[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
218 static const WCHAR threadingmodelW[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
219 WCHAR clsid[39], keyname[50], module[MAX_PATH];
220 HKEY key, subkey;
221 DWORD len;
222
223 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
224
225 if (!hDll) return E_HANDLE;
226 if (!*pProxyFileList) return E_NOINTERFACE;
227
228 if (pclsid)
229 format_clsid( clsid, pclsid );
230 else if ((*pProxyFileList)->TableSize > 0)
231 format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid);
232 else
233 return E_NOINTERFACE;
234
235 /* register interfaces to point to clsid */
236 while (*pProxyFileList) {
237 unsigned u;
238 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
239 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
240 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
241
242 TRACE("registering %s %s => %s\n",
243 debugstr_a(name), debugstr_guid(proxy->header.piid), debugstr_w(clsid));
244
245 strcpyW( keyname, interfaceW );
246 format_clsid( keyname + strlenW(keyname), proxy->header.piid );
247 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
248 WCHAR num[10];
249 if (name)
250 RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name)+1);
251 RegSetValueW( key, clsid32W, REG_SZ, clsid, 0 );
252 sprintfW(num, numformatW, proxy->header.DispatchTableCount);
253 RegSetValueW( key, nummethodsW, REG_SZ, num, 0 );
254 RegCloseKey(key);
255 }
256 }
257 pProxyFileList++;
258 }
259
260 /* register clsid to point to module */
261 strcpyW( keyname, clsidW );
262 strcatW( keyname, clsid );
263 len = GetModuleFileNameW(hDll, module, sizeof(module)/sizeof(WCHAR));
264 if (len && len < sizeof(module)) {
265 TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module));
266 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
267 RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW));
268 if (RegCreateKeyW(key, inprocserverW, &subkey) == ERROR_SUCCESS) {
269 RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (strlenW(module)+1)*sizeof(WCHAR));
270 RegSetValueExW(subkey, threadingmodelW, 0, REG_SZ, (const BYTE *)bothW, sizeof(bothW));
271 RegCloseKey(subkey);
272 }
273 RegCloseKey(key);
274 }
275 }
276
277 return S_OK;
278 }
279
280 /***********************************************************************
281 * NdrDllUnregisterProxy [RPCRT4.@]
282 */
283 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
284 const ProxyFileInfo **pProxyFileList,
285 const CLSID *pclsid)
286 {
287 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
288 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
289 WCHAR keyname[50];
290 WCHAR clsid[39];
291
292 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
293 if (pclsid)
294 format_clsid( clsid, pclsid );
295 else if ((*pProxyFileList)->TableSize > 0)
296 format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid);
297 else
298 return E_NOINTERFACE;
299
300 /* unregister interfaces */
301 while (*pProxyFileList) {
302 unsigned u;
303 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
304 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
305 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
306
307 TRACE("unregistering %s %s\n", debugstr_a(name), debugstr_guid(proxy->header.piid));
308
309 strcpyW( keyname, interfaceW );
310 format_clsid( keyname + strlenW(keyname), proxy->header.piid );
311 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
312 }
313 pProxyFileList++;
314 }
315
316 /* unregister clsid */
317 strcpyW( keyname, clsidW );
318 strcatW( keyname, clsid );
319 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
320
321 return S_OK;
322 }