diff --git a/packages/windows_foundation/lib/src/exports.g.dart b/packages/windows_foundation/lib/src/exports.g.dart index e0923e66..16689a28 100644 --- a/packages/windows_foundation/lib/src/exports.g.dart +++ b/packages/windows_foundation/lib/src/exports.g.dart @@ -18,6 +18,7 @@ export 'collections/stringmap.dart'; export 'collections/valueset.dart'; export 'extensions/extensions.dart'; export 'extensions/iunknown_helpers.dart'; +export 'guidhelper.dart'; export 'helpers.dart'; export 'iclosable.dart'; export 'imemorybuffer.dart'; diff --git a/packages/windows_foundation/lib/src/guidhelper.dart b/packages/windows_foundation/lib/src/guidhelper.dart new file mode 100644 index 00000000..93842175 --- /dev/null +++ b/packages/windows_foundation/lib/src/guidhelper.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// THIS FILE IS GENERATED AUTOMATICALLY AND SHOULD NOT BE EDITED DIRECTLY. + +// ignore_for_file: constant_identifier_names, non_constant_identifier_names +// ignore_for_file: unnecessary_import, unused_import + +import 'dart:async'; +import 'dart:ffi'; + +import 'package:ffi/ffi.dart'; +import 'package:win32/win32.dart' + hide DocumentProperties, WinRTStringConversion; + +import '../internal.dart'; +import 'collections/iiterator.dart'; +import 'extensions/extensions.dart'; +import 'helpers.dart'; +import 'iguidhelperstatics.dart'; + +/// A class containing static helper methods for working with the Guid type. +class GuidHelper extends IInspectable { + GuidHelper.fromPtr(super.ptr); + + static const _className = 'Windows.Foundation.GuidHelper'; + + static Guid createNewGuid() => createActivationFactory( + IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics) + .createNewGuid(); + + static Guid get empty => createActivationFactory( + IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics) + .empty; + + static bool equals(Guid target, Guid value) => createActivationFactory( + IGuidHelperStatics.fromPtr, _className, IID_IGuidHelperStatics) + .equals(target, value); +} diff --git a/packages/windows_foundation/lib/src/iguidhelperstatics.dart b/packages/windows_foundation/lib/src/iguidhelperstatics.dart new file mode 100644 index 00000000..a78b9b32 --- /dev/null +++ b/packages/windows_foundation/lib/src/iguidhelperstatics.dart @@ -0,0 +1,113 @@ +// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// THIS FILE IS GENERATED AUTOMATICALLY AND SHOULD NOT BE EDITED DIRECTLY. + +// ignore_for_file: constant_identifier_names, non_constant_identifier_names +// ignore_for_file: unnecessary_import, unused_import + +import 'dart:async'; +import 'dart:ffi'; + +import 'package:ffi/ffi.dart'; +import 'package:win32/win32.dart' + hide DocumentProperties, WinRTStringConversion; + +import '../internal.dart'; +import 'collections/iiterator.dart'; +import 'extensions/extensions.dart'; +import 'helpers.dart'; + +/// @nodoc +const IID_IGuidHelperStatics = '{59c7966b-ae52-5283-ad7f-a1b9e9678add}'; + +class IGuidHelperStatics extends IInspectable { + // vtable begins at 6, is 3 entries long. + IGuidHelperStatics.fromPtr(super.ptr); + + factory IGuidHelperStatics.from(IInspectable interface) => + interface.cast(IGuidHelperStatics.fromPtr, IID_IGuidHelperStatics); + + Guid createNewGuid() { + final result = calloc(); + + try { + final hr = ptr.ref.vtable + .elementAt(6) + .cast< + Pointer< + NativeFunction< + HRESULT Function( + VTablePointer lpVtbl, Pointer result)>>>() + .value + .asFunction< + int Function(VTablePointer lpVtbl, Pointer result)>()( + ptr.ref.lpVtbl, result); + + if (FAILED(hr)) throwWindowsException(hr); + + return result.toDartGuid(); + } finally { + free(result); + } + } + + Guid get empty { + final value = calloc(); + + try { + final hr = ptr.ref.vtable + .elementAt(7) + .cast< + Pointer< + NativeFunction< + HRESULT Function( + VTablePointer lpVtbl, Pointer value)>>>() + .value + .asFunction< + int Function(VTablePointer lpVtbl, Pointer value)>()( + ptr.ref.lpVtbl, value); + + if (FAILED(hr)) throwWindowsException(hr); + + return value.toDartGuid(); + } finally { + free(value); + } + } + + bool equals(Guid target, Guid value) { + final result = calloc(); + + try { + final targetNativeStructPtr = target.toNativeGUID(); + final valueNativeStructPtr = value.toNativeGUID(); + + final hr = ptr.ref.vtable + .elementAt(8) + .cast< + Pointer< + NativeFunction< + HRESULT Function( + VTablePointer lpVtbl, + Pointer target, + Pointer value, + Pointer result)>>>() + .value + .asFunction< + int Function(VTablePointer lpVtbl, Pointer target, + Pointer value, Pointer result)>()( + ptr.ref.lpVtbl, targetNativeStructPtr, valueNativeStructPtr, result); + + free(targetNativeStructPtr); + free(valueNativeStructPtr); + + if (FAILED(hr)) throwWindowsException(hr); + + return result.value; + } finally { + free(result); + } + } +} diff --git a/packages/windows_foundation/test/guidhelper_test.dart b/packages/windows_foundation/test/guidhelper_test.dart new file mode 100644 index 00000000..981afcfe --- /dev/null +++ b/packages/windows_foundation/test/guidhelper_test.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2023, Dart | Windows. Please see the AUTHORS file for details. +// All rights reserved. Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +@TestOn('windows') + +import 'package:test/test.dart'; +import 'package:win32/win32.dart' show isWindowsRuntimeAvailable; +import 'package:windows_foundation/internal.dart'; +import 'package:windows_foundation/windows_foundation.dart'; + +void main() { + if (!isWindowsRuntimeAvailable()) { + print('Skipping tests because Windows Runtime is not available.'); + return; + } + + group('GuidHelper', () { + test('createNewGuid', () { + final guid = GuidHelper.createNewGuid(); + final guidAsString = guid.toString(); + expect(guidAsString.length, equals(38)); + expect(guidAsString, startsWith('{')); + expect(guidAsString, endsWith('}')); + }); + + test('empty', () { + final guid = GuidHelper.empty; + expect(guid.toString(), equals('{00000000-0000-0000-0000-000000000000}')); + }); + + test('equals', () { + final emptyGuid = GuidHelper.empty; + expect(GuidHelper.equals(emptyGuid, emptyGuid), isTrue); + expect(GuidHelper.equals(GuidHelper.createNewGuid(), emptyGuid), isFalse); + }); + }); + + tearDownAll(forceGC); +} diff --git a/packages/winrtgen/assets/objects.json b/packages/winrtgen/assets/objects.json index e8cc47c8..655b73cb 100644 --- a/packages/winrtgen/assets/objects.json +++ b/packages/winrtgen/assets/objects.json @@ -83,6 +83,7 @@ "Windows.Foundation.Collections.PropertySet": "Represents a property set, which is a set of [PropertyValue] objects with string keys.", "Windows.Foundation.Collections.StringMap": "An associative collection, also known as a map or a dictionary.", "Windows.Foundation.Collections.ValueSet": "Implements a map with keys of type String and values of type Object. Object must be a WinRT [PropertyValue] or [ValueSet]. As a [PropertyValue], it can be any type except [PropertyType] `InspectableArray`. This limitation exists to ensure that the value can be serialized; passed by value across a process boundary.", + "Windows.Foundation.GuidHelper": "A class containing static helper methods for working with the Guid type.", "Windows.Foundation.IAsyncAction": "Represents an asynchronous action. This is the return type for many Windows Runtime asynchronous methods that don't have a result object, and don't report ongoing progress.", "Windows.Foundation.IAsyncInfo": "Supports asynchronous actions and operations. `IAsyncInfo` is a base interface for `IAsyncAction`, `IAsyncActionWithProgress`, `IAsyncOperation` and `IAsyncOperationWithProgress`, each of which support combinations of return type and progress for an asynchronous method.", "Windows.Foundation.IClosable": "Defines a method to release allocated resources.",