[英]How to Create a C# COM Object using Visual Studio 2019
I have upgraded from Windows 7/Visual Studio 2015 to Windows10/Visual Studio 2019. I wish to create a C# COM object which I can call from Excel(365) VBA code. I have upgraded from Windows 7/Visual Studio 2015 to Windows10/Visual Studio 2019. I wish to create a C# COM object which I can call from Excel(365) VBA code.
Using Win 7/Visual Studio 2015 the following very simple code compiles as a C# class library and creates a COM object that I can call successfully from Excel 2013 VBA:- Using Win 7/Visual Studio 2015 the following very simple code compiles as a C# class library and creates a COM object that I can call successfully from Excel 2013 VBA:-
using System.Runtime.InteropServices;
namespace TestLib {
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class C_Hello {
public string Hello() {
return "Hello";
}
}
}
In the Assembly Info I have ticked the checkbox " Make the Assembly COM Visible " and in the Build info I have ticked the checkbox " Register for COM interop "在装配信息中,我勾选了复选框“使装配 COM 可见”,在构建信息中,我勾选了复选框“注册 COM 互操作”
The Excel 2013 VBA code is equally simple: Excel 2013 VBA 代码同样简单:
Sub Test()
Dim x As TestLib.C_Hello
Set x = New TestLib.C_Hello
ActiveSheet.Range("C3").Value = x.Hello
End Sub
where TestLib is the C# COM module created in Visual Studio.其中 TestLib 是在 Visual Studio 中创建的 C# COM 模块。
In the Win 7/VS2015/Excel 2013 environment everything works fine, but under after transferring and compiling under Win 10/ VS2019 the Excel 365 VBA code errors at the line:-在 Win 7/VS2015/Excel 2013 环境中一切正常,但在 Win 10/VS2019 下传输和编译后,Excel 365 VBA 代码错误如下:-
Set x = New TestLib.C_Hello
with the error message带有错误消息
Run Time error '-2147221164 (80040154)'运行时错误“-2147221164 (80040154)”
Class not registered Class 未注册
Can anyone tell me what I have to do to get this working in the Windows 10 / VS2019 / Excel 365 environment.?谁能告诉我我必须做什么才能在 Windows 10 / VS2019 / Excel 365 环境中工作。? Is it Windows 10 or Visual Studio 2019 that is doing things differently?
是 Windows 10 还是 Visual Studio 2019 做事不同?
Make sure the bitness of excel matches your build in VS.确保 excel 的位数与您在 VS 中的构建相匹配。 You will likely need to add an x86 or x64 build for your project.
您可能需要为您的项目添加 x86 或 x64 版本。
It is possible to register an AnyCpu assembly, but visual studio does not do so with the “Register for COM interop” for both.可以注册 AnyCpu 程序集,但 Visual Studio 不会使用两者的“注册 COM 互操作”来注册。 You can manually do so by running both the 32- and 64-bit versions of regasm manually or from a post-build script.
您可以通过手动运行 32 位和 64 位版本的 regasm 或从构建后脚本手动执行此操作。
Failing that, you might use ProcMon to check what registration or file Excel is looking for and not finding.否则,您可以使用 ProcMon 检查 Excel 正在寻找和未找到的注册或文件。 Warning, though, that unless you are familiar with the “normal” errors which occur when running it is easy to troubleshoot a red herring.
但是警告,除非您熟悉运行时发生的“正常”错误,否则很容易排除红鲱鱼故障。 I would recommend filtering by “Path” contains “TestLib.C_Hello” then chase it from there.
我建议通过“路径”过滤包含“TestLib.C_Hello”然后从那里追逐它。
The solution in the Win10 / VS2019 environemnt was to change the Platform Target in VS's Build Tab from Any CPU to x64 , and then it worked. Win10 / VS2019 环境中的解决方案是将 VS 的 Build 选项卡中的Platform Target从Any CPU更改为x64 ,然后它就可以工作了。
In the Win7 / VS2015 environment a Platform Target of Any CPU worked fine.在 Win7 / VS2015 环境中,任何 CPU的平台目标都可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.