Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark optional parameters in Win32 functions and COM methods as nullable #822

Open
halildurmus opened this issue Feb 25, 2024 · 0 comments
Open
Assignees
Labels
enhancement New feature or request generator Issue with package:generator
Milestone

Comments

@halildurmus
Copy link
Member

In certain Win32 functions and COM methods, some parameters are designated as optional, indicating that a 0 (NULL) or nullptr can be passed as an argument, depending on the parameter type.

To enhance the developer experience, these optional parameters can be marked as nullable by the generator. This allows 0 or nullptr to be passed implicitly if the caller passes null.

Consider the CreateWindowEx function as an example, currently projected as:

int CreateWindowEx(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int X,
        int Y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam) =>
    _CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth,
        nHeight, hWndParent, hMenu, hInstance, lpParam);

final _CreateWindowEx = _user32.lookupFunction<
    IntPtr Function(
        Uint32 dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        Uint32 dwStyle,
        Int32 X,
        Int32 Y,
        Int32 nWidth,
        Int32 nHeight,
        IntPtr hWndParent,
        IntPtr hMenu,
        IntPtr hInstance,
        Pointer lpParam),
    int Function(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int X,
        int Y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam)>('CreateWindowExW');

Here's the proposed projection for this function, marking the optional parameters as nullable and implicitly passing 0 or nullptr if the caller passes null:

int CreateWindowEx(
        int dwExStyle,
        Pointer<Utf16>? lpClassName,
        Pointer<Utf16>? lpWindowName,
        int dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        int? hWndParent,
        int? hMenu,
        int? hInstance,
        Pointer? lpParam) =>
    _CreateWindowEx(
        dwExStyle,
        lpClassName ?? nullptr,
        lpWindowName ?? nullptr,
        dwStyle,
        x,
        y,
        nWidth,
        nHeight,
        hWndParent ?? 0,
        hMenu ?? 0,
        hInstance ?? 0,
        lpParam ?? nullptr);

final _CreateWindowEx = _user32.lookupFunction<
    HWND Function(
        Uint32 dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        Uint32 dwStyle,
        Int32 x,
        Int32 y,
        Int32 nWidth,
        Int32 nHeight,
        HWND hWndParent,
        HMENU hMenu,
        HINSTANCE hInstance,
        Pointer lpParam),
    int Function(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam)>('CreateWindowExW');
@halildurmus halildurmus added the enhancement New feature or request label Feb 25, 2024
@halildurmus halildurmus added this to the 6.0.0 milestone Feb 25, 2024
@halildurmus halildurmus self-assigned this Feb 25, 2024
@halildurmus halildurmus added the generator Issue with package:generator label Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request generator Issue with package:generator
Projects
None yet
Development

No branches or pull requests

1 participant