ctypes calling delphi dll

Thomas Heller theller at python.net
Wed Dec 18 05:45:21 EST 2002


Brian Elmegaard <brian at rk-speed.rugby.dk> writes:

> Hi,
> 
> I just discovered the ctypes module and I admit I have not worked much
> with dlls. However, I have given it a try and couldn't make it work.
> 
> I have a delphi dll and I read the thread on this, but the argument of
> one function is a string and I cannot reverse this, so what am I to do
> about the error: 
> cd c:/RefEqns/C, C++, Matlab, Simulink and Dymola/python/
> \python22\python example.py
> Traceback (most recent call last):
>   File "example.py", line 3, in ?
>     RefrigC.SetRNumber("R134")
> ValueError: Procedure probably called with too many arguments (4 bytes in excess)

The thread you mention talks about reversing the argument list,
because the *default delphi calling convention* is 'pascal', and
this pushes the arguments onto the stack in left to right order.

Looking at the RefrigC.h header file:

  __int32 __cdecl SetRNumber(char* ANum);
  __int32 __cdecl GetRNumber(char* ANum);

we see that this dll, although probably created by Delphi,
uses the __cdecl calling convention, which is used by C.

> 
> The code is:
> import ctypes
> RefrigC=ctypes.windll.RefrigC
> RefrigC.SetRNumber("R134")
> RefrigC.GetRNumber(AName)
> 
> print AName
> 

Change your code into this:

  import ctypes

  AName = ctypes.c_string("\0" * 200)

  RefrigC=ctypes.cdll.RefrigC
  RefrigC.SetRNumber("R134")
  RefrigC.GetRNumber(AName)
  print AName.value

and it seems to work. It prints 'R11'. Is this what you expected?

Thomas



More information about the Python-list mailing list