【问题标题】:How to use c byte pointer function's dll in c#?如何在c#中使用c字节指针函数的dll?
【发布时间】:2020-04-05 14:36:38
【问题描述】:

我有一个类似的 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?

【问题讨论】:

  • 有几件事...您不能声明 public static 局部变量。此外,方法名称区分大小写,decrypt!= Decrypt

标签: c# .net pointers dll


【解决方案1】:

写成如下(假设dll库知道key和数据大小):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2011-01-13
    • 2021-07-04
    • 2019-03-28
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多