To interact with COM classes like NetFwMgr, the approach depends on your marshaling settings:
Built-in COM Interop (Not AOT-compatible)
Use standard C# casting (e.g., (INetFwMgr)new NetFwMgr()) to perform the equivalent of a native QueryInterface.
COM Wrappers (AOT-compatible)
Use ClassName.CreateInstance<IInterface>(). Note that in this mode, properties are replaced by get_ methods (e.g., get_LocalPolicy()), and some types return ComVariant instead of managed objects. Use ComVariantMarshaller.ConvertToManaged(variant) to convert these to managed interfaces.
// Built-in COM Interop (Not AOT-compatible)
var fwMgr = (INetFwMgr)new NetFwMgr();
var authorizedApplications = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
var aaObjects = new object[authorizedApplications.Count];
var applicationsEnum = (IEnumVARIANT)authorizedApplications._NewEnum;
applicationsEnum.Next((uint)authorizedApplications.Count, aaObjects, out uint fetched);
foreach (var aaObject in aaObjects)
{
var app = (INetFwAuthorizedApplication)aaObject;
Console.WriteLine("---");
Console.WriteLine($"Name: {app.Name.ToString()}");
Console.WriteLine($"Enabled: {(bool)app.Enabled}");
Console.WriteLine($"Remote Addresses: {app.RemoteAddresses.ToString()}");
Console.WriteLine($"Scope: {app.Scope}");
Console.WriteLine($"Process Image Filename: {app.ProcessImageFileName.ToString()}");
Console.WriteLine($"IP Version: {app.IpVersion}");
}
// COM Wrappers (AOT-compatible)
var fwMgr = NetFwMgr.CreateInstance<INetFwMgr>();
var authorizedApplications = fwMgr.get_LocalPolicy().get_CurrentProfile().get_AuthorizedApplications();
var aaObjects = new ComVariant[authorizedApplications.get_Count()];
var applicationsEnum = (IEnumVARIANT)authorizedApplications.get__NewEnum();
applicationsEnum.Next((uint)authorizedApplications.get_Count(), aaObjects, out uint fetched);
foreach (var aaObject in aaObjects)
{
var app = (INetFwAuthorizedApplication)ComVariantMarshaller.ConvertToManaged(aaObject)!;
Console.WriteLine("---");
Console.WriteLine($"Name: {app.get_Name().ToString()}");
Console.WriteLine($"Enabled: {(bool)app.get_Enabled()}");
Console.WriteLine($"Remote Addresses: {app.get_RemoteAddresses().ToString()}");
Console.WriteLine($"Scope: {app.get_Scope()}");
Console.WriteLine($"Process Image Filename: {app.get_ProcessImageFileName().ToString()}");
Console.WriteLine($"IP Version: {app.get_IpVersion()}");
aaObject.Dispose();
}