14

I wrote a test dll in C++ to make sure things work before I start using a more important dll that I need. Basically it takes two doubles and adds them, then returns the result. I've been playing around and with other test functions I've gotten returns to work, I just can't pass an argument due to errors. My code is:

import ctypes
import string

nDLL = ctypes.WinDLL('test.dll')
func = nDLL['haloshg_add']
func.restype = ctypes.c_double
func.argtypes = (ctypes.c_double,ctypes.c_double)
print(func(5.0,5.0))

It returns the error for the line that called "func":

ValueError: Procedure probably called with too many arguments (8 bytes in excess)

What am I doing wrong? Thanks.

pajm
  • 1,788
  • 6
  • 24
  • 30

1 Answers1

22

You probably got the calling conventions mixed up. I'm guessing you have a C function declared something like this:

double haloshg_add(double d1, double s2)
{
    return d1+d2;
}

This will use the C calling convention by default. The simplest approach would be to change the calling convention in your ctypes code:

nDLL = ctypes.CDLL('test.dll')

If you wanted to change the calling convention in the C code to stdcall (to match ctypes.WinDLL) then you would do this:

double __stdcall haloshg_add(double d1, double s2)

Whatever you do, only do one of these changes. If you do both you'll have the reverse failure!

If it were me, I'd just change the Python code to use C calling convention (use CDLL). That change has the least impact.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Worked like a charm, thank you david for all your help! I'll select your question as the best one as soon as stackoverflow allows me. – pajm Mar 11 '11 at 00:09
  • Thanks. I was waiting for this one, after your last question. I expected this would be it, but it's better in separate questions. It's just tidier that way. – David Heffernan Mar 11 '11 at 00:12
  • No problem! You're a life-saver, I've never dealt with something like this before and I'm new to python anyway... – pajm Mar 11 '11 at 00:14