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
1.99k stars 84 forks source link

Invalid SupportedArchitectureAttribute on Win32 type #1083

Closed Ephron-WL closed 8 months ago

Ephron-WL commented 8 months ago

Actual behavior

Exception: PlatformIncompatibleException: Windows.Win32.System.Diagnostics.Debug.CONTEXT is not declared for this platform

Expected behavior

Code gen.

Repro steps

  1. NativeMethods.txt content:

IDebugControl4

  1. NativeMethods.json content (if present):

n/a

  1. Any of your own code that should be shared?

Context

The Windows.Win32.System.Diagnostics.Debug.CONTEXT appears to have this attribute:

[SupportedArchitecture(Windows.Win32.Foundation.Metadata.Architecture.Arm64)]

This attribute is misplaced. See CONTEXT structure.

Interestingly, there is also a ARM64_NT_CONTEXT that probably should have this attribute and possibly influenced the generation of the errant attribute on CONTEXT.

AArnott commented 8 months ago

I can't repro the problem.

What platform are you compiling for when you see this exception? When I target AnyCPU, I (rightly) see this as a compile error:

warning PInvoke005: This API is only available when targeting a specific CPU architecture. AnyCPU cannot generate this API.

When I target x64 or arm64, compilation succeeds, as expected.

This is using CsWin32 0.3.49-beta. What version are you using?

Additional info: The ARM64_NT_CONTEXT struct distinct from CONTEXT and is only defined for x64 and x86 architectures. CONTEXT itself is defined 3 times in the metadata, because it is architecture specific. The presence of the SupportedArchitecture attribute on the struct is therefore not misplaced as far as I can tell.

Ephron-WL commented 8 months ago

Ok, I see what you mean. There are three CONTEXT classes, each with an individual architecture attribute (before I only noticed the one). I'm not using the CsWin32 in a conventional manner. The Generator needs to resolve the target platform. I was able to modify my code so that it could do that and it works fine now. Thank you for your assistance. It is much appreciated.