Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

CallingConvention Enumeration

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

[Visual Basic]
<Serializable>
Public Enum CallingConvention
[C#]
[Serializable]
public enum CallingConvention
[C++]
[Serializable]
__value public enum CallingConvention
[JScript]
public
   Serializable
enum CallingConvention

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.

Members

Member name Description
Cdecl 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.
FastCall This calling convention is not supported.
StdCall The callee cleans the stack. This is the default convention for calling unmanaged functions with platform invoke.
ThisCall 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.
Winapi

Supported by the .NET Compact Framework.

This member is not actually a calling convention, but instead uses the default platform calling convention. For example, on Windows the default is StdCall and on Windows CE .NET it is Cdecl.

Example

[Visual Basic, C#, C++] The following example demonstrates how to apply the Cdecl calling convention, which you must use because the stack is cleaned up by the caller.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices

Public Class LibWrap
' Visual Basic does not 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", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
    format As String, i As Integer, d As Double) As Integer
End Function

<DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
    format As String, i As Integer, s As String) As Integer
End Function
End Class 'LibWrap

Public Class App
    Public Shared Sub Main()
        LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99, _
                       99.99)
        LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _
                       "abcd")
    End Sub 'Main
End Class 'App

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

public class LibWrap
{
// 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.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, double d); 

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

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class LibWrap
{
// 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]... )

public:
[DllImport(S"msvcrt.dll", CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl)]
static int printf(String* format, int i, double d); 

[DllImport(S"msvcrt.dll", CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl)]
static int printf(String* format, int i, String* s); 
};

int main()
{
    LibWrap::printf(S"\nPrint params: %i %f", 99, 99.99);
    LibWrap::printf(S"\nPrint params: %i %s", 99, S"abcd");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Runtime.InteropServices Namespace | DllImportAttribute | System.Reflection.Emit

Show:
© 2015 Microsoft