473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 6304
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tor Rustad | last post by:
Did a fun project some years ago.. a cdecl, but never really tested it. Here are some test cases I just tried: cdecl> char **argv; cdecl> argv is pointer to pointer to char Looks ok cdecl>...
3
by: Boots | last post by:
Hi all I am relatively new to C# programming and have run into a problem that I believe is caused by my C# function declarations. I believe that my C# function must use a CDecl CallingConvention...
2
by: KW | last post by:
I am converting a multithreaded MFC application to Managed C++ under VS2003 that uses a DLL that was compiled in Microsoft Visual Fortran 6. The main function of the fortran DLL (FVIS2FOR) takes a...
1
by: Subhash Bhartiya via .NET 247 | last post by:
hi all, can anyone help me how to implement callback functin in AVISAVE API function in VB.NET -------------------------------- From: Subhash Bhartiya ----------------------- Posted by a...
6
by: ReinhardH | last post by:
Hi, I have to use a cdecl dll (3 party dll). One of the functions needs a callback as a parameter. Unfortunately it seems that I'm not able to solve this issue. What I have done is: Declare...
1
by: ThinkRS232 | last post by:
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?...
6
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original project was written in VB. I have converted it to...
0
by: MalamisuraE | last post by:
I need to write a class that will process a qued list of items, each item could take a long or short time based on the items. I want to use Asynchronous Callback Methods to do this instead of...
2
by: Udi | last post by:
Hi All, I have a C dll exporting the following: ```````````````````````````````````````````````````` typedef void (__cdecl *pCallBack)(int i); int __cdecl Foo(pCallBack pFunc); I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.