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 #define NO_SHLWAPI_PATH 26 #define NO_SHLWAPI_STRFCNS 27 #define NO_SHLWAPI_GDI 28 #define NO_SHLWAPI_STREAM 29 #include <shlwapi.h> 30 31 WINE_DEFAULT_DEBUG_CHANNEL(ole); 32 33 static void format_clsid( WCHAR *buffer, const CLSID *clsid ) 34 { 35 static const WCHAR clsid_formatW[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-', 36 '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X', 37 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0}; 38 39 sprintfW( buffer, clsid_formatW, clsid->Data1, clsid->Data2, clsid->Data3, 40 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3], 41 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] ); 42 43 } 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 InterlockedIncrement( &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 InterlockedIncrement( &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 InterlockedDecrement( &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 static void init_psfactory( CStdPSFactoryBuffer *psfac, const ProxyFileInfo **file_list ) 137 { 138 DWORD i, j, k; 139 140 psfac->lpVtbl = &CStdPSFactory_Vtbl; 141 psfac->RefCount = 0; 142 psfac->pProxyFileList = file_list; 143 for (i = 0; file_list[i]; i++) 144 { 145 const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList; 146 const PCInterfaceStubVtblList *stubs = file_list[i]->pStubVtblList; 147 148 for (j = 0; j < file_list[i]->TableSize; j++) 149 { 150 /* FIXME: i think that different vtables should be copied for 151 * async interfaces */ 152 void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl; 153 void **pRpcStubVtbl = (void **)&stubs[j]->Vtbl; 154 155 if (file_list[i]->pDelegatedIIDs && file_list[i]->pDelegatedIIDs[j]) 156 { 157 void **vtbl = proxies[j]->Vtbl; 158 if (file_list[i]->TableVersion > 1) vtbl++; 159 fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount ); 160 pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl; 161 } 162 163 for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++) 164 if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k]; 165 } 166 } 167 } 168 169 170 /*********************************************************************** 171 * NdrDllGetClassObject [RPCRT4.@] 172 */ 173 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv, 174 const ProxyFileInfo **pProxyFileList, 175 const CLSID *pclsid, 176 CStdPSFactoryBuffer *pPSFactoryBuffer) 177 { 178 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid), 179 debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid), 180 pPSFactoryBuffer); 181 182 *ppv = NULL; 183 if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList ); 184 185 if (pclsid && IsEqualGUID(rclsid, pclsid)) 186 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv); 187 else { 188 const ProxyFileInfo *info; 189 int index; 190 /* otherwise, the dll may be using the iid as the clsid, so 191 * search for it in the proxy file list */ 192 if (FindProxyInfo(pProxyFileList, rclsid, &info, &index)) 193 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv); 194 195 WARN("class %s not available\n", debugstr_guid(rclsid)); 196 return CLASS_E_CLASSNOTAVAILABLE; 197 } 198 } 199 200 /*********************************************************************** 201 * NdrDllCanUnloadNow [RPCRT4.@] 202 */ 203 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer) 204 { 205 return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK; 206 } 207 208 209 /*********************************************************************** 210 * NdrDllRegisterProxy [RPCRT4.@] 211 */ 212 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll, 213 const ProxyFileInfo **pProxyFileList, 214 const CLSID *pclsid) 215 { 216 static const WCHAR bothW[] = {'B','o','t','h',0}; 217 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0}; 218 static const WCHAR clsid32W[] = {'P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0}; 219 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0}; 220 static const WCHAR psfactoryW[] = {'P','S','F','a','c','t','o','r','y','B','u','f','f','e','r',0}; 221 static const WCHAR numformatW[] = {'%','u',0}; 222 static const WCHAR nummethodsW[] = {'N','u','m','M','e','t','h','o','d','s',0}; 223 static const WCHAR inprocserverW[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0}; 224 static const WCHAR threadingmodelW[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0}; 225 WCHAR clsid[39], keyname[50], module[MAX_PATH]; 226 HKEY key, subkey; 227 DWORD len; 228 229 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid)); 230 231 if (!hDll) return E_HANDLE; 232 if (!*pProxyFileList) return E_NOINTERFACE; 233 234 if (pclsid) 235 format_clsid( clsid, pclsid ); 236 else if ((*pProxyFileList)->TableSize > 0) 237 format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid); 238 else 239 return E_NOINTERFACE; 240 241 /* register interfaces to point to clsid */ 242 while (*pProxyFileList) { 243 unsigned u; 244 for (u=0; u<(*pProxyFileList)->TableSize; u++) { 245 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u]; 246 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u]; 247 248 TRACE("registering %s %s => %s\n", 249 debugstr_a(name), debugstr_guid(proxy->header.piid), debugstr_w(clsid)); 250 251 strcpyW( keyname, interfaceW ); 252 format_clsid( keyname + strlenW(keyname), proxy->header.piid ); 253 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) { 254 WCHAR num[10]; 255 if (name) 256 RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name)+1); 257 RegSetValueW( key, clsid32W, REG_SZ, clsid, 0 ); 258 sprintfW(num, numformatW, proxy->header.DispatchTableCount); 259 RegSetValueW( key, nummethodsW, REG_SZ, num, 0 ); 260 RegCloseKey(key); 261 } 262 } 263 pProxyFileList++; 264 } 265 266 /* register clsid to point to module */ 267 strcpyW( keyname, clsidW ); 268 strcatW( keyname, clsid ); 269 len = GetModuleFileNameW(hDll, module, sizeof(module)/sizeof(WCHAR)); 270 if (len && len < sizeof(module)) { 271 TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module)); 272 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) { 273 RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW)); 274 if (RegCreateKeyW(key, inprocserverW, &subkey) == ERROR_SUCCESS) { 275 RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (strlenW(module)+1)*sizeof(WCHAR)); 276 RegSetValueExW(subkey, threadingmodelW, 0, REG_SZ, (const BYTE *)bothW, sizeof(bothW)); 277 RegCloseKey(subkey); 278 } 279 RegCloseKey(key); 280 } 281 } 282 283 return S_OK; 284 } 285 286 /*********************************************************************** 287 * NdrDllUnregisterProxy [RPCRT4.@] 288 */ 289 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll, 290 const ProxyFileInfo **pProxyFileList, 291 const CLSID *pclsid) 292 { 293 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0}; 294 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0}; 295 WCHAR keyname[50]; 296 WCHAR clsid[39]; 297 298 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid)); 299 if (pclsid) 300 format_clsid( clsid, pclsid ); 301 else if ((*pProxyFileList)->TableSize > 0) 302 format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid); 303 else 304 return E_NOINTERFACE; 305 306 /* unregister interfaces */ 307 while (*pProxyFileList) { 308 unsigned u; 309 for (u=0; u<(*pProxyFileList)->TableSize; u++) { 310 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u]; 311 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u]; 312 313 TRACE("unregistering %s %s\n", debugstr_a(name), debugstr_guid(proxy->header.piid)); 314 315 strcpyW( keyname, interfaceW ); 316 format_clsid( keyname + strlenW(keyname), proxy->header.piid ); 317 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname); 318 } 319 pProxyFileList++; 320 } 321 322 /* unregister clsid */ 323 strcpyW( keyname, clsidW ); 324 strcatW( keyname, clsid ); 325 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname); 326 327 return S_OK; 328 } 329