示例#1
0
        public ComClassFactory(Guid rclsid, IntPtr hComObjAddress)
        {
            Assembly loadedAssm = null;

            //We know the type being requested, because we have rclsid.
            //We have to look in the registry to figure out what the codebase
            //is (you *did* register your COM with the /codebase option, right? :)

            ComInfo comInfo = ComInfo.GetComInfoFromClsid(rclsid);

            loadedAssm = Assembly.LoadFile(comInfo.CodeBaseLocal);
            _BasePath  = AppDomain.CurrentDomain.BaseDirectory;
            _ComClass  = loadedAssm.GetType(comInfo.ClassName);

            IntPtr pComAddr = Marshal.GetComInterfaceForObject(this, typeof(IClassFactory));

            Marshal.WriteIntPtr(hComObjAddress, pComAddr);
        }
        public static ComInfo GetComInfoFromClsid(Guid clsId)
        {
            ComInfo result = new ComInfo();

            using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default))
            using (RegistryKey codeBaseKey = key.OpenSubKey(@"CLSID\" + clsId.ToString("B") + @"\InProcServer32"))
            {
                if (codeBaseKey != null)
                {
                    result.ClsId = clsId;
                    result.CodeBaseUri = codeBaseKey.GetValue("CodeBase")?.ToString();
                    result.ClassName = codeBaseKey.GetValue("Class")?.ToString();
                    result.CodeBaseLocal = new Uri(result.CodeBaseUri).LocalPath;
                    result.AppDomain = codeBaseKey.GetValue("AppDomain")?.ToString();
                }
                return result;
            }
        }