NetConnectionDispatch
///
/// Manage ICS using COMObject HNetCfg.HNetShare
///
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 nets = new List();
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
|