Delegate problems

G

Galen Somerville

I have looked through the Help stuff till I'm blue in the face.

The only 'AddressOf' examples dealing with a delegate for a Function do not
show the original Function in question. The following is my attempt which
fails.

Delegate Function DeviceAttachDelegate(ByVal hDevice As Integer, ByRef
pDeviceInfo As WDU_DEVICE, _
ByRef pUserData As DEVICE_CONTEXT) As Integer

Public Function DeviceAttach(ByVal hDevice As Integer, ByRef pDeviceInfo
As WDU_DEVICE, _
ByRef pUserData As DEVICE_CONTEXT) As Boolean

The DeviceAttach ( and DeviceDetach) function is a callback from a 3rd party
dll so must be exact. To set up the callback address it must be presented to
an API call as an Integer address (Yep, worked in VB6)

Public Sub SetEventTable(ByRef eventTable As WDU_EVENT_TABLE)
eventTable.pfDeviceAttach = AddressOf DeviceAttach
eventTable.pfDeviceDetach = AddressOf DeviceDetach
End Sub

and the error shown is

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

It's obvious I'm doing something way wrong. Probably need some pinvoke type
stuff. I would appreciate some guidance here.

GalenS
 
A

AlanT

It looks like you are trying to use C# syntax with VB.Net

try instead,


Addhandler eventTable.pfDeviceAttach, addressof DeviceAttach


hth,
Alan.
 
G

Galen Somerville

AlanT said:
It looks like you are trying to use C# syntax with VB.Net

try instead,


Addhandler eventTable.pfDeviceAttach, addressof DeviceAttach


hth,
Alan.

That doesn't work as eventTable is not really a series of events. It's a
series of addresses the dll will use when it does it's callback event.

GalenS
 
P

Peter Huang [MSFT]

Hi,

Based on my understanding, you are going to P/Invoke into an unmanaged dll
and pass the managed code as a callback function.

If I misunderstood, please feel free let me know.

Here are two links for your reference.

How to set a hook in Visual Basic .NET
http://support.microsoft.com/?id=319524

Managing Low-Level Keyboard Hooks with the Windows API for VB .NET
http://www.codeguru.com/vb/gen/vb_system/keyboard/article.php/c4831/

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Galen Somerville

After much thought, I am dropping my attempt to use unmanaged API calls
entirely. Instead I am going to make a VB6 Activex dll that does all the
work. Then NET only has to supply a few variables and the dll will do the
work.

My app uses a USB connection to our device. It is USB 2.0 because we need
the speed. The 3rd party USB drivers were recently upgraded to NET. The
problem is, the original version runs 54 times faster than the NET version.

GalenS
 
P

Peter Huang [MSFT]

Hi Galen,

Thanks for your feedback.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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