我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用win32process.CreateProcess()。
def execute (cmd, timeout = 0): if timeout == 0: timeout = win32event.INFINITE info = win32process.CreateProcess(None, cmd, None, None, 0, 0, None, None, win32process.STARTUPINFO()) subprocess = info [0] rc = win32event.WaitForSingleObject (subprocess, timeout) if rc == win32event.WAIT_FAILED: return -1 if rc == win32event.WAIT_TIMEOUT: try: win32process.TerminateProcess (subprocess, 0) except pywintypes.error: return -3 return -2 if rc == win32event.WAIT_OBJECT_0: return win32process.GetExitCodeProcess(subprocess)
def timeout_execute (cmd, timeout = 0): if timeout == 0: timeout = win32event.INFINITE info = win32process.CreateProcess(None, cmd, None, None, 0, 0, None, None, win32process.STARTUPINFO()) subprocess = info [0] rc = win32event.WaitForSingleObject (subprocess, timeout) if rc == win32event.WAIT_FAILED: return -1 if rc == win32event.WAIT_TIMEOUT: try: win32process.TerminateProcess (subprocess, 0) except pywintypes.error: return -3 return -2 if rc == win32event.WAIT_OBJECT_0: return win32process.GetExitCodeProcess(subprocess)
def create_desktop(desktop_name, start_explorer=1): """ Creates a new desktop and spawns a thread running on it Will also start a new icon thread on an existing desktop """ sa=pywintypes.SECURITY_ATTRIBUTES() sa.bInheritHandle=1 try: hdesk=win32service.CreateDesktop(desktop_name, 0, win32con.MAXIMUM_ALLOWED, sa) except win32service.error: traceback.print_exc() errbuf=cStringIO.StringIO() traceback.print_exc(None,errbuf) win32api.MessageBox(0, errbuf.getvalue(), 'Desktop creation failed') return if start_explorer: s=win32process.STARTUPINFO() s.lpDesktop=desktop_name prc_info=win32process.CreateProcess(None, "Explorer.exe",None,None,True,win32con.CREATE_NEW_CONSOLE,None,'c:\\',s) th=thread.start_new_thread(new_icon,(hdesk,desktop_name)) hdesk.SwitchDesktop()
def create_desktop(desktop_name, start_explorer=1): """ Creates a new desktop and spawns a thread running on it Will also start a new icon thread on an existing desktop """ sa=pywintypes.SECURITY_ATTRIBUTES() sa.bInheritHandle=1 try: hdesk=win32service.CreateDesktop(desktop_name, 0, win32con.MAXIMUM_ALLOWED, sa) except win32service.error: traceback.print_exc() errbuf=io.StringIO() traceback.print_exc(None,errbuf) win32api.MessageBox(0, errbuf.getvalue(), 'Desktop creation failed') return if start_explorer: s=win32process.STARTUPINFO() s.lpDesktop=desktop_name prc_info=win32process.CreateProcess(None, "Explorer.exe",None,None,True,win32con.CREATE_NEW_CONSOLE,None,'c:\\',s) th=_thread.start_new_thread(new_icon,(hdesk,desktop_name)) hdesk.SwitchDesktop()
def main(): if sys.argv[1] == 'child': if sys.argv[2] == 'windows': import win32api as api, win32process as proc info = proc.STARTUPINFO() info.hStdInput = api.GetStdHandle(api.STD_INPUT_HANDLE) info.hStdOutput = api.GetStdHandle(api.STD_OUTPUT_HANDLE) info.hStdError = api.GetStdHandle(api.STD_ERROR_HANDLE) python = sys.executable scriptDir = os.path.dirname(__file__) scriptName = os.path.basename(__file__) proc.CreateProcess( None, " ".join((python, scriptName, "grandchild")), None, None, 1, 0, os.environ, scriptDir, info) else: if os.fork() == 0: grandchild() else: grandchild()
def Open_TDX(): try: handle = win32process.CreateProcess(path+'TdxW.exe','',None,None,0,win32process.CREATE_NO_WINDOW,None,path,win32process.STARTUPINFO())#??TB,????? time.sleep(3) TDX_handle = win32gui.FindWindow('#32770','???????V7.35') except Exception as e: ReadEBK.wx_msg(corp_id, secret,agentid,sys._getframe().f_code.co_name+'\t'+str(e)) #????
def _invalidWin32App(pywinerr): """ Determine if a pywintypes.error is telling us that the given process is 'not a valid win32 application', i.e. not a PE format executable. @param pywinerr: a pywintypes.error instance raised by CreateProcess @return: a boolean """ # Let's do this better in the future, but I have no idea what this error # is; MSDN doesn't mention it, and there is no symbolic constant in # win32process module that represents 193. return pywinerr.args[0] == 193
def start(self): procHandle, threadHandle, procId, threadId = win32process.CreateProcess( None, # appName 'python.exe "%s" /run_test_process %s %s' % (this_file, self.BucketCount, self.threadCount), None, # process security None, # thread security 0, # inherit handles win32process.NORMAL_PRIORITY_CLASS, None, # new environment None, # Current directory win32process.STARTUPINFO(), # startup info ) self.processHandle = procHandle
def CommandLine(command, args): """Convert an executable path and a sequence of arguments into a command line that can be passed to CreateProcess""" cmd = "\"" + command.replace("\"", "\"\"") + "\"" for arg in args: cmd = cmd + " \"" + arg.replace("\"", "\"\"") + "\"" return cmd
def CreateProcess(cmd, hStdInput, hStdOutput, hStdError): """Creates a new process which uses the specified handles for its standard input, output, and error. The handles must be inheritable. 0 can be passed as a special handle indicating that the process should inherit the current process's input, output, or error streams, and None can be passed to discard the child process's output or to prevent it from reading any input.""" # initialize new process's startup info si = win32process.STARTUPINFO() si.dwFlags = win32process.STARTF_USESTDHANDLES if hStdInput == 0: si.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE) else: si.hStdInput = hStdInput if hStdOutput == 0: si.hStdOutput = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE) else: si.hStdOutput = hStdOutput if hStdError == 0: si.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE) else: si.hStdError = hStdError # create the process phandle, pid, thandle, tid = win32process.CreateProcess \ ( None, # appName cmd, # commandLine None, # processAttributes None, # threadAttributes 1, # bInheritHandles win32con.NORMAL_PRIORITY_CLASS, # dwCreationFlags None, # newEnvironment None, # currentDirectory si # startupinfo ) if hStdInput and hasattr(hStdInput, 'Close'): hStdInput.Close() if hStdOutput and hasattr(hStdOutput, 'Close'): hStdOutput.Close() if hStdError and hasattr(hStdError, 'Close'): hStdError.Close() return phandle, pid, thandle, tid
def MakePrivateHandle(handle, replace = 1): """Turn an inherited handle into a non inherited one. This avoids the handle duplication that occurs on CreateProcess calls which can create uncloseable pipes.""" ### Could change implementation to use SetHandleInformation()... flags = win32con.DUPLICATE_SAME_ACCESS proc = win32api.GetCurrentProcess() if replace: flags = flags | win32con.DUPLICATE_CLOSE_SOURCE newhandle = win32api.DuplicateHandle(proc,handle,proc,0,0,flags) if replace: handle.Detach() # handle was already deleted by the last call return newhandle
def CreateProcess(appName, cmdline, procSecurity, threadSecurity, inheritHandles, newEnvironment, env, workingDir, startupInfo): """ This function mocks the generated pid aspect of the win32.CreateProcess function. - the true win32process.CreateProcess is called - return values are harvested in a tuple. - all return values from createProcess are passed back to the calling function except for the pid, the returned pid is hardcoded to 42 """ hProcess, hThread, dwPid, dwTid = win32process.CreateProcess( appName, cmdline, procSecurity, threadSecurity, inheritHandles, newEnvironment, env, workingDir, startupInfo) dwPid = 42 return (hProcess, hThread, dwPid, dwTid)
def runExec(filename): handle = win32process.CreateProcess(u'yaz0enc.exe',' %s'%(r'"'+filename+r'"'),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1)
def runTexconv(filename): handle = win32process.CreateProcess(u'TexConv2.exe','-i %s -o \"tmp\\tmp.dds\"'%(r'"'+filename+r'"'),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1)
def runDDS2PNG(filename ,format_name): if format_name == "RGBA8888": dds_name = r'"'+filename+r'"' png_name = r'"'+filename[:-4] + '.png'+r'"' handle = win32process.CreateProcess(u'AMDCompressCLI.exe',' %s %s'%(dds_name , png_name),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1) else: handle = win32process.CreateProcess(u'nvdecompress.exe',' %s'%(r'"'+filename+r'"'),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1)
def runTexconv(dds_name ,gtx_name , swizzle): handle = win32process.CreateProcess(u'TexConv2.exe', ' -i \"%s\" -o \"%s\" -swizzle %d'%(dds_name , gtx_name , swizzle),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1)
def runPNG2DDS(format_name ,png_name , dds_name): if ".tga" in png_name: print('nvcompress.exe -nomips %s \"%s\" \"%s\"'%(format_dict[format_name] , png_name , dds_name)) handle = win32process.CreateProcess(u'nvcompress.exe',' -nomips %s \"%s\" \"%s\"'%(format_dict[format_name] , png_name , dds_name),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1) else: print('AMDCompressCLI.exe -fd %s \"%s\" \"%s\"'%(format_name , png_name , dds_name)) handle = win32process.CreateProcess(u'AMDCompressCLI.exe',' -fd %s \"%s\" \"%s\"'%("ARGB8888" , png_name , dds_name),\ None,None,0,win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) win32event.WaitForSingleObject(handle[0], -1)
def CreateProc(cls, exepath, paramstr="", cwd=None): exepath = u"%s" % (exepath) pos = exepath.rfind('\\') cwd = cwd if cwd else exepath[0:pos] (proc_hd, thread_hd, proc_id, thread_id) = win32process.CreateProcess(exepath, paramstr, None, None, 0, win32process.CREATE_NO_WINDOW, None, cwd, win32process.STARTUPINFO()) return (proc_hd, thread_hd, proc_id, thread_id)
def do_createprocess_exe(self,FILEPATH=None): if FILEPATH==None: FILEPATH = self.filepath exe_path = os.path.dirname(FILEPATH) exe_file = FILEPATH.split('\\')[-1] os.chdir(exe_path) try: handle = win32process.CreateProcess( os.path.join(exe_path, exe_file), '', None, None, 0, win32process.CREATE_NO_WINDOW, None , exe_path, win32process.STARTUPINFO()) self.running = True except Exception, e: print "Create Error!" handle = None self.running = False ''' while self.running : rc = win32event.WaitForSingleObject(handle[0], 1000) if rc == win32event.WAIT_OBJECT_0: self.running = False ''' print "GoodBye"