AddressOf/Delegate question clarified - VB6.0, .NET question

B

BoloBaby

OK, I've managed to clarify my question (whew).

I'll show two blocks of code - one in VB6.0 and one in VB.NET. The VB6.0
code manages to execute the callback function, the VB.NET does not. The
question is "how do I get the .NET code to execute the callback function?"

(Note - the declared function works to expose IRQs for a digital
input/output card. Not relevant, but worth knowing if you were curious.)

VB6.0 code:

'Dask.bas - module with lots of declarations
'relevant declaration
Declare Function DIO_INT1_EventMessage Lib "Pci-Dask.dll" (ByVal CardNumber
As Integer, ByVal Int1Mode As Integer, ByVal windowHandle As Long, ByVal
message As Long, ByVal callbackAddr As Long) As Integer

'Form1 code
Private Sub Form_Load()
'superfluous code eliminated
DIO_INT1_EventMessage 0, 0, 0, 0, AddressOf BE_ReadPort
End Sub

'Module1.bas module that contains BE_ReadPort
Function BE_ReadPort() As Long
'this fires properly when I press an electrical switch to send digital
input to the card
MsgBox("Bang!")
End Function

* * * * *

VB.NET code:

'DIOCard.vb - wrapper class
Public Class DIOCard
Declare Function DIO_INT1_EventMessage Lib "Pci-Dask.dll" (ByVal
CardNumber As Integer, ByVal Int1Mode As Integer, ByVal windowHandle As
Long, ByVal message As Long, ByVal callbackAddr As Form1.BP) As Integer
End Class

'Form1.vb - form code
Public Class Form1
Public Delegate Function BP() as Long

Public Function ButtonPressed() as long
'this does not go off
MsgBox("Bang!")
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'superfluous code eliminated
Dim intResult as integer
intResult = DIO_INT1_EventMessage(0, 0, 0, 0, AddressOf
ButtonPressed)
End Sub

End Class

* * * * *

That's it. Why won't the delegate function fire in .NET? No errors are
reported, the callback simply doesn't fire.

Gardner
 
K

Ken Tucker [MVP]

Hi,

In vb6 integer = vb.net short. vb6 long = vb.net integer. Try
this.

'DIOCard.vb - wrapper class
Public Class DIOCard
Declare Function DIO_INT1_EventMessage Lib "Pci-Dask.dll" (ByVal
CardNumber As short, ByVal Int1Mode As short, ByVal windowHandle As
integer, ByVal message As integer, ByVal callbackAddr As Form1.BP) As short
End Class

'Form1.vb - form code
Public Class Form1
Public Delegate Function BP() as integer
private myBP as bp

Public Function ButtonPressed() as integer
'this does not go off
MsgBox("Bang!")
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'superfluous code eliminated
Dim intResult as integer
mybp=new bp
intResult = DIO_INT1_EventMessage(0, 0, 0, 0, mybp)
End Sub

End Class

Ken
 
B

BoloBaby

(Whoops, might as well make this public!)

Thank you Ken. I'm officially an idiot. I didn't even think to look at the
type changes from 6.0 to .NET.

The "private myBP as bp" line is not needed from your solution when the type
changes are made. I can keep the "AddressOf ButtonPressed" instead of
switching to "Address myBP."

Thank you again.
 
K

Ken Tucker [MVP]

Hi,

I tried just using addressof instead of a variable and kept
getting object not set to reference of object errors when the api called the
callback a second time.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top