Skip to content

DE 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 – Funktionszeiger-Aufrufkonvention

Handbuch C# · Praktischer Leitfaden

„unmanaged“ für den Typ „delegate*“ gibt eine nicht verwaltete Aufrufkonvention an. Ohne Auflistung kommt die Plattformvereinbarung zur Anwendung; Cdecl kann explizit in eckigen Klammern angegeben werden. Eine statische C#-Methode mit UnmanagedCallersOnly wird über einen kompatiblen nicht verwalteten Zeiger anstelle eines normalen verwalteten Aufrufs aufgerufen. Das Projekt erlaubt unsichere Blöcke.

Einfaches Beispiel (Example.cs)

Speichern Sie dieses Beispiel separat als Example.cs. Führen Sie es unabhängig von den anderen Beispielen dieser Seite aus.

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

Aktivieren Sie <AllowUnsafeBlocks>true</AllowUnsafeBlocks> im Projekt. Der Zeiger ist nur im gezeigten Bereich gültig.

Ergebnis:

8

Syntax

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

Ein Anruf mit einer nicht verwalteten Plattformvereinbarung

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

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

Ergebnis:

24

Ein Rückruf mit einer explizit angegebenen Cdecl-Konvention

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

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

Ergebnis:

9

Offizielle Quelle: Microsoft Learn.

Clone this wiki locally