Skip to content

IT 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 — convenzione di chiamata del puntatore a funzione

Manuale C# · Guida pratica

non gestito sul tipo delegato* specifica una convenzione di chiamata non gestita. Senza elenco viene utilizzato il contratto di piattaforma; Cdecl può essere specificato esplicitamente tra parentesi quadre. Un metodo statico C# con UnmanagedCallersOnly viene chiamato tramite un puntatore non gestito compatibile invece di una normale chiamata gestita. Il progetto consente blocchi non sicuri.

Esempio semplice (Example.cs)

Salvare questo esempio separatamente come Example.cs. Eseguirlo indipendentemente dagli altri esempi di questa pagina.

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; }

Abilitare <AllowUnsafeBlocks>true</AllowUnsafeBlocks> nel progetto. Il puntatore è valido solo nell’ambito mostrato.

Risultato:

8

Sintassi

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

Una chiamata con un contratto di piattaforma non gestita

File 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>

File 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;
}

Risultato:

24

Un callback con una convenzione Cdecl specificata in modo esplicito

File 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>

File 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;
}

Risultato:

9

Fonte ufficiale: Microsoft Impara.

Clone this wiki locally