10

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.

3
  • 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? Commented Jan 9, 2012 at 15:03
  • Also, which version of Python are you using? Can make a difference to string encodings. Commented Jan 9, 2012 at 15:06
  • yeah, that var declaration is fishy. If you, user 1138... wrote that DLL, why did you do that? Commented Jan 9, 2012 at 15:07

2 Answers 2

13
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))
Sign up to request clarification or add additional context in comments.

2 Comments

Updated with argtypes and restype.
My python isn't able to find windll.OziAPI - stackoverflow.com/questions/27910191/…
1

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.

5 Comments

I also don't see in that Python code anything which suggests you're specifying the stdcall calling convention. Doesn't cdll in Python default to cdecl? If so, this will also cause you problems!
I'm pretty sure that anything you invoke via a dll and which the python runtime library loads and invokes, will always use the windows standard calling conventions.
@Warren No, LaKraven is right. cdll vs windll is the way to specify calling conventions. Also, there is no Windows standard for calling convention, at least on x86. Some libraries use stdcall and some use cdecl. Think of msvcrt for example. It's all cdecl.
Oh right. My bad. I am so Delphi centric that I think of stdcall as a Windows standard but it isn't. It's kind of the usual thing for "visual basic and delphi guys".
@DavidHeffernan That's the first time you and I have agreed today :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.