There is a DLL library that needs to be connected to my script. Connect via ctypes

imoport ctypes dll = ctypes.WinDLL('myDLL.dll') 

This library has a function SetCallback

 bool SetCallback(tcallback pCallback); // Тип tcallback объявлен следующим образом: typedef bool (*tcallback)(BYTE* pData); 

I have a function

 def myfync(): print "Hello from dll" 

How do I pass it to the SetCallback() function?

  • More relevant! Highly! - Fuzzz3r
  • Well, first, the SetCallback argument has an argument — a pointer to a BYTE. And secondly, and what does not work? Determine the type of callback via WINFUNCTYPE (see 15.17.1.17), then callback itself with this type, and set it with the parameter SetCallback. It seems so ... - alexlz

1 answer 1

Oh, where do you get such tasks =)

 /* main.c -> main.so (или .dll в windows) */ typedef void (*callback)(); void mainfunction(void *F) { ((callback)F)(); /* вызывает функцию */ } 

Go to the python (Python):

 >>> from ctypes import CFUNCTYPE >>> def pyfn(): ... print "Called the Python function" ... >>> CWRAPPER = CFUNCTYPE(None) >>> wrapped_py_func = CWRAPPER(pyfn) >>> main.mainfunction(wrapped_py_func) Called the Python function