Well, after helping Kyss, I was able to finish this. Here is my test code and the result to complete this problem:
My test.c code is:
#include <stdio.h> int test(unsigned char *test, int size){ int i; for(i=0;i<size;i++){ printf("item %d in test = %d\n",i, test[i]); } } int testout(unsigned char *test, int *size){ test[2]=237; test[3]=12; test[4]=222; *size = 5; } main () { test("hello", 5); unsigned char hello[] = "hi"; int size=0; int i; testout(hello,&size); for(i=0;i<size;i++){ printf("item %d in hello = %d\n",i, hello[i]); } }
I created the main part for testing my c-function. Here's the output of the function test:
item 0 in test = 104 item 1 in test = 101 item 2 in test = 108 item 3 in test = 108 item 4 in test = 111 item 0 in hello = 104 item 1 in hello = 105 item 2 in hello = 237 item 3 in hello = 12 item 4 in hello = 222
Then I compiled for sharing, so it could be used from python:
gcc -shared -o test.so test.c
And here is what I used for my Python code:
from ctypes import * lib = "test.so" dll = cdll.LoadLibrary(lib) testfunc = dll.test print "Testing pointer input" size = c_int(5) param1 = (c_byte * 5)() param1[3] = 235 dll.test(param1, size) print "Testing pointer output" dll.testout.argtypes = [POINTER(c_ubyte), POINTER(c_int)] sizeout = c_int(0) mem = (c_ubyte * 20)() dll.testout(mem, byref(sizeout)) print "Sizeout = " + str(sizeout.value) for i in range(0,sizeout.value): print "Item " + str(i) + " = " + str(mem[i])
And the conclusion:
Testing pointer input item 0 in test = 0 item 1 in test = 0 item 2 in test = 0 item 3 in test = 235 item 4 in test = 0 Testing pointer output Sizeout = 5 Item 0 = 0 Item 1 = 0 Item 2 = 237 Item 3 = 12 Item 4 = 222
Work!
My only problem now is with dynamically resizing the c_ubyte array based on the size of the output. However, I posted a separate question.
Thanks for your help Kyss!