PC Review


Reply
Thread Tools Rate Thread

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

 
 
BoloBaby
Guest
Posts: n/a
 
      17th May 2004
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      17th May 2004
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" <(E-Mail Removed)> wrote in message
news:heWdnV2kqPSUiDXdRVn-(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
BoloBaby
Guest
Posts: n/a
 
      17th May 2004
(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.


 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      17th May 2004
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" <(E-Mail Removed)> wrote in message
news:z5GdnWX7-JOVpzXdRVn-(E-Mail Removed)...
> (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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
EventHandler AddressOf Question Rob Meade Microsoft ASP .NET 2 4th Jun 2007 03:30 PM
Same question clarified Auto Forwarding Email =?Utf-8?B?Rm9ueg==?= Microsoft Outlook Discussion 2 8th Jun 2005 04:37 PM
Clarified Question =?Utf-8?B?QWlyLXJvbg==?= Microsoft Access Forms 9 2nd Jan 2005 11:49 PM
Which chart will do what I want? I Clarified My Question... Ginger Microsoft Excel Charting 1 27th Mar 2004 02:18 PM
AddressOf / Windows API question Amos Microsoft Excel Programming 1 25th Feb 2004 05:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:27 AM.