一个简单的 C# DLL - 我如何从 Excel、Access、VBA、VB6 调用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1170794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?
提问by divinci
I have a simple class library written in c#.
我有一个用 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)?
我的问题是如何从 Microsoft Office Visual Basic(我认为是 VB6)调用这个 HelloWorld 函数?
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.
我的第一步是添加 DLL 作为参考 - 但在浏览和选择编译的 DLL 时,消息“无法添加对指定文件的引用”。被抛出。
Can anyone point me in the right direction as to why/how to get this working?
任何人都可以指出我为什么/如何使这个工作正确的方向吗?
Thanks in advance SO!
提前致谢!
采纳答案by AnthonyWJones
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:
您无法通过 COM 互操作访问静态成员。事实上,您的代码甚至无法编译,该方法应该在一个类中。您可以这样做:
[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.
在项目属性 Build 选项卡中,选择 Register for COM interop。所以你可以很快看到结果。要在另一台机器上安装 dll,您需要使用 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:
您还可以引用 dll 并使用早期绑定:
Dim o As TestDll.Test
Set o = New TestDll.Text
MsgBox o.HelloWorld
回答by Matthew Dresser
To add to AnthonyWJones's good answer, you'll also need to register your DLL using Regasm.exewhich adds the necessary registry entries.
要添加 AnthonyWJones 的好答案,您还需要使用Regasm.exe注册您的 DLL ,这会添加必要的注册表项。
回答by divinci
And to expand on registering the DLL on different computers.
并扩展在不同计算机上注册 DLL。
Once you compile and build the above code on your development machine, if you have
一旦你在你的开发机器上编译并构建了上面的代码,如果你有
The project properties Build tab, select Register for COM interop.
在项目属性 Build 选项卡中,选择 Register for COM interop。
your Visual Studio output folder (usually bin\Debug) where the compiled *.dll is found will also have a *.tlb file.
找到已编译 *.dll 的 Visual Studio 输出文件夹(通常是 bin\Debug)也将有一个 *.tlb 文件。
This *.tlb file is a 'Type Library'. And is needed by the client machine to understand the different 'Types' in your *.dll and to basically tell the client machine how to use it.
这个 *.tlb 文件是一个“类型库”。并且客户端机器需要它来了解 *.dll 中的不同“类型”并基本上告诉客户端机器如何使用它。
By setting the above 'Register for COM interop' -- aswell as a *.tlb file being produced, the assembly(dll) is registered on your machine, and is therefore accessible.
通过设置上面的“注册 COM 互操作”——以及正在生成的 *.tlb 文件,程序集(dll)在您的机器上注册,因此可以访问。
In VBA you can now add this file as a reference by
在 VBA 中,您现在可以添加此文件作为参考
VBA Editor -> Tools -> References -> Browse -> Select
VBA 编辑器 -> 工具 -> 参考 -> 浏览 -> 选择
this will allow you to then declare the classes found in your library.
这将允许您随后声明在您的库中找到的类。
Dim TestClass As Test
Set TestClass = New Test
MsgBox TestClass.HelloWorld
HOWEVER - if you want to then use your dll on a different client machine, you will have to use regasm.exe - to register the assembly(dll) on that machine.
但是 - 如果你想在不同的客户端机器上使用你的 dll,你将不得不使用 regasm.exe - 在该机器上注册程序集(dll)。
This can be done by the command line,
这可以通过命令行完成,
regasm.exe
重新加气程序
in this case
在这种情况下
regasm.exe TestDll.dll
regasm.exe TestDll.dll
once you have registered the assembly on the new client machine, you will be able to access it by again adding a reference to its *.tlb
在新客户端计算机上注册程序集后,您将能够通过再次添加对其 *.tlb 的引用来访问它
Hope this helps!
希望这可以帮助!
回答by Jage
Just wanted to comment that in Visual Studio 2008, to get the .tlb file generated you must also go under the Application | Assembly Information and select "Make Assembly COM visible". Took me a while to find that, so hope it helps others out.
只是想评论一下,在 Visual Studio 2008 中,要生成 .tlb 文件,您还必须进入 Application | 程序集信息并选择“使程序集 COM 可见”。我花了一段时间才找到它,所以希望它可以帮助其他人。