C# VBA运行时错误:';459';使用自定义ActiveX控件运行userform时出现外部异常
我通过创建一个简单的COM可见类,在C#中创建了一个自定义ActiveX控件。我能够构建我的C#解决方案,我可以在Excel2010(32位)中看到该控件,我可以将自定义控件添加到VBA用户表单中并正确查看它。我正在运行Windows 7 64位企业版。但是,当我尝试运行UserForm时,VBA抛出运行时错误“459”:外部异常 从下图可以看出,我已成功地将自定义控件添加到UserForm(只需一个简单的按钮): 实际上,我可以单击ActiveX控件中的按钮,它将触发一个OnButtonClick事件,显示一个messagebox: 该控件也可以在其他控件下使用,这使我可以在VBA工具箱中查看它: 我已经尝试了很多解决方案,但到目前为止没有任何效果,我只是无法在VBA用户窗体中使用自定义控件。以下是我迄今为止所尝试的:C# VBA运行时错误:';459';使用自定义ActiveX控件运行userform时出现外部异常,c#,.net,vba,excel,C#,.net,Vba,Excel,我通过创建一个简单的COM可见类,在C#中创建了一个自定义ActiveX控件。我能够构建我的C#解决方案,我可以在Excel2010(32位)中看到该控件,我可以将自定义控件添加到VBA用户表单中并正确查看它。我正在运行Windows 7 64位企业版。但是,当我尝试运行UserForm时,VBA抛出运行时错误“459”:外部异常 从下图可以看出,我已成功地将自定义控件添加到UserForm(只需一个简单的按钮): 实际上,我可以单击ActiveX控件中的按钮,它将触发一个OnButtonCl
- 对64位和32位使用RegAsm.exe“控制DLL路径”/codebase
- 尝试了不带/codebase的RegAsm.exe
- 先将RegAsm.exe用于32位,然后使用64位,仅将其用于32位,并且仅用于64位
- 在VBA编辑器中的“工具/引用”下添加了对自定义用户控件类的引用,这现在允许我查看带有方法等的公开COM接口,这现在抛出
'459'对象或类不支持事件集
- 我的项目在properties/build和assembly information/make assembly COM Visible下勾选了COM Visible
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ActiveXTest2
{
[ProgId("ActiveXTest2.CustomUserControl")]
[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(IUserControlEvents))]
public partial class CustomUserControl : UserControl
{
private ListView listView1;
public CustomUserControl()
{
InitializeComponent();
}
// register COM ActiveX object
[ComRegisterFunction]
public static void RegisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT\", "");
Type myType1 = Type.GetTypeFromProgID("ActiveXTest2.CustomUserControl");
Console.WriteLine("ProgID=ActiveXTest2.CustomUserControl GUID={0}.", myType1.GUID);
TextWriter tw = File.CreateText("guid.txt");
tw.WriteLine(skey.ToString());
tw.WriteLine(myType1.GUID.ToString());
tw.Close();
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
RegistryKey ctrl = regKey.CreateSubKey("Control");
ctrl.Close();
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
regKey.Close();
}
// Unregister COM ActiveX object
[ComUnregisterFunction]
public static void UnregisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT\", "");
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
regKey.DeleteSubKey("Control", false);
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
regKey.DeleteSubKey("CodeBase", false);
regKey.Close();
}
public delegate void ControlEventHandler();
[Guid("0A415E38-372F-45fb-813B-D9558C787EB0")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IUserControlEvents
{
[DispId(0x60020001)]
void OnButtonClick();
}
public interface ICOMCallable
{
int TestValue();
}
public int TestValue()
{
return 0;
}
public event ControlEventHandler OnButtonClick;
protected virtual void OnOnButtonClick()
{
MessageBox.Show("TEST", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(4, 4);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(228, 210);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
//
// CustomUserControl
//
this.Controls.Add(this.listView1);
this.Name = "CustomUserControl";
this.Size = new System.Drawing.Size(235, 217);
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
OnOnButtonClick();
}
}
}
我正在看这个例子 他错了。我已经使用了下面的代码来注册COM,它成功了
[ComRegisterFunction()]
public static void RegisterFunction(Type _type)
{
if (_type != null)
{
string sCLSID = "CLSID\\" + _type.GUID.ToString("B");
try
{
RegistryKey _key = Registry.ClassesRoot.OpenSubKey(sCLSID, true);
try
{
Guid _libID = Marshal.GetTypeLibGuidForAssembly(_type.Assembly);
int _major, _minor;
Marshal.GetTypeLibVersionForAssembly(_type.Assembly, out _major, out _minor);
using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
using (RegistryKey _sub = _key.CreateSubKey("MiscStatus")) { _sub.SetValue("", "0", RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("TypeLib")) { _sub.SetValue("", _libID.ToString("B"), RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("Version")) { _sub.SetValue("", String.Format("{0}.{1}", _major, _minor), RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
using (RegistryKey _sub = _key.CreateSubKey("InprocServer32")) { _sub.SetValue("", Environment.SystemDirectory + "\\" + _sub.GetValue("", "mscoree.dll"), RegistryValueKind.String); }
}
finally
{
}
}
catch
{
}
}
}
[ComUnregisterFunction()]
public static void UnregisterClass(Type t) //(string key)
{
string keyName = @"CLSID\" + t.GUID.ToString("B");
Registry.ClassesRoot.DeleteSubKeyTree(keyName);
//StringBuilder skey = new StringBuilder(key);
//skey.Replace(@"HKEY_CLASSES_ROOT\", "");
//RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
//regKey.DeleteSubKey("Control", false);
//RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
//regKey.DeleteSubKey("CodeBase", false);
//regKey.Close();
}
我正在看这个例子 他错了。我已经使用了下面的代码来注册COM,它成功了
[ComRegisterFunction()]
public static void RegisterFunction(Type _type)
{
if (_type != null)
{
string sCLSID = "CLSID\\" + _type.GUID.ToString("B");
try
{
RegistryKey _key = Registry.ClassesRoot.OpenSubKey(sCLSID, true);
try
{
Guid _libID = Marshal.GetTypeLibGuidForAssembly(_type.Assembly);
int _major, _minor;
Marshal.GetTypeLibVersionForAssembly(_type.Assembly, out _major, out _minor);
using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
using (RegistryKey _sub = _key.CreateSubKey("MiscStatus")) { _sub.SetValue("", "0", RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("TypeLib")) { _sub.SetValue("", _libID.ToString("B"), RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("Version")) { _sub.SetValue("", String.Format("{0}.{1}", _major, _minor), RegistryValueKind.String); }
using (RegistryKey _sub = _key.CreateSubKey("Control")) { }
using (RegistryKey _sub = _key.CreateSubKey("InprocServer32")) { _sub.SetValue("", Environment.SystemDirectory + "\\" + _sub.GetValue("", "mscoree.dll"), RegistryValueKind.String); }
}
finally
{
}
}
catch
{
}
}
}
[ComUnregisterFunction()]
public static void UnregisterClass(Type t) //(string key)
{
string keyName = @"CLSID\" + t.GUID.ToString("B");
Registry.ClassesRoot.DeleteSubKeyTree(keyName);
//StringBuilder skey = new StringBuilder(key);
//skey.Replace(@"HKEY_CLASSES_ROOT\", "");
//RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
//regKey.DeleteSubKey("Control", false);
//RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
//regKey.DeleteSubKey("CodeBase", false);
//regKey.Close();
}
基本上,这个控件可以在x32操作系统上运行,但在x64上失败了?你没有阅读全部内容。它显示在x32的VBA工具箱中的附加控件下,但它仍然不起作用,我只能在UserForm上拉它,但在运行时,它不会运行,因为它会引发异常。在x64上,它甚至没有在其他控件下显示。您使用的是哪一版本的Visual Studio?我使用的是VS 2013 Professional。因此,基本上该控件在x32操作系统上工作,但在x64上失败?您没有阅读全部内容。它显示在x32的VBA工具箱中的附加控件下,但它仍然不起作用,我只能在UserForm上拉它,但在运行时,它不会运行,因为它会引发异常。在x64上,它甚至不会在其他控件下显示。您使用的是哪个版本的Visual Studio?我使用的是VS 2013 Professional。