본문 바로가기

파이썬 담아두기

파이썬에서 프로세스 생성

반응형

파이썬에서 프로세스 생성하기

CreateProcessA API MSDNhttps://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa


-------------------------------------------------------------------------------------------------------


from ctypes import *

from defines import *


kernel32 = windll.kernel32  #ctypes.windll.kernel32


if __name__ == '__main__':


path_to_exec = "python C:\\Users\\WIN7-PRO\\Desktop\\ori.py"

creation_flags = DEBUG_PROCESS


si = STARTUPINFO()

pi = PROCESS_INFORMATION()


si.cb = sizeof(si)


#Start the child process. 

if kernel32.CreateProcessA(None,path_to_exec, None, None, False, 0x10, None, None, byref(si), byref(pi) ):

print "[*] We have successfully launched the process!"

pid = pi.dwProcessId

print "[*] PID: %d" % pid

else:

print "melong failed~~!\n"


# Wait until child process exits.

kernel32.WaitForSingleObject( pi.hProcess, INFINITE );


# Close process and thread handles. 

kernel32.CloseHandle( pi.hProcess );

kernel32.CloseHandle( pi.hThread );




--------------------------------------------------------------------------------------------------------------

 path_to_exec = "cmd" 하면 cmd 실행     (환경변수 인식O)

 path_to_exec = "[exe파일 경로]"  -- 해당 exe파일 실행.

 path_to_exec = "python [파이썬파일경로]" -- 파이썬 파일을 실행하려면 앞에 python.exe를 달아줘야함. 환경변수 인식이 되므로, ptyhon이라 적어도 무방

반응형

'파이썬 담아두기' 카테고리의 다른 글

파이썬 한글 인코딩 해결  (0) 2018.09.21
파이썬 입출력  (0) 2018.09.21
PyFPDF 파이썬 모듈 설치  (0) 2018.08.10
BeautifulSoup4 설치 (bs4)  (0) 2018.08.10
파이썬 환경변수 설정  (0) 2018.08.10