Best Way to Get Network Cost on .Net Core 2.0

Is there a way to find out the cost of a network on .Net Core 2.0?
This is how I get the network cost in my C ++ code:

hr = pNetworkCostManager->GetCost(&dwCost, NULL); if (hr == S_OK) { switch (dwCost) { case NLM_CONNECTION_COST_UNRESTRICTED: case NLM_CONNECTION_COST_FIXED: case NLM_CONNECTION_COST_VARIABLE: case NLM_CONNECTION_COST_OVERDATALIMIT: case NLM_CONNECTION_COST_CONGESTED: case NLM_CONNECTION_COST_ROAMING: case NLM_CONNECTION_COST_APPROACHINGDATALIMIT: case NLM_CONNECTION_COST_UNKNOWN: } } 

One thing that .Net Core (2.0) has is NetworkInterfaceType ( https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterfacetype?view=netcore-1.0 )

Based on NetworkInterfaceType, I see if it has Wi-Fi, network or mobile connection, but this will not result in a cost.
Is there a way to find out the cost of a network on .Net Core 2.0?

+5
source share
1 answer

To call GetCost you have to use the COM interface. If you need to make this particular call, i.e. Pass NULL instead of the IP address in GetCost , then you will need to define the COM interface yourself to satisfy your needs, for example. eg:

 using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; [ComImport, Guid("DCB00C01-570F-4A9B-8D69-199FDBA5723B"), ClassInterface(ClassInterfaceType.None)] public class NetworkListManager : INetworkCostManager { [MethodImpl(MethodImplOptions.InternalCall)] public virtual extern void GetCost(out uint pCost, [In] IntPtr pDestIPAddr); } [ComImport, Guid("DCB00008-570F-4A9B-8D69-199FDBA5723B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface INetworkCostManager { void GetCost(out uint pCost, [In] IntPtr pDestIPAddr); } 

Then you can get the cost information as follows:

 new NetworkListManager().GetCost(out uint cost, IntPtr.Zero); 

If you do not have a requirement to pass NULL to GetCost , you can simply add a link to the COM type library “List List 1.0 Type Library”, set “Insert Interaction Types” to false and use these definitions .

+3
source

Source: https://habr.com/ru/post/1271991/


All Articles