473,466 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

AddressOf, .NET, and VB6.0 interoperability

OK, here's the problem...

I have a digital input/output card that exposes IRQs (digital input
change-of-state) through a DLL function.

In VB6.0, I can use this ability through code that looks like this:

[Class1]

Public sub test()
DIO_INT1_EventMessage 0, 0, 0, 0, AddressOf BE_ReadPort
end sub

[standard module]

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

Public sub BE_ReadPort()
'do some stuff
end sub

Take note that the sub that handles the event HAS to be in the standard
module and NOT in a class module. This is just a limitation of VB6.0. This
means that I can't raise any events in that sub... which is what I *really*
need to do.

Anyway, the inability to raise events led me to try and set up a PInvoke
wrapper in .NET. The problem is, the VB6.0 DLL for the digital I/O card
accepts only "ByVal callbackAddr As Long" as the address for the callback
function. In .NET, the AddressOf operator returns a System.Delegate type.

How can I make this code point to my delegate function in .NET? Or,
alternatively, how can I use VB6.0 to raise the event? I already tried
using a global object to raise the events. Doesn't seem to work...

Thanks.
Nov 20 '05 #1
3 2487
Two answers:

BoloBaby wrote:
Take note that the sub that handles the event HAS to be in the standard
module and NOT in a class module. This is just a limitation of VB6.0. This
means that I can't raise any events in that sub... which is what I *really*
need to do.
why don't you just invoke the class and let it handle the event? Just
declare an object of the class on module scope. In the class, before
calling the API, you set this object instance to "Me". then you can call
a public procedure of this class from the callback function. Now, back
in the class this certain public procedure uses RaiseEvent to raise an
event.
It's as simple as this.
Anyway, the inability to raise events led me to try and set up a PInvoke
wrapper in .NET. The problem is, the VB6.0 DLL for the digital I/O card
accepts only "ByVal callbackAddr As Long" as the address for the callback
function. In .NET, the AddressOf operator returns a System.Delegate type.


No problem at all, just declare the API accordingly, use the delegate
type. The framework will take care of the needed conversion.

Example:

\\
Public Delegate Function FontEnumProc( _
ByVal NLF As LOGFONT, _
ByVal NTM As TEXTMETRIC, _
ByVal FontType As Int32, _
ByVal LParam As Int32 _
) As Int32

Public Declare Function EnumFontFamilies Lib "gdi32" Alias
"EnumFontFamiliesA" ( _
ByVal hDC As Int32, _
ByVal lpszFamily As String, _
ByVal lpEnumFontFamProc As FontEnumProc, _
ByVal lParam As Int32 _
) As Long
///

should work just as well with your API.

--
Konrad -
http://madrat.net/
Nov 20 '05 #2
* "Konrad L. M. Rudolph" <ko************@madrat.net> scripsit:
Anyway, the inability to raise events led me to try and set up a PInvoke
wrapper in .NET. The problem is, the VB6.0 DLL for the digital I/O card
accepts only "ByVal callbackAddr As Long" as the address for the callback
function. In .NET, the AddressOf operator returns a System.Delegate type.


No problem at all, just declare the API accordingly, use the delegate
type. The framework will take care of the needed conversion.
Example:

\\
Public Delegate Function FontEnumProc( _
ByVal NLF As LOGFONT, _
ByVal NTM As TEXTMETRIC, _
ByVal FontType As Int32, _
ByVal LParam As Int32 _
) As Int32

Public Declare Function EnumFontFamilies Lib "gdi32" Alias
"EnumFontFamiliesA" ( _

ByVal hDC As Int32, _
ByVal lpszFamily As String, _
ByVal lpEnumFontFamProc As FontEnumProc, _
ByVal lParam As Int32 _
) As Long
///

should work just as well with your API.


ACK, but don't forget to store a reference to the delegate you pass to
the API function in order to prevent the GC from removing it.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

"BoloBaby" <bo******@hotmail.com> wrote in message
news:X7********************@comcast.com...
OK, here's the problem...

I have a digital input/output card that exposes IRQs (digital input
change-of-state) through a DLL function.

In VB6.0, I can use this ability through code that looks like this:

[Class1]

Public sub test()
DIO_INT1_EventMessage 0, 0, 0, 0, AddressOf BE_ReadPort
end sub

[standard module]

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


Declare Function DIO_INT1_EventMessage Lib "Pci-Dask.dll" _
(ByVal CardNumber As Short, _
ByVal Init1Mode As Short, _
ByVal windowHandle As IntPtr, _
ByVal message As Integer, _
ByVal callbackAddr As MethodInvoker) As Short

Private mi As MethodInvoker = New MethodInvoker(BE_ReadPort)

Private Sub CallIt()
DIO_INT1_EventMessage(..., ..., ..., Me.Handle, ..., mi)
End Sub

Private sub BE_ReadPort()
'do some stuff
end sub

Something like the above should get you up and running :)

--
Tom Shelton [MVP]
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j-integra_support | last post by:
Looking for Java/COM interoperability tools? Thousands of companies world-wide are using J-Integra for COM for interoperability between Java and Microsoft COM applications. J-Integra for COM is a...
4
by: Frederik Kesting | last post by:
Hi! I heard about the possibilty of using different languages for one project whith the .Net Framework. Is it f.e. possible to include Managed C++ code into a VB.Net project without changing...
3
by: sb | last post by:
I think streams are nice, but what do you do when you have to write to or, even worse, read from a FILE*, for example a UNIX stream? C++ streams can not be created from FILE*'s or have them...
1
by: louis | last post by:
Today I got the latest propaganda email from MSFT talking about XML and interoperability. It's been bothering me all day and while the notion of interoperability is good -- the ability to interact...
1
by: Ralph | last post by:
Does anybody know where could i find documentation of .NET interoperability with Office. I know it's through COM objects, but, where could i find documentation of those objects, their methods,...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
0
by: j-integra_support | last post by:
Looking for Java/.NET/CORBA interoperability tools? Intrinsyc Software has just released the latest addition to its popular J-Integra Interoperability Suite... J-Integra Espresso! J-Integra...
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...
0
by: Pradnya Patil | last post by:
Hi , I need to export some of the reports to MS EXCEL & MS WORD in a WEB APPLICATION.I also need to LOCK some of the Columns in EXCEL-sheet.Right now I need to run the Interoperability...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.