469,594 Members | 2,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,594 developers. It's quick & easy.

Implementing a Callback function (_cdecl) in VB.NET

--------------------------------------------------------------------------------

I need to do the exactly same thing in VB.NET.

Load a unmanaged C DLL dynamically and then call a function in which I pass the callback function as an argument. My C function being called callback as type _cdecl. Does anybody have any ideas?

Here is what I am trying to do:

-----------Native Code snippet----------------
typedef ErrorCode (__cdecl * CALLBACK_HANDLER)(
const int messageType, const char * message) ;

//This is the native function to be invoked
ErrorCode register(CALLBACK_HANDLER messageHandler, const char * parameters);
----------------------------------------------

-------------VB.NET code snippet-------------

......
Module CallbackHandler
Public Delegate Function Callback(ByVal messageType As Long, ByVal message As String) As Long
Public Declare Function register Lib "api.dll" (ByVal trafficType As Integer, ByVal listener As Callback) As Long
End Module
.....

......
Public Function myCallback(ByVal messageType As Long, ByVal message As String) As Long
Try
MsgBox(messageType)
MsgBox(message)
Catch ex As Exception
'MsgBox(ex)
Finally
End Try
End Function
.....
.....
Dim cb As CallbackHandler.myCallback
cb = AddressOf myCallback
CallBackHandler.register(2, cb)
......
----------------------------------------------

However I keep getting the error NullReferenceException which I guess is because my native declaration is _cdecl while I'm trying to make a _stdcall.

Any help would be greatly appreciated.

Peace!
Sep 7 '05 #1
0 8115

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ThinkRS232 | last post: by
5 posts views Thread by Mark | last post: by
7 posts views Thread by Frank Neuhaus | last post: by
7 posts views Thread by Amit Sharma | last post: by
2 posts views Thread by Jon E. Scott | last post: by
4 posts views Thread by =?Utf-8?B?TGVvbg==?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.