CallingConvention Enum

Definition

Specifies the calling convention required to call methods implemented in unmanaged code.

C#
public enum CallingConvention
C#
[System.Serializable]
public enum CallingConvention
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CallingConvention
Inheritance
CallingConvention
Attributes

Fields

Name Value Description
Winapi 1

This member is not actually a calling convention, but instead uses the default platform calling convention.

Cdecl 2

The caller cleans the stack. This enables calling functions with varargs, which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf.

StdCall 3

The callee cleans the stack.

ThisCall 4

The first parameter is the this pointer and is stored in register ECX. Other parameters are pushed on the stack. This calling convention is used to call methods on classes exported from an unmanaged DLL.

FastCall 5

This calling convention is not supported.

Examples

The following example demonstrates how to apply the Cdecl calling convention, which you must use because the stack is cleaned up by the caller.

C#
using System;
using System.Runtime.InteropServices;

internal static class NativeMethods
{
    // C# doesn't support varargs so all arguments must be explicitly defined.
    // CallingConvention.Cdecl must be used since the stack is
    // cleaned up by the caller.

    // int printf( const char *format [, argument]... )

    [DllImport("msvcrt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    internal static extern int printf(String format, int i, double d);

    [DllImport("msvcrt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    internal static extern int printf(String format, int i, String s);
}

public class App
{
    public static void Main()
    {
        NativeMethods.printf("\nPrint params: %i %f", 99, 99.99);
        NativeMethods.printf("\nPrint params: %i %s", 99, "abcd");
    }
}

Remarks

Always use the CallingConvention enumeration rather than the CALLCONV enumeration to specify a calling convention in managed code. The latter exists only for the sake of COM definitions. The CallingConvention enumeration is used by DllImportAttribute and several classes in System.Reflection.Emit to dynamically emit platform invoke signatures.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also