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 #define NO_SHLWAPI_PATH
26 #define NO_SHLWAPI_STRFCNS
27 #define NO_SHLWAPI_GDI
28 #define NO_SHLWAPI_STREAM
31 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
33 static void format_clsid( WCHAR
*buffer
, const CLSID
*clsid
)
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};
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] );
45 static BOOL
FindProxyInfo(const ProxyFileInfo
**pProxyFileList
, REFIID riid
, const ProxyFileInfo
**pProxyInfo
, int *pIndex
)
47 while (*pProxyFileList
) {
48 if ((*pProxyFileList
)->pIIDLookupRtn(riid
, pIndex
)) {
49 *pProxyInfo
= *pProxyFileList
;
50 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo
, *pIndex
);
59 static HRESULT WINAPI
CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface
,
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
)) {
68 InterlockedIncrement( &This
->RefCount
);
74 static ULONG WINAPI
CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface
)
76 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
77 TRACE("(%p)->AddRef()\n",iface
);
78 return InterlockedIncrement( &This
->RefCount
);
81 static ULONG WINAPI
CStdPSFactory_Release(LPPSFACTORYBUFFER iface
)
83 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
84 TRACE("(%p)->Release()\n",iface
);
85 return InterlockedDecrement( &This
->RefCount
);
88 static HRESULT WINAPI
CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface
,
91 LPRPCPROXYBUFFER
*ppProxy
,
94 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
95 const ProxyFileInfo
*ProxyInfo
;
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
);
104 static HRESULT WINAPI
CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface
,
106 LPUNKNOWN pUnkServer
,
107 LPRPCSTUBBUFFER
*ppStub
)
109 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
110 const ProxyFileInfo
*ProxyInfo
;
112 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface
,debugstr_guid(riid
),
114 if (!FindProxyInfo(This
->pProxyFileList
,riid
,&ProxyInfo
,&Index
))
115 return E_NOINTERFACE
;
117 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
118 return CStdStubBuffer_Delegating_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
119 ProxyInfo
->pStubVtblList
[Index
], ProxyInfo
->pDelegatedIIDs
[Index
],
122 return CStdStubBuffer_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
123 ProxyInfo
->pStubVtblList
[Index
], iface
, ppStub
);
126 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl
=
128 CStdPSFactory_QueryInterface
,
129 CStdPSFactory_AddRef
,
130 CStdPSFactory_Release
,
131 CStdPSFactory_CreateProxy
,
132 CStdPSFactory_CreateStub
136 static void init_psfactory( CStdPSFactoryBuffer
*psfac
, const ProxyFileInfo
**file_list
)
140 psfac
->lpVtbl
= &CStdPSFactory_Vtbl
;
142 psfac
->pProxyFileList
= file_list
;
143 for (i
= 0; file_list
[i
]; i
++)
145 const PCInterfaceProxyVtblList
*proxies
= file_list
[i
]->pProxyVtblList
;
146 const PCInterfaceStubVtblList
*stubs
= file_list
[i
]->pStubVtblList
;
148 for (j
= 0; j
< file_list
[i
]->TableSize
; j
++)
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
;
155 if (file_list
[i
]->pDelegatedIIDs
&& file_list
[i
]->pDelegatedIIDs
[j
])
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
;
163 for (k
= 0; k
< sizeof(IRpcStubBufferVtbl
)/sizeof(void *); k
++)
164 if (!pRpcStubVtbl
[k
]) pRpcStubVtbl
[k
] = pSrcRpcStubVtbl
[k
];
170 /***********************************************************************
171 * NdrDllGetClassObject [RPCRT4.@]
173 HRESULT WINAPI
NdrDllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
,
174 const ProxyFileInfo
**pProxyFileList
,
176 CStdPSFactoryBuffer
*pPSFactoryBuffer
)
178 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid
),
179 debugstr_guid(iid
), ppv
, pProxyFileList
, debugstr_guid(pclsid
),
183 if (!pPSFactoryBuffer
->lpVtbl
) init_psfactory( pPSFactoryBuffer
, pProxyFileList
);
185 if (pclsid
&& IsEqualGUID(rclsid
, pclsid
))
186 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
188 const ProxyFileInfo
*info
;
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
);
195 WARN("class %s not available\n", debugstr_guid(rclsid
));
196 return CLASS_E_CLASSNOTAVAILABLE
;
200 /***********************************************************************
201 * NdrDllCanUnloadNow [RPCRT4.@]
203 HRESULT WINAPI
NdrDllCanUnloadNow(CStdPSFactoryBuffer
*pPSFactoryBuffer
)
205 return pPSFactoryBuffer
->RefCount
!= 0 ? S_FALSE
: S_OK
;
209 /***********************************************************************
210 * NdrDllRegisterProxy [RPCRT4.@]
212 HRESULT WINAPI
NdrDllRegisterProxy(HMODULE hDll
,
213 const ProxyFileInfo
**pProxyFileList
,
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
];
229 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
231 if (!hDll
) return E_HANDLE
;
232 if (!*pProxyFileList
) return E_NOINTERFACE
;
235 format_clsid( clsid
, pclsid
);
236 else if ((*pProxyFileList
)->TableSize
> 0)
237 format_clsid( clsid
,(*pProxyFileList
)->pStubVtblList
[0]->header
.piid
);
239 return E_NOINTERFACE
;
241 /* register interfaces to point to clsid */
242 while (*pProxyFileList
) {
244 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
245 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
246 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
248 TRACE("registering %s %s => %s\n",
249 debugstr_a(name
), debugstr_guid(proxy
->header
.piid
), debugstr_w(clsid
));
251 strcpyW( keyname
, interfaceW
);
252 format_clsid( keyname
+ strlenW(keyname
), proxy
->header
.piid
);
253 if (RegCreateKeyW(HKEY_CLASSES_ROOT
, keyname
, &key
) == ERROR_SUCCESS
) {
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 );
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
));
286 /***********************************************************************
287 * NdrDllUnregisterProxy [RPCRT4.@]
289 HRESULT WINAPI
NdrDllUnregisterProxy(HMODULE hDll
,
290 const ProxyFileInfo
**pProxyFileList
,
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};
298 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
300 format_clsid( clsid
, pclsid
);
301 else if ((*pProxyFileList
)->TableSize
> 0)
302 format_clsid( clsid
,(*pProxyFileList
)->pStubVtblList
[0]->header
.piid
);
304 return E_NOINTERFACE
;
306 /* unregister interfaces */
307 while (*pProxyFileList
) {
309 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
310 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
311 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
313 TRACE("unregistering %s %s\n", debugstr_a(name
), debugstr_guid(proxy
->header
.piid
));
315 strcpyW( keyname
, interfaceW
);
316 format_clsid( keyname
+ strlenW(keyname
), proxy
->header
.piid
);
317 SHDeleteKeyW(HKEY_CLASSES_ROOT
, keyname
);
322 /* unregister clsid */
323 strcpyW( keyname
, clsidW
);
324 strcatW( keyname
, clsid
);
325 SHDeleteKeyW(HKEY_CLASSES_ROOT
, keyname
);