ctype模块提供了三种不同的方式加载dll:cdll(),windll()和oledll(),在windows下,可用动态链接库msvcrt.dll为我们提供运行时c库,位于C:\WINDOWS\System32\下,在Linux下为libc.so.6
1 from ctypes import * 2 msvcrt=cdll.msvcrt 3 message_string="Hello world!\n" 4 msvcrt.printf("Testing: %s",message_string)
模拟C语言的union,可写为:
1 #!/usr/bin/env python 2 from ctypes import * 3 class barley_amount(Union): 4 _fields_=[ 5 ("barley_long",c_long), 6 ("barley_int",c_int), 7 ("barley_char",c_char * 8), 8 ] 9 value=raw_input("Enter the amount of barley to put into the beer vat:") 10 my_barley=barley_amount(int(value)) 11 print "Barley amount as a long: %ld" % my_barley.barley_long 12 print "Barley amount as an int: %d" % my_barley.barley_int 13 print "Barley amount as a char: %s" % my_barley.barley_char