10
votes

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.

2
var name:pansichar implies that the name parameter can be modified and returned to the caller. Does the function really do that? Are you planning to read the contents of name after the function returns?David Heffernan
Also, which version of Python are you using? Can make a difference to string encodings.David Heffernan
yeah, that var declaration is fishy. If you, user 1138... wrote that DLL, why did you do that?Warren P

2 Answers

13
votes
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))
1
votes

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.