Skip to content

Improve friendly overloads when buffers are involved #1645

@DoctorKrolic

Description

@DoctorKrolic

Let's consider current CsWin32 friendly overload of GetUserName function:

BOOL GetUserName([Optional] Span<char> lpBuffer, ref uint pcbBuffer);

This is totally fine from the correctness point of view. But consider the following fact: lpBuffer is an [Optional] pointer to an array, which length is specified in pcbBuffer, which is not optional. In windows APIs such pattern means that user can pass null as a buffer and a pointer to a variable, that will recieve required length. CsWin32 just marks the buffer as [Optional], but for that specific pattern we can do better: generate 2 overloads, in one of which the buffer is not optional, but in the other it is removed and buffer length is turned into out reference:

BOOL GetUserName(Span<char> lpBuffer, ref uint pcbBuffer);
BOOL GetUserName(out uint pcbBuffer);

This very much improves usability of the case when the user wants to get the length upfront since out varible can be declared as it is returned. So the pattern will look like this:

PInvoke.GetUserName(out var requiredLength);
Span<char> data = stackalloc[(int)requiredLength];
uint dataLength = (uint)data.Length;
var err = PInvoke.GetUserName(data, ref dataLength);

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions