473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

very simple shared dll with ctypes

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!
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;

}


Jun 19 '07 #1
2 1638
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

Jun 19 '07 #2
Assigining restype and argtypes works!

"Thomas Heller" <th*****@ctypes.orgwrote in message
news:ma***************************************@pyt hon.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

Jun 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom Van Acker | last post by:
Hello, I'm getting overwhelmed by information if I google this question. But it is not clear to me. I have a shared library (.so file), created by some other company we work with. Now I have to...
3
by: Pro Grammer | last post by:
Hello, all, I am not sure if this is the right place to ask, but could you kindly tell me how to "load" a shared object (like libx.so) into python, so that the methods in the .so can be used? That...
1
by: Srijit Kumar Bhadra | last post by:
Hello, Here are code snippets to create and access shared memory in Python with and without ctypes module. With regards, Srijit Filename : SharedMemCreate.py import msvcrt, mmap
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
6
by: Ernesto | last post by:
I'm in the process of moving a Python application from Windows to Linux. This means that the drivers for windows ".dll" now must be Linux drivers "shared library file" (.so I think). With...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
0
by: Egor Zindy | last post by:
Egor Zindy wrote: #!/usr/bin/env python """ A generic chipid library based on ctypes This module handles most of the functions in FTChipID.dll
1
by: Dog Walker | last post by:
I need to call a function in a shared object with this signature: init_dialog(FILE *input, FILE *output) The FILE*'s are to stdin and stdout. The call from python is libdialog.init_dialog( x, y)...
0
by: member thudfoo | last post by:
On 11/8/08, Dog Walker <thudfoo@gmail.comwrote: I should have said "stderr" rather than "stdout". And the answer is: from ctypes import * from ctypes.util import find_library libc =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.