vb.net - VB6 程序看不到 VB.Net COM DLL 的属性

标签 vb.net com vb6

我的客户在世界各地的大量计算机上安装了 VB6 程序。它现在需要通过 REST API 访问外部系统。我在 VB.Net 中创建了一个面向 .NET Framework 4 的类库。(这是安装在任何地方的最新版本。)DLL 公开了大约十几个公共(public)函数和子例程,以及大约 40 个属性。我使用以下 PowerShell 代码测试了 DLL:

[System.Reflection.Assembly]::LoadFile( "$directorypath\Debug\MyExample.dll")
$quotaInstance = new-object MyExample.ComClass1

所有的方法和属性都可以从 PS 脚本访问。但是,当我将 DLL 加载到 VB6 IDE 中时,对象浏览器只显示方法,而不显示属性。如果我无法弄清楚这一点,我将不得不将它们重新编码为一堆函数和子例程。我讨厌那个主意。

问题How to make properties visible to COM in a .NET DLL (Methods DO work)看起来很相似,但我没有(明确地,见下文)使用界面,我不确定这个问题是否得到了回答。

有谁知道发生了什么,(更重要的是)如何解决它?谢谢!

更新:人们要求查看我的代码,所以这里是一个精简的示例。我从 InterfaceId 猜测 Visual Studio Community 2017 正在为我创建某种界面。
<ComClass(Example.ClassId, Example.InterfaceId, Example.EventsId)>
Public Class Example

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "099bfc91-1f82-4a26-b1e0-d9645bb1714d"
    Public Const InterfaceId As String = "13e9763d-455c-4d89-8323-cc4e7ae7648e"
    Public Const EventsId As String = "773feeba-e635-4411-b175-30b345afa841"
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

    ' *** Auto-implemented properties
    ' When you declare an auto-implemented property, Visual Basic automatically
    ' creates a hidden Private field called the backing field to contain the
    ' Property value. The backing field name is the auto-implemented Property
    ' name preceded by an underscore (_).

    Public Property Foo As String
    Public Property Bar As Integer

    Public Function InitiateSession(ExampleServer As String) As Boolean
        ' Creates a new API_session.
        ' Returns True if the connection was successful, otherwise False
        ' ...
    End Function

    Public Sub CloseSession()
        ' Releases the current API_session.
        ' ...
    End Sub
End Class

最佳答案

由于某些未知原因,VB 编译器为标有 ComClassAttribute 的类提供了自动接口(interface)生成。忽略自动实现的属性。您需要对属性的长格式进行显式编码。

因此,而不是自动实现的形式:

Public Property Foo As String

你需要:
Private _Foo As String
Public Property Foo As String
    Get
        Return _Foo
    End Get
    Set(value As String)
        _Foo = value
    End Set
End Property

Foo包含在 COM 接口(interface)中。

关于vb.net - VB6 程序看不到 VB.Net COM DLL 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164518/

相关文章:

vb.net - 从数据表中获取值(value)

wpf - 从按钮单击事件运行外部 .exe(例如卸载程序)

vb.net - 在“尝试-捕获如何找到错误线”中

.net - .NET 组件和 COM 组件有什么区别

vb6 - 为什么 VB6 IDE 在打开窗体时崩溃?

vb.net - 有没有一种优雅的方式来编写这段代码?

c++ - 我想从 Python 调用 Windows C++ 函数 WinHttpGetProxyForUrl - 这可以做到吗?

c++ - VirtualBox IGuestSession::ProcessCreate 返回 0x8000FFFF (E_UNEXPECTED)

c++ - CoMarshalInterThreadInterfaceInStream 为 Visual Basic 6 类返回 0x800A0062

database - 查找 ODBC 连接名称