英語で読む

次の方法で共有


DllImportAttribute.CallingConvention フィールド

定義

エントリ ポイントの呼び出し規約を示します。

public System.Runtime.InteropServices.CallingConvention CallingConvention;

フィールド値

場合によっては、Visual Basic 開発者は、 ステートメントのDeclare代わりに を使用DllImportAttributeして、マネージド コードで DLL 関数を定義します。 フィールドの CallingConvention 設定は、その 1 つです。

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.Ansi,
        CallingConvention = CallingConvention.Cdecl)]
    internal static extern int printf(string format, int i, double d);

    [DllImport("msvcrt.dll", CharSet = CharSet.Ansi,
        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");
    }
}

注釈

このフィールドを列挙メンバーのいずれかに設定します CallingConvention 。 フィールドのCallingConvention既定値は です。この既定値はWinapi、Windows およびCdecl他のすべてのプラットフォームで規則に設定StdCallされます。

適用対象

製品 バージョン
.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, 10
.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

こちらもご覧ください