wine.inf: We should not override existing associations.
[wine/hacks.git] / dlls / rpcrt4 / cpsf.c
bloba350c401b3d7d4fe2e3bbc44f7b9cb79b7904238
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winreg.h"
32 #include "objbase.h"
34 #include "rpcproxy.h"
36 #include "wine/debug.h"
38 #include "cpsf.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);
48 return TRUE;
50 pProxyFileList++;
52 TRACE("not found\n");
53 return FALSE;
56 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
57 REFIID riid,
58 LPVOID *obj)
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)) {
64 *obj = This;
65 This->RefCount++;
66 return S_OK;
68 return E_NOINTERFACE;
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,
86 LPUNKNOWN pUnkOuter,
87 REFIID riid,
88 LPRPCPROXYBUFFER *ppProxy,
89 LPVOID *ppv)
91 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
92 const ProxyFileInfo *ProxyInfo;
93 int Index;
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))
97 return E_NOINTERFACE;
98 return StdProxy_Construct(riid, pUnkOuter, ProxyInfo, Index, iface, ppProxy, ppv);
101 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
102 REFIID riid,
103 LPUNKNOWN pUnkServer,
104 LPRPCSTUBBUFFER *ppStub)
106 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
107 const ProxyFileInfo *ProxyInfo;
108 int Index;
109 TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
110 pUnkServer,ppStub);
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,
131 const CLSID *pclsid,
132 CStdPSFactoryBuffer *pPSFactoryBuffer)
134 TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
135 debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
136 pPSFactoryBuffer);
138 *ppv = NULL;
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++) {
145 int i;
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;
151 int j;
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);
161 else {
162 const ProxyFileInfo *info;
163 int index;
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,
187 const CLSID *pclsid)
189 LPSTR clsid;
190 char keyname[120], module[MAX_PATH];
191 HKEY key, subkey;
192 DWORD len;
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) {
199 unsigned u;
200 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
201 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
202 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
203 LPSTR iid;
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) {
212 if (name)
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));
218 RegCloseKey(subkey);
220 RegCloseKey(key);
223 pProxyFileList++;
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"));
238 RegCloseKey(subkey);
240 RegCloseKey(key);
244 /* done */
245 RpcStringFreeA((unsigned char**)&clsid);
246 return S_OK;
249 /***********************************************************************
250 * NdrDllUnregisterProxy [RPCRT4.@]
252 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
253 const ProxyFileInfo **pProxyFileList,
254 const CLSID *pclsid)
256 LPSTR clsid;
257 char keyname[120], module[MAX_PATH];
258 DWORD len;
260 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
261 UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
263 /* unregister interfaces */
264 while (*pProxyFileList) {
265 unsigned u;
266 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
267 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
268 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
269 LPSTR iid;
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);
278 pProxyFileList++;
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);
289 /* done */
290 RpcStringFreeA((unsigned char**)&clsid);
291 return S_OK;