以编程方式注册ActiveX控件

时间:2012-09-19 17:44:17

标签: visual-studio-2008 com visual-studio-2005 activex

我有一个在VS2005中开发的ActiveX控件。我已将它转换为VS2008。我的代码中有一个ComRegisterFunctionAttribute和一个ComUnregisterFunctionAttribute,当我在我的XP系统上编译和调试时,一切正常。当我尝试在家里的Vista笔记本电脑上运行它时,我收到以下错误:

“无法注册程序集”D:\ My Documents \ Visual Studio 2008 Projects \ C#\ BLINCodes \ BLINCodes \ bin \ Debug \ BLINCodes.dll“ - 访问被拒绝。请确保您以管理员身份运行该应用程序。访问注册表项'HKEY_CLASSES_ROOT \ RTPS.BLINCodes'被拒绝。“

据我所知,对于较新的64位操作系统,您必须以管理员身份运行才能注册dll,但是我可以按照我一直在做的方式进行编程吗?这是代码:

    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\Programmable");
        Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\Control");

        string path = System.Reflection.Assembly.GetAssembly(typeof(BLINCodes)).Location;
        Microsoft.Win32.RegistryKey subkey;
        subkey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\DefaultIcon");
        subkey.SetValue("", path + ",102");
        subkey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\ToolBoxBitmap32");
        subkey.SetValue("", path + ",102");
    }

    [ComUnregisterFunctionAttribute]
    public static void UnregisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\Programmable");
        Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\Control");
        Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\ToolBoxBitmap32");
        Registry.ClassesRoot.DeleteSubKey(@"CLSID\{" + t.GUID.ToString().ToUpper() + @"}\DefaultIcon");
    }

感谢您的帮助。

MM

0 个答案:

没有答案