我正在尝试使用Python和ctypes来使用ssdeep的fuzzy.dll
。到目前为止,我尝试的所有操作都因访问冲突错误而失败。以下是更改到包含 fuzzy.dll 和 fuzzy.def 文件的正确目录后所做的操作:
>>> import os,sys
>>> from ctypes import *
>>> fn = create_string_buffer(os.path.abspath("fuzzy.def"))
>>> fuzz = windll.fuzzy
>>> chash = c_char_p(512)
>>> hstat = fuzz.fuzzy_hash_filename(fn,chash)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: exception: access violation writing 0x00000200
>>>
据我了解,我已经通过了正确的c_types
。来自fuzzy.h
:
extern int fuzzy_hash_filename(char * filename, char * result)
我就是无法克服访问冲突。
最佳答案
您的代码有两个问题:
您不应使用
windll.fuzzy
,而应使用cdll.fuzzy
-- 来自 ctypes documentation :cdll loads libraries which export functions using the standard cdecl calling convention, while windll libraries call functions using the stdcall calling convention.
对于返回值 (
chash
),您应该声明一个缓冲区,而不是创建一个指向0x0000200
(=512) 的指针 - 这是访问的位置违规来自。请改用create_string_buffer('\000' * 512)
。
所以你的例子应该是这样的:
>>> import os, sys
>>> from ctypes import *
>>> fn = create_string_buffer(os.path.abspath("fuzzy.def"))
>>> fuzz = cdll.fuzzy
>>> chash = create_string_buffer('\000' * 512)
>>> hstat = fuzz.fuzzy_hash_filename(fn,chash)
>>> print hstat
0 # == success
关于python - 对 ssdeep 的 fuzzy.dll 使用 Python Ctypes 但收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/510443/