Q:

Trouble with CTypes - calling C dll function

I have the following C function exported from a dll
typedef struct _TStubMethod
{
    TBoolean stubAtEnd;
    TBoolean longStub;
} TStubMethod;

int JpmcdsStringToStubMethod
(char        *name,     
 TStubMethod *stubMethod 
);

In Python, I have defined the type & function and called the function as follows:
class TStubMethod(Structure):
    _fields_ = [
        ('stubAtEnd', c_int),
        ('longStub', c_int)
    ]

def JpmcdsStringToStubMethod(dll, name, stubmethod):
    func = dll.JpmcdsStringToStubMethod
    func.argtypes = [POINTER(c_char), POINTER(TStubMethod)]
    func.restype = c_int
    return func(name, stubmethod)

stubFS = TStubMethod(False, False)
ret = JpmcdsStringToStubMethod(dll, 'F/S', byref(stubFS))

I am getting an error as follows. What am I doing wrong? 
ArgumentError                             Traceback (most recent call last)
<ipython-input-61-ec7303283f89> in <module>
     25 
     26 stubFS = TStubMethod(False, False)
---> 27 ret = JpmcdsStringToStubMethod(dll, 'F/S', byref(stubFS))

<ipython-input-55-c8ab0da16b04> in JpmcdsStringToStubMethod(dll, name, stubmethod)
      9     func.argtypes = [POINTER(c_char), POINTER(TStubMethod)]
     10     func.restype = c_int
---> 11     return func(name, stubmethod)

ArgumentError: argument 1: <class 'TypeError'>: wrong type

Thanks for your help.

A:

When passing zero-terminated strings, use c_char_p. According to [Python 3]: class ctypes.c_char_p:

Represents the C char * datatype when it points to a zero-terminated string.

Translated to your code:

Change the function argtypes (1st one):
func.argtypes = [c_char_p, POINTER(TStubMethod)]

Use bytes (which is Python's equivalent for char*). You can either (not both):

Converting string to bytes (to keep changes in JpmcdsStringToStubMethod):
return func(name.encode(), stubmethod)

Pass a bytes instance to JpmcdsStringToStubMethod:
ret = JpmcdsStringToStubMethod(dll, b"F/S", byref(stubFS))

