python如何调用dll库


当前第2页 返回上一页

例子:
Python代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

def CreateGUID():       

"""     

创建一个全局唯一标识符     

类似:E06093E2-699A-4BF2-A325-4F1EADB50E18     

NewVersion     

"""       

try:           

# dll path           

strDllPath = sys.path[0] + str(os.sep) + "createguid.dll"           

dll = CDLL(strDllPath)           

b = dll.newGUID()           

a = c_char_p(b)       

except Exception, error:           

print error           

return ""       

return a.value

例子2:
这个例子是调用kernel32.dll中的createprocessA函数来启动一个记事本进程。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

from ctypes import *         

# 定义_PROCESS_INFORMATION结构体   

class _PROCESS_INFORMATION(Structure):       

_fields_ = [('hProcess', c_void_p),                   

('hThread', c_void_p),                   

('dwProcessId', c_ulong),                   

('dwThreadId', c_ulong)]        

# 定义_STARTUPINFO结构体   

class _STARTUPINFO(Structure):       

_fields_ = [('cb',c_ulong),                   

('lpReserved', c_char_p),                   

('lpDesktop', c_char_p),                   

('lpTitle', c_char_p),                   

('dwX', c_ulong),                   

('dwY', c_ulong),                   

('dwXSize', c_ulong),                   

('dwYSize', c_ulong),                   

('dwXCountChars', c_ulong),                   

('dwYCountChars', c_ulong),                   

('dwFillAttribute', c_ulong),                   

('dwFlags', c_ulong),                   

('wShowWindow', c_ushort),                   

('cbReserved2', c_ushort),                   

('lpReserved2', c_char_p),                   

('hStdInput', c_ulong),                   

('hStdOutput', c_ulong),                   

('hStdError', c_ulong)]        

NORMAL_PRIORITY_CLASS = 0x00000020 #定义NORMAL_PRIORITY_CLASS   

kernel32 = windll.LoadLibrary("kernel32.dll")  #加载kernel32.dll   

CreateProcess = kernel32.CreateProcessA   #获得CreateProcess函数地址   

ReadProcessMemory = kernel32.ReadProcessMemory #获得ReadProcessMemory函数地址   

WriteProcessMemory = kernel32.WriteProcessMemory #获得WriteProcessMemory函数地址   

TerminateProcess = kernel32.TerminateProcess         # 声明结构体   

ProcessInfo = _PROCESS_INFORMATION()   

StartupInfo = _STARTUPINFO()   

fileName = 'c:/windows/notepad.exe'       # 要进行修改的文件   

address = 0x0040103c        # 要修改的内存地址   

strbuf = c_char_p("_")        # 缓冲区地址   

bytesRead = c_ulong(0)       # 读入的字节数   

bufferSize =  len(strbuf.value)     # 缓冲区大小        

# 创建进程    

CreateProcess(fileName, 0, 0, 0, 0, NORMAL_PRIORITY_CLASS,0, 0, byref(StartupInfo), byref(ProcessInfo))

以上就是python如何调用dll库的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

py文件怎么执行

Python能在苹果手机上运行吗

Python3中时间处理与定时任务的方法介绍(附代码)

Python中reverse、sort、sorted三个列表排序使用方法详解

分享一下Python数据分析常用的8款工具

windows上使用Python增加或删除权限的方法

Python单引号和双引号、三引号的区别是什么

如何用Python代码温度转换?

Python爬虫基础之网页组成解析

Python之变量的学习介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...