question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SetLastError=true missing on some extern declarations

See original GitHub issue

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  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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
JeremyKuhnecommented, Apr 29, 2020

As far as pinvoke.net, the owner used to be a Microsoft employee but I am unsure if they are still here. Now that we have github I much prefer that.

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.

3reactions
AArnottcommented, Apr 29, 2020

It would be great to have something like https://www.pinvoke.net/ that has correct signatures and that align to our interop guidelines.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DllImportAttribute.SetLastError Field (System.Runtime. ...
Indicates whether the callee sets an error (SetLastError on Windows or errno on other platforms) before returning from the attributed method.
Read more >
c# - LoadLibrary not failing on missing DLL
For errors to get logged so that they can be read by Marshal.GetLastWin32Error() , you need to have SetLastError=true on the DllImport ...
Read more >
DllImportAttribute Class (System.Runtime.InteropServices)
Indicates whether the callee sets an error ( SetLastError on Windows or errno on other platforms) before returning from the attributed method.
Read more >
c# - What exactly is this API and what is it doing?
Your program somewhere is installing a hook into windows hook chain to monitor some events. The dllimport attribute itself lets the program ...
Read more >
C# code sample to use shared library (dll) generated from ...
In our model we have some inputs and output (which are extern variables as Struct in the shared lib ... [DllImport("kernel32.dll", SetLastError =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found