我试图在ssdeep fuzzy.dll上调用一个方法
.h文件是这里,友好的引用是这里
具体来说,我试着叫这个方法..。
int fuzzy_hash_filename (
const char * filename,
char * result
) 我有以下的..。
<DllImport("C:\SSDeep\Fuzzy.dll", EntryPoint:="fuzzy_hash_filename")>
Private Shared Function fuzzy_hash_filename(
<InAttribute(),
MarshalAsAttribute(UnmanagedType.LPStr)>
ByVal Filename As String, ByVal Result As StringBuilder) As Integer
End Function
Public Shared Function FuzzyHash(Filename As String) As String
Dim Ret As New StringBuilder
Ret.Capacity = NativeConstants.FUZZY_MAX_RESULT
Dim Success = fuzzy_hash_filename(Filename, Ret)
If Success <> 0 Then
Throw New Exception("SSDeep fuzzy hashing failed")
End If
Return Ret.ToString
End Function如果我运行这个代码,VS会给我一个模态对话。
调用PInvoke函数'(Blah)::fuzzy_hash_filename‘使堆栈不平衡。这可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
(FWIW,如果我无视警告,所以我必须接近),电话似乎成功了。
我需要对我的定义做什么改变才能让这一切顺利进行?
发布于 2012-08-07 09:20:02
我发现一个在MSDN论坛上也有同样问题的人
1.1这通常是由于API使用的调用约定与C#代码中为API声明的调用约定不匹配所致。
1.2默认情况下,如果未设置DllImportAttribute的DllImportAttribute参数,则默认使用StdCall。
1.3如果DoSomething() API要使用__cdecl (在C++项目中是默认的),那么您应该在C#代码中为DoSomething()使用以下声明: DllImport(@"dll.dll",CallingConvention=CallingConvention.Cdecl)
1.4此外,我建议您将API声明为extern "C“,否则它将受到C++编译器的名称损坏的影响。
发布于 2012-08-31 16:21:24
被接受的答案似乎解决了最初的提问者的问题,但是c#中的等效代码对我来说不起作用。在尝试了越来越复杂的注释之后,回到基础上,最终还是起了作用。作为每个人的参考,我包括了三个接口函数的声明和工作代码(构建在Ss深处版本2.9上)。
//Note: StringBuilder here is the standard way to do it, but is a perf hit because unicode stringbuilder can't be pinned when martialling char*.
//See http://msdn.microsoft.com/en-us/magazine/cc164193.aspx#S4
//int fuzzy_hash_buf(const unsigned char *buf, uint32_t buf_len, char *result)
[DllImport("fuzzy.dll")]
public static extern int fuzzy_hash_buf(StringBuilder buf, int buf_len, StringBuilder result);
//int fuzzy_hash_filename(const char* filename, char* result)
[DllImport("fuzzy.dll")]
static extern int fuzzy_hash_filename(string filename, StringBuilder result);
//int fuzzy_compare (const char *sig1, const char *sig2)
[DllImport("fuzzy.dll")]
static extern int fuzzy_compare(string sig1, string sig2);
static void Main(string[] args)
{
StringBuilder buf = new StringBuilder("test");
StringBuilder result0 = new StringBuilder(150);
fuzzy_hash_buf(buf, 4, result0);
Console.WriteLine(result0);
string filename = "test.txt";
StringBuilder result1 = new StringBuilder(150);
fuzzy_hash_filename(filename, result1);
Console.WriteLine(result1);
int matchScore = fuzzy_compare(result0.ToString(), result1.ToString());
Console.WriteLine("MatchScore: " + matchScore);
}输出:
24:gRnIM7stweRp+fEWU1XRk+/M98D6Dv3JrEeEnD/MGQbnEWqv3JW:gRIMwtrMU1Bk2I3Jrg53JW ssdeeptest.exe 3:Hn:Hn MatchScore: 0
https://stackoverflow.com/questions/11842486
复制相似问题