createActivationFactory<T extends IInspectable> function
T
createActivationFactory<T extends IInspectable>()
Creates the activation factory for the specified Windows Runtime class in
className.
creator must be the constructor of the class to be created (e.g.,
ICalendarFactory.fromPtr).
className must be the name of the Windows Runtime class (e.g.,
Windows.Globalization.Calendar).
iid must be the IID of the object to be created (e.g.,
IID_ICalendarFactory).
final calendarFactory = createActivationFactory(ICalendarFactory.fromPtr,
'Windows.Globalization.Calendar', IID_ICalendarFactory);
Implementation
T createActivationFactory<T extends IInspectable>(
T Function(Pointer<COMObject>) creator, String className, String iid) =>
using((arena) {
final classNameHString = className.toHString();
final pIID = GUIDFromString(iid, allocator: arena);
final activationFactoryPtr = calloc<COMObject>();
try {
final hr = RoGetActivationFactory(
classNameHString, pIID, activationFactoryPtr.cast());
if (FAILED(hr)) throwWindowsException(hr);
// Create and return the relevant interface
return creator(activationFactoryPtr);
} on WindowsException catch (e) {
// If RoGetActivationFactory fails because combase hasn't been loaded yet
// then load combase so that it "just works" for apartment-agnostic code.
if (e.hr == CO_E_NOTINITIALIZED) {
_initializeMTA();
final hr = RoGetActivationFactory(
classNameHString, pIID, activationFactoryPtr.cast());
if (FAILED(hr)) throwWindowsException(hr);
// Create and return the relevant interface
return creator(activationFactoryPtr);
}
rethrow;
}
});