Export (0) Print
Expand All

DllImportAttribute.CallingConvention Field

Indicates the calling convention of an entry point.

[Visual Basic]
Public CallingConvention As CallingConvention
[C#]
public CallingConvention CallingConvention;
[C++]
public: CallingConvention CallingConvention;
[JScript]
public var CallingConvention : CallingConvention;

Remarks

You set this field to one of the CallingConvention enumeration members. The default value for the CallingConvention field is WinAPI, which in turn defaults to StdCall convention.

For more information, see Calling Convention in the MSDN library.

Example

[Visual Basic, C#] In some cases, Visual Basic developers use the DllImportAttribute, instead of the Declare statement, to define a DLL function in managed code. Setting the CallingConvention field is one of those cases.

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

Requirements

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, Common Language Infrastructure (CLI) Standard

See Also

DllImportAttribute Class | DllImportAttribute Members | System.Runtime.InteropServices Namespace | CallingConvention

Show:
© 2014 Microsoft