dart-windows / win32

Build Win32 apps with Dart!
https://win32.pub
BSD 3-Clause "New" or "Revised" License
735 stars 118 forks source link

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

Open halildurmus opened 4 months ago

halildurmus commented 4 months ago

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

To enhance the developer experience, these reserved parameters can be abstracted away by the generator, allowing NULL or nullptr to be passed implicitly.

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

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 is marked as reserved, with a type of Pointer, indicating that the caller must pass nullptr as an argument:

CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

Here's the proposed projection for this function, abstracting away the reserved parameter and implicitly passing nullptr:

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 adjustment, the function can now be called simply as:

CoInitializeEx(COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);