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
36 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
42 static BOOL
FindProxyInfo(const ProxyFileInfo
**pProxyFileList
, REFIID riid
, const ProxyFileInfo
**pProxyInfo
, int *pIndex
)
44 while (*pProxyFileList
) {
45 if ((*pProxyFileList
)->pIIDLookupRtn(riid
, pIndex
)) {
46 *pProxyInfo
= *pProxyFileList
;
47 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo
, *pIndex
);
56 static HRESULT WINAPI
CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface
,
60 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
61 TRACE("(%p)->QueryInterface(%s,%p)\n",iface
,debugstr_guid(riid
),obj
);
62 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
63 IsEqualGUID(&IID_IPSFactoryBuffer
,riid
)) {
71 static ULONG WINAPI
CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface
)
73 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
74 TRACE("(%p)->AddRef()\n",iface
);
75 return ++(This
->RefCount
);
78 static ULONG WINAPI
CStdPSFactory_Release(LPPSFACTORYBUFFER iface
)
80 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
81 TRACE("(%p)->Release()\n",iface
);
82 return --(This
->RefCount
);
85 static HRESULT WINAPI
CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface
,
88 LPRPCPROXYBUFFER
*ppProxy
,
91 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
92 const ProxyFileInfo
*ProxyInfo
;
94 TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface
,pUnkOuter
,
95 debugstr_guid(riid
),ppProxy
,ppv
);
96 if (!FindProxyInfo(This
->pProxyFileList
,riid
,&ProxyInfo
,&Index
))
98 return StdProxy_Construct(riid
, pUnkOuter
, ProxyInfo
, Index
, iface
, ppProxy
, ppv
);
101 static HRESULT WINAPI
CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface
,
103 LPUNKNOWN pUnkServer
,
104 LPRPCSTUBBUFFER
*ppStub
)
106 CStdPSFactoryBuffer
*This
= (CStdPSFactoryBuffer
*)iface
;
107 const ProxyFileInfo
*ProxyInfo
;
109 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface
,debugstr_guid(riid
),
111 if (!FindProxyInfo(This
->pProxyFileList
,riid
,&ProxyInfo
,&Index
))
112 return E_NOINTERFACE
;
114 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
115 return CStdStubBuffer_Delegating_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
116 ProxyInfo
->pStubVtblList
[Index
], ProxyInfo
->pDelegatedIIDs
[Index
],
119 return CStdStubBuffer_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
120 ProxyInfo
->pStubVtblList
[Index
], iface
, ppStub
);
123 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl
=
125 CStdPSFactory_QueryInterface
,
126 CStdPSFactory_AddRef
,
127 CStdPSFactory_Release
,
128 CStdPSFactory_CreateProxy
,
129 CStdPSFactory_CreateStub
132 /***********************************************************************
133 * NdrDllGetClassObject [RPCRT4.@]
135 HRESULT WINAPI
NdrDllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
,
136 const ProxyFileInfo
**pProxyFileList
,
138 CStdPSFactoryBuffer
*pPSFactoryBuffer
)
140 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid
),
141 debugstr_guid(iid
), ppv
, pProxyFileList
, debugstr_guid(pclsid
),
145 if (!pPSFactoryBuffer
->lpVtbl
) {
146 const ProxyFileInfo
**pProxyFileList2
;
147 int max_delegating_vtbl_size
= 0;
148 pPSFactoryBuffer
->lpVtbl
= &CStdPSFactory_Vtbl
;
149 pPSFactoryBuffer
->RefCount
= 0;
150 pPSFactoryBuffer
->pProxyFileList
= pProxyFileList
;
151 for (pProxyFileList2
= pProxyFileList
; *pProxyFileList2
; pProxyFileList2
++) {
153 for (i
= 0; i
< (*pProxyFileList2
)->TableSize
; i
++) {
154 /* FIXME: i think that different vtables should be copied for
155 * async interfaces */
156 void * const *pSrcRpcStubVtbl
= (void * const *)&CStdStubBuffer_Vtbl
;
157 void **pRpcStubVtbl
= (void **)&(*pProxyFileList2
)->pStubVtblList
[i
]->Vtbl
;
160 if ((*pProxyFileList2
)->pDelegatedIIDs
&& (*pProxyFileList2
)->pDelegatedIIDs
[i
]) {
161 pSrcRpcStubVtbl
= (void * const *)&CStdStubBuffer_Delegating_Vtbl
;
162 if ((*pProxyFileList2
)->pStubVtblList
[i
]->header
.DispatchTableCount
> max_delegating_vtbl_size
)
163 max_delegating_vtbl_size
= (*pProxyFileList2
)->pStubVtblList
[i
]->header
.DispatchTableCount
;
166 for (j
= 0; j
< sizeof(IRpcStubBufferVtbl
)/sizeof(void *); j
++)
167 if (!pRpcStubVtbl
[j
])
168 pRpcStubVtbl
[j
] = pSrcRpcStubVtbl
[j
];
171 if(max_delegating_vtbl_size
> 0)
172 create_delegating_vtbl(max_delegating_vtbl_size
);
174 if (IsEqualGUID(rclsid
, pclsid
))
175 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
177 const ProxyFileInfo
*info
;
179 /* otherwise, the dll may be using the iid as the clsid, so
180 * search for it in the proxy file list */
181 if (FindProxyInfo(pProxyFileList
, rclsid
, &info
, &index
))
182 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
184 WARN("class %s not available\n", debugstr_guid(rclsid
));
185 return CLASS_E_CLASSNOTAVAILABLE
;
189 /***********************************************************************
190 * NdrDllCanUnloadNow [RPCRT4.@]
192 HRESULT WINAPI
NdrDllCanUnloadNow(CStdPSFactoryBuffer
*pPSFactoryBuffer
)
194 return !(pPSFactoryBuffer
->RefCount
);
197 /***********************************************************************
198 * NdrDllRegisterProxy [RPCRT4.@]
200 HRESULT WINAPI
NdrDllRegisterProxy(HMODULE hDll
,
201 const ProxyFileInfo
**pProxyFileList
,
205 char keyname
[120], module
[MAX_PATH
];
209 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
210 UuidToStringA((UUID
*)pclsid
, (unsigned char**)&clsid
);
212 /* register interfaces to point to clsid */
213 while (*pProxyFileList
) {
215 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
216 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
217 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
220 TRACE("registering %s %s => %s\n", name
, debugstr_guid(proxy
->header
.piid
), clsid
);
222 UuidToStringA((UUID
*)proxy
->header
.piid
, (unsigned char**)&iid
);
223 snprintf(keyname
, sizeof(keyname
), "Interface\\{%s}", iid
);
224 RpcStringFreeA((unsigned char**)&iid
);
225 if (RegCreateKeyExA(HKEY_CLASSES_ROOT
, keyname
, 0, NULL
, 0,
226 KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
) {
228 RegSetValueExA(key
, NULL
, 0, REG_SZ
, (const BYTE
*)name
, strlen(name
));
229 if (RegCreateKeyExA(key
, "ProxyStubClsid32", 0, NULL
, 0,
230 KEY_WRITE
, NULL
, &subkey
, NULL
) == ERROR_SUCCESS
) {
231 snprintf(module
, sizeof(module
), "{%s}", clsid
);
232 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (LPBYTE
)module
, strlen(module
));
241 /* register clsid to point to module */
242 snprintf(keyname
, sizeof(keyname
), "CLSID\\{%s}", clsid
);
243 len
= GetModuleFileNameA(hDll
, module
, sizeof(module
));
244 if (len
&& len
< sizeof(module
)) {
245 TRACE("registering CLSID %s => %s\n", clsid
, module
);
246 if (RegCreateKeyExA(HKEY_CLASSES_ROOT
, keyname
, 0, NULL
, 0,
247 KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
) {
248 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (const BYTE
*)"PSFactoryBuffer", strlen("PSFactoryBuffer"));
249 if (RegCreateKeyExA(key
, "InProcServer32", 0, NULL
, 0,
250 KEY_WRITE
, NULL
, &subkey
, NULL
) == ERROR_SUCCESS
) {
251 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (LPBYTE
)module
, strlen(module
));
252 RegSetValueExA(subkey
, "ThreadingModel", 0, REG_SZ
, (const BYTE
*)"Both", strlen("Both"));
260 RpcStringFreeA((unsigned char**)&clsid
);
264 /***********************************************************************
265 * NdrDllUnregisterProxy [RPCRT4.@]
267 HRESULT WINAPI
NdrDllUnregisterProxy(HMODULE hDll
,
268 const ProxyFileInfo
**pProxyFileList
,
272 char keyname
[120], module
[MAX_PATH
];
275 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
276 UuidToStringA((UUID
*)pclsid
, (unsigned char**)&clsid
);
278 /* unregister interfaces */
279 while (*pProxyFileList
) {
281 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
282 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
283 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
286 TRACE("unregistering %s %s <= %s\n", name
, debugstr_guid(proxy
->header
.piid
), clsid
);
288 UuidToStringA((UUID
*)proxy
->header
.piid
, (unsigned char**)&iid
);
289 snprintf(keyname
, sizeof(keyname
), "Interface\\{%s}", iid
);
290 RpcStringFreeA((unsigned char**)&iid
);
291 RegDeleteKeyA(HKEY_CLASSES_ROOT
, keyname
);
296 /* unregister clsid */
297 snprintf(keyname
, sizeof(keyname
), "CLSID\\{%s}", clsid
);
298 len
= GetModuleFileNameA(hDll
, module
, sizeof(module
));
299 if (len
&& len
< sizeof(module
)) {
300 TRACE("unregistering CLSID %s <= %s\n", clsid
, module
);
301 RegDeleteKeyA(HKEY_CLASSES_ROOT
, keyname
);
305 RpcStringFreeA((unsigned char**)&clsid
);