very simple shared dll with ctypes

M

mclaugb

I have created a simple windows small_dll.dll exporting a function that does
a computation using C and C++ classes which i wish to call from Python. I
have read lots of ctypes documentation but I still dont quite understand how
to call the function.

As a test, I have written a much simpler mathematical function below to try
to get it to work from Python.

I am trying to call this from python by:

Can someone give me a few tips to get going!
The DLL declaration is below.



#include<windows.h>

#if defined(_MSC_VER)

#define DLL extern "C" __declspec(dllexport)

#else

#define DLL

#endif


DLL double adder(double a, double b){

double c;

c=a+b;

return c;

}
 
T

Thomas Heller

mclaugb said:
I have created a simple windows small_dll.dll exporting a function that does
a computation using C and C++ classes which i wish to call from Python. I
have read lots of ctypes documentation but I still dont quite understand how
to call the function.

As a test, I have written a much simpler mathematical function below to try
to get it to work from Python.

I am trying to call this from python by:


Can someone give me a few tips to get going!

You should assign the .restype and the .argtypes attributes to your function.
That gives the the proper result, and you don't have to pack the arguments into
c_double yourself.
The DLL declaration is below.

#include<windows.h>
#if defined(_MSC_VER)
#define DLL extern "C" __declspec(dllexport)
#else
#define DLL
#endif

DLL double adder(double a, double b){
double c;
c=a+b;
return c;
}

from ctypes import *
adder = cdll.small_dll.adder
adder.restype = c_double
adder.argtypes = c_double, c_double

print adder(5.343534, 3.4432)


Thomas
 
M

mclaugb

Assigining restype and argtypes works!

Thomas Heller said:
You should assign the .restype and the .argtypes attributes to your
function.
That gives the the proper result, and you don't have to pack the arguments
into
c_double yourself.


from ctypes import *
adder = cdll.small_dll.adder
adder.restype = c_double
adder.argtypes = c_double, c_double

print adder(5.343534, 3.4432)


Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top