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