c# - Setting specific COM HRESULT value from .NET -


i'm creating .net assembly needs com callable e.g. vb6 etc.

most of stuff works fine - i'm fine-tuning error handling right now.

what i'd create specific hresult values exceptions - e.g. use values 0x88880001 etc. exception cases. i'd use common "prefix" (like 0x8888) , add internal error codes (running decimal 1001 up) number.

so internal error 1001 should become hresult = 0x888803e9 , forth.

i have own custom .net exception class, , know can set protected hresult property on base applicationexception class - somehow can't manage "prefix" plus error code work....

using system;

public class customexception : applicationexception {     public customexception(string message, int errorcode) : base(message)     {         base.hresult = (uint) 0x88880000 + errorcode;     } } 

no matter try here - cannot int value (for base.hresult, int) represents 0x888803e9 stored base class...

what missing here??

the c# compiler hassles stop tripping overflow bug. justifiably, lot of kind of code cannot survive overflow checking, not kind of mishap ever want in error handling code :) note how code in upvoted answer keels on project > properties > build > advanced > "check arithmetic overflow/underflow" option ticked. option doesn't used enough.

you have use unchecked keyword suppress runtime check:

  base.hresult = unchecked((int)(0x88880000u + errorcode)); 

or can suppress @ compile time:

  const int errorbase = unchecked((int)0x88880000);   base.hresult = errorbase + errorcode; 

excruciatingly: hresult you'll generate not valid one, doesn't have correct facility code. code provides hint error came from. interop code should use 0x80040000 error base select facility_itf. not sure you'll ever grief on 0x88880000 though, message string generate important. unfortunately isupporterrorinfo tends skipped in client code there's no great guarantee sees it.


Comments