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

Methods/properties that return WinRT objects should be nullable #647

Closed
halildurmus opened this issue Jan 6, 2023 · 2 comments · Fixed by #650
Closed

Methods/properties that return WinRT objects should be nullable #647

halildurmus opened this issue Jan 6, 2023 · 2 comments · Fixed by #650
Assignees
Labels
bug Something isn't working as expected package: generator Issue with package:generator

Comments

@halildurmus
Copy link
Owner

halildurmus commented Jan 6, 2023

For example, NetworkInformation.GetInternetConnectionProfile Method states:

The profile for the connection currently used to connect the machine to the Internet, or null if there is no connection profile with a suitable connection.

Here is an example that calls this method (make sure you are disconnected from the internet):

import 'package:win32/winrt.dart';

class ConnectionProfile extends IInspectable {
  ConnectionProfile.fromRawPointer(super.ptr);
}

ConnectionProfile getInternetConnectionProfile() {
  final activationFactoryPtr = CreateActivationFactory(
      'Windows.Networking.Connectivity.NetworkInformation',
      IID_INetworkInformationStatics);
  final object =
      INetworkInformationStatics.fromRawPointer(activationFactoryPtr);

  try {
    return ConnectionProfile.fromRawPointer(
        object.getInternetConnectionProfile());
  } finally {
    object.release();
  }
}

void main() {
  final profile = getInternetConnectionProfile();
  print(profile.runtimeClassName); // The program will exit with an Access Violation error code
}

There is no information on WinMD files on whether a method/property can return null or not. That's why we need to mark methods/properties that return WinRT objects (except async delegates e.g. IAsyncAction, collection interfaces e.g. IVector, IMap etc., and methods from XXFactory interfaces) as nullable (see microsoft/windows-rs#331).

I actually did a similar thing in #465. FileOpenPicker's pickSingleFileAsync() returns IAsyncOperation<StorageFile>. Since the type argument is a WinRT object, we consider it nullable and expose it as Future<StorageFile?>. As pickMultipleFilesAsync() returns IAsyncOperation<IVectorView<StorageFile>> and the type argument is a collection interface we don't consider it nullable and expose it as Future<List<StorageFile>>.

@halildurmus halildurmus added bug Something isn't working as expected winrt package: generator Issue with package:generator labels Jan 6, 2023
@halildurmus halildurmus self-assigned this Jan 6, 2023
@timsneath
Copy link
Contributor

Yuck. I don't love it, but I also see that it's necessary! Thanks for catching this.

@halildurmus
Copy link
Owner Author

Yep, I hate it too :(.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working as expected package: generator Issue with package:generator
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants