Sync to Wine-20040914:
[reactos.git] / reactos / lib / rpcrt4 / cpsf.c
1 /*
2 * COM proxy/stub factory (CStdPSFactory) implementation
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 *
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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winreg.h"
29
30 #include "objbase.h"
31
32 #include "rpcproxy.h"
33
34 #include "wine/debug.h"
35
36 #include "cpsf.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
41 {
42 while (*pProxyFileList) {
43 if ((*pProxyFileList)->pIIDLookupRtn(riid, pIndex)) {
44 *pProxyInfo = *pProxyFileList;
45 TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo, *pIndex);
46 return TRUE;
47 }
48 pProxyFileList++;
49 }
50 TRACE("not found\n");
51 return FALSE;
52 }
53
54 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
55 REFIID riid,
56 LPVOID *obj)
57 {
58 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
59 TRACE("(%p)->QueryInterface(%s,%p)\n",iface,debugstr_guid(riid),obj);
60 if (IsEqualGUID(&IID_IUnknown,riid) ||
61 IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
62 *obj = This;
63 This->RefCount++;
64 return S_OK;
65 }
66 return E_NOINTERFACE;
67 }
68
69 static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
70 {
71 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
72 TRACE("(%p)->AddRef()\n",iface);
73 return ++(This->RefCount);
74 }
75
76 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
77 {
78 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
79 TRACE("(%p)->Release()\n",iface);
80 return --(This->RefCount);
81 }
82
83 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
84 LPUNKNOWN pUnkOuter,
85 REFIID riid,
86 LPRPCPROXYBUFFER *ppProxy,
87 LPVOID *ppv)
88 {
89 CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
90 const ProxyFileInfo *ProxyInfo;
91 int Index;
92 TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface,pUnkOuter,
93 debugstr_guid(riid),ppProxy,ppv);
94 if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
95 return E_NOINTERFACE;
96 return StdProxy_Construct(riid, pUnkOuter, ProxyInfo->pNamesArray[Index],
97 ProxyInfo->pProxyVtblList[Index],
98 ProxyInfo->pStubVtblList[Index], iface, ppProxy, ppv);
99 }
100
101 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
102 REFIID riid,
103 LPUNKNOWN pUnkServer,
104 LPRPCSTUBBUFFER *ppStub)
105 {
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);
115 }
116
117 static IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
118 {
119 CStdPSFactory_QueryInterface,
120 CStdPSFactory_AddRef,
121 CStdPSFactory_Release,
122 CStdPSFactory_CreateProxy,
123 CStdPSFactory_CreateStub
124 };
125
126 /***********************************************************************
127 * NdrDllGetClassObject [RPCRT4.@]
128 */
129 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
130 const ProxyFileInfo **pProxyFileList,
131 const CLSID *pclsid,
132 CStdPSFactoryBuffer *pPSFactoryBuffer)
133 {
134 *ppv = NULL;
135 if (!pPSFactoryBuffer->lpVtbl) {
136 pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
137 pPSFactoryBuffer->RefCount = 0;
138 pPSFactoryBuffer->pProxyFileList = pProxyFileList;
139 }
140 if (IsEqualGUID(rclsid, pclsid))
141 return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
142 return CLASS_E_CLASSNOTAVAILABLE;
143 }
144
145 /***********************************************************************
146 * NdrDllCanUnloadNow [RPCRT4.@]
147 */
148 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
149 {
150 return !(pPSFactoryBuffer->RefCount);
151 }
152
153 /***********************************************************************
154 * NdrDllRegisterProxy [RPCRT4.@]
155 */
156 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
157 const ProxyFileInfo **pProxyFileList,
158 const CLSID *pclsid)
159 {
160 LPSTR clsid;
161 char keyname[120], module[MAX_PATH];
162 HKEY key, subkey;
163 DWORD len;
164
165 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
166 UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
167
168 /* register interfaces to point to clsid */
169 while (*pProxyFileList) {
170 unsigned u;
171 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
172 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
173 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
174 LPSTR iid;
175
176 TRACE("registering %s %s => %s\n", name, debugstr_guid(proxy->header.piid), clsid);
177
178 UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
179 snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
180 RpcStringFreeA((unsigned char**)&iid);
181 if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
182 KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
183 if (name)
184 RegSetValueExA(key, NULL, 0, REG_SZ, name, strlen(name));
185 if (RegCreateKeyExA(key, "ProxyStubClsid32", 0, NULL, 0,
186 KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
187 snprintf(module, sizeof(module), "{%s}", clsid);
188 RegSetValueExA(subkey, NULL, 0, REG_SZ, module, strlen(module));
189 RegCloseKey(subkey);
190 }
191 RegCloseKey(key);
192 }
193 }
194 pProxyFileList++;
195 }
196
197 /* register clsid to point to module */
198 snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
199 len = GetModuleFileNameA(hDll, module, sizeof(module));
200 if (len && len < sizeof(module)) {
201 TRACE("registering CLSID %s => %s\n", clsid, module);
202 if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
203 KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
204 if (RegCreateKeyExA(key, "InProcServer32", 0, NULL, 0,
205 KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
206 RegSetValueExA(subkey, NULL, 0, REG_SZ, module, strlen(module));
207 RegCloseKey(subkey);
208 }
209 RegCloseKey(key);
210 }
211 }
212
213 /* done */
214 RpcStringFreeA((unsigned char**)&clsid);
215 return S_OK;
216 }
217
218 /***********************************************************************
219 * NdrDllUnregisterProxy [RPCRT4.@]
220 */
221 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
222 const ProxyFileInfo **pProxyFileList,
223 const CLSID *pclsid)
224 {
225 LPSTR clsid;
226 char keyname[120], module[MAX_PATH];
227 DWORD len;
228
229 TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
230 UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
231
232 /* unregister interfaces */
233 while (*pProxyFileList) {
234 unsigned u;
235 for (u=0; u<(*pProxyFileList)->TableSize; u++) {
236 CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
237 PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
238 LPSTR iid;
239
240 TRACE("unregistering %s %s <= %s\n", name, debugstr_guid(proxy->header.piid), clsid);
241
242 UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
243 snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
244 RpcStringFreeA((unsigned char**)&iid);
245 RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
246 }
247 pProxyFileList++;
248 }
249
250 /* unregister clsid */
251 snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
252 len = GetModuleFileNameA(hDll, module, sizeof(module));
253 if (len && len < sizeof(module)) {
254 TRACE("unregistering CLSID %s <= %s\n", clsid, module);
255 RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
256 }
257
258 /* done */
259 RpcStringFreeA((unsigned char**)&clsid);
260 return S_OK;
261 }