Click here to Skip to main content
Click here to Skip to main content

Using C calling convention callback functions in C# and VB - the easy way

, 13 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Provides an easy way to use C calling convention callback functions in C# and VB.

The problem

Let's assume that the following is presented:

  1. Function in an unmanaged DLL that accepts function pointer as parameter.
  2. The type of the function pointer respects the C calling convention (is declared with __cdecl).
  3. You need to import that function in your C# or VB program and to pass a delegate to it.
  4. Your delegate would be called more than once.

Here is an example:

DLL library header file

typedef void (__cdecl *func_type)(int count);

TESTLIB_API void __cdecl SetCallback( func_type func );

C# code

[DllImport("TestLib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern void SetCallback( MulticastDelegate callback );

What you would do is define a delegate type:

public delegate void CallbackDelegate( int count );

and pass an instance of it to the unmanaged function.

[STAThread]
static void Main(string[] args)
{
    CallbackDelegate del = new CallbackDelegate( Callback );
    SetCallback( del );
}

private static void Callback( int count )
{
    Console.WriteLine( "Callback invoked for " + 
                                          count + " time" );
}

Then your delegate should be invoked one or more times.

The problem is that the function pointer that the unmanaged function accepts should respect the C calling convention but the delegate instance you pass to it does not (it respects the standard calling convention (__stdcall). As a result, after the first call of the method from unmanaged code, the stack becomes corrupted and on the second or third call, a System.NullReferenceException is thrown.

Description of the same problem can be found here. Note that the problem exists only for callback functions that accept parameters.

Solution

The solution can be found here and here. The solution is (as the above posts suggest) apply modification option on the "Invoke" method of the delegate's type:

.method public hidebysig virtual instance native int
    modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
    Invoke(int32 cb) runtime managed
{
} // end of method ...::Invoke

The original method generated by the C# and VB compilers look like this:

.method public hidebysig virtual instance native int
    Invoke(int32 cb) runtime managed
{
} // end of method ...::Invoke

(without modification option)

This cannot be done directly in your code as there is no way to specify modification options (or at least modification options that concern calling conventions) in C# and VB.

The drawback of the solution is that you need to disassemble your assembly, add the modification option and compile it. This is not a big deal, but what if you can apply the solution at run-time without the need to disassemble and recompile the code!

Run-time delegate type generation

The solution presented in this article does exactly the same but at run-time by generating the delegate types: it allows you to generate and use delegate types for any method.

More specifically it:

  • is able to generate delegate types for any method and specified System.Runtime.InteropServices.CallingConvention,
  • the generated types look exactly as the types generated by the C# and VB compilers but with modification option applied on the "Invoke" method,
  • is able to create instances of the generated types.
  • caches the generated types per method and CallingConvention.

However, something is not implemented: a delegate generated by C# or VB compiler can have marshaling information on parameters.

If you apply [MarshalAs] attribute on the parameters of a delegate declaration marshaling would be specified for those parameters in the delegate type generated by C# or VB compiler. This solution does not provide a way to specify marshaling of parameters, although it can be implemented. Any other parameter attributes like [In], [Out], ref, default value, etc. are handled (see System.Reflection.ParameterAttributes).

Using the solution

To obtain a delegate instance simply invoke one of the App.Runtime.InteropServices.DelegateGenerator methods. This method will generate a delegate type (if not already generated for the particular method and calling convention) and return an instance of it.

To make the delegate generation transparent to your code you can use the code like this:

/// <summary>
/// Callback delegate.
/// </summary>
public delegate void CallbackDelegate( int count );

/// <summary>
/// Sets specified callback method.
/// </summary>
/// <param name="callback"></param>
public static void SetCallback( CallbackDelegate callback )
{
    SetCallback( DelegateGenerator.CreateDelegate( callback ) );
}

/// <summary>
/// Sets specified callback method.
/// </summary>
/// <param name="callback"></param>
[DllImport("TestLib.dll",
           CallingConvention=CallingConvention.Cdecl)]
private static extern void SetCallback( 
                          MulticastDelegate callback );

You expose a delegate type and a "proxy" method that accepts an instance of that delegate type and passes adjusted version of it to the imported method.

Implementation

Delegate types are generated by using the types declared in the System.Reflection.Emit namespace. Unfortunately, they do not provide ways to specify modification options so modification options are added by a little "hack" on the generated methods' signatures. That involves the usage of reflection.

Source code and demo

The source code includes the App.Runtime.InteropServices.DelegateGenerator type and a reflection helper type.

There are two demo projects implemented in C# and VB that demonstrate the usage of generated delegate and normal delegate. Usage of normal delegate leads to stack corruption and System.NullReferenceException.

The demo includes C++ unmanaged library.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jecho Jekov
Software Developer (Senior)
Bulgaria Bulgaria
No Biography provided

Comments and Discussions

 
GeneralRe: Runtime exceptions in VS2005 Pinmemberroylory7-Nov-06 14:17 
GeneralRe: Runtime exceptions in VS2005 Pinmembermorpheus200627-Aug-07 22:14 
GeneralMinor bug PinmemberDavePaul6-Mar-06 9:31 
GeneralRe: Minor bug PinmemberDavePaul6-Mar-06 9:37 
GeneralVisual Studio 2003 &amp; 2005 PinmemberJonas Ruikis16-Dec-05 5:29 
GeneralRe: Visual Studio 2003 &amp;amp; 2005 PinmemberJonas Ruikis16-Dec-05 5:46 
GeneralRe: Visual Studio 2003 &amp;2005 PinmemberJecho Jekov20-Dec-05 5:11 
GeneralRe: Visual Studio 2003 &amp;2005 PinmemberAlgerChan12-Aug-06 17:37 
GeneralRe: Visual Studio 2003 &2005 PinmemberSharath C V22-Jun-10 23:44 
AnswerRe: Visual Studio 2003 &amp; 2005 PinmemberJohn Petersen17-Jul-06 22:19 
GeneralGR8 Pinmemberkamran16-Dec-05 0:46 
GeneralFantastic, awesome! PinmemberTriLogic1-Dec-05 11:25 
General.NET Framework 2.0 addresses the issue Pinmemberyletourneau28-Nov-05 5:50 
GeneralRe: .NET Framework 2.0 addresses the issue PinmemberNasenbaaer20-Feb-07 23:47 
AnswerRe: .NET Framework 2.0 addresses the issue PinmemberMuchSoft17-Sep-07 1:00 
GeneralRe: .NET Framework 2.0 addresses the issue PinmemberTodd Howard23-May-08 4:27 
GeneralRe: .NET Framework 2.0 addresses the issue Pinmvpleppie24-Jan-09 20:10 
GeneralThanks, exactly what I needed. Pinmembersherro26-Oct-05 16:07 
GeneralDelegate Basics Might Help Pinmemberdaddion13-Sep-05 8:05 
GeneralRe: Delegate Basics Might Help PinmemberMarc Leger17-Sep-05 11:45 
GeneralRe: Delegate Basics Might Help Pinmemberdaddion19-Sep-05 11:51 
GeneralRe: Delegate Basics Might Help Pinmemberhollyireland21-Sep-05 12:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 13 Sep 2005
Article Copyright 2005 by Jecho Jekov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid