PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ct/windows/error.rb

https://github.com/CMU-CREATE-Lab/timemachine-creator
Ruby | 504 lines | 417 code | 65 blank | 22 comment | 3 complexity | e136a1b81c2df496062d98467ae25c47 MD5 | raw file
  1. ############################################################################
  2. # error.rb
  3. #
  4. # Includes all of the error codes in error.h, msterr.h and some from
  5. # winerror.h.
  6. #
  7. # Adds the following convenience methods:
  8. #
  9. # get_last_error - Returns a human readable string for the error returned
  10. # by the GetLastError() function.
  11. ############################################################################
  12. require 'windows/api'
  13. module Windows
  14. module Error
  15. API.auto_namespace = 'Windows::Error'
  16. API.auto_method = true
  17. API.auto_constant = true
  18. API.auto_unicode = true
  19. private
  20. FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
  21. FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
  22. FORMAT_MESSAGE_FROM_STRING = 0x00000400
  23. FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
  24. FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
  25. FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
  26. FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF
  27. SEM_FAILCRITICALERRORS = 0x0001
  28. SEM_NOALIGNMENTFAULTEXCEPT = 0x0004
  29. SEM_NOGPFAULTERRORBOX = 0x0002
  30. SEM_NOOPENFILEERRORBOX = 0x8000
  31. S_OK = 0
  32. NO_ERROR = 0
  33. ERROR_SUCCESS = 0
  34. ERROR_INVALID_FUNCTION = 1
  35. ERROR_FILE_NOT_FOUND = 2
  36. ERROR_PATH_NOT_FOUND = 3
  37. ERROR_TOO_MANY_OPEN_FILES = 4
  38. ERROR_ACCESS_DENIED = 5
  39. ERROR_INVALID_HANDLE = 6
  40. ERROR_ARENA_TRASHED = 7
  41. ERROR_NOT_ENOUGH_MEMORY = 8
  42. ERROR_INVALID_BLOCK = 9
  43. ERROR_BAD_ENVIRONMENT = 10
  44. ERROR_BAD_FORMAT = 11
  45. ERROR_INVALID_ACCESS = 12
  46. ERROR_INVALID_DATA = 13
  47. ERROR_INVALID_DRIVE = 15
  48. ERROR_CURRENT_DIRECTORY = 16
  49. ERROR_NOT_SAME_DEVICE = 17
  50. ERROR_NO_MORE_FILES = 18
  51. ERROR_WRITE_PROTECT = 19
  52. ERROR_BAD_UNIT = 20
  53. ERROR_NOT_READY = 21
  54. ERROR_BAD_COMMAND = 22
  55. ERROR_CRC = 23
  56. ERROR_BAD_LENGTH = 24
  57. ERROR_SEEK = 25
  58. ERROR_NOT_DOS_DISK = 26
  59. ERROR_SECTOR_NOT_FOUND = 27
  60. ERROR_OUT_OF_PAPER = 28
  61. ERROR_WRITE_FAULT = 29
  62. ERROR_READ_FAULT = 30
  63. ERROR_GEN_FAILURE = 31
  64. ERROR_SHARING_VIOLATION = 32
  65. ERROR_LOCK_VIOLATION = 33
  66. ERROR_WRONG_DISK = 34
  67. ERROR_FCB_UNAVAILABLE = 35
  68. ERROR_SHARING_BUFFER_EXCEEDED = 36
  69. ERROR_HANDLE_EOF = 38
  70. ERROR_HANDLE_DISK_FULL = 39
  71. ERROR_NOT_SUPPORTED = 50
  72. ERROR_FILE_EXISTS = 80
  73. ERROR_DUP_FCB = 81
  74. ERROR_CANNOT_MAKE = 82
  75. ERROR_FAIL_I24 = 83
  76. ERROR_OUT_OF_STRUCTURES = 84
  77. ERROR_ALREADY_ASSIGNED = 85
  78. ERROR_INVALID_PASSWORD = 86
  79. ERROR_INVALID_PARAMETER = 87
  80. ERROR_NET_WRITE_FAULT = 88
  81. ERROR_NO_PROC_SLOTS = 89 # no process slots available
  82. ERROR_NOT_FROZEN = 90
  83. ERR_TSTOVFL = 91 # timer service table overflow
  84. ERR_TSTDUP = 92 # timer service table duplicate
  85. ERROR_NO_ITEMS = 93 # There were no items to operate upon
  86. ERROR_INTERRUPT = 95 # interrupted system call
  87. ERROR_TOO_MANY_SEMAPHORES = 100
  88. ERROR_EXCL_SEM_ALREADY_OWNED = 101
  89. ERROR_SEM_IS_SET = 102
  90. ERROR_TOO_MANY_SEM_REQUESTS = 103
  91. ERROR_INVALID_AT_INTERRUPT_TIME = 104
  92. ERROR_SEM_OWNER_DIED = 105 # waitsem found owner died
  93. ERROR_SEM_USER_LIMIT = 106 # too many procs have this sem
  94. ERROR_DISK_CHANGE = 107 # insert disk b into drive a
  95. ERROR_DRIVE_LOCKED = 108 # drive locked by another process
  96. ERROR_BROKEN_PIPE = 109 # write on pipe with no reader
  97. ERROR_OPEN_FAILED = 110 # open/created failed
  98. ERROR_DISK_FULL = 112 # not enough space
  99. ERROR_NO_MORE_SEARCH_HANDLES = 113 # can't allocate
  100. ERROR_INVALID_TARGET_HANDLE = 114 # handle in DOSDUPHANDLE is invalid
  101. ERROR_PROTECTION_VIOLATION = 115 # bad user virtual address
  102. ERROR_VIOKBD_REQUEST = 116
  103. ERROR_INVALID_CATEGORY = 117 # category for DEVIOCTL not defined
  104. ERROR_INVALID_VERIFY_SWITCH = 118 # invalid value
  105. ERROR_BAD_DRIVER_LEVEL = 119 # DosDevIOCTL not level four
  106. ERROR_CALL_NOT_IMPLEMENTED = 120
  107. ERROR_SEM_TIMEOUT = 121 # timeout from semaphore function
  108. ERROR_INSUFFICIENT_BUFFER = 122
  109. ERROR_INVALID_NAME = 123 # illegal char or malformed file system name
  110. ERROR_INVALID_LEVEL = 124 # unimplemented level for info retrieval
  111. ERROR_NO_VOLUME_LABEL = 125 # no volume label found
  112. ERROR_MOD_NOT_FOUND = 126 # w_getprocaddr, w_getmodhandle
  113. ERROR_PROC_NOT_FOUND = 127 # w_getprocaddr
  114. ERROR_WAIT_NO_CHILDREN = 128 # CWait finds to children
  115. ERROR_CHILD_NOT_COMPLETE = 129 # CWait children not dead yet
  116. ERROR_DIRECT_ACCESS_HANDLE = 130 # invalid for direct disk access
  117. ERROR_NEGATIVE_SEEK = 131 # tried to seek negative offset
  118. ERROR_SEEK_ON_DEVICE = 132 # tried to seek on device or pipe
  119. ERROR_IS_JOIN_TARGET = 133
  120. ERROR_IS_JOINED = 134
  121. ERROR_IS_SUBSTED = 135
  122. ERROR_NOT_JOINED = 136
  123. ERROR_NOT_SUBSTED = 137
  124. ERROR_JOIN_TO_JOIN = 138
  125. ERROR_SUBST_TO_SUBST = 139
  126. ERROR_JOIN_TO_SUBST = 140
  127. ERROR_SUBST_TO_JOIN = 141
  128. ERROR_BUSY_DRIVE = 142
  129. ERROR_SAME_DRIVE = 143
  130. ERROR_DIR_NOT_ROOT = 144
  131. ERROR_DIR_NOT_EMPTY = 145
  132. ERROR_IS_SUBST_PATH = 146
  133. ERROR_IS_JOIN_PATH = 147
  134. ERROR_PATH_BUSY = 148
  135. ERROR_IS_SUBST_TARGET = 149
  136. ERROR_SYSTEM_TRACE = 150 # system trace error
  137. ERROR_INVALID_EVENT_COUNT = 151 # DosMuxSemWait errors
  138. ERROR_TOO_MANY_MUXWAITERS = 152
  139. ERROR_INVALID_LIST_FORMAT = 153
  140. ERROR_LABEL_TOO_LONG = 154
  141. ERROR_TOO_MANY_TCBS = 155
  142. ERROR_SIGNAL_REFUSED = 156
  143. ERROR_DISCARDED = 157
  144. ERROR_NOT_LOCKED = 158
  145. ERROR_BAD_THREADID_ADDR = 159
  146. ERROR_BAD_ARGUMENTS = 160
  147. ERROR_BAD_PATHNAME = 161
  148. ERROR_SIGNAL_PENDING = 162
  149. ERROR_UNCERTAIN_MEDIA = 163
  150. ERROR_MAX_THRDS_REACHED = 164
  151. ERROR_MONITORS_NOT_SUPPORTED = 165
  152. ERROR_INVALID_SEGMENT_NUMBER = 180
  153. ERROR_INVALID_CALLGATE = 181
  154. ERROR_INVALID_ORDINAL = 182
  155. ERROR_ALREADY_EXISTS = 183
  156. ERROR_NO_CHILD_PROCESS = 184
  157. ERROR_CHILD_ALIVE_NOWAIT = 185
  158. ERROR_INVALID_FLAG_NUMBER = 186
  159. ERROR_SEM_NOT_FOUND = 187
  160. ERROR_INVALID_STARTING_CODESEG = 188
  161. ERROR_INVALID_STACKSEG = 189
  162. ERROR_INVALID_MODULETYPE = 190
  163. ERROR_INVALID_EXE_SIGNATURE = 191
  164. ERROR_EXE_MARKED_INVALID = 192
  165. ERROR_BAD_EXE_FORMAT = 193
  166. ERROR_ITERATED_DATA_EXCEEDS_64k = 194
  167. ERROR_INVALID_MINALLOCSIZE = 195
  168. ERROR_DYNLINK_FROM_INVALID_RING = 196
  169. ERROR_IOPL_NOT_ENABLED = 197
  170. ERROR_INVALID_SEGDPL = 198
  171. ERROR_AUTODATASEG_EXCEEDS_64k = 199
  172. ERROR_RING2SEG_MUST_BE_MOVABLE = 200
  173. ERROR_RELOC_CHAIN_XEEDS_SEGLIM = 201
  174. ERROR_INFLOOP_IN_RELOC_CHAIN = 202
  175. ERROR_ENVVAR_NOT_FOUND = 203
  176. ERROR_NOT_CURRENT_CTRY = 204
  177. ERROR_NO_SIGNAL_SENT = 205
  178. ERROR_FILENAME_EXCED_RANGE = 206 # if filename > 8.3
  179. ERROR_RING2_STACK_IN_USE = 207 # for FAPI
  180. ERROR_META_EXPANSION_TOO_LONG = 208 # if "*a" > 8.3
  181. ERROR_INVALID_SIGNAL_NUMBER = 209
  182. ERROR_THREAD_1_INACTIVE = 210
  183. ERROR_INFO_NOT_AVAIL = 211 #@@ PTM 5550
  184. ERROR_LOCKED = 212
  185. ERROR_BAD_DYNALINK = 213 #@@ PTM 5760
  186. ERROR_TOO_MANY_MODULES = 214
  187. ERROR_NESTING_NOT_ALLOWED = 215
  188. ERROR_BAD_PIPE = 230
  189. ERROR_PIPE_BUSY = 231
  190. ERROR_NO_DATA = 232
  191. ERROR_PIPE_NOT_CONNECTED = 233
  192. ERROR_MORE_DATA = 234
  193. ERROR_PIPE_CONNECTED = 535
  194. ERROR_PIPE_LISTENING = 536
  195. ERROR_OPERATION_ABORTED = 995
  196. ERROR_IO_INCOMPLETE = 996
  197. ERROR_IO_PENDING = 997
  198. ERROR_USER_DEFINED_BASE = 0xF000
  199. ERROR_I24_WRITE_PROTECT = 0
  200. ERROR_I24_BAD_UNIT = 1
  201. ERROR_I24_NOT_READY = 2
  202. ERROR_I24_BAD_COMMAND = 3
  203. ERROR_I24_CRC = 4
  204. ERROR_I24_BAD_LENGTH = 5
  205. ERROR_I24_SEEK = 6
  206. ERROR_I24_NOT_DOS_DISK = 7
  207. ERROR_I24_SECTOR_NOT_FOUND = 8
  208. ERROR_I24_OUT_OF_PAPER = 9
  209. ERROR_I24_WRITE_FAULT = 0x0A
  210. ERROR_I24_READ_FAULT = 0x0B
  211. ERROR_I24_GEN_FAILURE = 0x0C
  212. ERROR_I24_DISK_CHANGE = 0x0D
  213. ERROR_I24_WRONG_DISK = 0x0F
  214. ERROR_I24_UNCERTAIN_MEDIA = 0x10
  215. ERROR_I24_CHAR_CALL_INTERRUPTED = 0x11
  216. ERROR_I24_NO_MONITOR_SUPPORT = 0x12
  217. ERROR_I24_INVALID_PARAMETER = 0x13
  218. APPLICATION_ERROR_MASK = 0x20000000
  219. ERROR_SEVERITY_SUCCESS = 0x00000000
  220. ERROR_SEVERITY_INFORMATIONAL = 0x40000000
  221. ERROR_SEVERITY_WARNING = 0x80000000
  222. ERROR_SEVERITY_ERROR = 0xc0000000
  223. ALLOWED_FAIL = 0x0001
  224. ALLOWED_ABORT = 0x0002
  225. ALLOWED_RETRY = 0x0004
  226. ALLOWED_IGNORE = 0x0008
  227. I24_OPERATION = 0x1
  228. I24_AREA = 0x6
  229. I24_CLASS = 0x80
  230. ERRCLASS_OUTRES = 1 # Out of Resource
  231. ERRCLASS_TEMPSIT = 2 # Temporary Situation
  232. ERRCLASS_AUTH = 3 # Permission problem
  233. ERRCLASS_INTRN = 4 # Internal System Error
  234. ERRCLASS_HRDFAIL = 5 # Hardware Failure
  235. ERRCLASS_SYSFAIL = 6 # System Failure
  236. ERRCLASS_APPERR = 7 # Application Error
  237. ERRCLASS_NOTFND = 8 # Not Found
  238. ERRCLASS_BADFMT = 9 # Bad Format
  239. ERRCLASS_LOCKED = 10 # Locked
  240. ERRCLASS_MEDIA = 11 # Media Failure
  241. ERRCLASS_ALREADY = 12 # Collision with Existing Item
  242. ERRCLASS_UNK = 13 # Unknown/other
  243. ERRCLASS_CANT = 14
  244. ERRCLASS_TIME = 15
  245. ERRACT_RETRY = 1 # Retry
  246. ERRACT_DLYRET = 2 # Delay Retry, retry after pause
  247. ERRACT_USER = 3 # Ask user to regive info
  248. ERRACT_ABORT = 4 # abort with clean up
  249. ERRACT_PANIC = 5 # abort immediately
  250. ERRACT_IGNORE = 6 # ignore
  251. ERRACT_INTRET = 7 # Retry after User Intervention
  252. ERRLOC_UNK = 1 # No appropriate value
  253. ERRLOC_DISK = 2 # Random Access Mass Storage
  254. ERRLOC_NET = 3 # Network
  255. ERRLOC_SERDEV = 4 # Serial Device
  256. ERRLOC_MEM = 5 # Memory
  257. TC_NORMAL = 0
  258. TC_HARDERR = 1
  259. TC_GP_TRAP = 2
  260. TC_SIGNAL = 3
  261. # From WinError.h
  262. ERROR_INVALID_FLAGS = 0x1004
  263. ERROR_NO_UNICODE_TRANSLATION = 0x1113
  264. E_INVALIDARG = 0x80070057
  265. E_NOINTERFACE = 0x80004002
  266. E_NOTIMPL = 0x80004001
  267. E_OUTOFMEMORY = 0x8007000E
  268. E_UNEXPECTED = 0x8000FFFF
  269. RPC_E_TIMEOUT = 0x8001011F
  270. CO_E_NOT_SUPPORTED = 0x80004021
  271. CLASS_E_NOAGGREGATION = 0x80040110
  272. DISP_E_BADINDEX = 0x8002000B
  273. DISP_E_PARAMNOTFOUND = 0x80020004
  274. DISP_E_EXCEPTION = 0x80020009
  275. DISP_E_MEMBERNOTFOUND = 0x80020003
  276. # Registry errors
  277. REGDB_E_CLASSNOTREG = 0x80040154
  278. # msterr.h
  279. SCHED_S_TASK_READY = 0x00041300
  280. SCHED_S_TASK_RUNNING = 0x00041301
  281. SCHED_S_TASK_DISABLED = 0x00041302
  282. SCHED_S_TASK_HAS_NOT_RUN = 0x00041303
  283. SCHED_S_TASK_HAS_NO_MORE_RUNS = 0x00041304
  284. SCHED_S_TASK_NOT_SCHEDULED = 0x00041305
  285. SCHED_S_TASK_TERMINATED = 0x00041306
  286. SCHED_S_TASK_NO_VALID_TRIGGERS = 0x00041307
  287. SCHED_S_EVENT_TRIGGER = 0x00041308
  288. SCHED_E_TRIGGER_NOT_FOUND = 0x00041309
  289. SCHED_E_TASK_NOT_READY = 0x0004130A
  290. SCHED_E_TASK_NOT_RUNNING = 0x0004130B
  291. SCHED_E_SERVICE_NOT_INSTALLED = 0x0004130C
  292. SCHED_E_CANNOT_OPEN_TASK = 0x0004130D
  293. SCHED_E_INVALID_TASK = 0x0004130E
  294. SCHED_E_ACCOUNT_INFORMATION_NOT_SET = 0x0004130F
  295. SCHED_E_ACCOUNT_NAME_NOT_FOUND = 0x00041310
  296. SCHED_E_ACCOUNT_DBASE_CORRUPT = 0x00041311
  297. SCHED_E_NO_SECURITY_SERVICES = 0x00041312
  298. SCHED_E_UNKNOWN_OBJECT_VERSION = 0x00041313
  299. # Socket errors
  300. WSA_INVALID_HANDLE = 6
  301. WSA_NOT_ENOUGH_MEMORY = 8
  302. WSA_INVALID_PARAMETER = 87
  303. WSA_OPERATION_ABORTED = 995
  304. WSA_IO_INCOMPLETE = 996
  305. WSA_IO_PENDING = 997
  306. WSAEINTR = 10004
  307. WSAEBADF = 10009
  308. WSAEACCESS = 10013
  309. WSAEFAULT = 10014
  310. WSAEINVAL = 10022
  311. WSAEMFILE = 10024
  312. WSAEWOULDBLOCK = 10035
  313. WSAEINPROGRESS = 10036
  314. WSAEALREADY = 10037
  315. WSAENOTSOCK = 10038
  316. WSAEDESTADDRREQ = 10039
  317. WSAEMSGSIZE = 10040
  318. WSAEPROTOTYPE = 10041
  319. WSAENOPROTOOPT = 10042
  320. WSAEPROTONOSUPPORT = 10043
  321. WSAESOCKTNOSUPPORT = 10044
  322. WSAEOPNOTSUPP = 10045
  323. WSAEPFNOSUPPORT = 10046
  324. WSAEAFNOSUPPORT = 10047
  325. WSAEADDRINUSE = 10048
  326. WSAEADDRNOTAVAIL = 10049
  327. WSAENETDOWN = 10050
  328. WSAENETUNREACH = 10051
  329. WSAENETRESET = 10052
  330. WSAECONNABORTED = 10053
  331. WSAECONNRESET = 10054
  332. WSAENOBUFS = 10055
  333. WSAEISCONN = 10056
  334. WSAENOTCONN = 10057
  335. WSAESHUTDOWN = 10058
  336. WSAETOOMANYREFS = 10059
  337. WSATIMEDOUT = 10060
  338. WSAECONNREFUSED = 10061
  339. WSAELOOP = 10062
  340. WSAENAMETOOLONG = 10063
  341. WSAEHOSTDOWN = 10064
  342. WSAEHOSTUNREACH = 10065
  343. WSAENOEMPTY = 10066
  344. WSAEPROCLIM = 10067
  345. WSAEUSERS = 10068
  346. WSAEDQUOT = 10069
  347. WSAESTALE = 10070
  348. WSAEREMOTE = 10071
  349. WSASYSNOTREADY = 10091
  350. WSAVERNOTSUPPORTED = 10092
  351. WSANOTINITIALISED = 10093
  352. WSAEDISCON = 10101
  353. WSAENOMORE = 10102
  354. WSAECANCELLED = 10103
  355. WSAEINVALIDPROCTABLE = 10104
  356. WSAEINVALIDPROVIDER = 10105
  357. WSAEPROVIDERFAILEDINIT = 10106
  358. WSASYSCALLFAILURE = 10107
  359. WSASERVICE_NOT_FOUND = 10108
  360. WSATYPE_NOT_FOUND = 10109
  361. WSA_E_NO_MORE = 10110
  362. WSA_E_CANCELLED = 10111
  363. WSAEREFUSED = 10112
  364. WSAHOST_NOT_FOUND = 11001
  365. WSATRY_AGAIN = 11002
  366. WSANO_RECOVERY = 11003
  367. WSANO_DATA = 11004
  368. WSA_QOS_RECEIVERS = 11005
  369. WSA_QOS_SENDERS = 11006
  370. # Token related
  371. ERROR_NO_TOKEN = 1008
  372. ERROR_NO_IMPERSONATION_TOKEN = 1309
  373. ERROR_BAD_TOKEN_TYPE = 1349
  374. ERROR_TOKEN_ALREADY_IN_USE = 1375
  375. ERROR_SXS_INCORRECT_PUBLIC_KEY_TOKEN = 14095
  376. API.new('GetLastError', 'V', 'L')
  377. API.new('SetLastError', 'L', 'V')
  378. API.new('SetErrorMode', 'I', 'I')
  379. API.new('FormatMessage', 'LLLLPLP', 'L')
  380. begin
  381. API.new('GetErrorMode', 'V', 'I')
  382. rescue Win32::API::LoadLibraryError
  383. # Vista or later
  384. end
  385. begin
  386. API.new('SetLastErrorEx', 'LL', 'V', 'user32')
  387. rescue Win32::API::LoadLibraryError
  388. # VC++ 7.0 or later
  389. end
  390. # Convenience method that wraps FormatMessage with some sane defaults and
  391. # returns a human readable string.
  392. #
  393. def get_last_error(err_num = GetLastError.call)
  394. buf = 0.chr * 260
  395. flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY
  396. FormatMessageA.call(flags, 0, err_num, 0, buf, buf.size, 0)
  397. if RUBY_VERSION < '1.9.1'
  398. buf.strip
  399. else
  400. buf.force_encoding(Encoding.default_external).strip
  401. end
  402. end
  403. # Macros from WinError.h
  404. def IS_ERROR(status)
  405. status >> 31 == 1
  406. end
  407. def MAKE_HRESULT(sev, fac, code)
  408. sev << 31 | fac << 16 | code
  409. end
  410. def MAKE_SCODE(sev, fac, code)
  411. sev << 31 | fac << 16 | code
  412. end
  413. def HRESULT_CODE(hr)
  414. hr & 0xFFFF
  415. end
  416. def HRESULT_FACILITY(hr)
  417. (hr >> 16) & 0x1fff
  418. end
  419. def HRESULT_FROM_NT(x)
  420. x | 0x10000000 # FACILITY_NT_BIT
  421. end
  422. def HRESULT_FROM_WIN32(x)
  423. if x <= 0
  424. x
  425. else
  426. (x & 0x0000FFFF) | (7 << 16) | 0x80000000
  427. end
  428. end
  429. def HRESULT_SEVERITY(hr)
  430. (hr >> 31) & 0x1
  431. end
  432. def FAILED(status)
  433. status < 0
  434. end
  435. def SUCCEEDED(status)
  436. status >= 0
  437. end
  438. end
  439. end