首页 > 解决方案 > 在 C# 中按地址调用非托管 fastcall C 函数

问题描述

我将我的 C# dll 注入到用 C 编写的非托管进程中。我将如何在此进程中调用函数?我知道函数的地址和函数签名。该函数使用fastcall调用约定。我不确定这是否会使事情复杂化。

我试过像这样使用代表:

    [UnmanagedFunctionPointer(CallingConvention.FastCall, CharSet = CharSet.Unicode)]
    public delegate IntPtr _PrintText(string msg, int color);

这引发了关于FastCall不受支持的警告。

这是我尝试过的更完整,更简单的示例:

C++ 函数:

void __fastcall PrintText (wchar_t * wMessage, int nColor)
Address: 0x31E3A0

C#代码:

public class Example
{
    [UnmanagedFunctionPointer(CallingConvention.FastCall, CharSet = CharSet.Unicode)]
    public delegate IntPtr _PrintText(string msg, int color);

    public static int EntryPoint(string pwzArgument)
    {
        var ptr = new IntPtr(0x31E3A0);
        _PrintText MyPrint = Marshal.GetDelegateForFunctionPointer<_PrintText>(ptr);
        MyPrint("TESTING", 0);
    }
}

如何FastCall使用 C# 正确定义和调用 C 函数?

标签: c#c.netpointersdll

解决方案


推荐阅读