从VB6调用.NET类 [英] Calling .NET classes from VB6

查看:99
本文介绍了从VB6调用.NET类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C ++ 2005(CLR,旧语法)编写了一个类库。它工作正常,可以在所有.NET IDE中使用,包括VB,C#和C ++,使用方法和属性公开类,枚举...



现在我想从VB6中使用它,我知道我需要以某种方式使它与COM兼容。



我读过不同的东西,一些关于COM Interop项目属性,其他人提到ComVisibleAttribute,其他类型库......



到目前为止,我的不同尝试都失败了(IDE中没有合适的选项,没有生成TLB文件, ComVisibleAttribute语法引发未找到属性错误...)



我在丛林中迷路了,我需要一些指导。谁可以帮助我?

I have written a class library using Visual C++ 2005 (CLR, old syntax). It is working fine and can be used from all .NET IDE''s, including VB, C# and C++, exposing classes with methods and properties, enumerations...

Now I want to use it from VB6 and I understand that I need to somehow make it COM compatible.

I have read different things, some about COM Interop project properties, others mentioning the ComVisibleAttribute, others Type Libraries...

So far my different attempts have lamentably failed (no suitable options in the IDE, no TLB file being generated, the ComVisibleAttribute syntax raises an "Attribute Not Found" error...)

I am lost in the jungle and I need some guidance here. Who can help me ?

推荐答案

一旦你驯服它,COM Interop工作得相当好。



1 )避免使用静态类成员,因为COM不支持这些成员。还要避免仅通过大小写区分的标识符。



2)将以下属性添加到程序集中(在 AssemblyInfo.cpp ):

COM Interop works fairly well once you have tamed it.

1) Avoid static class members, as these are not supported by COM. Also avoid identifiers that just differ by casing.

2) Add the following attribute to the assembly (in AssemblyInfo.cpp):
[assembly:ClassInterfaceAttribute(ClassInterfaceType::AutoDual)]; // This will let VB6 pre-load the interface definitions

使用 System :: Runtime :: InteropServices 命名空间。



3)创建相应的类型库文件并使用 regasm 命令作为构建后事件(项目属性 - >构建事件 - >构建后事件 - >命令行):

Uses the System::Runtime::InteropServices namespace.

3) Create the corresponding Type Library file and register it using the regasm command as a post-build event (project Properties -> Build Events -> Post-Build Events -> Command Line):

regasm "


(TargetPath)/ tlb
(TargetPath)" /tlb



从VC ++构建类库,让 MyLib.dll 。文件 MyLib.tlb 也将构建。



4)在VB6下,注册对类型的引用图书馆:项目 - >引用... ,然后浏览到 .tlb 文件,并选中列表中新条目旁边的复选框。



5)从那时起,对象浏览器(F2)将显示库的内容,并且编辑器将执行自动完成。



6)复制 MyLib.dll 旁边的 VB6.exe 可执行文件IDE。如果您计划 Make 一个,您还需要将其复制到已编译的应用程序旁边。



7)实现类是通过以下方式实现的:


Build the class library from VC++, let MyLib.dll. The file MyLib.tlb will also be built.

4) Under VB6, register the reference to the Type Library: Project -> References..., then browse to the .tlb file and check the checkbox next to the new entry in the list.

5) From then on, the Object Browser (F2) will show you the contents of the library, and autocompletion will be performed by the editor.

6) Copy MyLib.dll next to the VB6.exe executable to work from the IDE. You will also need to copy it next to the compiled application if you plan to Make one.

7) Instantiating a class is achieved with:

Dim MyInstance As MyLib.MyClass
Set MyInstance = New MyLib.MyClass



你已经完成了!



您可能会在某些阶段遇到故障和错误消息。这些可能是由于在搜索路径中找不到dll(运行时库,.NET框架或 MyLib.dll )。



COM不支持的数据类型也会引发问题。


You''re done !

You may experience malfunctions and error messages at some stages. These can be due to dlls (run-time libraires, .NET framework or MyLib.dll) not being found in the search path.

Datatypes unsupported by COM also raise issues.


你会发现这篇文章对解决你的问题很有帮助,尽管它是C ++而不是vb6



呼叫管理.NET C#来自非托管C ++代码的COM对象 [ ^ ]
You can find this article useful to solve your problem although it if of C++ instead of vb6

Calling Managed .NET C# COM Objects from Unmanaged C++ Code[^]


这篇关于从VB6调用.NET类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆