What exceptions can occur when using PInvoke or are all errors handled by the method return values and it is up to the developer to check and raise exceptions if needed?
With P/Invoke it's safe to say there are two kinds of errors you need to handle.
With group 1 there are a couple of exceptions that can occur (not the definitive list):
With group 2 you need to check the return result of your P/Invoked method/function call and act appropriately. Marshal.GetLastWin32Error() comes in handy here.
This is why it's always best to create wrapper classes for any native stuff you need to use. That way you can convert your return results to exceptions and separate your managed and native code.
I'm not sure if there is a definitive list of the exceptions that can be thrown, but I know at least the following can occur
Most of these exceptions types are not specific to PInvoke and can occur at any point in the program. The only one specific to the PInvoke call is the DLL not found exception (who's type I can't remember).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With