microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.07k stars 87 forks source link

CA1416: is only supported on: 'windows' 5.1.2600 and later #1147

Closed myokeeh closed 5 months ago

myokeeh commented 7 months ago

Using

_ = PInvoke.CreateHardLink(pcwstr_pHardlink_filename, pcwstr_pPhoto_filename, null);

and getting the following warning:

Warning CA1416  This call site is reachable on: 'windows' all versions. 'PInvoke.CreateHardLink(PCWSTR, PCWSTR, SECURITY_ATTRIBUTES*)' is only supported on: 'windows' 5.1.2600 and later.

Context

AArnott commented 7 months ago

That looks like it's working as expected. Have you looked at the docs for CA1416 to see how to resolve it?

myokeeh commented 7 months ago

@AArnott, thanks. It seems I left out the part about what I've tried. I've added this to the method, but it doesn't have the desired effect of "muting" that warning.

[System.Runtime.Versioning.SupportedOSPlatform("windows")]

I had other CA1416 warnings and the above worked on those except for ones for PInvoke.CreateHardLink()

AArnott commented 7 months ago

Your attribute doesn't mention what version of Windows the method requires. The warning you're seeing mentions specifically that the API is only available on 5.1.2600 and later, so your attribute needs to carry the same version requirement in order to suppress that warning.

Of course, that may have the effect of moving the warning from the CsWin32 call to the call of your own method. That might be good, if your callers will include runtime version checks (which should suppress the warning automatically) before calling your method. Or I believe if you just change your TargetFramework property from windows to windows5.1.2600 (or whatever the syntax is, then that effectively raises the water-level of your whole project to assume that your minimum version requirement has been met, thereby removing all warnings.