473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
3 7889
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
(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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ECVerify.com | last post by:
I am trying to figure out how to do this in C Sharp In VB this works perfect MenuItem2.MenuItems.Add("123", AddressOf Me.MenuItem1_Click) The problem is there is no address of in C#, I tried...
2
by: john | last post by:
Hello, I am trying to convert from vb6 to vb.net and I have an issue with the addressOf function. Below is the code any help would be greatly appreciated. Thanks in advance. code erroring...
4
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal...
4
by: Anthony Coelho | last post by:
Hello Guru's! I am trying to pass a function pointer as a parameter to a method and can't seem to get it working. Basically I want to do the exact same thing that the System.Threading.Thread...
1
by: peter | last post by:
Probably you might help me regarding this.. I'm new to these Vb.net language. My question is Does AddressOf is accepted in Vb.net? in this VbCode: I have this API...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
3
by: eBob.com | last post by:
(Not that size matters, but ...) I have a program which is getting too big. So I'd like to split it into several source files. I know that I can create a ModuleX.vb source file which looks like...
4
by: kaczmar2 | last post by:
I have a custom web control that is called from an aspx page. The custom control dynamically renders out buttons via a public method (AddButton). One of the attributes of the method is a...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Is there a difference between AddressOf and Delegate for assiging a function/Sub to an event? Add_Handler txt1.Click AddressOf WhatColorFont or Private Sub Delegate WhatColorFont() ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.