Encoding HRESULT values: use FACILITY_ITF or set bit 29?
If I'm designing my own COM error codes that coexist with Microsoft's HRESULT values, should I set the facility code to FACILITY_ITF ("defined solely by the developer of the interface or function that returns the status code"), or should I set bit 29 which indicates it is a customer code? Should I do both?
If I set bit 29, can I define my own facility codes that conflict with Microsoft's?
If you are returning an error code from an interface you have created and control (the vast majority of cases), then using
FACILITY_ITFis the way to go.
As a way of evidence, look up the definition of the vbObjectError constant used in Visual Basic 6 to define custom error codes. VB6 applications should “add” that constant to any custom error code they define. It turns out that that constant is 0x80040000, which is
FACILITY_ITFwith the Severity bit set to
SEVERITY_ERROR(= 1). The requirement to “add” custom codes to it is just a sloppy way to say “OR the constant with your custom code”.
Returning custom error codes on an interface you don’t control is tricky, unless the particular interface naturally lends itself to that sort of thing. You don’t know how the other side is going to react to it. Realistically, unless the other side is a programming environment that can plainly stop and report the code to the user, there is nothing sensible it could do with a custom error. I’ve never had the need to do that, and I would try to stick to documented error codes for such interfaces.
As far as the C bit (bit-29): at least some documentation (e.g. https://docs.microsoft.com/en-us/windows/desktop/com/structure-of-com-error-codes) lists it as RESERVED. I would leave “C” bit-29 alone (leave as zero), unless there is a clear and specific documentation otherwise.