
    	h                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrS r	 " S S5      r
SS jr\S:X  a  \" S5        SSKrS	rS
\-  r\R"                  " 5       r\S-   r \" \S5      r\" \S5      r\R/                  \R1                  S5      5        \R3                  S5        \" S\" SS\\\S95        \R5                  5         \" S\" S\R6                  -  \R8                  \S-  S95        \R5                  5         \\4 H  r \R<                  " \5        M     gg! \ a     M%  f = f! \\4 H#  r \R<                  " \5        M  ! \ a     M!  f = f   f = f)a  
Windows Process Control

winprocess.run launches a child process and returns the exit code.
Optionally, it can:
  redirect stdin, stdout & stderr to files
  run the command as another user
  limit the process's running time
  control the process window (location, size, window state, desktop)
Works on Windows NT, 2000 & XP. Requires Mark Hammond's win32
extensions.

This code is free for any purpose, with no warranty of any kind.
-- John B. Dell'Aquila <jbd@alum.mit.edu>
    Nc                     U R                  S5      u  pn[        R                  " UUU[        R                  [        R
                  5      $ )z
    Login as specified user and return handle.
    loginString:  'Domain
User
Password'; for local
        login use . or empty string as domain
        e.g. '.
administrator
secret_password'
    
)splitwin32security	LogonUserwin32conLOGON32_LOGON_INTERACTIVELOGON32_PROVIDER_DEFAULT)loginStringdomainuserpasswds       iC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\win32/Demos/winprocess.py	logonUserr      sG     ',,T2F&""**))     c                   R    \ rS rSrSr        S
S jrSS jrSS jrS rS r	S	r
g)Process-   z
A Windows process.
Nc
           	         [         R                  " 5       n
[        R                  [        R                  -  U
l        Uc*  [        R                  " [        R                  5      U
l	        OX:l	        Uc*  [        R                  " [        R                  5      U
l        OXJl        Uc*  [        R                  " [        R                  5      U
l        OXZl        Xjl        Ub2  Uu  U
l        U
l        U
=R
                  [        R"                  -  sl        Ub2  Uu  U
l        U
l        U
=R
                  [        R(                  -  sl        U	b  Xl        SUSSS[         R,                  SSU
4	nUbN  [/        U5      n[0        R2                  " U5        [         R4                  " U/UQ76 n[0        R6                  " 5         O[         R8                  " U6 nUu  U l        U l        U l        U l         g)a(  
        Create a Windows process.
        cmd:     command to run
        login:   run as user 'Domain
User
Password'
        hStdin, hStdout, hStderr:
                 handles for process I/O; default is caller's stdin,
                 stdout & stderr
        show:    wShowWindow (0=SW_HIDE, 1=SW_NORMAL, ...)
        xy:      window offset (x, y) of upper left corner in pixels
        xySize:  window size (width, height) in pixels
        desktop: lpDesktop - name of desktop e.g. 'winsta0\default'
                 None = inherit current desktop
                 '' = create new desktop if necessary

        User calling login requires additional privileges:
          Act as part of the operating system [not needed on Windows XP]
          Increase quotas
          Replace a process level token
        Login string must EITHER be an administrator's account
        (ordinary user can't access current desktop - see Microsoft
        Q165194) OR use desktop='' to run another desktop invisibly
        (may be very slow to startup & finalize).
        N   )!win32processSTARTUPINFOr   STARTF_USESTDHANDLESSTARTF_USESHOWWINDOWdwFlagswin32apiGetStdHandleSTD_INPUT_HANDLE	hStdInputSTD_OUTPUT_HANDLE
hStdOutputSTD_ERROR_HANDLE	hStdErrorwShowWindowdwXdwYSTARTF_USEPOSITIONdwXSizedwYSizeSTARTF_USESIZE	lpDesktopCREATE_NEW_CONSOLEr   r   ImpersonateLoggedOnUserCreateProcessAsUserRevertToSelfCreateProcesshProcesshThreadPIdTId)selfcmdloginhStdinhStdouthStderrshowxyxySizedesktopsiprocArgshUserprocHandless                 r   __init__Process.__init__2   s~   F %%'22X5R5RR
>#001J1JKBL!L?$11(2L2LMBM#M?#001J1JKBL"L>NBFBFJJ(555J%+"BJ
JJ(111J"L++

 e$E11%8&::5L8LK&&(&44h?K:E7t|TXtxr   c                 j    Uc  [         R                  n[         R                  " U R                  U5      $ )zO
Wait for process to finish or for specified number of
milliseconds to elapse.
)
win32eventINFINITEWaitForSingleObjectr1   )r5   mSecs     r   waitProcess.wait   s+    
 <&&D--dmmTBBr   c                     [         R                  " U R                  S5        U R                  U5      [        R
                  :w  a8  [        R                  " U R                  S5        [        R                  " S5        gg)zz
Kill process. Try for an orderly shutdown via WM_CLOSE.  If
still running after gracePeriod (5 sec. default), terminate.
r   d   N)win32guiEnumWindows	__close__rJ   rF   WAIT_OBJECT_0r   TerminateProcessr1   r   Sleep)r5   gracePeriods     r   killProcess.kill   sT    
 	T^^Q/99[!Z%=%==))$--;NN3 >r   c                     [         R                  " U5      u  p4X@R                  :X  a(  [        R                  " U[
        R                  SS5        gg)zL
EnumWindows callback - sends WM_CLOSE to any window
owned by this process.
r   N)r   GetWindowThreadProcessIdr3   rN   PostMessager   WM_CLOSE)r5   hwnddummyr4   r3   s        r   rP   Process.__close__   s>    
  88>((?  x'8'8!Q? r   c                 B    [         R                  " U R                  5      $ )z
Return process exit code.
)r   GetExitCodeProcessr1   )r5   s    r   exitCodeProcess.exitCode   s     ..t}}==r   )r3   r4   r1   r2   )NNNNr   NNN)N)i  )__name__
__module____qualname____firstlineno____doc__rC   rJ   rU   rP   r`   __static_attributes__ r   r   r   r   -   s?     LF\C @>r   r   c                    Ub'  [         R                  " UR                  5       5      US'   Ub'  [         R                  " UR                  5       5      US'   Ub'  [         R                  " UR                  5       5      US'   [        U 40 UD6nUR	                  U5      [
        R                  :w  a  UR                  5         [        S5      eUR                  5       $ )aL  
Run cmd as a child process and return exit code.
mSec:  terminate cmd after specified number of milliseconds
stdin, stdout, stderr:
       file objects for child I/O (use hStdin etc. to attach
       handles instead of files); default is caller's stdin,
       stdout & stderr;
kw:    see Process.__init__ for more keyword options
r8   r9   r:   zprocess timeout exceeded)
msvcrtget_osfhandlefilenor   rJ   rF   rQ   rU   OSErrorr`   )r6   rI   stdinstdoutstderrkwchilds          r   runrs      s     ++ELLN;8,,V]]_=9,,V]]_=9C2Ezz$:333

011>>r   __main__zTesting winprocess.py...   zREM      Test of winprocess.py piping commands to a shell.
REM      This 'notepad' process will terminate in %d seconds.
vol
net user
_this_is_a_test_of_stderr_
z.txtzw+bmbcszCMD.EXE exit code:zcmd.exe)r;   rn   ro   rp   zNOTEPAD exit code:znotepad.exe %si  )r;   rI   )NNNN) rf   rj   osr   r   rF   rN   r   r   r   r   rs   rb   printtempfiletimeoutSeconds	cmdStringmktempcmd_nameout_nameopenr6   outwriteencodeseekclosenameSW_MAXIMIZEnunlinkrm   rh   r   r   <module>r      s     	      "s> s>l. z	
$%N	 	   H& H8U#8U#		)""6*+ 	SE	
 			  388+))#d*	
 			H%A		(# &Q V   H%A		(#  &sC   %BD/ 
D""D,+D,/E8E
EE	EE	E