cpsf.c 11.4 KB
Newer Older
1 2 3
/*
 * COM proxy/stub factory (CStdPSFactory) implementation
 *
4
 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20
 */

21
#include <stdarg.h>
22 23 24
#include <stdio.h>
#include <string.h>

25 26
#define COBJMACROS

27 28 29 30 31
#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winreg.h"

32
#include "objbase.h"
33 34 35 36 37 38 39 40 41

#include "rpcproxy.h"

#include "wine/debug.h"

#include "cpsf.h"

WINE_DEFAULT_DEBUG_CHANNEL(ole);

42 43
static void format_clsid( WCHAR *buffer, const CLSID *clsid )
{
44 45
    swprintf( buffer, 39, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
              clsid->Data1, clsid->Data2, clsid->Data3,
46 47 48 49 50
              clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
              clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );

}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
{
  while (*pProxyFileList) {
    if ((*pProxyFileList)->pIIDLookupRtn(riid, pIndex)) {
      *pProxyInfo = *pProxyFileList;
      TRACE("found: ProxyInfo %p Index %d\n", *pProxyInfo, *pIndex);
      return TRUE;
    }
    pProxyFileList++;
  }
  TRACE("not found\n");
  return FALSE;
}

static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
                                                  REFIID riid,
                                                  LPVOID *obj)
{
69
  CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
70 71 72 73
  TRACE("(%p)->QueryInterface(%s,%p)\n",iface,debugstr_guid(riid),obj);
  if (IsEqualGUID(&IID_IUnknown,riid) ||
      IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
    *obj = This;
74
    InterlockedIncrement( &This->RefCount );
75 76 77 78 79 80 81
    return S_OK;
  }
  return E_NOINTERFACE;
}

static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
{
82
  CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
83
  TRACE("(%p)->AddRef()\n",iface);
84
  return InterlockedIncrement( &This->RefCount );
85 86 87 88
}

static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
{
89
  CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
90
  TRACE("(%p)->Release()\n",iface);
91
  return InterlockedDecrement( &This->RefCount );
92 93 94 95 96 97 98 99
}

static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
                                               LPUNKNOWN pUnkOuter,
                                               REFIID riid,
                                               LPRPCPROXYBUFFER *ppProxy,
                                               LPVOID *ppv)
{
100
  CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
101 102 103 104 105 106
  const ProxyFileInfo *ProxyInfo;
  int Index;
  TRACE("(%p)->CreateProxy(%p,%s,%p,%p)\n",iface,pUnkOuter,
       debugstr_guid(riid),ppProxy,ppv);
  if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
    return E_NOINTERFACE;
107
  return StdProxy_Construct(riid, pUnkOuter, ProxyInfo, Index, iface, ppProxy, ppv);
108 109 110 111 112 113 114
}

static HRESULT WINAPI CStdPSFactory_CreateStub(LPPSFACTORYBUFFER iface,
                                              REFIID riid,
                                              LPUNKNOWN pUnkServer,
                                              LPRPCSTUBBUFFER *ppStub)
{
115
  CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
116 117 118 119 120 121
  const ProxyFileInfo *ProxyInfo;
  int Index;
  TRACE("(%p)->CreateStub(%s,%p,%p)\n",iface,debugstr_guid(riid),
       pUnkServer,ppStub);
  if (!FindProxyInfo(This->pProxyFileList,riid,&ProxyInfo,&Index))
    return E_NOINTERFACE;
122 123 124 125 126 127

  if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
    return  CStdStubBuffer_Delegating_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
                                                ProxyInfo->pStubVtblList[Index], ProxyInfo->pDelegatedIIDs[Index],
                                                iface, ppStub);

128 129
  return CStdStubBuffer_Construct(riid, pUnkServer, ProxyInfo->pNamesArray[Index],
                                  ProxyInfo->pStubVtblList[Index], iface, ppStub);
130 131
}

132
static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
133 134 135 136 137 138 139 140
{
  CStdPSFactory_QueryInterface,
  CStdPSFactory_AddRef,
  CStdPSFactory_Release,
  CStdPSFactory_CreateProxy,
  CStdPSFactory_CreateStub
};

141 142 143 144 145 146 147 148 149 150

static void init_psfactory( CStdPSFactoryBuffer *psfac, const ProxyFileInfo **file_list )
{
    DWORD i, j, k;

    psfac->lpVtbl = &CStdPSFactory_Vtbl;
    psfac->RefCount = 0;
    psfac->pProxyFileList = file_list;
    for (i = 0; file_list[i]; i++)
    {
151
        const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList;
152 153 154 155 156 157 158 159 160 161
        const PCInterfaceStubVtblList *stubs = file_list[i]->pStubVtblList;

        for (j = 0; j < file_list[i]->TableSize; j++)
        {
            /* FIXME: i think that different vtables should be copied for
             * async interfaces */
            void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
            void **pRpcStubVtbl = (void **)&stubs[j]->Vtbl;

            if (file_list[i]->pDelegatedIIDs && file_list[i]->pDelegatedIIDs[j])
162
            {
163 164 165
                void **vtbl = proxies[j]->Vtbl;
                if (file_list[i]->TableVersion > 1) vtbl++;
                fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount );
166
                pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
167
            }
168 169 170 171 172 173 174 175

            for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++)
                if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k];
        }
    }
}


176 177 178 179 180 181 182 183
/***********************************************************************
 *           NdrDllGetClassObject [RPCRT4.@]
 */
HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
                                   const ProxyFileInfo **pProxyFileList,
                                   const CLSID *pclsid,
                                   CStdPSFactoryBuffer *pPSFactoryBuffer)
{
184 185 186 187
  TRACE("(%s, %s, %p, %p, %s, %p)\n", debugstr_guid(rclsid),
    debugstr_guid(iid), ppv, pProxyFileList, debugstr_guid(pclsid),
    pPSFactoryBuffer);

188
  *ppv = NULL;
189 190
  if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList );

191
  if (pclsid && IsEqualGUID(rclsid, pclsid))
192
    return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
193 194 195 196 197 198 199 200 201 202 203
  else {
    const ProxyFileInfo *info;
    int index;
    /* otherwise, the dll may be using the iid as the clsid, so
     * search for it in the proxy file list */
    if (FindProxyInfo(pProxyFileList, rclsid, &info, &index))
      return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);

    WARN("class %s not available\n", debugstr_guid(rclsid));
    return CLASS_E_CLASSNOTAVAILABLE;
  }
204 205 206 207 208 209 210
}

/***********************************************************************
 *           NdrDllCanUnloadNow [RPCRT4.@]
 */
HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
{
211
  return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK;
212 213
}

214

215 216 217 218 219 220 221
/***********************************************************************
 *           NdrDllRegisterProxy [RPCRT4.@]
 */
HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
                                  const ProxyFileInfo **pProxyFileList,
                                  const CLSID *pclsid)
{
222
  WCHAR clsid[39], keyname[50], module[MAX_PATH];
223
  HKEY key, subkey;
224
  DWORD len;
225

226
  TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
227 228 229 230

  if (!hDll) return E_HANDLE;
  if (!*pProxyFileList) return E_NOINTERFACE;

231 232 233 234 235 236
  if (pclsid)
      format_clsid( clsid, pclsid );
  else if ((*pProxyFileList)->TableSize > 0)
      format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid);
  else
      return E_NOINTERFACE;
237 238 239 240 241

  /* register interfaces to point to clsid */
  while (*pProxyFileList) {
    unsigned u;
    for (u=0; u<(*pProxyFileList)->TableSize; u++) {
242
      CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
243 244
      PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];

245 246
      TRACE("registering %s %s => %s\n",
            debugstr_a(name), debugstr_guid(proxy->header.piid), debugstr_w(clsid));
247

248
      lstrcpyW( keyname, L"Interface\\" );
249
      format_clsid( keyname + lstrlenW(keyname), proxy->header.piid );
250
      if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
251
        WCHAR num[10];
252
        if (name)
253
          RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name)+1);
254 255 256
        RegSetValueW( key, L"ProxyStubClsid32", REG_SZ, clsid, 0 );
        swprintf(num, ARRAY_SIZE(num), L"%u", proxy->header.DispatchTableCount);
        RegSetValueW( key, L"NumMethods", REG_SZ, num, 0 );
257 258 259 260 261 262 263
        RegCloseKey(key);
      }
    }
    pProxyFileList++;
  }

  /* register clsid to point to module */
264
  lstrcpyW( keyname, L"CLSID\\" );
265
  lstrcatW( keyname, clsid );
266
  len = GetModuleFileNameW(hDll, module, ARRAY_SIZE(module));
267
  if (len && len < sizeof(module)) {
268 269
      TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module));
      if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
270 271
          RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)L"PSFactoryBuffer", sizeof(L"PSFactoryBuffer"));
          if (RegCreateKeyW(key, L"InProcServer32", &subkey) == ERROR_SUCCESS) {
272
              RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (lstrlenW(module)+1)*sizeof(WCHAR));
273
              RegSetValueExW(subkey, L"ThreadingModel", 0, REG_SZ, (const BYTE *)L"Both", sizeof(L"Both"));
274 275 276
              RegCloseKey(subkey);
          }
          RegCloseKey(key);
277
      }
278 279 280 281 282 283 284 285 286 287 288 289
  }

  return S_OK;
}

/***********************************************************************
 *           NdrDllUnregisterProxy [RPCRT4.@]
 */
HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
                                    const ProxyFileInfo **pProxyFileList,
                                    const CLSID *pclsid)
{
290
  WCHAR keyname[50];
291
  WCHAR clsid[39];
292

293
  TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
294 295 296 297 298 299
  if (pclsid)
      format_clsid( clsid, pclsid );
  else if ((*pProxyFileList)->TableSize > 0)
      format_clsid( clsid,(*pProxyFileList)->pStubVtblList[0]->header.piid);
  else
      return E_NOINTERFACE;
300 301 302 303 304

  /* unregister interfaces */
  while (*pProxyFileList) {
    unsigned u;
    for (u=0; u<(*pProxyFileList)->TableSize; u++) {
305
      CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
306 307
      PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];

308
      TRACE("unregistering %s %s\n", debugstr_a(name), debugstr_guid(proxy->header.piid));
309

310
      lstrcpyW( keyname, L"Interface\\" );
311
      format_clsid( keyname + lstrlenW(keyname), proxy->header.piid );
312
      RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
313 314 315 316 317
    }
    pProxyFileList++;
  }

  /* unregister clsid */
318
  lstrcpyW( keyname, L"CLSID\\" );
319
  lstrcatW( keyname, clsid );
320
  RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
321 322 323

  return S_OK;
}