ctypes使用说明

1, 首先确定你的python支持不支持ctypes 

python2.7以后ctypes已经是标配了,2.4以后的版本得自己装下ctypes

2,加载动态库 

     两种加载方式

     >>> from ctypes import *
     >>> libc = cdll . LoadLibrary ( "libc.so.6" )
     >>> libc.printf("%d",2)
     >>> from ctypes import *
     >>> libc = CDLL ( "libc.so.6" )
     >>> libc.printf("%d",2)
 

3, 调用系统函数 

   上面的例子已经调用了系统函数printf,这里再给几个其他例子

     >>> from ctypes import *
     >>> libc = CDLL ( "libc.so.6" )
     >>> print libc . time ( None )
     1308019893
     >>> print libc.atoi("234")
     234
 

4,ctypes 数据类型和 C数据类型 对照表 
ctypes typeC typePython type
c_bool_Boolbool (1)
c_charchar1-character string
c_wcharwchar_t1-character unicode string
c_bytecharint/long
c_ubyteunsigned charint/long
c_shortshortint/long
c_ushortunsigned shortint/long
c_intintint/long
c_uintunsigned intint/long
c_longlongint/long
c_ulongunsigned longint/long
c_longlong__int64 or long longint/long
c_ulonglongunsigned __int64 or unsigned long longint/long
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar * (NUL terminated)string or None
c_wchar_pwchar_t * (NUL terminated)unicode or None
c_void_pvoid *int/long or None



这些数据都可以用一个默认值进行创建

>>> c_int()
c_long(0)
>>> c_char_p("Hello, World")
c_char_p('Hello, World')
>>> c_ushort(-3)
c_ushort(65533)
>>>
 

这些数据也可以被改变:

>>> i = c_int(42)
>>> print i
c_long(42)
>>> print i.value
42
>>> i.value = -99
>>> print i.value
-99
>>>
 

赋值给 c_char_p,c_wchar_p,c_void_p 

只改变他们指向的内存地址,而不是改变内存的内容


>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> c_s.value = "Hi, there"
>>> print c_s
c_char_p('Hi, there')
>>> print s                 # first string is unchanged
Hello, World
>>>
 

如果需要可改变内容的字符串,需要使用 create_string_buffer()


>>> from ctypes import *
>>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
>>> print sizeof(p), repr(p.raw)
3 '/x00/x00/x00'
>>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
>>> print sizeof(p), repr(p.raw)
6 'Hello/x00'
>>> print repr(p.value)
'Hello'
>>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
>>> print sizeof(p), repr(p.raw)
10 'Hello/x00/x00/x00/x00/x00'
>>> p.value = "Hi"
>>> print sizeof(p), repr(p.raw)
10 'Hi/x00lo/x00/x00/x00/x00/x00'
>>>
 

5,函数返回类型 

函数默认返回 C int 类型,如果需要返回其他类型,需要设置函数的 restype 属性

>>> strchr = libc.strchr
>>> strchr("abcdef", ord("d")) # doctest: +SKIP
8059983
>>> strchr.restype = c_char_p # c_char_p is a pointer to a string
>>> strchr("abcdef", ord("d"))
'def'
>>> print strchr("abcdef", ord("x"))
None
>>>
 

6,传递指针或者引用 

很多情况下 C 函数需要传递指针或者引用,ctypes也完美的支持这一点
byref() 用来传递引用参数,pointer() 函数也可以完成同样的工作,但pointer()会创建一个实际的指针对象,如果你不需要一个指针对象,
用byref()会快很多

>>> i = c_int()
>>> f = c_float()
>>> s = create_string_buffer('/000' * 32)
>>> print i.value, f.value, repr(s.value)
0 0.0 ''
>>> libc.sscanf("1 3.14 Hello", "%d %f %s",... byref(i), byref(f), s)
3
>>> print i.value, f.value, repr(s.value)
1 3.1400001049 'Hello'
>>>
 

7,结构体和联合 
结构体和联合必须从 Structure 和 Union 继承,子类必须定义 
_fields_ 属性,_fields_ 属性必须是一个2元组的列表,
包括一个field名字和field的类型
field类型 必须是一个ctypes的类型例如 c_int, 或者其他继承自ctypes的类型,结构体,联合,数组,指针。

下面的例子演示一个 POINT结构体,包括 field  X,Y

>>> from ctypes import *
>>> class POINT(Structure):. 
    _fields_ = [("x", c_int),
                ("y", c_int)]

>>> point = POINT(10, 20)
>>> print point.x, point.y
10 20
>>> point = POINT(y=5)
>>> print point.x, point.y
0 5
>>> POINT(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1
, in ?
ValueError
: too many initializers
>>>
 

一个复杂点的例子,field类型也是一个结构体

>>> class RECT(Structure):
...     _fields_ = [("upperleft", POINT),
...                 ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print rc.upperleft.x, rc.upperleft.y
0 5
>>> print rc.lowerright.x, rc.lowerright.y
0 0
>>>
 

多种方式进行初始化
>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))
 

8,数组 

数组定义很简单

定义一个有10个POINT元素的数组

TenPointsArrayType = POINT * 10 

初始化和使用数组:

>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print ii
<c_long_Array_10 object at 0x...>
>>> for i in ii: print i,
...
1 2 3 4 5 6 7 8 9 10
>>> 



9,指针 
pointer() 函数可以创建一个指针

Pointer实例有一个 contents属性 返回指针指向的对象

>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>> pi.contents
c_long(42) 


可以改变指针指向的内容

>>> i = c_int(99)
>>> pi.contents = i
>>> pi.contents
c_long(99)
>>> 


可以按数组方式访问:

>>> pi[0]
99
>>> 


按数组方式改变值
>>> print i
c_long(99)
>>> pi[0] = 22
>>> print i
c_long(22)
>>> 


以上都是ctypes的基本用法,对普通的开发人员来说,基本够用了

更详细的说明请参考:http://docs.python.org/library/ctypes.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MathMLControl.DLL是一个用于在Windows系统中显示数学公式的动态链接文件。它可以通过调用Windows API函数来实现数学公式的显示和渲染。 以下是一个简单的使用MathMLControl.DLL显示数学公式的例子: 1. 首先,你需要将MathMLControl.DLL文件复制到你的Windows系统目录下,例如:C:\Windows\System32\。 2. 在Python中,你可以使用ctypes模块来调用MathMLControl.DLL。为此,你需要先导入ctypes模块,并使用ctypes.cdll.LoadLibrary()函数加载MathMLControl.DLL文件,如下所示: ``` import ctypes mathml = ctypes.cdll.LoadLibrary("MathMLControl.dll") ``` 3. 接下来,你需要定义一个窗口来显示数学公式。可以使用Windows API函数CreateWindowEx()来创建一个窗口,如下所示: ``` import win32api import win32con hwnd = win32api.CreateWindowEx(0, "STATIC", "", win32con.WS_CHILD | win32con.WS_VISIBLE, 0, 0, 300, 300, parent_hwnd, None, None, None) ``` 其中,parent_hwnd是父窗口的句柄。如果你想在一个单独的窗口中显示数学公式,可以将parent_hwnd设置为0,表示没有父窗口。 4. 最后,你可以使用MathMLControl.DLL提供的函数来显示数学公式。例如,你可以调用MathMLControl.DLL中的MathMLControl_SetMathML()函数来设置要显示的数学公式,如下所示: ``` mathml.MathMLControl_SetMathML(hwnd, "<math><mrow><mi>x</mi><mo>+</mo><mi>y</mi></mrow></math>") ``` 其中,第一个参数hwnd是窗口句柄,第二个参数是要显示的数学公式的MathML代码。 以上就是一个简单的使用MathMLControl.DLL显示数学公式的例子。当然,具体的使用方法还需要根据你的具体需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值