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
;
113 return CStdStubBuffer_Construct(riid
, pUnkServer
, ProxyInfo
->pNamesArray
[Index
],
114 ProxyInfo
->pStubVtblList
[Index
], iface
, ppStub
);
117 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl
=
119 CStdPSFactory_QueryInterface
,
120 CStdPSFactory_AddRef
,
121 CStdPSFactory_Release
,
122 CStdPSFactory_CreateProxy
,
123 CStdPSFactory_CreateStub
126 /***********************************************************************
127 * NdrDllGetClassObject [RPCRT4.@]
129 HRESULT WINAPI
NdrDllGetClassObject(REFCLSID rclsid
, REFIID iid
, LPVOID
*ppv
,
130 const ProxyFileInfo
**pProxyFileList
,
132 CStdPSFactoryBuffer
*pPSFactoryBuffer
)
134 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid
),
135 debugstr_guid(iid
), ppv
, pProxyFileList
, debugstr_guid(pclsid
),
139 if (!pPSFactoryBuffer
->lpVtbl
) {
140 const ProxyFileInfo
**pProxyFileList2
;
141 pPSFactoryBuffer
->lpVtbl
= &CStdPSFactory_Vtbl
;
142 pPSFactoryBuffer
->RefCount
= 0;
143 pPSFactoryBuffer
->pProxyFileList
= pProxyFileList
;
144 for (pProxyFileList2
= pProxyFileList
; *pProxyFileList2
; pProxyFileList2
++) {
146 for (i
= 0; i
< (*pProxyFileList2
)->TableSize
; i
++) {
147 /* FIXME: i think that different vtables should be copied for
148 * async interfaces */
149 void * const *pSrcRpcStubVtbl
= (void * const *)&CStdStubBuffer_Vtbl
;
150 void **pRpcStubVtbl
= (void **)&(*pProxyFileList2
)->pStubVtblList
[i
]->Vtbl
;
153 for (j
= 0; j
< sizeof(IRpcStubBufferVtbl
)/sizeof(void *); j
++)
154 if (!pRpcStubVtbl
[j
])
155 pRpcStubVtbl
[j
] = pSrcRpcStubVtbl
[j
];
159 if (IsEqualGUID(rclsid
, pclsid
))
160 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
162 const ProxyFileInfo
*info
;
164 /* otherwise, the dll may be using the iid as the clsid, so
165 * search for it in the proxy file list */
166 if (FindProxyInfo(pProxyFileList
, rclsid
, &info
, &index
))
167 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER
)pPSFactoryBuffer
, iid
, ppv
);
169 WARN("class %s not available\n", debugstr_guid(rclsid
));
170 return CLASS_E_CLASSNOTAVAILABLE
;
174 /***********************************************************************
175 * NdrDllCanUnloadNow [RPCRT4.@]
177 HRESULT WINAPI
NdrDllCanUnloadNow(CStdPSFactoryBuffer
*pPSFactoryBuffer
)
179 return !(pPSFactoryBuffer
->RefCount
);
182 /***********************************************************************
183 * NdrDllRegisterProxy [RPCRT4.@]
185 HRESULT WINAPI
NdrDllRegisterProxy(HMODULE hDll
,
186 const ProxyFileInfo
**pProxyFileList
,
190 char keyname
[120], module
[MAX_PATH
];
194 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
195 UuidToStringA((UUID
*)pclsid
, (unsigned char**)&clsid
);
197 /* register interfaces to point to clsid */
198 while (*pProxyFileList
) {
200 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
201 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
202 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
205 TRACE("registering %s %s => %s\n", name
, debugstr_guid(proxy
->header
.piid
), clsid
);
207 UuidToStringA((UUID
*)proxy
->header
.piid
, (unsigned char**)&iid
);
208 snprintf(keyname
, sizeof(keyname
), "Interface\\{%s}", iid
);
209 RpcStringFreeA((unsigned char**)&iid
);
210 if (RegCreateKeyExA(HKEY_CLASSES_ROOT
, keyname
, 0, NULL
, 0,
211 KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
) {
213 RegSetValueExA(key
, NULL
, 0, REG_SZ
, (LPBYTE
)name
, strlen(name
));
214 if (RegCreateKeyExA(key
, "ProxyStubClsid32", 0, NULL
, 0,
215 KEY_WRITE
, NULL
, &subkey
, NULL
) == ERROR_SUCCESS
) {
216 snprintf(module
, sizeof(module
), "{%s}", clsid
);
217 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (LPBYTE
)module
, strlen(module
));
226 /* register clsid to point to module */
227 snprintf(keyname
, sizeof(keyname
), "CLSID\\{%s}", clsid
);
228 len
= GetModuleFileNameA(hDll
, module
, sizeof(module
));
229 if (len
&& len
< sizeof(module
)) {
230 TRACE("registering CLSID %s => %s\n", clsid
, module
);
231 if (RegCreateKeyExA(HKEY_CLASSES_ROOT
, keyname
, 0, NULL
, 0,
232 KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
) {
233 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (LPBYTE
)"PSFactoryBuffer", strlen("PSFactoryBuffer"));
234 if (RegCreateKeyExA(key
, "InProcServer32", 0, NULL
, 0,
235 KEY_WRITE
, NULL
, &subkey
, NULL
) == ERROR_SUCCESS
) {
236 RegSetValueExA(subkey
, NULL
, 0, REG_SZ
, (LPBYTE
)module
, strlen(module
));
237 RegSetValueExA(subkey
, "ThreadingModel", 0, REG_SZ
, (LPBYTE
)"Both", strlen("Both"));
245 RpcStringFreeA((unsigned char**)&clsid
);
249 /***********************************************************************
250 * NdrDllUnregisterProxy [RPCRT4.@]
252 HRESULT WINAPI
NdrDllUnregisterProxy(HMODULE hDll
,
253 const ProxyFileInfo
**pProxyFileList
,
257 char keyname
[120], module
[MAX_PATH
];
260 TRACE("(%p,%p,%s)\n", hDll
, pProxyFileList
, debugstr_guid(pclsid
));
261 UuidToStringA((UUID
*)pclsid
, (unsigned char**)&clsid
);
263 /* unregister interfaces */
264 while (*pProxyFileList
) {
266 for (u
=0; u
<(*pProxyFileList
)->TableSize
; u
++) {
267 CInterfaceStubVtbl
*proxy
= (*pProxyFileList
)->pStubVtblList
[u
];
268 PCInterfaceName name
= (*pProxyFileList
)->pNamesArray
[u
];
271 TRACE("unregistering %s %s <= %s\n", name
, debugstr_guid(proxy
->header
.piid
), clsid
);
273 UuidToStringA((UUID
*)proxy
->header
.piid
, (unsigned char**)&iid
);
274 snprintf(keyname
, sizeof(keyname
), "Interface\\{%s}", iid
);
275 RpcStringFreeA((unsigned char**)&iid
);
276 RegDeleteKeyA(HKEY_CLASSES_ROOT
, keyname
);
281 /* unregister clsid */
282 snprintf(keyname
, sizeof(keyname
), "CLSID\\{%s}", clsid
);
283 len
= GetModuleFileNameA(hDll
, module
, sizeof(module
));
284 if (len
&& len
< sizeof(module
)) {
285 TRACE("unregistering CLSID %s <= %s\n", clsid
, module
);
286 RegDeleteKeyA(HKEY_CLASSES_ROOT
, keyname
);
290 RpcStringFreeA((unsigned char**)&clsid
);