Skip to content

Instantly share code, notes, and snippets.

@timsneath
Created March 23, 2021 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsneath/4ddbb6276cc519ebc5a3c0935e1ab088 to your computer and use it in GitHub Desktop.
Save timsneath/4ddbb6276cc519ebc5a3c0935e1ab088 to your computer and use it in GitHub Desktop.
Demonstrates failed attempt to retrieve system interfaces
Resolving Windows.Media.Playback.IMediaPlaybackItem:
This type implements 1 interface(s).
rImpl token: 0x090002ba.
iface token: 0x010004f6. typeRef: true
iface represents: Windows.Media.Playback.IMediaPlaybackSource
ptkResolutionScope is: 0x00000001
Resolving Windows.Foundation.Collections.IPropertySet:
This type implements 3 interface(s).
rImpl token: 0x09000005.
iface token: 0x1b000006. typeRef: true
iface represents:
ptkResolutionScope is: 0x00000000
rImpl token: 0x09000006.
iface token: 0x1b000007. typeRef: true
iface represents:
ptkResolutionScope is: 0x00000000
rImpl token: 0x09000007.
iface token: 0x1b000008. typeRef: true
iface represents:
ptkResolutionScope is: 0x00000000
Resolving Windows.Foundation.Collections.IObservableMap`2:
This type implements 1 interface(s).
rImpl token: 0x09000003.
iface token: 0x1b000002. typeRef: true
iface represents:
ptkResolutionScope is: 0x00000000
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
import 'package:winmd/winmd.dart';
void main() {
const types = <String>[
// This works; it implements IMediaPlaybackSource
'Windows.Media.Playback.IMediaPlaybackItem',
// Gives interfaces but can't resolve them
'Windows.Foundation.Collections.IPropertySet',
// This does not work; it implements IDictionary`2, which is in mscorlib
'Windows.Foundation.Collections.IObservableMap`2',
];
for (final type in types) {
final typeDef = MetadataStore.getMetadataForType(type)!;
print('Resolving ${typeDef.typeName}:');
final reader = typeDef.reader;
// Get first implemented interface
final phEnum = calloc<IntPtr>();
final rImpls = calloc<Uint32>(256);
final pcImpls = calloc<Uint32>();
var hr =
reader.EnumInterfaceImpls(phEnum, typeDef.token, rImpls, 256, pcImpls);
print('This type implements ${pcImpls.value} interface(s).');
if (FAILED(hr)) {
throw WindowsException(hr);
}
for (var i = 0; i < pcImpls.value; i++) {
final rImpl = rImpls.elementAt(i).value;
print('rImpl token: ${rImpl.toHexString(32)}.');
// Process interface token
final pClass = calloc<Uint32>();
final ptkIface = calloc<Uint32>();
hr = reader.GetInterfaceImplProps(rImpl, pClass, ptkIface);
if (FAILED(hr)) {
throw WindowsException(hr);
}
final iface = ptkIface.value;
print('iface token: ${iface.toHexString(32)}. '
'typeRef: ${tokenIsTypeRef(iface)}');
// Resolve interface token
final ptkResolutionScope = calloc<Uint32>();
final szName = calloc<Uint16>(256).cast<Utf16>();
final pchName = calloc<Uint32>();
hr = reader.GetTypeRefProps(
iface, ptkResolutionScope, szName, 256, pchName);
if (FAILED(hr)) {
throw WindowsException(hr);
}
print('iface represents: ${szName.toDartString()}');
print(
'ptkResolutionScope is: ${ptkResolutionScope.value.toHexString(32)}');
}
print('');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment