马上注册,结交更多易友,享用更多功能,让你轻松玩转觅风论坛。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
最近在搞nei网穿透,用到cha询网卡信息并修改,翻了论坛,下载的一些都有些问题,比如兼容性不好,有的在win10是好的,我拿到win7或者windows server2008,2019上去,网卡就枚举有问题了,哎,所以就有了这个dll,c#弄 的,兼容win7~11,server等平台。
- .版本 2
- .支持库 spec
- .程序集 窗口程序集_启动窗口
- .子程序 __启动窗口_创建完毕
- .局部变量 a, 整数型
- .局部变量 i, 整数型
- a = 获取网卡数量 ()
- ' 编辑框1.内容 = 到文本 (获取网卡信息 (4))
- .计次循环首 (a, i)
- 调试输出 (获取网卡信息 (i - 1))
- i = i + 1
- .计次循环尾 ()
- 调试输出 (获取当前联网网卡信息 ())
- 调试输出 (设置网卡 (“WLAN”, “10.36.0.199”, “255.255.255.0”, “10.36.0.1”))
复制代码
- .版本 2
- .DLL命令 获取网卡数量, 整数型, "NetworkInfoDll.dll", "GetNetworkAdaptersCount", 公开, 获取网卡数量:返回值 >= 0: 表示找到的网卡数量,返回值 < 0: 表示出现错误
- .DLL命令 获取网卡信息, 文本型, "NetworkInfoDll.dll", "GetNetworkAdapterInfo", 公开, 获取网卡信息:网卡索引 (从0开始),每一组网卡信息字符串格式:"连接名称|网卡名称|连接状态|IP地址|子网掩码|网关",分隔符是|
- .参数 网卡索引, 整数型, , 网卡索引 (从0开始)
- .DLL命令 获取当前联网网卡信息, 文本型, "NetworkInfoDll.dll", "GetActiveNetworkAdapterInfo", 公开
- .DLL命令 设置网卡, 文本型, "NetworkInfoDll.dll", "SetNetworkAdapterInfo", 公开, 修改网络设置需要管理员权限,注意修改IP等信息是根据“连接名称“来的
- .参数 连接名称, 文本型
- .参数 新IP, 文本型
- .参数 新掩码, 文本型
- .参数 新网关, 文本型
复制代码- using System;
- using System.Net.NetworkInformation;
- using System.Runtime.InteropServices;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;
- using RGiesecke.DllExport;
-
- namespace NetworkInfoDll
- {
- public class NetworkInfo
- {
- private static List<NetworkInterface> networkInterfaces;
-
- [DllExport("GetNetworkAdaptersCount", CallingConvention = CallingConvention.StdCall)]
- public static int GetNetworkAdaptersCount()
- {
- networkInterfaces = new List<NetworkInterface>(NetworkInterface.GetAllNetworkInterfaces());
- return networkInterfaces.Count;
- }
-
- [DllExport("GetNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
- [return: MarshalAs(UnmanagedType.LPStr)]
- public static string GetNetworkAdapterInfo(int index)
- {
- if (networkInterfaces == null || index < 0 || index >= networkInterfaces.Count)
- {
- return "Error: Invalid index";
- }
-
- NetworkInterface nic = networkInterfaces[index];
- return GetNetworkAdapterInfoByInterface(nic);
- }
-
- [DllExport("GetActiveNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
- [return: MarshalAs(UnmanagedType.LPStr)]
- public static string GetActiveNetworkAdapterInfo()
- {
- NetworkInterface activeNic = GetActiveNetworkInterface();
- if (activeNic == null)
- {
- return "Error: No active network adapter found";
- }
-
- return GetNetworkAdapterInfoByInterface(activeNic);
- }
-
- [DllExport("SetNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
- [return: MarshalAs(UnmanagedType.LPStr)]
- public static string SetNetworkAdapterInfo(string connectionName, string ipAddress, string subnetMask, string gateway)
- {
- try
- {
- if (!IsAdministrator())
- {
- return "Error: 需要管理员权限来修改网络设置";
- }
-
- using (var networkConfigMng = new ManagementClass("Win32_NetworkAdapter"))
- {
- using (var networkConfigs = networkConfigMng.GetInstances())
- {
- foreach (ManagementObject networkAdapter in networkConfigs)
- {
- string netConnectionID = networkAdapter["NetConnectionID"] as string;
- if (netConnectionID == connectionName)
- {
- uint index = (uint)networkAdapter["Index"];
- using (var networkConfigMng2 = new ManagementClass("Win32_NetworkAdapterConfiguration"))
- {
- using (var networkConfigs2 = networkConfigMng2.GetInstances())
- {
- foreach (ManagementObject networkConfig in networkConfigs2)
- {
- if ((uint)networkConfig["Index"] == index)
- {
- ManagementBaseObject newIP = networkConfig.GetMethodParameters("EnableStatic");
- newIP["IPAddress"] = new string[] { ipAddress };
- newIP["SubnetMask"] = new string[] { subnetMask };
-
- ManagementBaseObject newGateway = networkConfig.GetMethodParameters("SetGateways");
- newGateway["DefaultIPGateway"] = new string[] { gateway };
-
- ManagementBaseObject setIP = networkConfig.InvokeMethod("EnableStatic", newIP, null);
- ManagementBaseObject setGateway = networkConfig.InvokeMethod("SetGateways", newGateway, null);
-
- if ((uint)setIP["ReturnValue"] == 0 && (uint)setGateway["ReturnValue"] == 0)
- {
- return "Success: 网络设置已更新";
- }
- else
- {
- return $"Error: 设置IP返回值 {setIP["ReturnValue"]}, 设置网关返回值 {setGateway["ReturnValue"]}";
- }
- }
- }
- }
- }
- }
- }
- }
- }
-
- return $"Error: 未找到名为 '{connectionName}' 的网络连接";
- }
- catch (Exception ex)
- {
- return $"Error: {ex.Message}";
- }
- }
-
- private static NetworkInterface GetActiveNetworkInterface()
- {
- return NetworkInterface.GetAllNetworkInterfaces()
- .OrderByDescending(nic => nic.Speed)
- .FirstOrDefault(nic =>
- nic.OperationalStatus == OperationalStatus.Up &&
- nic.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
- nic.GetIPProperties().UnicastAddresses.Any(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork));
- }
-
- private static string GetNetworkAdapterInfoByInterface(NetworkInterface nic)
- {
- IPInterfaceProperties properties = nic.GetIPProperties();
-
- string connectionName = nic.Name;
- string adapterName = nic.Description;
- string status = GetChineseStatus(nic.OperationalStatus);
- string ipAddress = "";
- string subnetMask = "";
- string gateway = "";
-
- foreach (UnicastIPAddressInformation ip in properties.UnicastAddresses)
- {
- if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- ipAddress = ip.Address.ToString();
- subnetMask = ip.IPv4Mask.ToString();
- break;
- }
- }
-
- if (properties.GatewayAddresses.Count > 0)
- {
- gateway = properties.GatewayAddresses[0].Address.ToString();
- }
-
- return $"{connectionName}|{adapterName}|{status}|{ipAddress}|{subnetMask}|{gateway}";
- }
-
- private static string GetChineseStatus(OperationalStatus status)
- {
- switch (status)
- {
- case OperationalStatus.Up:
- return "已连接";
- case OperationalStatus.Down:
- return "已断开";
- case OperationalStatus.Testing:
- return "正在测试";
- case OperationalStatus.Unknown:
- return "未知";
- case OperationalStatus.Dormant:
- return "休眠";
- case OperationalStatus.NotPresent:
- return "不存在";
- case OperationalStatus.LowerLayerDown:
- return "底层已断开";
- default:
- return status.ToString();
- }
- }
-
- private static bool IsAdministrator()
- {
- System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
- System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
- return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
- }
- }
- }
复制代码
|
|