c# - 如何在c#中使用c字节指针函数的dll?
问题描述
我有一个 dll 库,就像
void Decrypt(BYTE* RoundKey, BYTE* Data){...}
它是一个简单的解密函数,它接收密钥和数据指针并仅解密数据。我想在 C# 中使用这个 dll,所以我写了一个示例测试代码
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
extern private static unsafe void decrypt(byte* RoundKey, byte* Data);
static unsafe void Main(string[] args) {
public static byte[] Key = {0x00, ....};
public static byte[] data = {0x00, ....};
decrypt(&Key, &data);
}
而且这段代码没有编译。我想知道如何在 C# 中使用这个 dll?
解决方案
只需按如下方式编写(假设 dll 库知道密钥和数据大小):
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
private static unsafe extern void Decrypt(byte* roundKey, byte* data);
public static byte[] Key = { 0x00, ...};
public static byte[] Data = { 0x00, ... };
static unsafe void Main(string[] args)
{
fixed (byte* keyPtr = Key)
{
fixed(byte* dataPtr = Data)
{
Decrypt(keyPtr, dataPtr);
}
}
}
推荐阅读
- javafx - Javafx2.2画布放大缩小失真问题
- javascript - 如何在其他文件中导出/使用 socket.io?
- vba - 使用 VBA/条件格式的热图
- laravel - 如何将 JSON 数据检索到 Laravel 并在视图上显示它的元素?
- gimp - 有没有办法在 GIMP 中将精灵表切割成单个图像?
- jquery - 填写表格,点击按钮,在线网站每 5 分钟显示一次提醒
- python - python matplotlib.pyplot log-log 绘图点未显示,因为 python 选择的 y 范围不好
- php - PDO 语句在 PHP 7 中不再起作用
- c - 用 C 编写的 Shell 有输出重定向错误:分段错误
- android - 为什么 onClick 方法将视图作为参数?