I am creating a PowerShell module which uses an unmanaged API. The way it works looks something like this:
PowerShell Module
|
˅
.NET Wrapper DLL
|
˅
Native DLL
This native DLL has two ways of communicating whether an operation failed or not - by writing status codes to stdout or by throwing Win32 exceptions. You can specify your preference by calling the API's UseExceptions()
or DontUseExceptions()
. However both options suck:
- Without exceptions, any time I do a successful function call,
0
gets printed on the screen. If a function call fails, a warning message is printed on the screen. Not only does this make it hard to detect errors, it also clutters up my module's output to the user. - With exceptions, if there's a minor error, the whole PowerShell process will crash because it's a Win32 exception as opposed to a .NET exception.
Is there a way to prevent a Win32 exception from crashing the whole PowerShell process? Or is there a way to intercept this DLL's stdout and parse the status codes myself?
The API I'm using is the widely-used Geographic Data Abstraction Library.