我是靠谱客的博主 笨笨鸵鸟,这篇文章主要介绍Wince中修改IP地址而无需重启,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
namespace Utitlity.Windows { using System; using System.Runtime.InteropServices; using Microsoft.Win32; /** <summary> /// 用于读取和配置以太网卡的IP地址信息 /// </summary> public static class IpHelper { private const int OPEN_EXISTING = 3; private const int FILE_ATTRIBUTE_NORMAL = 0x80; private const int INVALID_HANDLE_VALUE = -1; private const int IOCTL_NDIS_REBIND_ADAPTER = 1507374; private const int IOCTL_NDIS_GET_ADAPTER_NAMES = 1507386; [DllImport("Coredll.dll", EntryPoint = "CreateFile")] private static extern int CreateFile( string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile ); [DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")] private static extern int DeviceIoControl( int hDevice, int dwIoControlCode, string lpInBuffer, int nInBufferSize, string lpOutBuffer, int nOutBufferSize, int lpBytesReturned, int lpOverlapped ); [DllImport("Coredll.dll", EntryPoint = "DeviceIoControl")] private static extern int DeviceIoControl2( int hDevice, int dwIoControlCode, string lpInBuffer, int nInBufferSize, string lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, int lpOverlapped ); [DllImport("Coredll.dll", EntryPoint = "CloseHandle")] private static extern int CloseHandle(int hObject); /** <summary> /// 保存以太网卡IP地址信息的注册表节点 /// </summary> private const string TcpIpRegKeyName = @"HKEY_LOCAL_MACHINEComm{0}ParmsTcpIp"; /** <summary> /// IP地址注册表项名称 /// </summary> private const string IpAddressItem = "IpAddress"; /** <summary> /// 子网掩码的注册表项名称 /// </summary> private const string SubnetMaskItem = "Subnetmask"; /** <summary> /// 默认网关的注册表项名称 /// </summary> private const string DefaultGatewayItem = "DefaultGageway"; /** <summary> /// 以太网卡的设备文件名称 /// </summary> private const string EtherCardFileName = "NDS0:"; /** <summary> /// 以太网卡的名称 /// </summary> private static string EtherCardName = string.Empty; /** <summary> /// 保存真实的以太网卡IP地址信息的注册表节点 /// </summary> private static string RealTcpIpRegKeyName = string.Empty; /** <summary> /// 在注册表中对IP信息进行修改后,禁用网卡然后重启网卡以应用修改 /// </summary> public static bool ApplyIpAddress() { int hNdis = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE); if (hNdis == INVALID_HANDLE_VALUE) { return false; } // Send the device command. if (DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER, EtherCardName, EtherCardName.Length * 2 + 2, null, 0, 0, 0) == 0) { return false; } CloseHandle(hNdis); return true; } /** <summary> /// 获取子网掩码 /// </summary> /// <returns></returns> public static string GetSubnetMask() { return GetRegValue(SubnetMaskItem, "0.0.0.0"); } /** <summary> /// 设置子网掩码 /// </summary> /// <param name="subnetMask"></param> /// <returns></returns> public static bool SetSubnetMask(string subnetMask) { if (string.IsNullOrEmpty(subnetMask)) { throw new ArgumentNullException(subnetMask); } return SetRegValue(SubnetMaskItem, subnetMask); } /** <summary> /// 获取IP地址 /// </summary> /// <returns></returns> public static string GetIpAddress() { return GetRegValue(IpAddressItem, "0.0.0.0"); } /** <summary> /// 设置Ip地址 /// </summary> /// <param name="ip"></param> public static bool SetIpAddress(string ip) { if (string.IsNullOrEmpty(ip)) throw new ArgumentNullException("ip"); return SetRegValue(IpAddressItem, ip); } /** <summary> /// 获得网卡的名称 /// </summary> /// <returns></returns> private static string GetEtherCardName() { string names = new string(' ', 255); int bytes = 0; int fileHandle = CreateFile(EtherCardFileName, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE); if (fileHandle == INVALID_HANDLE_VALUE) { return string.Empty; } if (DeviceIoControl2(fileHandle, IOCTL_NDIS_GET_ADAPTER_NAMES, null, 0, names, 255, ref bytes, 0) == 0) { return string.Empty; } int index = names.IndexOf(''); string result = names.Substring(0, index); return result; } /** <summary> /// 获取注删表项的值 /// </summary> private static string GetRegValue(string regItemName, string defaultValue) { if (string.IsNullOrEmpty(EtherCardName)) { EtherCardName = GetEtherCardName(); RealTcpIpRegKeyName = string.Format(TcpIpRegKeyName, EtherCardName); } if (!string.IsNullOrEmpty(EtherCardName)) { try { object value = Registry.GetValue(RealTcpIpRegKeyName, regItemName, defaultValue); if(value!=null) { if(value.GetType() == typeof(string)) { return (string) value; } if(value.GetType() == typeof(string[])) { return ((string[]) value)[0]; } } } catch (Exception) { } } return defaultValue; } /** <summary> /// 设置注册表项的值 /// </summary> private static bool SetRegValue(string regItemName, string value) { if (string.IsNullOrEmpty(EtherCardName)) { EtherCardName = GetEtherCardName(); RealTcpIpRegKeyName = string.Format(TcpIpRegKeyName, EtherCardName); } try { Registry.SetValue(RealTcpIpRegKeyName, regItemName, value); return true; } catch (Exception) { return false; } } } }

使用方法如下:

 

复制代码
1
2
3
4
5
6
// IpHelper类的使用方法 IpHelper.SetIpAddress("192.168.0.111"); IpHelper.SetSubnetMask("192.168.0.111"); IpHelper.ApplyIpAddress(); // 通过修改IPHelper类,也可以实现修改某认网关的信息,因为我的项目不需要,所以未实现。


最后

以上就是笨笨鸵鸟最近收集整理的关于Wince中修改IP地址而无需重启的全部内容,更多相关Wince中修改IP地址而无需重启内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部