I have been trying to learn ctypes so as I can call C++ DLLs in python and have no problem on Loading DLLs. My question are: What are procedures of calling functions in DLLs especially since Py and C++ have different data types? I see argtypes and restypes: What are they?

Can someone provide very simple theoretical example from Loading DLL until releasing it? Here is my code for Loading the DLL :

import ctypes as ct
#load DLL
dll = ct.cdll.test_dll
print dll

result: <CDLL 'test_dll', handle 6eb40000 at 2914f30>

Recommended Answers

All 10 Replies

Here is an example ...

# using the module ctypes you can access DLLs written in C/C++
# ctypes converts between Python types and C types
# there are two ways to write DLLs:
# most common uses cdecl, import with cdll
# Win and VB may use stdcall, import with windll
# module ctypes is built into Python25 and higher
# look under ctypes in the Python manual

from ctypes import *

# as an example let's use msvcrt.dll
# this is the MS C library containing many standard C functions
# import the C function strchr() from msvcrt.dll
strchr = cdll.msvcrt.strchr

# declare the return type ...
# a 'pointer to a string' (which then converts to a Python string)
strchr.restype = c_char_p

# these are the argument types ...
# Python string converts to 'pointer to a string' --> c_char_p
# Python single char string converts to C char --> c_char
strchr.argtypes = [c_char_p, c_char]

# now call the function and show the result
print strchr('abcdefg', 'd')  # result --> defg

Thanks vega, I see now how easy it can be. Can you give few words on how pointers to result and rguments work.
Thanks for brilliant example

I read somewhere an article on ctypes and made life easier.
However, I still don't know where to use pointers and where to use other ctypes variables like c_char, c_int etc. Anyone to help?

Thanks vega, I see now how easy it can be. Can you give few words on how pointers to result and rguments work.
Thanks for brilliant example

Well, the only somewhat confusing thing for Python folks would be how C handles strings. It points to the first character of the string and then ends the string with a NULL character. So, if you move the pointer one notch up with a pointer++ each time, you get all the characters of the string and you stop at the NULL. A single character is still a string in Python, but not necessarily with C. Also C has several types of integers. All this came from the earlier days of computing when memory was expensive.

It's been twenty years since I have done any serious C programming, so my brain is rather lame on the subject. You need to ask experts like Narue.

Thanks for your contribution so far vega, it count alot!

Here are the data types in C -- the actual size of the data is not specified by the language because it may be different on each platform. You have to look in limits.h header file to find out the minimum and maximum values that any given data type can hold.

Here are the data types, and most common size for 32-bit C compilers on MS-Windows and *nix. I believe they are different for both 16-bit and 64-bit compilers.

ctype              size in bytes
char                     1
short                    2
int                        4
long                     4
float                     4
double                 8

A string is just an array of characters (char).

Each of the data types above can be either signed or unsigned, but the size is the same, only the minimum and maximum values change between signed and unsigned.

Here is A little complex but good example (I don't know if it is right to copy here)
http://code.activestate.com/recipes/499341/
an someone explain it to a layman programmer level?
I can understand few but as I read it I got Lost in the world of codes

Help! Help! developers!

Ok After finding a DLL that can be having simple functions, I found Bass.DLL, and here is sample method for Loading Audio files. Can someone give me a start. I cannot find the ctypes variable equivalent of BOOL, QWORD etc. And also how to form a python function from this C/C++ code??

""" Loads a WAV, AIFF, MP3, MP2, MP1, OGG or plugin supported sample.
     HSAMPLE BASS_SampleLoad(
     BOOL mem,
         void *file,
         QWORD offset,
         DWORD length,
         DWORD max,
         DWORD flags
     );  """
            
    Bass_Load = ct.CFUNCTYPE(ct.DEFAULT_MODE) #Load function

I have failed to re-write the function in python because I don't know how to convert QWORD offset, and BOOLto ctypes. Someone help me please!

Anyone who knows QWORD, DWORD and BOOL ctypes Equivalents. I find c_int, c_float etc but not the Equivalent above!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.