CsWin32 can generate better friendly overload when optional native arrays are involved
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.5k
- Forks
- 124
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 9
Description
Currently generating RegEnumValue will produce the following friendly signatures:
WIN32_ERROR RegEnumValue(SafeHandle hKey, uint dwIndex, [Optional] Span<char>lpValueName, ref uint lpcchValueName, out uint lpType, [Optional] Span<byte> lpData, ref uint lpcbData)
WIN32_ERROR RegEnumValue(SafeHandle hKey, uint dwIndex, [Optional] Span<char>lpValueName, ref uint lpcchValueName, [Optional] Span<byte> lpData)
Let's focus on last 2 parameters: lpData and lpcbData. lpData is a pointer to a byte array and lpcbData is count of these bytes. This is correctly annotated in metadata via [MemorySize(BytesParamIndex = 7)] attribute. The point here is that both lpData and lpcbData are [Optional]. Since lpcbData is [In], it means it must be assigned a number of bytes in lpData on input. Therefore if this value is not provided (aka passed as null), lpData cannot be anything then also a null. Since lpcbData is upgraded to managed reference in friendly overload, CsWin32 generates 2 different overloads, in one of which it is precent, but in the other it is not. However, lpData is still provided in the second overload with omitted lpcbData, even though the only valid value for it in such circumstances is null. Given that matadata provides all necessary annotations, CsWin32 can be smart here and exclude lpData from the second overload as well and pass a null under the hood. Note, that in the "full" version of friendly overload lpData is still needed to be tagged with [Optional] since lpcbData is also [Out], thus it gets an actual expected number of bytes if lpData is null. If lpcbData was only [In], then that [Optional] annotation on lpData could be dropped as well
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the RegEnumValue friendly overload generation and the metadata for lpData and lpcbData, especially MemorySize(BytesParamIndex = 7), Optional, In, and Out annotations. Trace how the two overloads are produced; done means the overload omitting lpcbData also omits lpData and passes null, while the full overload retains the required optional behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100