VS 2008中的VB6 AddressOf和Callback

时间:2017-07-14 03:51:59

标签: vb.net vb6 addressof

嘿所有我试图通过其自动VB6代码转换器将一些VB6代码转换为VS 2008。大多数都做得很好,但有一些需要修补。

有人说这是一段代码:

InitializeGrCap = GrCapInitialize(AddressOf GrCapStatusEventHandler)

GrCapInitialize 是这样的:

Public Declare Function GrCapInitialize Lib "GrFinger.dll" Alias "_GrCapInitialize@4" (ByVal StatusEventHandler As Integer) As Integer

GrCapStatusEventHandler 是:

Public Sub GrCapStatusEventHandler(ByVal pidSensor As Integer, ByVal eventRaised As Integer)
    While fireStatus = True
        System.Windows.Forms.Application.DoEvents()
    End While

    myPIdSensor = pidSensor
    myEventRaised = eventRaised     
    fireStatus = True

    While fireStatus = True
        System.Windows.Forms.Application.DoEvents()
    End While
End Sub

我不确定如何重新编写以解决以下问题:

  

错误44' AddressOf'表达式无法转换为'整数'因为'整数'不是代表类型。

第二个说起来就是这段代码:

GrCapStartCapture(myIdSensor, AddressOf GrCapFingerEventHandler, AddressOf GrCapImageEventHandler)

同样, AddressOf ... 是这个中的错误:

GrCapFingerEventHandler

Public Sub GrCapFingerEventHandler(ByVal pidSensor As Integer, ByVal eventRaised As Integer)
    While fireFinger = True
        System.Windows.Forms.Application.DoEvents()
    End While

    myPIdSensor = pidSensor
    myEventRaised = eventRaised
    fireFinger = True

    While fireFinger = True
        System.Windows.Forms.Application.DoEvents()
    End While
End Sub

GrCapImageEventHandler

Public Sub GrCapImageEventHandler(ByVal pidSensor As Integer, ByVal width As Integer, ByVal height As Integer, ByVal pRawImage As Integer, ByVal res As Integer)
    While fireImage = True
        System.Windows.Forms.Application.DoEvents()
    End While

    myPIdSensor = pidSensor
    myWidth = width
    myHeight = height
    myRes = res
    myRawImage = pRawImage
    fireImage = True

    While fireImage = True
        System.Windows.Forms.Application.DoEvents()
    End While
End Sub

同样,错误是:

  

错误44' AddressOf'表达式无法转换为'整数'因为'整数'不是代表类型。

任何人都可以帮我将这两个代码区域转换为.net吗?

1 个答案:

答案 0 :(得分:4)

在VB6中,传递给非托管函数的//Check customDestRB is checked if (customDestRB.Checked == true) { //Check getFileCB is checked if (getFileCB.Checked == true) { //Check if the filename is in use if (File.Exists(destTB.Text + fileNameTB.Text)) { //If filename is in use then prompt the user to pick another filename MessageBox.Show("File already exists, choose another filename"); } //Check if directory exists else if (Directory.Exists(destTB.Text)) { //If it exists, //Do something here here } } } 将是一个函数指针。 VB.NET不做函数指针。 .NET使用委托,而不是引用方法的对象。这意味着您需要一个带有与托管方法匹配的签名的委托类型,然后您可以将非托管函数的参数声明为该类型,并在调用该函数时传递该类型的实例。您可以声明自己的委托类型,例如

Integer

声明您的外部函数使用该类型:

Public Delegate Sub GrCapStatusCallback(ByVal pidSensor As Integer, ByVal eventRaised As Integer)

然后你的呼叫应该按原样工作,或者你可以明确地创建一个委托实例:

Public Declare Function GrCapInitialize Lib "GrFinger.dll" Alias "_GrCapInitialize@4" (ByVal StatusEventHandler As GrCapStatusCallback) As Integer

或者,您可以使用现有的InitializeGrCap = GrCapInitialize(New GrCapStatusCallback(AddressOf GrCapStatusEventHandler)) Action代理,分别用于FuncSubs,并且可以支持0到16个参数:

Functions