sync with trunk r46493
[reactos.git] / dll / win32 / rpcrt4 / cpsf.c
index dcf3721..ac1eccf 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * COM proxy/stub factory (CStdPSFactory) implementation
  *
- * Copyright 2001 Ove Kåven, TransGaming Technologies
+ * Copyright 2001 Ove Kåven, TransGaming Technologies
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,9 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include "config.h"
+#include "wine/port.h"
+
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 
 #include "rpcproxy.h"
 
+#include "wine/unicode.h"
 #include "wine/debug.h"
 
 #include "cpsf.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(ole);
 
+static void format_clsid( WCHAR *buffer, const CLSID *clsid )
+{
+    static const WCHAR clsid_formatW[] = {'{','%','0','8','X','-','%','0','4','X','-','%','0','4','X','-',
+                                    '%','0','2','X','%','0','2','X','-','%','0','2','X','%','0','2','X',
+                                    '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X','}',0};
+
+    sprintfW( buffer, clsid_formatW, clsid->Data1, clsid->Data2, clsid->Data3,
+              clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
+              clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
+
+}
+
 static BOOL FindProxyInfo(const ProxyFileInfo **pProxyFileList, REFIID riid, const ProxyFileInfo **pProxyInfo, int *pIndex)
 {
   while (*pProxyFileList) {
@@ -62,7 +78,7 @@ static HRESULT WINAPI CStdPSFactory_QueryInterface(LPPSFACTORYBUFFER iface,
   if (IsEqualGUID(&IID_IUnknown,riid) ||
       IsEqualGUID(&IID_IPSFactoryBuffer,riid)) {
     *obj = This;
-    This->RefCount++;
+    InterlockedIncrement( &This->RefCount );
     return S_OK;
   }
   return E_NOINTERFACE;
@@ -72,14 +88,14 @@ static ULONG WINAPI CStdPSFactory_AddRef(LPPSFACTORYBUFFER iface)
 {
   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
   TRACE("(%p)->AddRef()\n",iface);
-  return ++(This->RefCount);
+  return InterlockedIncrement( &This->RefCount );
 }
 
 static ULONG WINAPI CStdPSFactory_Release(LPPSFACTORYBUFFER iface)
 {
   CStdPSFactoryBuffer *This = (CStdPSFactoryBuffer *)iface;
   TRACE("(%p)->Release()\n",iface);
-  return --(This->RefCount);
+  return InterlockedDecrement( &This->RefCount );
 }
 
 static HRESULT WINAPI CStdPSFactory_CreateProxy(LPPSFACTORYBUFFER iface,
@@ -129,6 +145,41 @@ static const IPSFactoryBufferVtbl CStdPSFactory_Vtbl =
   CStdPSFactory_CreateStub
 };
 
+
+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++)
+    {
+        const PCInterfaceProxyVtblList *proxies = file_list[i]->pProxyVtblList;
+        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])
+            {
+                void **vtbl = proxies[j]->Vtbl;
+                if (file_list[i]->TableVersion > 1) vtbl++;
+                fill_delegated_proxy_table( (IUnknownVtbl *)vtbl, stubs[j]->header.DispatchTableCount );
+                pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
+            }
+
+            for (k = 0; k < sizeof(IRpcStubBufferVtbl)/sizeof(void *); k++)
+                if (!pRpcStubVtbl[k]) pRpcStubVtbl[k] = pSrcRpcStubVtbl[k];
+        }
+    }
+}
+
+
 /***********************************************************************
  *           NdrDllGetClassObject [RPCRT4.@]
  */
@@ -142,36 +193,9 @@ HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
     pPSFactoryBuffer);
 
   *ppv = NULL;
-  if (!pPSFactoryBuffer->lpVtbl) {
-    const ProxyFileInfo **pProxyFileList2;
-    int max_delegating_vtbl_size = 0;
-    pPSFactoryBuffer->lpVtbl = &CStdPSFactory_Vtbl;
-    pPSFactoryBuffer->RefCount = 0;
-    pPSFactoryBuffer->pProxyFileList = pProxyFileList;
-    for (pProxyFileList2 = pProxyFileList; *pProxyFileList2; pProxyFileList2++) {
-      int i;
-      for (i = 0; i < (*pProxyFileList2)->TableSize; i++) {
-        /* FIXME: i think that different vtables should be copied for
-         * async interfaces */
-        void * const *pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Vtbl;
-        void **pRpcStubVtbl = (void **)&(*pProxyFileList2)->pStubVtblList[i]->Vtbl;
-        int j;
-
-        if ((*pProxyFileList2)->pDelegatedIIDs && (*pProxyFileList2)->pDelegatedIIDs[i]) {
-          pSrcRpcStubVtbl = (void * const *)&CStdStubBuffer_Delegating_Vtbl;
-          if ((*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount > max_delegating_vtbl_size)
-            max_delegating_vtbl_size = (*pProxyFileList2)->pStubVtblList[i]->header.DispatchTableCount;
-        }
+  if (!pPSFactoryBuffer->lpVtbl) init_psfactory( pPSFactoryBuffer, pProxyFileList );
 
-        for (j = 0; j < sizeof(IRpcStubBufferVtbl)/sizeof(void *); j++)
-          if (!pRpcStubVtbl[j])
-            pRpcStubVtbl[j] = pSrcRpcStubVtbl[j];
-      }
-    }
-    if(max_delegating_vtbl_size > 0)
-      create_delegating_vtbl(max_delegating_vtbl_size);
-  }
-  if (IsEqualGUID(rclsid, pclsid))
+  if (pclsid && IsEqualGUID(rclsid, pclsid))
     return IPSFactoryBuffer_QueryInterface((LPPSFACTORYBUFFER)pPSFactoryBuffer, iid, ppv);
   else {
     const ProxyFileInfo *info;
@@ -191,9 +215,10 @@ HRESULT WINAPI NdrDllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv,
  */
 HRESULT WINAPI NdrDllCanUnloadNow(CStdPSFactoryBuffer *pPSFactoryBuffer)
 {
-  return !(pPSFactoryBuffer->RefCount);
+  return pPSFactoryBuffer->RefCount != 0 ? S_FALSE : S_OK;
 }
 
+
 /***********************************************************************
  *           NdrDllRegisterProxy [RPCRT4.@]
  */
@@ -201,13 +226,21 @@ HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
                                   const ProxyFileInfo **pProxyFileList,
                                   const CLSID *pclsid)
 {
-  LPSTR clsid;
-  char keyname[120], module[MAX_PATH];
+  static const WCHAR bothW[] = {'B','o','t','h',0};
+  static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
+  static const WCHAR clsid32W[] = {'P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2',0};
+  static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
+  static const WCHAR psfactoryW[] = {'P','S','F','a','c','t','o','r','y','B','u','f','f','e','r',0};
+  static const WCHAR numformatW[] = {'%','u',0};
+  static const WCHAR nummethodsW[] = {'N','u','m','M','e','t','h','o','d','s',0};
+  static const WCHAR inprocserverW[] = {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
+  static const WCHAR threadingmodelW[] = {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
+  WCHAR clsid[39], keyname[50], module[MAX_PATH];
   HKEY key, subkey;
   DWORD len;
 
   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
-  UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
+  format_clsid( clsid, pclsid );
 
   /* register interfaces to point to clsid */
   while (*pProxyFileList) {
@@ -215,23 +248,19 @@ HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
-      LPSTR iid;
 
-      TRACE("registering %s %s => %s\n", name, debugstr_guid(proxy->header.piid), clsid);
+      TRACE("registering %s %s => %s\n",
+            debugstr_a(name), debugstr_guid(proxy->header.piid), debugstr_w(clsid));
 
-      UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
-      snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
-      RpcStringFreeA((unsigned char**)&iid);
-      if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
-                          KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
+      strcpyW( keyname, interfaceW );
+      format_clsid( keyname + strlenW(keyname), proxy->header.piid );
+      if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
+        WCHAR num[10];
         if (name)
-          RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name));
-        if (RegCreateKeyExA(key, "ProxyStubClsid32", 0, NULL, 0,
-                            KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
-          snprintf(module, sizeof(module), "{%s}", clsid);
-          RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
-          RegCloseKey(subkey);
-        }
+          RegSetValueExA(key, NULL, 0, REG_SZ, (const BYTE *)name, strlen(name)+1);
+        RegSetValueW( key, clsid32W, REG_SZ, clsid, 0 );
+        sprintfW(num, numformatW, proxy->header.DispatchTableCount);
+        RegSetValueW( key, nummethodsW, REG_SZ, num, 0 );
         RegCloseKey(key);
       }
     }
@@ -239,25 +268,22 @@ HRESULT WINAPI NdrDllRegisterProxy(HMODULE hDll,
   }
 
   /* register clsid to point to module */
-  snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
-  len = GetModuleFileNameA(hDll, module, sizeof(module));
+  strcpyW( keyname, clsidW );
+  strcatW( keyname, clsid );
+  len = GetModuleFileNameW(hDll, module, sizeof(module)/sizeof(WCHAR));
   if (len && len < sizeof(module)) {
-    TRACE("registering CLSID %s => %s\n", clsid, module);
-    if (RegCreateKeyExA(HKEY_CLASSES_ROOT, keyname, 0, NULL, 0,
-                        KEY_WRITE, NULL, &key, NULL) == ERROR_SUCCESS) {
-      RegSetValueExA(subkey, NULL, 0, REG_SZ, (const BYTE *)"PSFactoryBuffer", strlen("PSFactoryBuffer"));
-      if (RegCreateKeyExA(key, "InProcServer32", 0, NULL, 0,
-                          KEY_WRITE, NULL, &subkey, NULL) == ERROR_SUCCESS) {
-        RegSetValueExA(subkey, NULL, 0, REG_SZ, (LPBYTE)module, strlen(module));
-        RegSetValueExA(subkey, "ThreadingModel", 0, REG_SZ, (const BYTE *)"Both", strlen("Both"));
-        RegCloseKey(subkey);
+      TRACE("registering CLSID %s => %s\n", debugstr_w(clsid), debugstr_w(module));
+      if (RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key) == ERROR_SUCCESS) {
+          RegSetValueExW(key, NULL, 0, REG_SZ, (const BYTE *)psfactoryW, sizeof(psfactoryW));
+          if (RegCreateKeyW(key, inprocserverW, &subkey) == ERROR_SUCCESS) {
+              RegSetValueExW(subkey, NULL, 0, REG_SZ, (LPBYTE)module, (strlenW(module)+1)*sizeof(WCHAR));
+              RegSetValueExW(subkey, threadingmodelW, 0, REG_SZ, (const BYTE *)bothW, sizeof(bothW));
+              RegCloseKey(subkey);
+          }
+          RegCloseKey(key);
       }
-      RegCloseKey(key);
-    }
   }
 
-  /* done */
-  RpcStringFreeA((unsigned char**)&clsid);
   return S_OK;
 }
 
@@ -268,12 +294,11 @@ HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
                                     const ProxyFileInfo **pProxyFileList,
                                     const CLSID *pclsid)
 {
-  LPSTR clsid;
-  char keyname[120], module[MAX_PATH];
-  DWORD len;
+  static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
+  static const WCHAR interfaceW[] = {'I','n','t','e','r','f','a','c','e','\\',0};
+  WCHAR keyname[50];
 
   TRACE("(%p,%p,%s)\n", hDll, pProxyFileList, debugstr_guid(pclsid));
-  UuidToStringA((UUID*)pclsid, (unsigned char**)&clsid);
 
   /* unregister interfaces */
   while (*pProxyFileList) {
@@ -281,27 +306,20 @@ HRESULT WINAPI NdrDllUnregisterProxy(HMODULE hDll,
     for (u=0; u<(*pProxyFileList)->TableSize; u++) {
       CInterfaceStubVtbl *proxy = (*pProxyFileList)->pStubVtblList[u];
       PCInterfaceName name = (*pProxyFileList)->pNamesArray[u];
-      LPSTR iid;
 
-      TRACE("unregistering %s %s <= %s\n", name, debugstr_guid(proxy->header.piid), clsid);
+      TRACE("unregistering %s %s\n", debugstr_a(name), debugstr_guid(proxy->header.piid));
 
-      UuidToStringA((UUID*)proxy->header.piid, (unsigned char**)&iid);
-      snprintf(keyname, sizeof(keyname), "Interface\\{%s}", iid);
-      RpcStringFreeA((unsigned char**)&iid);
-      RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
+      strcpyW( keyname, interfaceW );
+      format_clsid( keyname + strlenW(keyname), proxy->header.piid );
+      RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
     }
     pProxyFileList++;
   }
 
   /* unregister clsid */
-  snprintf(keyname, sizeof(keyname), "CLSID\\{%s}", clsid);
-  len = GetModuleFileNameA(hDll, module, sizeof(module));
-  if (len && len < sizeof(module)) {
-    TRACE("unregistering CLSID %s <= %s\n", clsid, module);
-    RegDeleteKeyA(HKEY_CLASSES_ROOT, keyname);
-  }
+  strcpyW( keyname, clsidW );
+  format_clsid( keyname + strlenW(keyname), pclsid );
+  RegDeleteTreeW(HKEY_CLASSES_ROOT, keyname);
 
-  /* done */
-  RpcStringFreeA((unsigned char**)&clsid);
   return S_OK;
 }