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
25 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
27 static void format_clsid( WCHAR
*buffer
, const CLSID
*clsid
)
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};
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] );
39 static BOOL
FindProxyInfo(const ProxyFileInfo
**pProxyFileList
, REFIID riid
, const ProxyFileInfo
**pProxyInfo
, int *pIndex
)
41 while (*pProxyFileList
) {
42 if ((*pProxyFileList
)->pIIDLookupRtn(riid
, pIndex
)) {
43 *pProxyInfo
= *pProxyFileList
;
44 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo
, *pIndex
);
53 static HRESULT WINAPI
CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface
,
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
)) {
62 InterlockedIncrement( &This
->RefCount
);
68 static ULONG WINAPI
CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface
)
70 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
71 TRACE("(%p)->AddRef()\n",iface
);
72 return InterlockedIncrement( &This
->RefCount
);
75 static ULONG WINAPI
CStdPSFactory_Release(LPPSFACTORYBUFFER iface
)
77 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
78 TRACE("(%p)->Release()\n",iface
);
79 return InterlockedDecrement( &This
->RefCount
);
82 static HRESULT WINAPI
CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface
,
85 LPRPCPROXYBUFFER
*ppProxy
,
88 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
89 const ProxyFileInfo
*ProxyInfo
;
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
))
95 return StdProxy_Construct(riid
, pUnkOuter
, ProxyInfo
, Index
, iface
, ppProxy
, ppv
);
98 static HRESULT WINAPI
CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface
,
100 LPUNKNOWN pUnkServer
,
101 LPRPCSTUBBUFFER
*ppStub
)
103 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
104 const ProxyFileInfo
*ProxyInfo
;
106 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface
,debugstr_guid(riid
),
108 if (!FindProxyInfo(This
->pProxyFileList
,riid
,&ProxyInfo
,&Index
))
109 return E_NOINTERFACE
;
111 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
112 return CStdStubBuffer_Delegating_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
113 ProxyInfo
->pStubVtblList
[Index
], ProxyInfo
->pDelegatedIIDs
[Index
],
116 return CStdStubBuffer_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
117 ProxyInfo
->pStubVtblList
[Index
], iface
, ppStub
);
120 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl
=
122 CStdPSFactory_QueryInterface
,
123 CStdPSFactory_AddRef
,
124 CStdPSFactory_Release
,
125 CStdPSFactory_CreateProxy
,
126 CStdPSFactory_CreateStub
130 static void init_psfactory( CStdPSFactoryBuffer
*psfac
, const ProxyFileInfo
**file_list
)
134 psfac
->lpVtbl
= &CStdPSFactory_Vtbl
;
136 psfac
->pProxyFileList
= file_list
;
137 for (i
= 0; file_list
[i
]; i
++)
139 const PCInterfaceProxyVtblList
*proxies
= file_list
[i
]->pProxyVtblList
;
140 const PCInterfaceStubVtblList
*stubs
= file_list
[i
]->pStubVtblList
;
142 for (j
= 0; j
< file_list
[i
]->TableSize
; j
++)
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
;
149 if (file_list
[i
]->pDelegatedIIDs
&& file_list
[i
]->pDelegatedIIDs
[j
])
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
;
157 for (k
= 0; k
< sizeof(IRpcStubBufferVtbl
)/sizeof(void *); k
++)
158 if (!pRpcStubVtbl
[k
]) pRpcStubVtbl
[k
] = pSrcRpcStubVtbl
[k
];
164 /***********************************************************************
165 * NdrDllGetClassObject [RPCRT4.@]
167 HRESULT WINAPI
NdrDllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
,
168 const ProxyFileInfo
**pProxyFileList
,
170 CStdPSFactoryBuffer
*pPSFactoryBuffer
)
172 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid
),
173 debugstr_guid(iid
), ppv
, pProxyFileList
, debugstr_guid(pclsid
),
177 if (!pPSFactoryBuffer
->lpVtbl
) init_psfactory( pPSFactoryBuffer
, pProxyFileList
);
179 if (pclsid
&& IsEqualGUID(rclsid
, pclsid
))
180 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
182 const ProxyFileInfo
*info
;
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
);
189 WARN("class %s not available\n", debugstr_guid(rclsid
));
190 return CLASS_E_CLASSNOTAVAILABLE
;
194 /***********************************************************************
195 * NdrDllCanUnloadNow [RPCRT4.@]
197 HRESULT WINAPI
NdrDllCanUnloadNow(CStdPSFactoryBuffer
*pPSFactoryBuffer
)
199 return pPSFactoryBuffer
->RefCount
!= 0 ? S_FALSE
: S_OK
;
203 /***********************************************************************
204 * NdrDllRegisterProxy [RPCRT4.@]
206 HRESULT WINAPI
NdrDllRegisterProxy(HMODULE hDll
,
207 const ProxyFileInfo
**pProxyFileList
,
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
];
223 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
225 if (!hDll
) return E_HANDLE
;
226 if (!*pProxyFileList
) return E_NOINTERFACE
;
229 format_clsid( clsid
, pclsid
);
230 else if ((*pProxyFileList
)->TableSize
> 0)
231 format_clsid( clsid
,(*pProxyFileList
)->pStubVtblList
[0]->header
.piid
);
233 return E_NOINTERFACE
;
235 /* register interfaces to point to clsid */
236 while (*pProxyFileList
) {
238 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
239 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
240 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
242 TRACE("registering %s %s => %s\n",
243 debugstr_a(name
), debugstr_guid(proxy
->header
.piid
), debugstr_w(clsid
));
245 strcpyW( keyname
, interfaceW
);
246 format_clsid( keyname
+ strlenW(keyname
), proxy
->header
.piid
);
247 if (RegCreateKeyW(HKEY_CLASSES_ROOT
, keyname
, &key
) == ERROR_SUCCESS
) {
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 );
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
));
280 /***********************************************************************
281 * NdrDllUnregisterProxy [RPCRT4.@]
283 HRESULT WINAPI
NdrDllUnregisterProxy(HMODULE hDll
,
284 const ProxyFileInfo
**pProxyFileList
,
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};
292 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
294 format_clsid( clsid
, pclsid
);
295 else if ((*pProxyFileList
)->TableSize
> 0)
296 format_clsid( clsid
,(*pProxyFileList
)->pStubVtblList
[0]->header
.piid
);
298 return E_NOINTERFACE
;
300 /* unregister interfaces */
301 while (*pProxyFileList
) {
303 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
304 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
305 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
307 TRACE("unregistering %s %s\n", debugstr_a(name
), debugstr_guid(proxy
->header
.piid
));
309 strcpyW( keyname
, interfaceW
);
310 format_clsid( keyname
+ strlenW(keyname
), proxy
->header
.piid
);
311 RegDeleteTreeW(HKEY_CLASSES_ROOT
, keyname
);
316 /* unregister clsid */
317 strcpyW( keyname
, clsidW
);
318 strcatW( keyname
, clsid
);
319 RegDeleteTreeW(HKEY_CLASSES_ROOT
, keyname
);