Feature #8083

Exit status is limited to one-byte values which is invalid for Windows

Added by Vladimir Rutsky almost 2 years ago. Updated almost 2 years ago.



Windows uses 32-bit process exit codes so Ruby incorrectly truncates them to one byte:

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 255'); puts $?.exitstatus"

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 256'); puts $?.exitstatus"

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 257'); puts $?.exitstatus"

Similar code works correctly in Python:

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 255')"

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 256')"

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 257')"


#1 Updated by Luis Lavena almost 2 years ago

  • Status changed from Open to Feedback

Can you provide links to MSDN article that states this? (32-bits exit codes)

Thank you.

#2 Updated by Heesob Park almost 2 years ago

According to the document http://en.wikipedia.org/wiki/Exit_status#Windows,
"Windows uses 32-bit signed integers as exit codes. If a process fails initialization, a Windows system error code may be returned. Windows system error codes are available online.
Exit codes are directly referenced, for example, by the command line interpreter CMD.exe in the errorlevel terminology inherited from DOS. .NET Framework processes and the Windows PowerShell refer to it as the ExitCode property of the Process object."

Refer to

#3 Updated by Usaku NAKAMURA almost 2 years ago

  • Tracker changed from Bug to Feature
  • Status changed from Feedback to Assigned

This may be not a bug but a feature.

#4 Updated by Vladimir Rutsky almost 2 years ago

I cannot find exact definition of possible exit codes on Windows, but here is ExitProcess function declaration [1]:

VOID WINAPI ExitProcess(
In UINT uExitCode

UINT type definition [2]:

An unsigned INT. The range is 0 through 4294967295 decimal.

GetExitCodeProcess declaration 3:

BOOL WINAPI GetExitCodeProcess(
In HANDLE hProcess,
Out LPDWORD lpExitCode

LPDWORD type definition [4]:


DWORD type definition [5]:

A 32-bit unsigned integer. The range is 0 through 4294967295 decimal.

Not sure about all Windows platforms, but at least on Windows 7 error codes in full 32-bit range can be retrieved in practice:

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(30))"

C:\Python27>echo %errorlevel%

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**31 - 1))"

C:\Python27>echo %errorlevel%

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**31))"

C:\Python27>echo %errorlevel%

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**32 - 1))"

C:\Python27>echo %errorlevel%

[1] http://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

[2] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#UINT

[3] http://msdn.microsoft.com/en-us/library/windows/desktop/ms683189%28v=vs.85%29.aspx

[4] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#LPDWORD

[5] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#DWORD

Also available in: Atom PDF