Skip to content

FR CSharp Keyword unmanaged function pointer calling convention

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C# : unmanaged — convention d'appel du pointeur de fonction

Manuel C# · Guide pratique

non géré sur le type délégué* spécifie une convention d'appel non gérée. Sans liste, l'accord de plateforme est utilisé ; Cdecl peut être explicitement spécifié entre crochets. Une méthode statique C# avec UnmanagedCallersOnly est appelée via un pointeur non géré compatible au lieu d'un appel géré normal. Le projet autorise les blocs dangereux.

Exemple simple (Example.cs)

Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.

Example.cs:

// PROJECT-EXAMPLE
unsafe
{
    delegate* unmanaged[Cdecl]<int, int> twice = &Tools.Twice;
    System.Console.WriteLine(twice(4));
}
static class Tools { [System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] public static int Twice(int value) => value * 2; }

Activez <AllowUnsafeBlocks>true</AllowUnsafeBlocks> dans le projet. Le pointeur est valide uniquement dans la portée indiquée.

Résultat:

8

Syntaxe

delegate* unmanaged<int, int> operation = &Transform;
delegate* unmanaged[Cdecl]<int, int> callback = &TransformCdecl;

Un appel avec un accord de plateforme non gérée

Fichier Example.csproj :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

</Project>

Fichier Program.cs :

using System;
using System.Runtime.InteropServices;

internal static unsafe class Program
{
    public static void Main()
    {
        delegate* unmanaged<int, int> doubleValue = &DoubleValue;
        Console.WriteLine(doubleValue(12));
    }
    [UnmanagedCallersOnly]
    private static int DoubleValue(int value) => value * 2;
}

Résultat:

24

Un rappel avec une convention Cdecl explicitement spécifiée

Fichier Example.csproj :

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

</Project>

Fichier Program.cs :

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static unsafe class Program
{
    public static void Main()
    {
        delegate* unmanaged[Cdecl]<int, int, int> callback = &Difference;
        int result = InvokeCallback(callback, 14, 5);
        Console.WriteLine(result);
    }
    private static int InvokeCallback(delegate* unmanaged[Cdecl]<int, int, int> callback,
        int left, int right) => callback(left, right);
    [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
    private static int Difference(int left, int right) => left - right;
}

Résultat:

9

Source officielle : Microsoft Apprendre.

Clone this wiki locally