1

Similar question was asked before, but i haven't found some answers there.

As i understand, for making custom HRESULT codes i create an .mc file where i describe the codes, then compile it and get an .h file. But i can't get it how to prevent possible overlapping with the system ones.

Example:

LanguageNames=(English=0x409:MSG00409)
MessageId = 0x0
Severity = Success
Facility = Application
SymbolicName = CUSTOME_CODE
Language=English
Cusome message
.

produces

#define CUSTOME_CODE                     0x00000000L

which is actually the same with S_OK.

If i return CUSTOME_CODE from my method, a caller can't distinguish it from S_OK;

Wiki says HRESULT contains a bit for distinguishing Microsoft / User defined code properties, but i don't understand how to set it. Also MSDN says that the bit is just "reserved".

How do i make code which can't overlap the system ones?

fogbit
  • 1,961
  • 6
  • 27
  • 41
  • 2
    See [HRESULT](https://msdn.microsoft.com/en-us/library/cc231198), the docs for [mc.exe](https://msdn.microsoft.com/en-us/library/aa385638), and the spec for [message text files](https://msdn.microsoft.com/en-us/library/dd996906). For creating an `HRESULT` header, use the `-o` command-line option. To set the customer bit, use the `-c` command-line option. You may want some symbolic names defined at the top, e.g. `SeverityNames=(Success=0 Fail=2)` and `FacilityNames=(FACILITY_ITF=4)`. – Eryk Sun Feb 14 '18 at 13:20
  • 1
    There's no technical way to distinguish. You're supposed to use FACILITY_ITF like all non-Microsoft developers on the planet, who can define colliding error codes happily, so it's just a plain nonsense. Note that to add to the confusion, there are some Microsoft components that use FACILITY_ITF ... – Simon Mourier Feb 14 '18 at 13:49
  • 1
    @SimonMourier, you can also set the customer bit in the generated codes via `-c`, but really that's non-standard for `HRESULT` codes. It's reserved for `NTSTATUS` codes. – Eryk Sun Feb 14 '18 at 14:03
  • When working with my own code (if it's not something distributed worldwide, say), I use custom a facility number of mine, one that does not collide with Microsoft ones (like a high number), although I know it's bad :-) – Simon Mourier Feb 14 '18 at 14:17
  • 1
    @eryksun what's the point to set the customer bit if there is the an agreement that custom HRESULTs must use FACILITY_ITF? – fogbit Feb 15 '18 at 09:18
  • @fogbit, I suggested it to Simon in response to the claim that "there are some Microsoft components that use FACILITY_ITF", though I don't typically see the customer bit set for `HRESULT` values, and winerror.h states that it's reserved for use with kernel `NTSTATUS` values. Really I wouldn't worry about it. – Eryk Sun Feb 15 '18 at 10:38
  • @ery The [reference](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/0642cb2f-2075-4469-918c-4441e69c548a) seems to explicitly allow setting the customer bit for `HRESULT` values (at least all the way back to 2013). It doesn't appear to be reserved to `NTSTATUS` values. – IInspectable Feb 27 '22 at 22:41

1 Answers1

4

A HRESULT, which describes an interface-specific return code, should contain the FACILTY_ITF facility code. Note however that return values between 0x0000 and 0x01FF are reserved for COM-defined FACILITY_ITF codes.

See this link for further details.

Aurora
  • 1,334
  • 8
  • 21
  • Also there is an internal structure to the HRESULT. (Wikipedia) https://en.wikipedia.org/wiki/HRESULT and (MSDN) https://msdn.microsoft.com/en-us/library/windows/desktop/ms690088(v=vs.85).aspx – Eljay Feb 14 '18 at 12:38