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 ErroCode (__cdecl * CALLBACK_HANDLER)(
const SABREAPICODE messageType, const char * message, const char * properties) ;
//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 Integer
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!
I have a Win32 DLL that has a standard _stdcall (WINAPI) exports. I am able
to call these fine from C#. One call in particular however has a callback to
a CDECL function. How would I set that up? Following is the specific.
Win32 DLL Declaration for function in MyDLL.dll
extern "C" int WINAPI SpecialTimerFunction(int Val, int (*Callback)(int
InVal))
C# Declaration
public class MyClass
{
public delegate int MyCallback(int InVal);
[DllImport("MyDLL.dll")]
public static extern int SpecialTimerFunction(int Val,MyCallback Callback);
}
How do I declare the delegate to ensure that the marshaller will understand
that the callback is of type CDECL?
Thanks a million.
Berto
--
<hr><a href="http://www.wcscnet.com">WCSC (Willies Computer Software Co)
http://www.wcscnet.com</a>