Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Internet Connection Sharing

/// <summary>
/// Manage ICS using COMObject HNetCfg.HNetShare
/// </summary>
static class NetConnectionDispatch {
    private static object _netShare;

    static NetConnectionDispatch() {
        Process.Start(new ProcessStartInfo("regsvr32.exe", "/s hnetcfg.dll"));
    }

    public static NetConnectionSharing[] GetAllNetConnections() {
        _netShare = ProgIdInstance("HNetCfg.HNetShare");

        List<NetConnectionSharing> nets = new List<NetConnectionSharing>();
        foreach (var i in EnumEveryConnection()) {
            nets.Add(GetNetConnectionSharingObject(i));
        }
        return nets.ToArray();
    }

    private static NetConnectionSharing GetNetConnectionSharingObject(object i) {
        var ncp = Invoke(_netShare, "NetConnectionProps", new[] { i });
        var inscfinc = Invoke(_netShare, "INetSharingConfigurationForINetConnection", new[] { i });

        NetConnectionSharing netConnection = new NetConnectionSharing() {
            Guid = (string)GetPropertyValue(ncp, "Guid"),
            Name = (string)GetPropertyValue(ncp, "Name"),
            DeviceName = (string)GetPropertyValue(ncp, "DeviceName"),
            Status = (NETCON_STATUS)GetPropertyValue(ncp, "Status"),
            MediaType = (NETCON_MEDIATYPE)GetPropertyValue(ncp, "MediaType"),
            Characteristics = (uint)GetPropertyValue(ncp, "Characteristics"),
            SharingEnabled = (bool)GetPropertyValue(inscfinc, "SharingEnabled"),
            SharingConnectionType = (SHARINGCONNECTIONTYPE)GetPropertyValue(inscfinc, "SharingConnectionType"),
            InternetFirewallEnabled = (bool)GetPropertyValue(inscfinc, "InternetFirewallEnabled")
        };
        return netConnection;
    }

    private static IEnumerable<object> EnumEveryConnection() {
        List<object> result = new List<object>();
        var eec = Invoke(_netShare, "EnumEveryConnection", null);
        int count = (int)Invoke(eec, "Count", null);
        var @enum = Invoke(eec, "_NewEnum", null);
        while (count-- > 0) {
            @enum.GetType().InvokeMember("MoveNext", BindingFlags.InvokeMethod, null, @enum, null);
            var current = @enum.GetType().GetProperties()[0].GetValue(@enum, null);
            result.Add(current);
        }
        return result;
    }

    private static object GetPropertyValue(object target, string propertyName) {
        return target.GetType().InvokeMember(propertyName, BindingFlags.InvokeMethod | BindingFlags.GetProperty, null, target, null, null);
    }

    private static object ProgIdInstance(string progId, params string[] memberNames) {
        Type standardType = Type.GetTypeFromProgID(progId);
        object obj = Activator.CreateInstance(standardType);

        // Make sure it implements IDispatch.
        if (!DispatchUtility.ImplementsIDispatch(obj)) {
            throw new ArgumentException("The object created for " + progId + " doesn't implement IDispatch.");
        }

        // See if we can get Type info and then do some reflection.
        //Type dispatchType = DispatchUtility.GetType(obj, false);

        return obj;
    }

    public static bool EnableSharing(NetConnectionSharing netConnection, int SHARINGCONNECTIONTYPE) {
        Process.Start(new ProcessStartInfo("regsvr32.exe", "/u /s hnetcfg.dll"));
        Process.Start(new ProcessStartInfo("regsvr32.exe", "/s hnetcfg.dll"));
        var INetConnection = EnumEveryConnection().Where(d => GetNetConnectionSharingObject(d).Guid == netConnection.Guid).FirstOrDefault();
        if (INetConnection == null) return false;
        var inscfinc = Invoke(_netShare, "INetSharingConfigurationForINetConnection", new[] { INetConnection });
        var _result = inscfinc.GetType().InvokeMember("EnableSharing", BindingFlags.InvokeMethod, null, inscfinc, new object[] { SHARINGCONNECTIONTYPE }, null);
        return false;
    }

    public static bool DisableSharing(NetConnectionSharing netConnection) {
        Process.Start(new ProcessStartInfo("regsvr32.exe", "/u /s hnetcfg.dll"));
        Process.Start(new ProcessStartInfo("regsvr32.exe", "/s hnetcfg.dll"));
        var INetConnection = EnumEveryConnection().Where(d => GetNetConnectionSharingObject(d).Guid == netConnection.Guid).FirstOrDefault();
        if (INetConnection == null) return false;
        var inscfinc = Invoke(_netShare, "INetSharingConfigurationForINetConnection", new[] { INetConnection });
        var _result = inscfinc.GetType().InvokeMember("DisableSharing", BindingFlags.InvokeMethod, null, inscfinc, null);
        return true;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: k8s You must be logged in to the server (Unauthorized) 
Csharp :: c# show hidden window wpf 
Csharp :: c# SQLite execute Command 
Csharp :: c# sort word 
Csharp :: If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. 
Csharp :: how to full screen login form using C# MVC 
Csharp :: dotnet target specific framework 
Csharp :: C# predict rotation by an angular velocity 
Csharp :: conveyor function in f# 
Csharp :: stateteach.net 
Csharp :: windows forms link listbox to array 
Csharp :: c# read only file used by other app 
Csharp :: Delegates in UntiyC# 
Csharp :: C# create delegate type at runtime 
Csharp :: wait for threadpool to complete with decrement 
Csharp :: Worker service as Windows Service 
Csharp :: c# replace foreach with lambda 
Csharp :: screenshot c# WinForms 
Csharp :: percentage random c# 
Csharp :: how to use external resource.resx file in c# 
Csharp :: vb.net how insert event inside an event 
Csharp :: how to get image from resource folder in c# 
Csharp :: call ienumerator unity 
Csharp :: swagger skip endpoint .net core 
Csharp :: unity3d sort list 
Csharp :: get selected rows gridcontrol devexpress 
Csharp :: c# web page show 2nd page of tiff on an image control 
Csharp :: C# program to find the longest Palindrome in a string. 
Html :: ml5 cdn 
Html :: flutter build web release html renderer 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =