引用动态库

1、引用so库:

from ctypes import *

>>>CDLL("libc.so.6").printf("12345\n")
12345

>>>cdll.LoadLibrary("libc.so.6").printf("12345\n")

12345

2、引用DLL库:

2.1、python2.7.13下的验证

>>>cdll.LoadLibrary("msvcrt.dll").printf("test\n")

test

>>>cdll.LoadLibrary("msvcrt").printf("test\n")

test

>>>cdll.msvcrt.printf("test\n")

test

>>>CDLL("msvcrt").printf("test\n")

test

>>>CDLL("msvcrt.dll").printf("test\n")

test

 

2.2、python3.5下的验证

>>>CDLL("libc.so.6").printf("test\n".encode("utf-8"))

test

>>>cdll.LoadLibrary("msvcrt.dll").printf("test\n".encode("utf-8"))

test

>>>CDLL("msvcrt").printf("test\n".encode("utf-8"))

test

>>>cdll.msvcrt.printf("test\n".encode("utf-8"))

test

说明:cdll.LoadLibrary和CDLL加载动态包效果一样,均可加载linux下的so和windows下的dll,但是windows下的dll后缀可省略,linux下的so后缀不可缺省,

python2可全量指定utf-8编码,python3用的是unicode,要在参数自行定义utf-8编码。

posted @ 2017-08-02 17:51  fosonR  阅读(356)  评论(0编辑  收藏  举报