从 C++ 程序访问公共变量

问题描述 投票:0回答:1

我有一个用 VB2012 编写的程序,我正在尝试在 C++ DLL 中进行大量的数字运算。 C++ 程序需要访问许多变量。我想做的是将一个 Class 对象传递到 DLL(我们称之为 Class_fred)并通过类传递变量。

VB代码

class class_fred
   public var_1 = 10
   public var_2 = 20

   heavyLifter (&class_fred)

end class

C++代码

void heavyLifter (object* cf) {
  int a, b;
  a = cf->var_1 + cf->var_2;
}
c++ vb.net late-binding
1个回答
0
投票

您需要在项目中添加对 C++ DLL 的 COM 引用。然后,在您的代码中,您可以通过将它传递给您的 Fred 对象来访问它。此代码是有关如何完成的一般指南:

Imports YourCPPDLL

Public Class Fred

    ' Reference to external Heavy Lifter object
    Private _heavyLifter

    Public Property var_1 As Integer = 10
    Public Property var_2 As Integer = 20

    ''' <summary>
    ''' Creates a new instance of the Fred class with a reference to the heavy lifter object.
    ''' </summary>
    ''' <param name="heavyLifter"></param>
    Public Sub New(heavyLifter As YourCPPDLL.HeavyLifter)
        _heavyLifter = heavyLifter
    End Sub

    ''' <summary>
    ''' Processes the object's properties using the HeavyLifter object reference.
    ''' </summary>
    Public Sub Process()
        If _heavyLifter IsNot Nothing Then
            _heavyLifter.heavyLifter(Me)
        End If
    End Sub

End Class
© www.soinside.com 2019 - 2024. All rights reserved.