2

Following is the C function para_trans_test.

void para_trans_test(char x [] [100])
{
    strncpy(x[0],"zzz",100);
}

Following is the python code which doesnt work.

lib.para_trans_test.argtypes= [ctypes.POINTER(ctypes.c_char_p)]
numParams=2
L=numpy.array(['xxx','yyy'])
print(type(L))
py_s1 = (ctypes.c_char_p * numParams) ()
py_s1[:]=L
print("py_s1=",py_s1)
lib.para_trans_test(py_s1)
print(py_s1)

Initially array L is ('xxx','yyy').

After calling the C function para_trans_test I want array L to be ('zzz','yyy')

1
  • 1
    which doesn't work how? Commented Aug 24, 2017 at 5:00

1 Answer 1

3

The argument type is wrong. POINTER(c_char_p) is equivalent to char**. What is needed is a pointer to a c_char array:

Test DLL:

#include <string.h>
__declspec(dllexport) void para_trans_test(char x [] [100])
{
    strncpy(x[0],"zzz",100);
}

Python:

from ctypes import *
lib = CDLL('test')
lib.para_trans_test.argtypes = [POINTER(c_char * 100)]
py_s1 = (c_char * 100 * 2)()
py_s1[0].value = b'xxx'
py_s1[1].value = b'yyy'
print(py_s1[0].value,py_s1[1].value)
lib.para_trans_test(py_s1)
print(py_s1[0].value,py_s1[1].value)

Output:

b'xxx' b'yyy'
b'zzz' b'yyy'
Sign up to request clarification or add additional context in comments.

Comments

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.