Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Python call Delphi functions in a DLL?

I am trying to call functions from a DLL which seems to be created in Delphi. An example of a some functions supported by the DLL are:

function oziDeleteWpByName(var name:pansichar):integer;stdcall

The Python code I have written to access the above functions is not working.

from ctypes import *
libc = cdll.OziAPI
name ='test'

pi = pointer(name)

delname = libc.oziDeleteWpByName

delname(name)

It seems I am passing the wrong data type to the function. Any ideas on how to do it right?

Thanks it worked. Now please help with this function:

function oziGetOziVersion(var Version:pansichar;var DataLength:integer):integer;stdcall; The version of OziExplorer is returned in the Version variable.

Now how do I pass 'var version' when it the one which will also be returned.

like image 504
user1138880 Avatar asked Jan 09 '12 14:01

user1138880


2 Answers

from ctypes import *

# Not strictly needed but it's good to be explicit.
windll.OziAPI.oziDeleteWpByName.argtypes = [POINTER(c_char_p)]
windll.OziAPI.oziDeleteWpByName.restype = c_int

p = c_char_p('test')
retval = windll.OziAPI.oziDeleteWpByName(byref(p))
like image 154
yak Avatar answered Nov 03 '22 00:11

yak


In Delphi, a var parameter is passed by reference. So what you have there is a pointer to a PAnsiChar (aka C-style string pointer). If you're passing it a string pointer, instead of a pointer to a string pointer, it won't work.

like image 26
Mason Wheeler Avatar answered Nov 03 '22 01:11

Mason Wheeler