如何在c#.net窗体中创建activex控件以及如何部署

时间:2016-12-23 17:12:57

标签: c# asp.net activex c#-2.0

我创建了一个activeX控件,但在Internet Explorer中部署它时遇到了一个问题。浏览器(IE 11)无法下载activeX控件。我不确定出了什么问题,或者哪些代码可能效果不好。我正在使用.net 2010,framekwork 4.0

这是我尝试过的代码。

[ProgId("Newcomp.UserControl1")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("5FE8E181-7D6D-4CE2-AB83-BAEB9906EF48")]
[ComVisible(true)]
public partial class UserControl1: UserControl
{

    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.BackColor = Color.YellowGreen;
    }
    /// 
    /// Register the class as a control and set it's CodeBase entry
    /// 
    /// The registry key of the control
    [ComRegisterFunction()]
    public static void RegisterClass(string key)
    {
        // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
        StringBuilder sb = new StringBuilder;
        sb.Replace(@"HKEY_CLASSES_ROOT\", "");

        // Open the CLSID\{guid} key for write access
        RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

        // And create the 'Control' key - this allows it to show up in
        // the ActiveX control container
        RegistryKey ctrl = k.CreateSubKey("Control");
        ctrl.Close();

        // Next create the CodeBase entry - needed if not string named and GACced.
        RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
        inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
        inprocServer32.Close();

        // Finally close the main key
        k.Close();

    }

    /// 
    /// Called to unregister the control
    /// 
    /// Tke registry key
    [ComUnregisterFunction()]
    public static void UnregisterClass(string key)
    {
        StringBuilder sb = new StringBuilder;
        sb.Replace(@"HKEY_CLASSES_ROOT\", "");

        // Open HKCR\CLSID\{guid} for write access
        RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

        // Delete the 'Control' key, but don't throw an exception if it does not exist
        k.DeleteSubKey("Control", false);

        // Next open up InprocServer32
        RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

        // And delete the CodeBase key, again not throwing if missing
        k.DeleteSubKey("CodeBase", false);

        // Finally close the main key
        k.Close();
    }
}

我也跟着this

1 个答案:

答案 0 :(得分:1)

您遇到的问题是由于默认情况下 IE10 (及以后)不允许您以默认配置下载/运行Active X控件。这是出于安全考虑而完成的。换句话说,ActiveX控件被恶意使用。

不幸的是,您使用的“如何”文章(虽然准确):https://blogs.msdn.microsoft.com/asiatech/2011/12/05/how-to-develop-and-deploy-activex-control-in-c/, 在此默认限制之前,因此忽略警告用户。

Microsoft提供了一种在 IE10 IE11 中为AciveX禁用此过滤的方法: https://support.microsoft.com/en-us/help/17469/windows-internet-explorer-use-activex-controls

但是,这个解决方案需要最终用户采取行动,程序员无法强制执行此选项,除了向最终用户提供建议。这可以适用于内容较少的内部网应用,但对于更广泛的受众群体来说无法管理。