你会如何声明DLL导入签名?

时间:2011-06-10 16:20:05

标签: c# .net c++ dllimport

这是Using pHash from .NET

的后续帖子

您如何在.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);

enter image description here

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

3 个答案:

答案 0 :(得分:11)

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

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

C#中的函数签名(返回值和参数)是正确的。

答案 1 :(得分:1)

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

所以你应该尝试:

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

答案 2 :(得分:0)

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

即使这不是问题,每当Dll函数处理文本时,都要习惯指定CharSet属性。

相关问题