very simple shared dll with ctypes

mclaugb mclaugb at nospm.yahoo.com
Tue Jun 19 09:19:43 EDT 2007


Assigining restype and argtypes works!

"Thomas Heller" <theller at ctypes.org> wrote in message 
news:mailman.9228.1182256599.32031.python-list at python.org...
> mclaugb schrieb:
>> 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:
>>
>>>>from ctypes import *
>>>>print cdll.small_dll.adder(c_double(5.343534),c_double(3.4432))
>>>>2223968
>>
>> 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
> 





More information about the Python-list mailing list