python弹出提示框 ctypes_神奇的Python ctypes模块

本文介绍如何利用Python的ctypes模块调用C/C++编写的DLL文件中的函数及变量,包括加载系统DLL文件、自定义DLL文件、定义结构体、数组、指针、字符串及函数指针的方法。

利用python的ctypes模块可以在python中调用c/c++写的代码。

但是c/c++写的代码要编译成dll文件,在dll中导出你想在python中调用的函数或变量。

这里只是个人的一个简单总结。

1. 加载Windows系统自带的dll文件:

#加载cdecl调用约定的dll

msvcrt =cdll.msvcrt

#加载stdcall调用约定的dll

kernel32 =windll.kernel32

2. 加载自己dll文件,假如为addFuncDll,方式如下:

mydll =CDLL("addFuncDll.dll")

或者 mydll = cdll.addFuncDll

如果其中有函数add,计算两个整数的和,则使用方式如下:

result=mydll.add(4,5)

可以多一步指明add函数的参数类型(也可不指明):

mydll.add.argtypes= [c_int,c_int]

3. 结构体在python中定义为Structure的子类如下:

class POINT(Structure):

_fields_ = [("x", c_int),

("y",c_int)]

_fields中每一项为元组(成员名称,类型)

结构体还可以用于其他的结构体:

class RECT(Structure):

_fields_ = [("upperleft",POINT),

("lowerright",POINT)]

4. 数组定义

一维数组:

ARRAY = (c_int *4) #包含4个整数的数组类型

a = ARRAY() #声明包含4个整数的数组,初值均为0

二维数组:

ARRAY2D = (c_int *4)*5 #4×5的数组类型

a = ARRAY2D() #声明4*5的数组,初值均为0,要两个for循环遍历

5. 指针

函数byref()和pointer()可以获取变量对应的指针

使用byref()更快,而pointer()会构造一个新的指针对象

6.  字符串

#第一种方式:c_char_p

s = "Hello,World"

c_s = c_char_p(s)   #改变c_s.value不会改变原字符串s

# 第二种方式:create_string_buffer,字符串以空字符结尾

p =create_string_buffer("Hello")

print sizeof(p),repr(p.raw)          #输出 6 'Hello\x00'

p =create_string_buffer("Hello", 10)

print sizeof(p),repr(p.raw),p.value   #输出 10 'Hello\x00\x00\x00\x00\x00' Hello

7. 有时碰到返回值或参数为函数指针,在ctypes中声明该种类型有两种方式:WINFUNCTYPE和CFUNCTYPE

Windows函数可以这样:

prototype =WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)

即返回值类型为c_int,其他参数类型依次为HWND, LPCSTR, LPCSTR, UINT

调用MessageBox函数如下:

fromctypes.wintypesimportHWND, LPCSTR, UINT

prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)

paramflags = (1,"hwnd",0), (1,"text","Hi"), (1,"caption",None), (1,"flags",0)

MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

MessageBox()

MessageBox(text="Spam, spam, spam")

MessageBox(flags=2, text="foo bar")

自己定义的函数可以这样,以上面提到的add函数为例:

ADDFUNC = CFUNCTYPE(c_int, c_int, c_int)

即返回值类型为c_int,其他参数类型依次为c_int,c_int

8. ctypes.wintypes 其定义了许多Windows API常用的数据类型,如HWND, LPCSTR, UINT等

stellaechen@STELLAECHEN-MC1 Python-2.7.13 % make ./python.exe -E -S -m sysconfig --generate-posix-vars ;\ if test $? -ne 0 ; then \ echo "generate-posix-vars failed" ; \ rm -f ./pybuilddir.txt ; \ exit 1 ; \ fi running build running build_ext building '_Qt' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.o -Wno-deprecated-declarations error: unable to open output file 'build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Mac/Modules/qt/_Qtmodule.o': 'Operation not permitted' 1 error generated. building '_tkinter' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DWITH_APPINIT=1 -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders -I/System/Library/Frameworks/Tk.framework/Headers -I/System/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders -I/usr/X11R6/include -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.o -framework Tk clang: warning: -framework Tk: 'linker' input unused [-Wunused-command-line-argument] In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_tkinter.c:71: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/tk.h:86:11: fatal error: 'X11/Xlib.h' file not found 86 | # include <X11/Xlib.h> | ^~~~~~~~~~~~ 1 error generated. building '_ctypes' extension gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I_ctypes/darwin -I/Users/stellaechen/download/Python-2.7.13/Mac/Include -I/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include -I/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/powerpc -I. -IInclude -I./Include -I/usr/local/include -I/Users/stellaechen/download/Python-2.7.13/Include -I/Users/stellaechen/download/Python-2.7.13 -c /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c -o build/temp.macosx-15.4-arm64-2.7/Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.o -DMACOSX In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:65:3: error: "Unsupported MacOS X CPU type" 65 | # error "Unsupported MacOS X CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:73: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffitarget.h:12:2: error: "Unsupported CPU type" 12 | #error "Unsupported CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:74: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/fficonfig.h:51:2: error: "Unknown CPU type" 51 | #error "Unknown CPU type" | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:113: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:171:5: error: unknown type name 'ffi_abi' 171 | ffi_abi abi; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:193:2: error: unknown type name 'ffi_sarg' 193 | ffi_sarg sint; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:194:2: error: unknown type name 'ffi_arg' 194 | ffi_arg uint; | ^ /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/libffi_osx/include/ffi.h:307:11: error: unknown type name 'ffi_abi' 307 | ffi_abi abi, | ^ In file included from /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/_ctypes.c:129: /Users/stellaechen/download/Python-2.7.13/Modules/_ctypes/ctypes.h:99:5: error: unknown type name 'ffi_closure' 99 | ffi_closure *pcl_write; /* the C callable, writeable */ | ^ 8 errors generated. Python build finished, but the necessary bits to build these modules were not found: _bsddb _curses _curses_panel _sqlite3 _ssl bsddb185 bz2 dbm dl gdbm imageop linuxaudiodev nis ossaudiodev readline spwd sunaudiodev zlib To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _ctypes _Qt _tkinter running build_scripts stellaechen@STELLAECHEN-MC1 Python-2.7.13 % make install /usr/bin/install -c python.exe /usr/local/bin/python2.7 install: /usr/local/bin/INS@20xsco: Permission denied make: *** [altbininstall] Error 71 stellaechen@STELLAECHEN-MC1 Python-2.7.13 %
07-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值