Added nullability to NetListMgr

nullableenabled
David Hall 2023-09-02 13:35:33 -06:00
parent c2e744fb63
commit dcb28cb409
2 changed files with 26 additions and 28 deletions

View File

@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using Vanara.Collections;
[assembly: Guid("DCB00D01-570F-4A9B-8D69-199FDBA5723B")]
#if !NETSTANDARD2_0
@ -174,7 +175,7 @@ public enum NLM_NETWORK_PROPERTY_CHANGE
/// <seealso cref="System.Collections.IEnumerable"/>
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00006-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370706")]
public interface IEnumNetworkConnections : IEnumerable
public interface IEnumNetworkConnections : IEnumerable, ICOMEnum<INetworkConnection>
{
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.</returns>
@ -213,7 +214,7 @@ public interface IEnumNetworkConnections : IEnumerable
/// <seealso cref="System.Collections.IEnumerable"/>
[ComImport, TypeLibType(TypeLibTypeFlags.FDispatchable | TypeLibTypeFlags.FDual), Guid("DCB00003-570F-4A9B-8D69-199FDBA5723B")]
[PInvokeData("Netlistmgr.h", MSDNShortId = "aa370735")]
public interface IEnumNetworks : IEnumerable
public interface IEnumNetworks : IEnumerable, ICOMEnum<INetwork>
{
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.</returns>
@ -463,7 +464,7 @@ public interface INetworkCostManager
/// An <see cref="NLM_SOCKADDR"/> structure containing the destination IPv4/IPv6 address. If NULL, this method will instead return the cost associated with the
/// preferred connection used for machine Internet connectivity.
/// </param>
void GetCost(out NLM_CONNECTION_COST pCost, [In, Optional] NLM_SOCKADDR pDestIPAddr);
void GetCost(out NLM_CONNECTION_COST pCost, [In, Optional] NLM_SOCKADDR? pDestIPAddr);
/// <summary>
/// The GetDataPlanStatus retrieves the data plan status for either a machine-wide internet connection , or the first-hop of routing to a specific
@ -478,7 +479,7 @@ public interface INetworkCostManager
/// An <see cref="NLM_SOCKADDR"/> structure containing the destination IPv4/IPv6 or tunnel address. If NULL, this method returns the cost associated with the
/// preferred connection used for machine Internet connectivity.
/// </param>
void GetDataPlanStatus(out NLM_DATAPLAN_STATUS pDataPlanStatus, [In, Optional] NLM_SOCKADDR pDestIPAddr);
void GetDataPlanStatus(out NLM_DATAPLAN_STATUS pDataPlanStatus, [In, Optional] NLM_SOCKADDR? pDestIPAddr);
/// <summary>
/// The <c>SetDestinationAddresses</c> method registers specified destination IPv4/IPv6 addresses to receive cost or data plan
@ -575,7 +576,7 @@ public interface INetworkCostManagerEvents
/// change is a machine-wide Internet connectivity change.
/// </param>
[PreserveSig]
HRESULT CostChanged([In] NLM_CONNECTION_COST newCost, [In, Optional] NLM_SOCKADDR pDestAddr);
HRESULT CostChanged([In] NLM_CONNECTION_COST newCost, [In, Optional] NLM_SOCKADDR? pDestAddr);
/// <summary>
/// The DataPlanStatusChanged method is called to indicate a change to the status of a data plan associated with either a connection used for
@ -586,7 +587,7 @@ public interface INetworkCostManagerEvents
/// change is a machine-wide Internet connectivity change.
/// </param>
[PreserveSig]
HRESULT DataPlanStatusChanged([In, Optional] NLM_SOCKADDR pDestAddr);
HRESULT DataPlanStatusChanged([In, Optional] NLM_SOCKADDR? pDestAddr);
}
/// <summary>
@ -841,7 +842,7 @@ public sealed class NLM_SOCKADDR
/// <summary>Creates a <see cref="NLM_SOCKADDR"/> from an <see cref="IPAddress"/> instance.</summary>
/// <param name="address">The IP address to encapsulate.</param>
/// <returns>A <see cref="NLM_SOCKADDR"/> instance with its data field set to either the IPv4 or IPv6 address supplied by <paramref name="address"/>.</returns>
public static NLM_SOCKADDR FromIPAddress(IPAddress address)
public static NLM_SOCKADDR? FromIPAddress(IPAddress? address)
{
const ushort AF_INET = 2;
const ushort AF_INET6 = 23;

View File

@ -1,10 +1,7 @@
using NUnit.Framework;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Vanara.Extensions;
using Vanara.PInvoke.NetListMgr;
using Vanara.InteropServices;
using System.Runtime.InteropServices;
//using NETWORKLIST;
@ -13,8 +10,8 @@ namespace Vanara.PInvoke.Tests;
[TestFixture]
public class NetListMgrTests
{
private static INetworkListManager mgr;
private static INetworkCostManager coster;
private static INetworkListManager? mgr;
private static INetworkCostManager? coster;
[OneTimeSetUp]
public static void OneTimeSetup()
@ -26,19 +23,19 @@ public class NetListMgrTests
[Test]
public void GetNetworksTest()
{
var ns = mgr.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
var ns = mgr!.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
Assert.That(ns, Is.Not.Null);
var n = ns.Cast<INetwork>().FirstOrDefault();
Assert.That(n, Is.Not.Null);
Assert.That(mgr.IsConnected, Is.True);
Assert.That(mgr.IsConnectedToInternet, Is.True);
Assert.That((int)mgr.GetConnectivity(), Is.GreaterThan(0));
Assert.That(mgr!.IsConnected, Is.True);
Assert.That(mgr!.IsConnectedToInternet, Is.True);
Assert.That((int)mgr!.GetConnectivity(), Is.GreaterThan(0));
}
[Test]
public void GetNetworksTest1()
{
using var ns = ComReleaserFactory.Create(mgr.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED));
using var ns = ComReleaserFactory.Create(mgr!.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED));
Assert.That(ns.Item, Is.Not.Null);
var connections = new INetwork[5];
ns.Item.Next((uint)connections.Length, connections, out uint fetched);
@ -50,7 +47,7 @@ public class NetListMgrTests
[Test]
public void GetNetworkConnectionsTest()
{
var ns = mgr.GetNetworkConnections();
var ns = mgr!.GetNetworkConnections();
Assert.That(ns, Is.Not.Null);
var n = ns.Cast<INetworkConnection>().FirstOrDefault();
Assert.That(n, Is.Not.Null);
@ -59,7 +56,7 @@ public class NetListMgrTests
[Test]
public void GetNetworkConnectionsTest1()
{
var ns = ComReleaserFactory.Create(mgr.GetNetworkConnections());
var ns = ComReleaserFactory.Create(mgr!.GetNetworkConnections());
Assert.That(ns.Item, Is.Not.Null);
var connections = new INetworkConnection[5];
ns.Item.Next((uint)connections.Length, connections, out uint fetched);
@ -71,12 +68,12 @@ public class NetListMgrTests
[Test]
public void GetNetworkTest()
{
var ns = mgr.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
var ns = mgr!.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED);
Assert.That(ns, Is.Not.Null);
var n = ns.Cast<INetwork>().FirstOrDefault();
Assert.That(n, Is.Not.Null);
var g = n.GetNetworkId();
var n1 = mgr.GetNetwork(g);
var g = n!.GetNetworkId();
var n1 = mgr!.GetNetwork(g);
Assert.That(n.GetName() == n1.GetName());
Assert.That(n.GetName(), Is.Not.Null);
var nm = n.GetName();
@ -106,12 +103,12 @@ public class NetListMgrTests
[Test]
public void GetNetworkConnectionTest()
{
var ns = mgr.GetNetworkConnections();
var ns = mgr!.GetNetworkConnections();
Assert.That(ns, Is.Not.Null);
var n = ns.Cast<INetworkConnection>().FirstOrDefault();
Assert.That(n, Is.Not.Null);
var g = n.GetConnectionId();
var n1 = mgr.GetNetworkConnection(g);
var g = n!.GetConnectionId();
var n1 = mgr!.GetNetworkConnection(g);
Assert.That(n.GetConnectionId() == n1.GetConnectionId());
Assert.That(n.GetAdapterId() == n1.GetAdapterId());
Assert.That(n.GetNetwork(), Is.Not.Null);
@ -132,11 +129,11 @@ public class NetListMgrTests
public void GetCostTest()
{
NLM_CONNECTION_COST ret = 0;
Assert.That(() => coster.GetCost(out ret), Throws.Nothing);
Assert.That(() => coster!.GetCost(out ret), Throws.Nothing);
TestContext.WriteLine($"Cost:{ret}");
Assert.That((int)ret, Is.GreaterThan(0));
var status = new NLM_DATAPLAN_STATUS();
Assert.That(() => coster.GetDataPlanStatus(out status), Throws.Nothing);
Assert.That(() => coster!.GetDataPlanStatus(out status), Throws.Nothing);
Assert.That(status.InterfaceGuid, Is.Not.EqualTo(Guid.Empty));
TestContext.WriteLine($"Guid:{status.InterfaceGuid}; Limit:{status.DataLimitInMegabytes:X}; Xfer:{status.MaxTransferSizeInMegabytes:X}");
}