Skip to content

ES 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 — convención de llamada de puntero de función

Manual C# · Guía Práctica

no administrado en el tipo delegado* especifica una convención de llamada no administrada. Sin lista, se utiliza el acuerdo de plataforma; Cdecl se puede especificar explícitamente entre corchetes. Se llama a un método C# estático con UnmanagedCallersOnly mediante un puntero no administrado compatible en lugar de una llamada administrada normal. El proyecto permite bloques inseguros.

Ejemplo sencillo (Example.cs)

Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.

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

Habilite <AllowUnsafeBlocks>true</AllowUnsafeBlocks> en el proyecto. El puntero solo es válido dentro del ámbito mostrado.

Resultado:

8

Sintaxis

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

Una llamada con un acuerdo de plataforma no administrada

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

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

Resultado:

24

Una devolución de llamada con una convención Cdecl explícitamente especificada

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

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

Resultado:

9

Fuente oficial: Microsoft Aprender.

Clone this wiki locally