Skip to content

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

@halildurmus

Description

@halildurmus

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);

Metadata

Metadata

Assignees

Labels

P2Medium-priority issuebreaking changeChanges that break existing functionalityfeatureA new feature or requestpackage: generatorIssue with package:generator

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions