简体   繁体   English

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

[英]How would you declare DLL import signature?

this is a follow-up post of Using pHash from .NET这是使用来自 .NET的 pHash 的后续帖子

How would you declare following C++ declaration in .NET?您将如何在 .NET 中声明遵循 C++ 声明?

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

So far i have到目前为止我有

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

But I am now getting following error for但我现在收到以下错误

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

在此处输入图像描述

It basically says that my declartion of ph_dtc_imagehash is wrong.它基本上说我对 ph_dtc_imagehash 的声明是错误的。
What am I doing wrong here?我在这里做错了什么?

Stack imbalance indicates that the C++ code uses cdecl and your C# uses stdcall calling convention.堆栈不平衡表明 C++ 代码使用cdecl而您的 C# 使用stdcall调用约定。 Change your DLLImport to this:将您的DLLImport更改为:

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

The function signature in C# (return value and parameters) is otherwise correct. C#(返回值和参数)中的 function 签名在其他方面是正确的。

Check the calling convention.检查调用约定。 If you don't specify one on the DllImport attribute, it defaults to WinApi (stdcall).如果您未在 DllImport 属性上指定一个,则默认为 WinApi (stdcall)。 The C snippet you posted doesn't specify a calling convention, and at least in VC++ the default calling convention is cdecl.您发布的 C 片段没有指定调用约定,至少在 VC++ 中,默认调用约定是 cdecl。

So you should try:所以你应该尝试:

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

Try explicitly setting DllImportAttribute.CharSet property to CharSet.Auto as if you don't specify it, it will default to Ansi .尝试将DllImportAttribute.CharSet属性显式设置为CharSet.Auto ,就好像您没有指定它一样,它将默认为Ansi This may be causing issues as your declaration seems to be fine.这可能会导致问题,因为您的声明似乎很好。

Even if this is not the issue, take habit of specifying the CharSet property whenever a Dll function deals with text.即使这不是问题,也要养成在 Dll function 处理文本时指定CharSet属性的习惯。

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

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