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

Abstract away reserved parameters in Win32 functions and COM methods #821

Open
halildurmus opened this issue Feb 25, 2024 · 0 comments
Open
Assignees
Labels
breaking change Changes that break existing functionality feature A new feature or request P2 Medium-priority issue package: generator Issue with package:generator
Milestone

Comments

@halildurmus
Copy link
Owner

halildurmus commented Feb 25, 2024

In certain Win32 functions and COM methods, some parameters are designated as reserved, meaning they must be explicitly set to NULL or nullptr, depending on the parameter's type. These reserved parameters often serve no functional purpose for the caller and only exist to maintain API compatibility.

To improve the developer experience, these reserved parameters can be abstracted away by the generator, allowing NULL or nullptr to be passed automatically behind the scenes.

Take the CoInitializeEx function as an example. Its current projection in Dart is as follows:

int CoInitializeEx(Pointer pvReserved, int dwCoInit) =>
    _CoInitializeEx(pvReserved, dwCoInit);

final _CoInitializeEx = _ole32.lookupFunction<
    Int32 Function(Pointer pvReserved, Int32 dwCoInit),
    int Function(Pointer pvReserved, int dwCoInit)>('CoInitializeEx');

The first parameter, pvReserved, is marked as reserved and must be set to nullptr. This results in a typical call like this:

CoInitializeEx(nullptr, COINIT_MULTITHREADED);

To simplify this, the function projection can be adjusted to implicitly pass nullptr for the reserved parameter, eliminating the need for the caller to provide it manually:

int CoInitializeEx(int dwCoInit) => _CoInitializeEx(nullptr, dwCoInit);

final _CoInitializeEx = _ole32.lookupFunction<
    HRESULT Function(Pointer pvReserved, Uint32 dwCoInit),
    int Function(Pointer pvReserved, int dwCoInit)>('CoInitializeEx');

With this change, calling the function becomes much cleaner and more intuitive:

CoInitializeEx(COINIT_MULTITHREADED);
@halildurmus halildurmus added the feature A 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 package: generator Issue with package:generator label Feb 25, 2024
@halildurmus halildurmus added P2 Medium-priority issue breaking change Changes that break existing functionality labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Changes that break existing functionality feature A new feature or request P2 Medium-priority issue package: generator Issue with package:generator
Projects
None yet
Development

No branches or pull requests

1 participant