繁体   English   中英

您将如何声明 DLL 导入签名?

[英]How would you declare DLL import signature?

这是使用来自 .NET的 pHash 的后续帖子

您将如何在 .NET 中声明遵循 C++ 声明?

int ph_dct_imagehash(const char* file,ulong64 &hash);

到目前为止我有

[DllImport(@"pHash.dll")]
public static extern int ph_dct_imagehash(string file, ref ulong hash);

但我现在收到以下错误

ulong hash1 = 0, hash2 = 0;
string firstImage = @"C:\Users\dance2die\Pictures\2011-01-23\177.JPG";
string secondImage = @"C:\Users\dance2die\Pictures\2011-01-23\176.JPG";
ph_dct_imagehash(firstImage, ref hash1);
ph_dct_imagehash(secondImage, ref hash2);

在此处输入图像描述

它基本上说我对 ph_dtc_imagehash 的声明是错误的。
我在这里做错了什么?

堆栈不平衡表明 C++ 代码使用cdecl而您的 C# 使用stdcall调用约定。 将您的DLLImport更改为:

[DLLImport(@"pHash.dll", CallingConvention=CallingConvention.Cdecl)]

C#(返回值和参数)中的 function 签名在其他方面是正确的。

检查调用约定。 如果您未在 DllImport 属性上指定一个,则默认为 WinApi (stdcall)。 您发布的 C 片段没有指定调用约定,至少在 VC++ 中,默认调用约定是 cdecl。

所以你应该尝试:

[DllImport(@"pHash.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int ph_dct_imagehash(string file, ref ulong hash);

尝试将DllImportAttribute.CharSet属性显式设置为CharSet.Auto ,就好像您没有指定它一样,它将默认为Ansi 这可能会导致问题,因为您的声明似乎很好。

即使这不是问题,也要养成在 Dll function 处理文本时指定CharSet属性的习惯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM