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!
提前谢谢!
4 个解决方案
#1
48
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 interop访问静态成员。实际上,您的代码甚至没有编译,方法应该在类中。你可以这样做:
[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.
project properties 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
#2
19
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.
project properties 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.
您的Visual Studio输出文件夹(通常是bin\Debug),其中编译的*。发现dll也有一个*。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 interop”——aswell as a *。正在生成的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
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!
希望这可以帮助!
#3
6
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文件,您还必须在应用程序|程序组装信息下并选择“使Assembly COM可见”。我花了一段时间才发现,所以希望它能帮助别人。
#4
0
To add to AnthonyWJones's good answer, you'll also need to register your DLL using Regasm.exe which adds the necessary registry entries.
为了增加AnthonyWJones的好答案,您还需要使用Regasm注册您的DLL。exe,添加必要的注册表项。
#1
48
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 interop访问静态成员。实际上,您的代码甚至没有编译,方法应该在类中。你可以这样做:
[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.
project properties 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
#2
19
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.
project properties 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.
您的Visual Studio输出文件夹(通常是bin\Debug),其中编译的*。发现dll也有一个*。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 interop”——aswell as a *。正在生成的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
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!
希望这可以帮助!
#3
6
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文件,您还必须在应用程序|程序组装信息下并选择“使Assembly COM可见”。我花了一段时间才发现,所以希望它能帮助别人。
#4
0
To add to AnthonyWJones's good answer, you'll also need to register your DLL using Regasm.exe which adds the necessary registry entries.
为了增加AnthonyWJones的好答案,您还需要使用Regasm注册您的DLL。exe,添加必要的注册表项。