passing a single character/multiple character user input from a python function to a C++ function
I have a C++ .dll function that I am accessing through python 3.2 with the syntax:
int Create(char argID)
and I am trying to access this function using python. I can access it, but I am confused on why it is telling for 'wrong type' whenever I try to pass the function an argument. Py开发者_如何学运维thon Script:
Create.argtypes = [c_char]
Create.restype = c_int
ID = c_char
ID = input("Enter ID:")
Create(ID) #ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
Not sure where I am going wrong or is there a different way to pass a character or characters from the user input? Kindly help.
Cheers.
A c_char is a one-byte value, not a Unicode string. Set ID = bytes(ID, 'utf8')[0]
(or use a different 8-bit character mapping than 'utf8', such as 'latin-1'). Then you can call Create(ID)
.
By the way, assigning ID to c_char only gives you another reference to c_char, and then you immediately reassign ID to the returned string from input
. The only time I see this is when people are working with ctypes. It's like the brain gets stuck in static typing mode.
Your Create function accept a single char as argument, not a string.
s = input("enter ID:")
c = ctypes.c_char(s[0])
Create(c)
精彩评论