473,598 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SpecialTimerFun ction(int Val, int (*Callback)(int
InVal))

C# Declaration
public class MyClass
{
public delegate int MyCallback(int InVal);

[DllImport("MyDL L.dll")]
public static extern int SpecialTimerFun ction(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 6321
>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
wasabikun
1 New Member
Not entirely true.
Check out this link:

http://baby.homeip.net/patrick/archi..._calling_c.php
Aug 6 '05 #3
kurtcobain1978
2 New Member
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_HANDLE R)(
const SABREAPICODE messageType, const char * message, const char * properties) ;

//This is the native function to be invoked
ErrorCode register(CALLBA CK_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(ByVa l messageType As Long, ByVal message As String) As Long
Try
MsgBox(messageT ype)
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 NullReferenceEx ception 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 SpecialTimerFun ction(int Val, int (*Callback)(int
InVal))

C# Declaration
public class MyClass
{
public delegate int MyCallback(int InVal);

[DllImport("MyDL L.dll")]
public static extern int SpecialTimerFun ction(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
2523
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> int (*daytab); cdecl> daytab is pointer to array of int
3
2933
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 but I do not know how to set this I am attempting to have a C style DLL perform a Callback to a given function in my C# class passing a pointer to a struct. Currently, I have the callback working ( that is I get my expected events ) but the...
2
2410
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 pointer to a function (UpdateVC) that updates a status window periodically. I am using a delegate to attempt to accomplish the same thing in .NET, but it doesn't quite work. The DLL will call the function and the function appears to act...
1
1482
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 user from .NET 247 (http://www.dotnet247.com/)
6
2390
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 the callback as Public Delegate Sub CallbackFunction(ByVal s1 As structure1, ByVal s2 as Structure2)
1
268
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? 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
6
2399
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 C#. One of the problem is passing a function address to a COM function as a parameter with another progress value. My callback function is very simple using the progress value to update my progressbar. Because this COM function usually takes a long...
0
1180
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 creating my own threads as I am convinced this is the way to go from researching the material. For threads you can create an array of threads, but for Asynchrouness Delegates you cant since each one needs its own seperate delagate and AsyncCallback...
2
2923
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 written an interop dll:
0
7981
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8046
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.