A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?(一个简单的 C# DLL - 如何从 Excel、Access、VBA、VB6 调用它?)
问题描述
I have a simple class library written in c#.
using System;
namespace TestDll
{
public class Test
{
public string HelloWorld
{
get
{
return "Hello World";
}
}
}
}
My question is how can I call this HelloWorld function from Microsoft Office Visual Basic (which I think is VB6)?
My first step was to add the DLL as a reference - but on browsing and selecting the compiled DLL the message "Can't add a reference to the specified file." was thrown.
Can anyone point me in the right direction as to why/how to get this working?
Thanks in advance SO!
You can't access a static member via COM interop. In fact your code doesn't even compile, the method should be in a class. Here is how you can do it:
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("01A31113-9353-44cc-A1F4-C6F1210E4B30")] //Allocate your own GUID
public interface _Test
{
string HelloWorld { get; }
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("E2F07CD4-CE73-4102-B35D-119362624C47")] //Allocate your own GUID
[ProgId("TestDll.Test")]
public class Test : _Test
{
public string HelloWorld { get { return "Hello, World! "; } }
}
The project properties Build tab, select Register for COM interop. So you can see the results quickly. To install the dll on another machine you need to use regasm.
To then consume this:
Dim o : Set o = CreateObject("TestDll.Test")
MsgBox o.HelloWorld
You can also reference the dll and use early binding:
Dim o As TestDll.Test
Set o = New TestDll.Text
MsgBox o.HelloWorld
这篇关于一个简单的 C# DLL - 如何从 Excel、Access、VBA、VB6 调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一个简单的 C# DLL - 如何从 Excel、Access、VBA、VB6 调用它?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30