472,104 Members | 1,127 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to setup a CDECL callback to C# Delagate

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>
Jul 24 '05 #1
3 6169
>How do I declare the delegate to ensure that the marshaller will understand
that the callback is of type CDECL?


There's no way to do that in C# v1.x.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 25 '05 #2
Not entirely true.
Check out this link:

http://baby.homeip.net/patrick/archi..._calling_c.php
Aug 6 '05 #3
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>
Sep 6 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Tor Rustad | last post: by
3 posts views Thread by Boots | last post: by
1 post views Thread by Subhash Bhartiya via .NET 247 | last post: by
6 posts views Thread by ReinhardH | last post: by
1 post views Thread by ThinkRS232 | last post: by
reply views Thread by MalamisuraE | last post: by
2 posts views Thread by Udi | last post: by

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.