私はこれを終えることができました。ここに私のテストコードですと、この問題への完了を引き起こす:
マイtest.cのコード:
#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]);
}
}
私はCの関数をテストするためのメインを作成しました。ここでは、機能テストの出力があります:
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
それから私は、共有用にコンパイルされ、それは、Pythonから使用することができます
gcc -shared -o test.so test.c
そして、ここでは、私は私のPythonのコードのために使用したものです:
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])
そして出力:
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
作品!
私の唯一の問題は、出力のサイズに基づいてc_ubyte配列を動的にサイズ変更することにあります。私はそれについて別の質問を投稿しました。
あなたのお役に立てありがとうございますKyss!
Kyss、これは非常に近いです。正しい方向に押してくれてありがとう。私は以下の答えを投稿します。 – CharlieY