欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 系统调用的实例详解

程序员文章站 2024-03-03 15:08:58
python 系统调用的实例详解            &n...

python 系统调用的实例详解

              本文将通过两种方法对python 系统调用进行讲解,包括python使用CreateProcess函数运行其他程序和ctypes模块的实例,

一 python使用CreateProcess函数运行其他程序

>>> import win32process 
>>> handle = win32process.CreateProcess('c:\\windows\\notepad.exe','',None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) 
>>> win32process.TerminateProcess(handle[0],0) 
>>> import win32event 
>>> handle = win32process.CreateProcess('c:\\windows\\notepad.exe','',None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) 
>>> win32event.WaitForSingleObject(handle[0],-1) 
0 
 

二 ctypes模块简介

以下代码是使用ctype模块在windows下直接调用user32.dll中的MessageBoxA函数。

>>> from ctypes import * 
>>> user32 = windll.LoadLibrary('user32.dll') 
>>> user32.MessageBoxA(0,str.encode('Ctype is cool!'),str.encode('Ctype'),0) 
1 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!