2

我已使用 PInvoke 互操作助手生成 C# PInvoke 签名。我想确认一下。我在调用 DLL 时收到“找不到 PInvoke DLL”消息。我正在导出函数。DLL 存在可执行文件。消息和密文是原始字节的输入/输出 blob,并且是相同的缓冲区。

extern "C" int __declspec(dllexport) EncryptDeviceName(uint8 *message, uint8 *ciphertext, uint64 msglength)
{
    ...

    return 0;
}

它生成了以下 C# PInvoke 签名:

   /// Return Type: int
   ///message: UINT8*
   ///ciphertext: UINT8*
   ///msglength: UINT64->unsigned __int64
   [DllImport("HC128.dll", EntryPoint = "EncryptDeviceName")]
   public static extern int EncryptDeviceName(System.IntPtr message, System.IntPtr     ciphertext, ulong msglength);

我将遵循以下类似问题中的建议并提供更新。

更新

我的签名确实适用于带有 marshal alloc/dealloc 的 Windows CE 6。Tergiver 的签名也适用于 Windows CE 6,它不需要 marshal alloc/dealloc。

4

2 回答 2

2

你不见了CallingConvention.Cdecl。或者__stdcall在C端使用。

于 2012-07-25T03:07:47.397 回答
0

编写 ap/Invoke 声明的方法有很多种。可以使用给定的一个,但是它需要您为它执行 Marshaller 的工作(分配非托管内存、复制和执行字符编码转换)。

该本地声明没有提供足够的信息来猜测所需的编组,这可能就是它不存在的原因。

本机函数期望的字符编码是什么message?是ciphertext一团原始字节还是它也有字符编码(输入和输出)?

更新

如果两者message都是cipherText原始字节数组,我们可以将它们编组为

[DllImport("HC128.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "EncryptDeviceName")]
 public static extern int EncryptDeviceName([In] byte[] message, [In, Out] byte[] ciphertext, ulong msglength);

In[Attribute]Out[Attribute]告诉编组器执行副本的方式。[In, Out]是默认值,我喜欢明确。

于 2012-07-24T21:49:51.867 回答