我尝试使用kernel32.CreateProcessA在python中运行一个进程。下面是我的代码,运行的脚本只返回-1(因此名称为returns-1.py)。但是,当我访问退出代码值时(以我认为正确的方式),我得到0。我不明白如何访问返回码,它应该是-1。任何建议都将不胜感激。在from ctypes import *
HANDLE = c_void_p
LPVOID = c_void_p
DWORD = c_ulong
WORD = c_ushort
CHAR = c_char
ULONG = c_ulong
LPSTR = POINTER(CHAR)
BYTE = c_char
LPBYTE = POINTER(BYTE)
LPDWORD = POINTER(DWORD)
#http://msdn.microsoft.com/en-us/library/windows/desktop/ms684873(v=vs.85).aspx
class ProcessInfo(Structure):
_fields_ = [('hProcess', HANDLE),
('hThread', HANDLE),
('dwProcessId', DWORD),
('dwThreadId', DWORD),
]
#http://msdn.microsoft.com/en-us/library/windows/desktop/ms686331(v=vs.85).aspx
class StartupInfo(Structure):
_fields_ = [ ('cb', DWORD),
('lpReserved', LPSTR),
('lpDesktop', LPSTR),
('lpTitle', LPSTR),
('dwX', DWORD),
('dwY', DWORD),
('dwXSize', DWORD),
('dwYSize', DWORD),
('dwXCountChars', DWORD),
('dwYCountChars', DWORD),
('dwFillAttribute', DWORD),
('dwFlags', DWORD),
('wShowWindow', WORD),
('cbReserved2', WORD),
('lpReserved2', LPBYTE),
('hStdInput', HANDLE),
('hStdOutput', HANDLE),
('hStdError', HANDLE),
]
class ExitCodeProcess(Structure):
_fields_ = [ ('hProcess', HANDLE),
('lpExitCode', LPDWORD)]
pi = ProcessInfo()
si = StartupInfo()
#Running cmd in a new process
#http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
success = kernel32.CreateProcessA(c_char_p(0),
c_char_p("python Returns-1.py"),
0,
0,
0,
1, #follow forks
0,
0,
byref(si),
byref(pi))
#CODE TO ACCESS RETURN CODE
c_long_p = POINTER(c_long)
ec = ExitCodeProcess()
success = kernel32.GetExitCodeProcess(pi.hProcess, byref(ec))
print "success is:", success #prints success is: 1
print str(cast(addressof(ec.lpExitCode), c_long_p).contents) #prints 'c_long(0)' not -1