あまりに汚さ過ぎるけど、とりあえず動作概要は把握できたかな。
[test.py]
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4
5 import ctypes
6
7 lpcell = ctypes.POINTER("cell")
8 class cell(ctypes.Structure):
9 _fields_ = [("name", ctypes.c_char_p),
10 ("next", lpcell)]
11 ctypes.SetPointerType(lpcell, cell)
12
13 def main():
14 lib = ctypes.CDLL('./libsample.dylib')
15 mfunc = lib.set_cell_name
16 c1 = cell()
17 mfunc(ctypes.pointer(c1))
18 print c1.name
19
20 #-------------------------------------------------
21 if __name__ == "__main__":
22 main()
[sample.c]
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 static int cnt = 0;
6 const char buf[256] = "test.name";
7
8 struct cell {
9 char *name;
10 struct cell *next;
11 };
12
13 struct cell *
14 set_cell_name (struct cell *c)
15 {
16 c->name = malloc(strlen(buf)+2);
17 if (NULL == c->name)
18 return c;
19 memset(c->name, 0, strlen(buf)+2);
20 snprintf(c->name, strlen(buf)+1, "%s%d", buf, cnt++);
21 return c;
22 }
0 件のコメント:
コメントを投稿