1

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. Python 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.

3
  • Shouldn't you use raw_input instead of input? Commented Sep 16, 2011 at 8:43
  • 2
    I am working on python version 3.2. raw_input() is replaced by input(). Commented Sep 16, 2011 at 9:00
  • Ah. Good to know. Thanks. Sorry I didn't know that. Commented Sep 16, 2011 at 9:09

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

By the way I am using ctypes because of the fact that I am accessing a C++ dll from Python. I tried your way and I am not getting any errors, but the Create function is not being executed because ID now has a value(66) instead of a character('B'). Since my C++ function accepts a character, what would you suggest?
Very helpful information :). Just another question in the same context, how do we do the same for a string of characters? Thanks again for your answer. It works now.
Thanks a lot. +1 from my side :). You have been really helpful.
0

Your Create function accept a single char as argument, not a string.

s = input("enter ID:")
c = ctypes.c_char(s[0])
Create(c)

3 Comments

Yes certainly. But I just input one character as an input. Does it read as a string? If so, what is the equivalent command so that it could read a single character?
I think the ID name implies it is some sort of numeric value converted as a char, then you should use s = int(input("Enter ID")) c = ctypes.c_char(chr(s)) Edit: Formatting is a bitch
@ Leo: No, it is a single character input and not a numeric value. Something like 'a', 'b' etc. @MatthieuW: It did not work the way you suggested via the code. Same error again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.