0

I need run a c++ method from my python program
but the c++ method need a few parameters
what need for call that method in my python code with the paramethers

2
Contributors
1
Reply
11
Views
7 Years
Discussion Span
Last Post by vegaseat
1

Here is a code 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

# some C functions are easier to access ...
printf = cdll.msvcrt.printf
printf("Hello, %s\n", "World!")
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.