1

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:

  1. 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.
  2. 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.

4

1 回答 1

1

在您的 .NET Wrapper DLL 中尝试捕获 SEHException 以查看是否可以捕获 Win32 异常。另一种选择是使用 CLI/C++ 编写 .NET 包装器,然后使用 C++ 异常处理机制来捕获异常。

于 2012-09-15T03:03:30.560 回答