Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

NetConnectionDispatch

/// <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 :: unitydont play sound until finsihed 
Csharp :: c# hardcode datetime quoting 
Csharp :: Diplay player final score in new scene in unity 
Csharp :: ExecuteResultAsync ActionContext statuscode 
Csharp :: calculated field gridview asp.net 
Csharp :: missing integer c# 
Csharp :: convert string csv line to list long c# 
Csharp :: telerik mvc grid round sum result 
Csharp :: catwherehouse 
Csharp :: how can i replace Any for All method and vice versa linq in c# 
Csharp :: unity convert pixels to units 
Csharp :: process run teamviewer address parametr c# 
Csharp :: parsing object from text file c# 
Csharp :: move position smoth unity 
Csharp :: using mediamanager how to play mp3 files 
Csharp :: Garbage collect every 30 frames unity 
Csharp :: Implementing Banner Ads Unity 
Csharp :: php encrypt message encrypt() decrypt 
Csharp :: unity set terrain to image 
Csharp :: c# control datagridview null value 
Csharp :: save checkbox value to database c# 
Csharp :: action c# but returns value 
Csharp :: blender how to switch cameras 
Csharp :: hdrp lit change emmision values 
Csharp :: barcode print c# 
Csharp :: WPF raotate Icon 
Csharp :: subarray c# 
Csharp :: Linq join update without creating new 
Csharp :: how to trigger event when a com device is connected in c# 
Csharp :: is odd c# stackoverflow 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =