PresentationNative DllImport workaround to SetLastErrror(0) to deal with ambiguous returns (eg NULL is both valid & err) can be removed
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
https://github.com/dotnet/wpf/blob/ac9d1b7a6b0ee7c44fd2875a1174b820b3940619/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsSetLastError.cs#L45-L46
Several P/Invokes exist into PresentationNative that do this:
```C
SetLastError(0);
return CallWin32Fn(...);
```
For eg, `GetWindowLong`, `GetWindowTextLength` etc. use this pattern. This is needed because the function can return NULL, `0` etc. both as a valid value and as an error, and the caller must rely `GetLastErrror` to disambiguate and know for sure whether the call succeeded or failed when such a return value is observed.
In . NET Core, [Marshal.GetLastWin32Error Method](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error) says:
> There is a difference in the behavior of the GetLastWin32Error method on .NET Core and .NET Framework when DllImportAttribute.SetLastError is true. On .NET Framework, the GetLastWin32Error method can retain error information from one P/Invoke call to the next. On .NET Core, error information is cleared before P/Invoke call, and the GetLastWin32Error represents only error information from the last method call.
Given this, it’s likely that these P/Invokes can be simplified and the dependency on PresentationNative can be removed.
/cc @SamBent
Contributor guide
Assessment
This issue has not been assessed yet.