Browse Source

Bug 2098: .NET assembly type library cannot be used in WSH

https://winscp.net/tracker/2098

+ Making use of Marshal.GetTypeLibGuidForAssembly

Source commit: 959593a6231f2d8f6d8f9b92d6f8ecba9ba6aa7e
Martin Prikryl 3 years ago
parent
commit
bb45f5b660
1 changed files with 28 additions and 17 deletions
  1. 28 17
      dotnet/Session.cs

+ 28 - 17
dotnet/Session.cs

@@ -1719,22 +1719,40 @@ namespace WinSCP
         [ComRegisterFunction]
         private static void ComRegister(Type t)
         {
-            string subKey = GetTypeLibKey(t);
             Assembly assembly = Assembly.GetAssembly(t);
-            object[] attributes = assembly.GetCustomAttributes(typeof(GuidAttribute), false);
-            if (attributes.Length == 0)
-            {
-                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Cannot find {0} attribute for assembly {1}", typeof(GuidAttribute), assembly));
-            }
-            GuidAttribute guidAttribute = (GuidAttribute)attributes[0];
-            Registry.ClassesRoot.CreateSubKey(subKey).SetValue(null, guidAttribute.Value);
+
+            string guid = Marshal.GetTypeLibGuidForAssembly(assembly).ToString();
+
+            int major, minor;
+            Marshal.GetTypeLibVersionForAssembly(assembly, out major, out minor);
+            string version = $"{major}.{minor}";
+
+            RegistryKey root = Registry.ClassesRoot;
+            root.CreateSubKey(GetTypeLibKey(t)).SetValue(null, guid);
+            root.CreateSubKey(GetVersionKey(t)).SetValue(null, version);
         }
 
         [ComUnregisterFunction]
         private static void ComUnregister(Type t)
         {
-            string subKey = GetTypeLibKey(t);
-            Registry.ClassesRoot.DeleteSubKey(subKey, false);
+            RegistryKey root = Registry.ClassesRoot;
+            root.DeleteSubKey(GetTypeLibKey(t), false);
+            root.DeleteSubKey(GetVersionKey(t), false);
+        }
+
+        private static string GetClsidKey(Type t)
+        {
+            return "CLSID\\" + t.GUID.ToString("B").ToUpperInvariant();
+        }
+
+        private static string GetTypeLibKey(Type t)
+        {
+            return GetClsidKey(t) + "\\TypeLib";
+        }
+
+        private static string GetVersionKey(Type t)
+        {
+            return GetClsidKey(t) + "\\Version";
         }
 #endif
 
@@ -2550,13 +2568,6 @@ namespace WinSCP
         }
 
 
-#if !NETSTANDARD
-        private static string GetTypeLibKey(Type t)
-        {
-            return "CLSID\\{" + t.GUID.ToString().ToUpperInvariant() + "}\\TypeLib";
-        }
-#endif
-
         FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
         {
             using (Logger.CreateCallstack())