Is it possible to import a native method in .NET (using the P/Invoke), so that the interop throws Win32Exception automatically? -
numerous win32 methods use setlasterror indicate error. in .net use such method so:
if (!somenativeboolmethod(...)) { throw new win32exception(); } or
var res = somenativeintmethod(...); if (res != 0) { throw new win32exception(res); } i wondering if .net interop infrastructure can me. in case, import somenativeboolmethod , somenativeintmethod void methods. (for somenativeintmethod should optional, depending whether wish able see positive return codes or not).
is possible?
thanks.
it sounds you're looking feature similar com interop feature whereby hresult signatures translated exception throwing methods. if i'm sorry feature doesn't exist.
very due many many different ways win32 apis report failures. there no universal convention. unlike com there universal convention returning hresult.
the best bet introduce helper function wraps pinvoke calls , check error code , throws appropriately.
Comments
Post a Comment