Exit Code history and best practice

1

0

I am finding a bunch of duplicate error codes when dealing with Autodesk installs and uninstalls. For example

3010 ERROR_SUCCESS_REBOOT_REQUIRED

-2147021886 ERROR_SUCCESS_REBOOT_REQUIRED

and

1618 ERROR_INSTALL_ALREADY_RUNNING

-2147023278 ERROR_INSTALL_ALREADY_RUNNING

I am curious, is one or the other the "newer" approach, or has Microso0ft always had parallel and redundant exit codes? And in either case, is one or the other the preferred approach? I am beginning to build my own installers, and I want to be sure I am using the correct sequence if there is such a thing. If there is no best practice I feel like the positive numbers are better for logs being shorter.

Gordon

Posted 2019-06-29T08:14:50.577

Reputation: 171

Answers

2

In Windows user mode, the error codes are used in two forms mainly. One is HRESULT and another one is Win32 error codes. HRESULT is generally used in COM programming and indicated as HEX value. Methods return S_OK (Zero) after success, negative for failure (starting with 0x8). Win32 error codes are integers in between 0 and 65535 (2^16 - 1). Both can be converted to each other with some macros specified in WinError.h header file in Windows SDK. Here are the sample:

#define FACILITY_WIN32 7

#define __HRESULT_FROM_WIN32(x) \
((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000)))

#define HRESULT_CODE(hr) ((hr) & 0xFFFF)

For the above example,the conversion will be like this:

-2147021886 = 0x80070BC2 = (0x80070BC2 & 0xFFFF) = 0xBC2 = 3010
-2147023278 = 0x80070625 = (0x80070625 & 0xFFFF) = 0x652 = 1618

Both of this error codes defined in WinError.h header file. If you are doing some COM programming then use HRESULT (installer uses COM a lot). If you are using Win32 APIs then use Win32 error codes (returned by GetLastError). Both error code formats can be passed to FormatMessage() to get equivalent error message (like strerror() in Linux world).

Further readings:

Biswapriyo

Posted 2019-06-29T08:14:50.577

Reputation: 6 640

So, for a standard installer for a desktop program (like AutoCAD, or Word, or Photoshop) Win32 seems like the right answer, is that right. Or would some of the components of those programs be considered COM objects while the overall program is Win32? – Gordon – 2019-07-02T18:00:52.627

Use what the function returns and consult MS Docs. – Biswapriyo – 2019-07-02T18:47:43.870

I guess what I mean is, if I was doing my own app, I would use Win32 codes not HRESULT. The latter is "correctly" for use when creating interprocess communication components, not regular desktop apps. – Gordon – 2019-07-02T19:56:14.183

No one restricts you to use anyone of them because both of them convertible to each other. In my opinion, Win32 is better because as a human we understand decimals better :) – Biswapriyo – 2019-07-02T20:13:45.070

I almost wish Microsoft WOULD enforce some standards. Autodesk is totally inconsistent with which flavor they use, which is really the worst option. I agree, for myself I'll stick with Win32, both for positive numbers and smaller numbers. Both are easier to quickly grasp. – Gordon – 2019-07-03T08:44:46.333

0

I'd say that those pairs of error exit codes represent the same errors, respectively:

$errCodes = 3010, -2147021886, 1618, -2147023278

foreach ($errCode in $errCodes) {
    '{0,12} 0x{1:x8}' -f $errCode, $errCode
}
        3010 0x00000bc2
 -2147021886 0x80070bc2
        1618 0x00000652
 -2147023278 0x80070652
foreach ($errCode in $errCodes) {
    '{0,12} 0x{1:x4}' -f $errCode, ($errCode -band 0xFFFF)
}
        3010 0x0bc2
 -2147021886 0x0bc2
        1618 0x0652
 -2147023278 0x0652

JosefZ

Posted 2019-06-29T08:14:50.577

Reputation: 9 121