dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

Set HResult in own Win32Exception #332

Closed NN--- closed 7 years ago

NN--- commented 7 years ago

The System.ComponentModel.Win32Exception has "_HResult" field which is not updated accordingly to Win32 error code. There is call to SetErrorCode in several cases for predefined codes but not for the Win32 codes. I don't know why the code in .NET doesn't do this while this is super useful.

I suggest to call this internal function via reflection and have the correct HResult value.

AArnott commented 7 years ago

I'm unclear what you're actually asking for here. What code in PInvoke are you suggesting should set this value?

If within an Exception type that we define, there would be no reason to use reflection to call SetErrorCode as all it does is set the HResult property, which has a protected setter (so derived types can call it).

NN--- commented 7 years ago

If the purpose to have same behavior with .NET then this change is not needed.