VB6 AddressOf and Callbacks in VS 2008

Hi everyone, I am trying to convert VB6 code to VS 2008 through its automatic VB6 code converter. Most of them are fine, but there are a few that need a touch.

One of them touched on this part of the code:

InitializeGrCap = GrCapInitialize(AddressOf GrCapStatusEventHandler)

      

And GrCapInitialize :

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

      

And 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

      

I'm not sure how to rewrite this to fix the problem:

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

The second touch mentioned is a piece of code:

GrCapStartCapture(myIdSensor, AddressOf GrCapFingerEventHandler, AddressOf GrCapImageEventHandler)

      

Again, AddressOf ... are errors in this:

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

      

And 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

      

And again the error:

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

Can anyone help me convert these 2 areas of code to .net?

+3


source to share


1 answer


In VB6, this Integer

one you passed to your unmanaged function will be a function pointer. VB.NET does not execute function pointers. Instead, .NET uses delegates, which are method-related objects. This means that you need a delegate type with a signature that matches your managed method, and then you can declare your unmanaged function parameter as that type and pass an instance of that type when you call that function. You can declare your own delegate type eg.

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

      

declare an external function like this:

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

      



and then your call should work as is, or you can instantiate the delegate explicitly:

InitializeGrCap = GrCapInitialize(New GrCapStatusCallback(AddressOf GrCapStatusEventHandler))

      

Alternatively, you can use the existing delegates Action

and Func

, which for Subs

and Functions

respectively, and can support 0 to 16 parameters:

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

      

+4


source







All Articles