By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
------------------
"BoloBaby" <bo******@hotmail.com> wrote in message
news:he********************@comcast.com...
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

Nov 20 '05 #2

P: n/a
(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.
Nov 20 '05 #3

P: n/a
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
----------------
"BoloBaby" <bo******@hotmail.com> wrote in message
news:z5********************@comcast.com...
(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.

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.