More Related Content
PDF
パワーポイントの品質と生産性を向上させるデザイン・テンプレート PPTX
世界一わかりやすいClean Architecture PPTX
PDF
PDF
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ PDF
C# ゲームプログラミングはホントにメモリのことに無頓着でいいの? PDF
PDF
What's hot
PDF
PDF
しょぼいプレゼンをパワポのせいにするな! by @jessedee PDF
インフラエンジニアの綺麗で優しい手順書の書き方 PDF
PPTX
新卒3年目のぼくが、でぶおぷす???なインフラおじさん方にAnsibleを導入してみた PDF
45分間で「ユーザー中心のものづくり」ができるまで詰め込む PDF
PPTX
PPTX
PDF
PPTX
プレゼン用 きれいでわかりやすいパワーポイントを作る方法 PDF
PDF
PPTX
PDF
Building the Game Server both API and Realtime via c# PPTX
チャットコミュニケーションの問題と心理的安全性の課題 #EOF2019 PPTX
PDF
PPTX
境界付けられたコンテキスト 概念編 (ドメイン駆動設計用語解説シリーズ) PDF
パワポは「最後」に開く-すぐできる!プレゼン資料作成術「大掃除編」 Viewers also liked
ODP
C Types - Extending Python KEY
PDF
戦国時代を生きた「黒田官兵衛」とWeb時代を生きる「エンジニア」 PDF
PDF
Pythonで画像処理をやってみよう! 第1回 - ヒストグラムと濃度変換 - PDF
More from Moriyoshi Koizumi
PDF
PPTX
PPTX
Authentication, Authorization, OAuth, OpenID Connect and Pyramid PDF
All I know about rsc.io/c2go PPTX
PPTX
Pyramidのrendererをカスタマイズする PPTX
Hacking Go Compiler Internals / GoCon 2014 Autumn PDF
PDF
PDF
PDF
PDF
PDF
PDF
PHP language update 201211 PDF
PDF
PDF
PDF
PDF
PDF
ctypes拡張モジュール
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Show me whatit looks like!
import ctypes
# 標準Cライブラリを読み込む
dll = ctypes.CDLL("/usr/lib/libSystem.B.dylib")
# ライブラリの関数を呼び出す
dll.printf("Hello, %s world!n", "bucho")
- 12.
- 13.
- 14.
値ラッパー
import ctypes
dll =ctypes.CDLL("/usr/lib/libSystem.B.dylib")
dll.printf("%gn", 3.14)
Traceback (most recent call last):
File <stdin>, line 3, in <module>
dll.printf("%gn", 3.14)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>:
Don't know how to convert parameter 2
実行結果
- 15.
- 16.
- 17.
値ラッパー
値ラッパークラス 対応するCの型
c_byte, c_ubyte,c_short,
c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong,
c_ulonglong
char, unsigned char, short,
unsigned short, int,
unsigned int, long, unsigned
long, long long, unsigned
long long
c_foat, c_double,
c_longdouble
foat, double, long double
c_char_p, c_wchar_p char *, wchar_t*
c_void_p void *
など
- 18.
もうちょっと複雑な例
import ctypes
# 標準Cライブラリを読み込む
dll= ctypes.CDLL("/usr/lib/libSystem.B.dylib")
# ライブラリの関数を呼び出す
dll.getcwd.restype = ctypes.c_char_p
print dll.getcwd() # 現在の作業ディレクトリ
dll.sqrt.restype = ctypes.c_double
dll.sqrt.argtypes = (ctypes.c_double, )
print dll.sqrt(16) # 16の平方根
- 19.
- 20.
- 21.
- 22.
- 23.
ctypes.POINTER
import ctypes
c_int_p =ctypes.POINTER(ctypes.c_int)
dll =
ctypes.CDLL("/usr/lib/libSystem.B.dylib")
retval = ctypes.c_int()
dll.scanf("%d", c_int_p(retval))
print retval
※このケースだと、ctypes.byref() 使った方が早いです。
- 24.
- 25.
構造体
struct tm {
inttm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
char *tm_zone;
long tm_gmtoff;
};
class TMStructure(ctypes.Structure):
_fields_ = [
('tm_sec', ctypes.c_int),
('tm_min', ctypes.c_int),
('tm_hour', ctypes.c_int),
('tm_mday', ctypes.c_int),
('tm_mon', ctypes.c_int),
('tm_year', ctypes.c_int),
('tm_wday', ctypes.c_int),
('tm_yday', ctypes.c_int),
('tm_isdst', ctypes.c_int),
('tm_zone', ctypes.c_char_p),
('tm_gmtoff', ctypes.c_long)]
- 26.
- 27.
- 28.
- 29.
- 30.
まとめ
● dll =ctypes.CDLL()で読み込み
● dll.[関数名]() で関数を呼び出す
● 関数のシグニチャを指定したい場合はrestypeと
argtypes
● ラッパー型 c_*
● ポインタ渡しは ctypes.byref()
● 構造体は ctypes.Structure
● コールバック関数は ctypes.CFUNCTYPE()
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.