.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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -