tags:

views:

385

answers:

2

I am trying to use python and ctypes to use the fuzzy.dll from ssdeep. So far all I have tried fails with and access violation error. Here is what I do after changing to the proper directory which contains the fuzzy.dll and fuzzy.def files.

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

From what I understand, I have passed the proper c_types. From fuzzy.h

extern int fuzzy_hash_filename(char * filename, char * result)

I just cannot get past that access violation. Thank you in advance.

+3  A: 

There are two problems with your code:

  1. You should not use windll.fuzzy, but cdll.fuzzy -- from 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. For return value (chash), you should declare a buffer rather than creating a pointer to 0x0000200 (=512) -- this is where the access violation comes from. Use create_string_buffer('\000' * 512) instead.

So your example should look like this:

>>> 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
DzinX
A: 

@DzinX

Perfect. Thank you.

Marked as SOLVED

Cutaway
Glad to help! A little StackOverflow advice, by the way: we write thanks and other comments in comments such as this one, not as answers to the question. Also, to select the accepted answer, click a giant tick under it. This way, other people will see this message as solved (and we'll get some rep).
DzinX
Cutaway, you should click the big checkmark under DzinX's score on the left so you can mark it as the correct answer.
Evan Fosmark