通过C#中的地址调用非托管的快速调用C函数

时间:2019-01-06 04:36:30

标签: c# c .net pointers dll

我正在将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);
    }
}

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

0 个答案:

没有答案