communication between python and a dll [solved]

hoja folks,

i have to seperate all the performance killers into a dll. and its possible to read this dll. but how to i send any information from python into this dll?

here the dll:

DLLtest.cpp

#include <iostream>
#define DLL_EXPORT

#include "DLLtest.h"

extern "C"
{
DECLDIR int multiply(int a,int b);
DECLDIR void test(void);
}

DLLtest.h

#ifndef _DLL_TEST
#define _DLL_TEST
#include <iostream>

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C" {
	DECLDIR void test(void)
	{
	std::cout << "test" << std::endl;
	}

	DECLDIR int multiply( int a, int b )
	{
      return a*b;
	}
}

#endif

python dll call

import ctypes
from ctypes import windll

windll = ctypes.windll.LoadLibrary("DLLtest.dll")

print windll.multiply(10,20)
print windll.test()

error:

print windll.multiply(10,20)
ValueError: Procedure probably called with too many arguments (8 bytes in excess)

greetz
rootWOLF

While I’ve not used ctypes on Windows, I know that with .so (Linux) and .dylib (Mac) libraries, you need to need to explicitly set up function prototypes to ensure proper conversion. I’m gonna go out on a limb and guess that Python is sending your arguments as int64 rather than int32.

Try setting up the function before calling it:

mylib = ctypes.windll.LoadLibrary("DLLtest.dll")
mylib.multiply.argtypes = [c_int, c_int]
mylib.multiply.restype = c_int

(It’s probably also a good idea to name your library something besides windll, so you don’t hide ctypes.windll)

thank you,

import ctypes
from ctypes import windll, c_int

testDLL = ctypes.windll.LoadLibrary("DLLtest.dll")
testDLL.multiply.argtypes = [c_int, c_int] 
testDLL.multiply.restype  = c_int

print testDLL.multiply(10,20)

still the same error.

do you have a clue how to convert a int64 into a int32?

i tried this but without a different result.
(and i do think this doesnt make any sense)

a = 10
a = ctypes.c_uint32(a).value

i got it working.

import ctypes
from ctypes import *

testDLL = ctypes.CDLL("DLLtest.dll")
print testDLL.multiply(20,10)

Well you can use swig or boost.python or the-thing-panda-uses-too-and-i-currently-remember-its-name (EDIT: its called interrogat). Of course you can do everything using ctypes but i guess the “fencier” stuff is easier done with on of these bindings. I.e. for boost.python the “hardest” thing is to download and compile boost itself (which can be done by a simple bootstrap + bjam). The usage then is very easy: short examples spieleprogrammierer.de/index … tID=185452 .

cool thx!
i will take a look into after a short nap.