2 * COM proxy/stub factory (CStdPSFactory) implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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.
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.
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
22 #include "wine/port.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 static void format_clsid( WCHAR *buffer, const CLSID *clsid )
48 static const WCHAR clsid_formatW[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
49 '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X',
50 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
52 sprintfW( buffer, clsid_formatW, clsid->Data1, clsid->Data2, clsid->Data3,
53 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
54 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
58 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
60 while (*pProxyFileList) {
61 if ((*pProxyFileList)->pIIDLookupRtn(riid, pIndex)) {
62 *pProxyInfo = *pProxyFileList;
63 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo, *pIndex);
72 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
76 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
77 TRACE("(%p)->QueryInterface(%s,%p)\n",iface,debugstr_guid(riid),obj);
78 if (IsEqualGUID(&IID_IUnknown,riid) ||
79 IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
81 InterlockedIncrement( &This->RefCount );
87 static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
89 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
90 TRACE("(%p)->AddRef()\n",iface);
91 return InterlockedIncrement( &This->RefCount );
94 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
96 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
97 TRACE("(%p)->Release()\n",iface);
98 return InterlockedDecrement( &This->RefCount );
101 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
104 LPRPCPROXYBUFFER *ppProxy,
107 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
108 const ProxyFileInfo *ProxyInfo;
110 TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface,pUnkOuter,
111 debugstr_guid(riid),ppProxy,ppv);
112 if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
113 return E_NOINTERFACE;
114 return StdProxy_Construct(riid, pUnkOuter, ProxyInfo, Index, iface, ppProxy, ppv);
117 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
119 LPUNKNOWN pUnkServer,
120 LPRPCSTUBBUFFER *ppStub)
122 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
123 const ProxyFileInfo *ProxyInfo;
125 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
127 if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
128 return E_NOINTERFACE;
130 if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
131 return CStdStubBuffer_Delegating_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
132 ProxyInfo->pStubVtblList[Index], ProxyInfo->pDelegatedIIDs[Index],
135 return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
136 ProxyInfo->pStubVtblList[Index], iface, ppStub);
139 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
141 CStdPSFactory_QueryInterface,
142 CStdPSFactory_AddRef,
143 CStdPSFactory_Release,
144 CStdPSFactory_CreateProxy,
145 CStdPSFactory_CreateStub
149 static void init_psfactory( CStdPSFactoryBuffer *psfac, const ProxyFileInfo **file_list )
153 psfac->lpVtbl = &CStdPSFactory_Vtbl;
155 psfac->pProxyFileList = file_list;
156 for (i = 0; file_list[i]; i++)
158 const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList;
159 const PCInterfaceStubVtblList *stubs = file_list[i]->pStubVtblList;
161 for (j = 0; j < file_list[i]->TableSize; j++)
163 /* FIXME: i think that different vtables should be copied for
164 * async interfaces */
165 void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
166 void **pRpcStubVtbl = (void **)&stubs[j]->Vtbl;
168 if (file_list[i]->pDelegatedIIDs && file_list[i]->pDelegatedIIDs[j])
170 void **vtbl = proxies[j]->Vtbl;
171 if (file_list[i]->TableVersion > 1) vtbl++;
172 fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount );
173 pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
176 for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++)
177 if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k];
183 /***********************************************************************
184 * NdrDllGetClassObject [RPCRT4.@]
186 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
187 const ProxyFileInfo **pProxyFileList,
189 CStdPSFactoryBuffer *pPSFactoryBuffer)
191 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
192 debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
196 if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList );
198 if (pclsid && IsEqualGUID(rclsid, pclsid))
199 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
201 const ProxyFileInfo *info;
203 /* otherwise, the dll may be using the iid as the clsid, so
204 * search for it in the proxy file list */
205 if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
206 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
208 WARN("class %s not available\n", debugstr_guid(rclsid));
209 return CLASS_E_CLASSNOTAVAILABLE;
213 /***********************************************************************
214 * NdrDllCanUnloadNow [RPCRT4.@]
216 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
218 return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK;
222 /***********************************************************************
223 * NdrDllRegisterProxy [RPCRT4.@]
225 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
226 const ProxyFileInfo **pProxyFileList,
229 static const WCHAR bothW[] = {'B','o','t','h',0};
230 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
231 static const WCHAR clsid32W[] = {'P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
232 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
233 static const WCHAR psfactoryW[] = {'P','S','F','a','c','t','o','r','y','B','u','f','f','e','r',0};
234 static const WCHAR numformatW[] = {'%','u',0};
235 static const WCHAR nummethodsW[] = {'N','u','m','M','e','t','h','o','d','s',0};
236 static const WCHAR inprocserverW[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
237 static const WCHAR threadingmodelW[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
238 WCHAR clsid[39], keyname[50], module[MAX_PATH];
242 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
243 format_clsid( clsid, pclsid );
245 /* register interfaces to point to clsid */
246 while (*pProxyFileList) {
248 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
249 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
250 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
252 TRACE("registering %s %s => %s\n",
253 debugstr_a(name), debugstr_guid(proxy->header.piid), debugstr_w(clsid));
255 strcpyW( keyname, interfaceW );
256 format_clsid( keyname + strlenW(keyname), proxy->header.piid );
257 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
260 RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name)+1);
261 RegSetValueW( key, clsid32W, REG_SZ, clsid, 0 );
262 sprintfW(num, numformatW, proxy->header.DispatchTableCount);
263 RegSetValueW( key, nummethodsW, REG_SZ, num, 0 );
270 /* register clsid to point to module */
271 strcpyW( keyname, clsidW );
272 strcatW( keyname, clsid );
273 len = GetModuleFileNameW(hDll, module, sizeof(module)/sizeof(WCHAR));
274 if (len && len < sizeof(module)) {
275 TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module));
276 if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
277 RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW));
278 if (RegCreateKeyW(key, inprocserverW, &subkey) == ERROR_SUCCESS) {
279 RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (strlenW(module)+1)*sizeof(WCHAR));
280 RegSetValueExW(subkey, threadingmodelW, 0, REG_SZ, (const BYTE *)bothW, sizeof(bothW));
290 /***********************************************************************
291 * NdrDllUnregisterProxy [RPCRT4.@]
293 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
294 const ProxyFileInfo **pProxyFileList,
297 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
298 static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
301 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
303 /* unregister interfaces */
304 while (*pProxyFileList) {
306 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
307 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
308 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
310 TRACE("unregistering %s %s\n", debugstr_a(name), debugstr_guid(proxy->header.piid));
312 strcpyW( keyname, interfaceW );
313 format_clsid( keyname + strlenW(keyname), proxy->header.piid );
314 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
319 /* unregister clsid */
320 strcpyW( keyname, clsidW );
321 format_clsid( keyname + strlenW(keyname), pclsid );
322 RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);