.net - Raise positive VB style error codes via COM interop from C# -


i have base library created in vb6 exposes standard com interface used in number of applications. exposed number of error code constants, used err.raise indicate conditions.

public enum ioerrors   ioerrorbase = 45000   ioerrorconnectionfailed   ioerrorauthfailed   ioerrornotconnected   ioerrorinvalidportdirection   ioerrorgettingvalue   ioerrornovalueyet end enum 

come on 10 years , we're creating c# objects implementing same set of interfaces , want throw exceptions in way calling application recognise them.

i can find 2 relevant classes, win32exception , comexception.

throwing win32exception((int)ioerrors.ioerrorconnectionfailed, "connect failed") passes message correctly error code ignored , err.number &h80004005.

throwing comexception("connect failed", ioerrors.ioerrorconnectionfailed) results in no error being picked in calling application, presumably because error code not hresult , positive, meaning success.

tl;dr how can throw exception c# such com interop translate 1 of recognised (positive) error codes above?

the "positive" vb style error numbers translated hresults "failure" severity , facility of facility_control/0xa, i.e. 0x800aafc9.

you can suitable hresult using:

int hresult = (int)(0x800a0000 | (int)errorcode); 

this can raised calling process using plain comexception, or throwing own subclass of comexception:

/// <summary> /// exception returns icio error wrapped in exception. /// </summary> internal class icioerrorexception : comexception {     internal icioerrorexception(icio.ioerrors errorcode, string message)         : base(message) {         this.hresult = (int)(0x800a0000 | (int)errorcode);     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -