SetLastError=true missing on some extern declarations
See original GitHub issueThere 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 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.
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (16 by maintainers)

Top Related StackOverflow Question
A repo with tests is the best thing for confidence. https://www.pinvoke.net/ is a great idea and it has been helpful, but it is full of errors- some of which are not very obvious. While they could be fixed a wiki isn’t nearly as robust as libraries in a gated public repo, particularly one that has governance like the .NET Foundation provides. I don’t use the wiki and I don’t contribute to it as I don’t have the time to validate that my contributions don’t get undone or the confidence that anyone else will keep it from degrading.
As to my own repo, my goals were somewhat different. My largest priority was getting work that I did out in the open so it could be a reference. I experimented quite heavily on its composition, trying to (at one point) handle all usages, including producing libraries that contained only “blessed” Windows Store APIs. I gave up on that and have been pushing on creating wraps that allow writing Win32 apps in a C# style that still feels Win32 like. I don’t really have any known consumers, and I’ve leveraged that to throw different approaches against the wall to see what will stick.
I’ve done several hundred signature wraps and I’m totally fine with people using the code to help contribute to Andrew’s library. I grant full usage without code attribution in this case, or for any .NET Foundation hosted repo. I haven’t done it myself just because I don’t have the need to consume his library. If his moves into the .NET Foundation I might allocate some of my free time to fill in some holes. I might even transition to using it for my base layer.
I like to think that that is exactly the charter of https://github.com/aarnott/pinvoke. We have been meticulous to write signatures that are exactly correct, written to be alloc free, using blittable types so no marshaling is necessary, with test infrastructure, etc. I would hope you can contribute to it, and perhaps if you don’t want to depend on it as an assembly reference you could share source in another way so that customers can continue to reference the pinvoke repo as the authoritative source.
I’ll be happy to respond quickly to your group with any issues or PRs.