python - 对 ssdeep 的 fuzzy.dll 使用 Python Ctypes 但收到错误

标签 python ctypes

我正在尝试使用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)

我就是无法克服访问冲突。

最佳答案

您的代码有两个问题:

  1. 您不应使用 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.

  2. 对于返回值 (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/

相关文章:

python - 使用 ctypes (python) 在带括号的路径中加载 dll 时出错

python - 使用 distutils 构建 ctypes -"based"C 库

python - ctypes.windll.user32.GetKeyState 无法识别按键

python - 在一个范围内生成均匀分布的倍数/样本

python - 执行时 tkinter 窗口为空白

python - 从 Python 逐步执行 Simulink 模型

python - 如何从 QTableWidget 列获取行数据?

Python:如何用一系列数字填充数组?

python - Python 包内共享 C 库的路径无关管理

python - 如何将 C++ 中的空字符序列转换为 Python 中的等效字符序列?