Internet Explorer找不到我的activex控件引用的dll

时间:2018-02-18 15:13:45

标签: c# internet-explorer dll activex

我在c#中有一个activex控件项目,它引用了我自己构建的dll文件。

现在我点击一个按钮,我看到它失败了,但是这个例外:

  

System.IO.FileNotFoundException:无法加载文件或程序集   'MtnLib,版本= 1.0.0.0,文化=中立,   PublicKeyToken = 111045ceeaeac3e4'或其依赖项之一。该   系统找不到指定的文件。文件名称:'MtnLib,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 111045ceeaeac3e4'
  在CSActiveX.MtnControl.button1_Click(Object sender,EventArgs e)
  在System.Windows.Forms.Control.OnClick(EventArgs e)at   System.Windows.Forms.Button.OnClick(EventArgs e)at   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at at   System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons   按钮,Int32点击)at   System.Windows.Forms.Control.WndProc(Message& m)at   System.Windows.Forms.ButtonBase.WndProc(Message& m)at   System.Windows.Forms.Button.WndProc(Message& m)at   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&   m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32)   msg,IntPtr wparam,IntPtr lparam)

我启用了日志,这些是日志:

  

日志:DisplayName = MtnLib,版本= 1.0.0.0,文化=中性,   PublicKeyToken = 111045ceeaeac3e4(完全指定)LOG:Appbase =   file:/// C:/ Program Files(x86)/ Internet Explorer / LOG:Initial   PrivatePath = NULL调用程序集:CSActiveX,Version = 1.0.0.0,   Culture = neutral,PublicKeyToken = null。   ===日志:此绑定在LoadFrom加载上下文中启动。警告:不会在LoadFrom上下文中探测本机映像。原生图像只会是   在默认加载上下文中进行探测,与Assembly.Load()一样。日志:没有   找到应用配置文件。日志:使用主机配置   file:LOG:使用来自的机器配置文件   C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ CONFIG \ machine.config中。   日志:政策后参考:MtnLib,版本= 1.0.0.0,文化=中立,   PublicKeyToken = 111045ceeaeac3e4日志:尝试下载新网址   file:/// C:/ Program Files(x86)/ Internet Explorer / MtnLib.DLL。日志:   试图下载新的URL文件:/// C:/ Program Files(x86)/ Internet   资源管理器/ MtnLib / MtnLib.DLL。日志:尝试下载新网址   file:/// C:/ Program Files(x86)/ Internet Explorer / MtnLib.EXE。日志:   试图下载新的URL文件:/// C:/ Program Files(x86)/ Internet   资源管理器/ MtnLib / MtnLib.EXE。日志:尝试下载新网址   文件:/// C:/temp/MtnControl/Debug/MtnLib.DLL。日志:尝试下载   新URL文件:/// C:/temp/MtnControl/Debug/MtnLib/MtnLib.DLL。日志:   试图下载新的URL   文件:/// C:/temp/MtnControl/Debug/MtnLib.EXE。日志:尝试下载   新的URL文件:/// C:/temp/MtnControl/Debug/MtnLib/MtnLib.EXE。

我的项目引用了MtnLib.dll,我确实看到了C:\ temp \ MtnControl \ Debug \ MtnLib.dll下的文件所以我不明白为什么IE没有找到该文件。

这是我的控制:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MtnLib;    

namespace CSActiveX
{
    #region Interfaces

    /// <summary>
    /// AxCSActiveXCtrl describes the COM interface of the coclass 
    /// </summary>
    [Guid("D4B8539E-3839-3913-8B1A-C551A9930864")]
    public interface AxCSActiveXCtrl
    {
        #region Properties

        bool Visible { get; set; }          // Typical control property
        bool Enabled { get; set; }          // Typical control property
        #endregion

        #region Methods
        void Refresh();
        #endregion
    }

    #endregion

    [ClassInterface(ClassInterfaceType.None)]        
    [Guid("80B59B58-98EA-303C-BE83-D26E5D8D6794")]
    public partial class MtnControl : UserControl, AxCSActiveXCtrl
    {
        #region ActiveX Control Registration

        // These routines perform the additional COM registration needed by 
        // ActiveX controls

        [EditorBrowsable(EditorBrowsableState.Never)]
        [ComRegisterFunction()]
        public static void Register(Type t)
        {
            try
            {
                ActiveXCtrlHelper.RegasmRegisterControl(t);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); // Log the error
                throw;  // Re-throw the exception
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        [ComUnregisterFunction()]
        public static void Unregister(Type t)
        {
            try
            {
                ActiveXCtrlHelper.RegasmUnregisterControl(t);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); // Log the error
                throw; // Re-throw the exception
            }
        }


        #endregion           

        private void button1_Click(object sender, EventArgs e)
        {
            //call a function from MtnLib
        }
    }

} // namespace CSActiveX

这是我的ActiveXCtrlHelper:

[ComVisible(false)]
internal class ActiveXCtrlHelper : AxHost
{
    internal ActiveXCtrlHelper()
        : base(null)
    {
    }

    #region ActiveX Control Registration

    #region OLEMISC Enumeration

    // Ref: http://msdn.microsoft.com/en-us/library/ms678497.aspx
    const int OLEMISC_RECOMPOSEONRESIZE     = 1;
    const int OLEMISC_CANTLINKINSIDE        = 16;
    const int OLEMISC_INSIDEOUT             = 128;
    const int OLEMISC_ACTIVATEWHENVISIBLE   = 256;
    const int OLEMISC_SETCLIENTSITEFIRST    = 131072;

    #endregion

    /// <summary>
    /// Register the control as an ActiveX control.
    /// </summary>
    /// <param name="t"></param>
    public static void RegasmRegisterControl(Type t)
    {
        // Check the argument
        GuardNullType(t, "t");
        GuardTypeIsControl(t);

        // Open the CLSID key of the control
        using (RegistryKey keyCLSID = Registry.ClassesRoot.OpenSubKey(
            @"CLSID\" + t.GUID.ToString("B"), /*writable*/true))
        {
            RegistryKey subkey = null;

            // Set "InprocServer32" to register a 32-bit in-process server.
            // InprocServer32 = <path to 32-bit inproc server>
            // Ref: http://msdn.microsoft.com/en-us/library/ms683844.aspx

            subkey = keyCLSID.OpenSubKey("InprocServer32", /*writable*/true);
            if (subkey != null) 
                // .NET runtime engine (mscoree.dll) for .NET assemblies
                subkey.SetValue(null, Environment.SystemDirectory + @"\mscoree.dll");


            // Create "Control" to identify it as an ActiveX Control.
            // Ref: http://msdn.microsoft.com/en-us/library/ms680056.aspx

            using (subkey = keyCLSID.CreateSubKey("Control")) { };


            // Create "MiscStatus" to specify how to create/display an object. 
            // MiscStatus = <combination of values from OLEMISC enumeration>
            // Ref: http://msdn.microsoft.com/en-us/library/ms683733.aspx

            using (subkey = keyCLSID.CreateSubKey("MiscStatus"))
            {
                int nMiscStatus = OLEMISC_RECOMPOSEONRESIZE +
                    OLEMISC_CANTLINKINSIDE + OLEMISC_INSIDEOUT +
                    OLEMISC_ACTIVATEWHENVISIBLE + OLEMISC_SETCLIENTSITEFIRST;

                subkey.SetValue("", nMiscStatus.ToString(), RegistryValueKind.String);
            }


            // Create "ToolBoxBitmap32" to identify the module name and the resource  
            // ID for a 16 x 16 bitmap as the toolbar button face.
            // ToolBoxBitmap32 = <filename>.<ext>, <resourceID>
            // Ref: http://msdn.microsoft.com/en-us/library/ms687316.aspx

            using (subkey = keyCLSID.CreateSubKey("ToolBoxBitmap32"))
            {
                // If you want different icons for each control in the assembly you 
                // can modify this section to specify a different icon each time. 
                // Each specified icon must be embedded as a win32 resource in the 
                // assembly; the default one is at the index 101, but you can use 
                // additional ones.
                subkey.SetValue("", Assembly.GetExecutingAssembly().Location + ", 101",
                    RegistryValueKind.String);
            }


            // Create "TypeLib" to specify the typelib GUID associated with the class. 

            using (subkey = keyCLSID.CreateSubKey("TypeLib"))
            {
                Guid libId = Marshal.GetTypeLibGuidForAssembly(t.Assembly);
                subkey.SetValue("", libId.ToString("B"), RegistryValueKind.String);
            }


            // Create "Version" to specify the version of the control. 
            // Ref: http://msdn.microsoft.com/en-us/library/ms686568.aspx

            using (subkey = keyCLSID.CreateSubKey("Version"))
            {
                int nMajor, nMinor;
                Marshal.GetTypeLibVersionForAssembly(t.Assembly, out nMajor, out nMinor);
                subkey.SetValue("", String.Format("{0}.{1}", nMajor, nMinor));
            }

        }
    }

    /// <summary>
    /// Unregister the control.
    /// </summary>
    /// <param name="t"></param>
    public static void RegasmUnregisterControl(Type t)
    {
        // Check the argument
        GuardNullType(t, "t");
        GuardTypeIsControl(t);

        // Delete the CLSID key of the control
        Registry.ClassesRoot.DeleteSubKeyTree(@"CLSID\" + t.GUID.ToString("B"));
    }

    private static void GuardNullType(Type t, String param)
    {
        if (t == null)
        {
            throw new ArgumentException("The CLR type must be specified.", param);
        }
    }

    private static void GuardTypeIsControl(Type t)
    {
        if (!typeof(Control).IsAssignableFrom(t))
        {
            throw new ArgumentException(
                "Type argument must be a Windows Forms control.");
        }
    }
}

我在visual studio中检查了Register for COM Interop,我的控件显示正确,只找不到MtnLib.dll。

顺便说一句,我在Windows 7 64位中使用IE 8。

1 个答案:

答案 0 :(得分:0)

当MtnLib是x64库时,我发现我使用32位IE测试控件。