gpt4 book ai didi

vb.net - VS 2008 中的 VB6 AddressOf 和回调

转载 作者:行者123 更新时间:2023-12-02 02:24:54 28 4
gpt4 key购买 nike

嘿,我正在尝试通过自动 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

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

Error 44 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

第二个所说的修饰是这段代码:

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

再次,错误是:

Error 44 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.

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

最佳答案

在 VB6 中,传递给非托管函数的Integer 将是函数指针。 VB.NET 不支持函数指针。 .NET 使用委托(delegate),即引用方法的对象。这意味着您需要一个具有与托管方法匹配的签名的委托(delegate)类型,然后您可以将非托管函数的参数声明为该类型,并在调用该函数时传递该类型的实例。您可以声明自己的委托(delegate)类型,例如

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

然后您的调用应该按原样工作,或者您可以显式创建委托(delegate)实例:

InitializeGrCap = GrCapInitialize(New GrCapStatusCallback(AddressOf GrCapStatusEventHandler))

或者,您可以使用现有的 ActionFunc 委托(delegate),它们分别用于 SubsFunctions并且可以支持0到16个参数:

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

关于vb.net - VS 2008 中的 VB6 AddressOf 和回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094306/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com