c# - 在 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 函数?
解决方案
推荐阅读
- google-app-engine - GAE 标准异步获取不起作用
- python - 注释子类的方法?
- php - 有没有办法从作曲家创建“我们使用的图书馆”页面?
- javascript - 使用托管应用程序在客户端打印
- point-clouds - 使用 PDAL 的点云密度可视化
- java - Android Recyclerview 以编程方式设置项目的位置
- mysql - SQL prepared statements
- macos - Mac OS 调整大小按钮
- macos - 管理团队的计算机
- objective-c - 在 iPhone 上使用 Actionsheet 选择器获取文本输入弹出对话框的简单方法是什么