riched20: Fixed calculation of left indent and first indent paragraph
[wine] / dlls / 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 #define COBJMACROS
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winreg.h"
31
32 #include "objbase.h"
33
34 #include "rpcproxy.h"
35
36 #include "wine/debug.h"
37
38 #include "cpsf.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
43 {
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;
49     }
50     pProxyFileList++;
51   }
52   TRACE("not found\n");
53   return FALSE;
54 }
55
56 static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
57                                                   REFIID riid,
58                                                   LPVOID *obj)
59 {
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;
67   }
68   return E_NOINTERFACE;
69 }
70
71 static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
72 {
73   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
74   TRACE("(%p)->AddRef()\n",iface);
75   return ++(This->RefCount);
76 }
77
78 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
79 {
80   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
81   TRACE("(%p)->Release()\n",iface);
82   return --(This->RefCount);
83 }
84
85 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
86                                                LPUNKNOWN pUnkOuter,
87                                                REFIID riid,
88                                                LPRPCPROXYBUFFER *ppProxy,
89                                                LPVOID *ppv)
90 {
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->pNamesArray[Index],
99                             ProxyInfo->pProxyVtblList[Index],
100                             ProxyInfo->pStubVtblList[Index], iface, ppProxy, ppv);
101 }
102
103 static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
104                                               REFIID riid,
105                                               LPUNKNOWN pUnkServer,
106                                               LPRPCSTUBBUFFER *ppStub)
107 {
108   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
109   const ProxyFileInfo *ProxyInfo;
110   int Index;
111   TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
112        pUnkServer,ppStub);
113   if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
114     return E_NOINTERFACE;
115   return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
116                                   ProxyInfo->pStubVtblList[Index], iface, ppStub);
117 }
118
119 static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
120 {
121   CStdPSFactory_QueryInterface,
122   CStdPSFactory_AddRef,
123   CStdPSFactory_Release,
124   CStdPSFactory_CreateProxy,
125   CStdPSFactory_CreateStub
126 };
127
128 /***********************************************************************
129  *           NdrDllGetClassObject [RPCRT4.@]
130  */
131 HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
132                                    const ProxyFileInfo **pProxyFileList,
133                                    const CLSID *pclsid,
134                                    CStdPSFactoryBuffer *pPSFactoryBuffer)
135 {
136   TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
137     debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
138     pPSFactoryBuffer);
139
140   *ppv = NULL;
141   if (!pPSFactoryBuffer->lpVtbl) {
142     pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
143     pPSFactoryBuffer->RefCount = 0;
144     pPSFactoryBuffer->pProxyFileList = pProxyFileList;
145   }
146   if (IsEqualGUID(rclsid, pclsid))
147     return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
148   else {
149     const ProxyFileInfo *info;
150     int index;
151     /* otherwise, the dll may be using the iid as the clsid, so
152      * search for it in the proxy file list */
153     if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
154       return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
155
156     WARN("class %s not available\n", debugstr_guid(rclsid));
157     return CLASS_E_CLASSNOTAVAILABLE;
158   }
159 }
160
161 /***********************************************************************
162  *           NdrDllCanUnloadNow [RPCRT4.@]
163  */
164 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
165 {
166   return !(pPSFactoryBuffer->RefCount);
167 }
168
169 /***********************************************************************
170  *           NdrDllRegisterProxy [RPCRT4.@]
171  */
172 HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
173                                   const ProxyFileInfo **pProxyFileList,
174                                   const CLSID *pclsid)
175 {
176   LPSTR clsid;
177   char keyname[120], module[MAX_PATH];
178   HKEY key, subkey;
179   DWORD len;
180
181   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
182   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
183
184   /* register interfaces to point to clsid */
185   while (*pProxyFileList) {
186     unsigned u;
187     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
188       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
189       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
190       LPSTR iid;
191
192       TRACE("registering %s %s => %s\n", name, debugstr_guid(proxy->header.piid), clsid);
193
194       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
195       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
196       RpcStringFreeA((unsigned char**)&iid);
197       if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
198                           KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
199         if (name)
200           RegSetValueExA(key, NULL, 0, REG_SZ, (LPBYTE)name, strlen(name));
201         if (RegCreateKeyExA(key, "ProxyStubClsid32", 0, NULL, 0,
202                             KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
203           snprintf(module, sizeof(module), "{%s}", clsid);
204           RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
205           RegCloseKey(subkey);
206         }
207         RegCloseKey(key);
208       }
209     }
210     pProxyFileList++;
211   }
212
213   /* register clsid to point to module */
214   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
215   len = GetModuleFileNameA(hDll, module, sizeof(module));
216   if (len && len < sizeof(module)) {
217     TRACE("registering CLSID %s => %s\n", clsid, module);
218     if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
219                         KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
220       if (RegCreateKeyExA(key, "InProcServer32", 0, NULL, 0,
221                           KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
222         RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
223         RegCloseKey(subkey);
224       }
225       RegCloseKey(key);
226     }
227   }
228
229   /* done */
230   RpcStringFreeA((unsigned char**)&clsid);
231   return S_OK;
232 }
233
234 /***********************************************************************
235  *           NdrDllUnregisterProxy [RPCRT4.@]
236  */
237 HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
238                                     const ProxyFileInfo **pProxyFileList,
239                                     const CLSID *pclsid)
240 {
241   LPSTR clsid;
242   char keyname[120], module[MAX_PATH];
243   DWORD len;
244
245   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
246   UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
247
248   /* unregister interfaces */
249   while (*pProxyFileList) {
250     unsigned u;
251     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
252       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
253       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
254       LPSTR iid;
255
256       TRACE("unregistering %s %s <= %s\n", name, debugstr_guid(proxy->header.piid), clsid);
257
258       UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
259       snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
260       RpcStringFreeA((unsigned char**)&iid);
261       RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
262     }
263     pProxyFileList++;
264   }
265
266   /* unregister clsid */
267   snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
268   len = GetModuleFileNameA(hDll, module, sizeof(module));
269   if (len && len < sizeof(module)) {
270     TRACE("unregistering CLSID %s <= %s\n", clsid, module);
271     RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
272   }
273
274   /* done */
275   RpcStringFreeA((unsigned char**)&clsid);
276   return S_OK;
277 }