dotnet / dotnet/winforms

SetLastError=true missing on some extern declarations

Open
#3,151 16 comments 0 reactions 0 assignees View on GitHub
area-Interop design-discussion
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

There are quite a lot of extern declarations for Win32 API in the winforms repo that don't use SetLastError=true even though the docs indicate the API sets an error retrievable with GetLastError. (I attached an approximate list of some).

Of course in many, possibly all of those cases the code never calls `Marshal.GetLastWin32Error()` nor throws `Win32Exception` so it doesn't matter. I am not sure whether to interpret [our guidance](https://docs.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#dllimport-attribute-settings)  to mean "apply SetLastError=true whenever the API is documented to set last error" or "apply SetLastError=true whenever the API sets last error and you also intend to retrieve it". The latter saves the infrastructure calling GetLastError() for you unnecessarily, but that is presumably very fast - and avoids the possibility of reading some previous API's error because you hadn't updated the extern declaration.

@AaronRobinsonMSFT @JeremyKuhne ?

[list.txt](https://github.com/dotnet/winforms/files/4543243/list.txt)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.