【发布时间】:2016-07-06 13:01:49
【问题描述】:
我通过创建一个简单的 COM-Visible 类在 C# 中创建了一个自定义 ActiveX 控件。我能够构建我的 C# 解决方案,我可以在 Excel 2010(32 位)中看到该控件,我可以将我的自定义控件添加到 VBA 用户窗体并正确查看它。我正在运行 Windows 7 64 位企业版。但是,当我尝试运行用户窗体时,VBA 会抛出 Run-time error '459': External exception。
从下图中可以看出,我已经成功的将我的自定义控件添加到了UserForm中(只是一个简单的按钮):
我实际上可以单击我的 ActiveX 控件中的按钮,它会触发一个显示消息框的 OnButtonClick 事件:
该控件也可以在其他控件下使用,这使我可以在 VBA 工具箱中看到它:
我尝试了许多解决方案,但到目前为止都没有奏效,我根本无法在 VBA 用户窗体中使用我的自定义控件。到目前为止,这是我尝试过的:
- 对 64 位和 32 位都使用 RegAsm.exe“控制 DLL 路径”/codebase
- 尝试了没有 /codebase 的 RegAsm.exe
- 首先将 RegAsm.exe 用于 32 位,然后是 64 位,仅在 32 位和 64 位中使用它
- 在 VBA 编辑器的 Tools/References 下添加了对我的自定义用户控件类的引用,这现在允许我查看带有方法等的公开 COM 接口,现在抛出
'459' Object or class does not support the set of events - 我的项目在属性/构建和程序集信息/使程序集 COM-Visible 下勾选了 COM-Visible
我还尝试在装有 Excel 2016 64 位的 Windows 10 64 位机器上使用我的代码。我可以构建解决方案,但该控件在 VBA 中的其他控件下不可见。我已经执行了所有注册 DLL 文件的 RegAsm 命令,但它根本不会出现在那里。这是超级奇怪的行为。我正在尝试创建一个适用于 32 位和 64 位 Excel 的 ActiveX 控件,因为 64 位版本的 Office 不支持 32 位现有 VBA 控件(我们需要 ListView、DateTimePicker)。我们有一个庞大的代码库,我们无法从 VBA 切换到不同的平台,我们正在从 32 位系统迁移到 64 位系统。我找不到比我目前的解决方案更优雅的解决方案,这将是理想的,但它的行为方式如此奇怪并且拒绝工作。
这是我的 C# 代码(它是垃圾,只是做了一个快速测试解决方案),遵循我找到的示例,这是唯一允许我将控件添加到用户窗体的解决方案:
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();
}
}
}
【问题讨论】:
-
所以基本上该控件在 x32 操作系统上工作,但在 x64 上失败?
-
您没有阅读全文。它显示在 x32 上的附加控件下的 VBA 工具箱中,但它仍然不起作用,我只能将它拉到 UserForm 上,但是运行时它不会运行,因为它会引发异常。在 x64 上,它甚至不会显示在其他控件下。
-
您使用的是哪个版本的 Visual Studio?
-
我正在使用 VS 2013 Professional。