簡體   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