Berbagi Koneksi Internet

/// <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;
    }
}
Said HR