win32
win32 copied to clipboard
Figure out async calls to WinRT
As an example, this doesn't work yet: https://github.com/timsneath/win32/blob/main/example/winrt_picker.dart#L20
FileOpenPicker is here: https://github.com/timsneath/win32/blob/main/lib/src/com/IFileOpenPicker.dart
which is derived using the Windows Runtime metadata using my Dart-based parser (https://pub.dev/packages/winmd).
The original signature is here: https://docs.microsoft.com/en-us/uwp/api/windows.storage.pickers.fileopenpicker.picksinglefileasync?view=winrt-19041
Here's a Go-based example from @mraleph: https://github.com/mraleph/go_dart_ffi_example
and more on the general issue: https://github.com/dart-lang/sdk/issues/37022
I don't know anything about Dart (yet) 😉 but am the creator of C++/WinRT and more recently started creating Rust/WinRT.
A WinRT async method returns one of four async interfaces (IAsyncXxx). The caller is responsible for either blocking the calling thread until the async object is complete or using some form of coroutine/future, depending on what the language provides, to cooperatively wait for completion. Either way, the caller is notified of completion by providing a delegate implementation that is provided to the async implementation via the Completed property. Obviously, you'll need a way to create WinRT delegates in Dart. Once complete, the caller uses the GetResults method that will either return the successful result of the async object or rethrow/return any error that may have occurred. Both C++/WinRT and Rust/WinRT provide implementations of this pattern for reference. As an example, a C++ blocking call looks something like this:
auto file = picker.PickSingleFileAsync().get();
And C++ coroutines are used for cooperatively waiting (without blocking calling thread):
auto file = co_await picker.PickSingleFileAsync();
Happy to answer any questions you might have: [email protected]
I also wanted to know, how async callbacks work for flutter/dart (not necessarily in conjunction with WinRT though). I built a proof of concept here: https://github.com/ghost23/win32_midi
If you're interested, the method in question there is Win32Midi.openMidiInput(...) in ./lib/win32_midi.dart and its counterpart openMidiInput(...) in ./windows/win32_midi_plugin.cpp, which is taking an async callback and then answers it in the function MidiInProc in the same file.
Also important is the CallbackManager.h/cpp , which manages the port and the conversion of data from c++ to dart.
@jhancock4d from https://github.com/flutter/flutter/issues/64958#issuecomment-870581871
You could track the WinRT projection here