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.12k stars 90 forks source link

Emit nullable ref annotated code #130

Open AArnott opened 3 years ago

AArnott commented 3 years ago

When the project langversion is >= 8, could you also put #nullable enable or at least #nullable annotations in the file or around the parameter declaration and use SafeHandle? as the parameter type so that IntelliSense shows me that nulls are anticipated?

Originally posted by @jnm2 in https://github.com/microsoft/CsWin32/issues/129#issuecomment-781466704

If we annotate at all, we need to be consistent so users can trust that a parameter is required when we don't annotate it. The metadata has many [Optional] annotations on its inputs to help with this, but I don't know how consistently it is applied.

jnm2 commented 3 years ago

The metadata has many [Optional] annotations on its inputs to help with this, but I don't know how consistently it is applied.

One place where [Optional] is currently missing is on SafeHandle parameters.

JeremyKuhne commented 2 years ago

This would be nice to have as a consuming library that has nullable enabled.

AArnott commented 2 years ago

@JeremyKuhne, as nullable refs only apply for reference types, how often do you expect to be passing reference types into interop? Can you give a few examples of APIs that you use where a null ref is allowed and you'd like to see that expressed?

JeremyKuhne commented 2 years ago

@AArnott Not looking for this for the [DllImport]s themselves, looking for the wrappers you generate. Most notably for us this comes up with string. CreateWindowEx is an example where there are optional LPCWSTR parameters.

AArnott commented 2 years ago

We ought to be able to do that. CreateWindowEx's parameter is annotated as optional in the metadata.

rampaa commented 5 months ago

Can you give a few examples of APIs that you use where a null ref is allowed and you'd like to see that expressed?

FindWindowW

Considering migrating my app from LibraryImport to CsWin32 but not having the nullability annotations feels like a step down.