Wince5.0中修改IP地址需不重启即可生效

本文介绍了一种在Windows CE 5.0系统中动态修改IP地址的方法,并提供了一个名为IpHelper的类来实现这一功能,无需重启系统即可使更改生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wince中如何修改IP地址而无需重启

最近做的一个项目中需要动态修改Wince5.0系统的IP地址,我们希望IP地址修改后可以立即生效而无需重启操作系统。经过一番Google.写出如下代码:

ContractedBlock.gifExpandedBlockStart.gifIpHelper
namespace Utitlity.Windows
ExpandedBlockStart.gifContractedBlock.gif
{
    
using System;
    
using System.Runtime.InteropServices;
    
using Microsoft.Win32;

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 用于读取和配置以太网卡的IP地址信息
    
/// </summary>

    public static class IpHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
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);

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 保存以太网卡IP地址信息的注册表节点
        
/// </summary>

        private const string TcpIpRegKeyName = @"HKEY_LOCAL_MACHINE\Comm\{0}\Parms\TcpIp";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// IP地址注册表项名称
        
/// </summary>

        private const string IpAddressItem = "IpAddress";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 子网掩码的注册表项名称
        
/// </summary>

        private const string SubnetMaskItem = "Subnetmask";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 默认网关的注册表项名称
        
/// </summary>

        private const string DefaultGatewayItem = "DefaultGageway";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 以太网卡的设备文件名称
        
/// </summary>

        private const string EtherCardFileName = "NDS0:";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 以太网卡的名称
        
/// </summary>

        private static string EtherCardName = string.Empty;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 保存真实的以太网卡IP地址信息的注册表节点
        
/// </summary>

        private static string RealTcpIpRegKeyName = string.Empty;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 在注册表中对IP信息进行修改后,禁用网卡然后重启网卡以应用修改
        
/// </summary>

        public static bool ApplyIpAddress()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int hNdis = CreateFile(EtherCardFileName, 000, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
            
if (hNdis == INVALID_HANDLE_VALUE)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }


            
// Send the device command.
            if (DeviceIoControl(hNdis, IOCTL_NDIS_REBIND_ADAPTER, EtherCardName, EtherCardName.Length * 2 + 2null000== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }


            CloseHandle(hNdis);
            
return true;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获取子网掩码
        
/// </summary>
        
/// <returns></returns>

        public static string GetSubnetMask()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return GetRegValue(SubnetMaskItem, "0.0.0.0");
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置子网掩码
        
/// </summary>
        
/// <param name="subnetMask"></param>
        
/// <returns></returns>

        public static bool SetSubnetMask(string subnetMask)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (string.IsNullOrEmpty(subnetMask))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw new ArgumentNullException(subnetMask);
            }


            
return SetRegValue(SubnetMaskItem, subnetMask);
        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获取IP地址
        
/// </summary>
        
/// <returns></returns>

        public static string GetIpAddress()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return GetRegValue(IpAddressItem, "0.0.0.0");
        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置Ip地址
        
/// </summary>
        
/// <param name="ip"></param>

        public static bool SetIpAddress(string ip)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (string.IsNullOrEmpty(ip))
                
throw new ArgumentNullException("ip");

            
return SetRegValue(IpAddressItem, ip);
        }




ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获得网卡的名称
        
/// </summary>
        
/// <returns></returns>

        private static string GetEtherCardName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string names = new string(' '255);
            
int bytes = 0;

            
int fileHandle = CreateFile(EtherCardFileName, 000, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, INVALID_HANDLE_VALUE);
            
if (fileHandle == INVALID_HANDLE_VALUE)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return string.Empty;
            }


            
if (DeviceIoControl2(fileHandle, IOCTL_NDIS_GET_ADAPTER_NAMES, null0, names, 255ref bytes, 0== 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return string.Empty;
            }


            
int index = names.IndexOf('\0');
            
string result = names.Substring(0, index);
            
return result;
        }



ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获取注删表项的值
        
/// </summary>

        private static string GetRegValue(string regItemName, string defaultValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (string.IsNullOrEmpty(EtherCardName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                EtherCardName 
= GetEtherCardName();
                RealTcpIpRegKeyName 
= string.Format(TcpIpRegKeyName, EtherCardName);
            }


            
if (!string.IsNullOrEmpty(EtherCardName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
object value = Registry.GetValue(RealTcpIpRegKeyName, regItemName, defaultValue);
                    
if(value!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
if(value.GetType() == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
return (string) value;
                        }

                        
if(value.GetType() == typeof(string[]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
return ((string[]) value)[0];
                        }

                    }

                }

                
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{

                }

            }

            
return defaultValue;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 设置注册表项的值
        
/// </summary>

        private static bool SetRegValue(string regItemName, string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (string.IsNullOrEmpty(EtherCardName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                EtherCardName 
= GetEtherCardName();
                RealTcpIpRegKeyName 
= string.Format(TcpIpRegKeyName, EtherCardName);
            }

            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Registry.SetValue(RealTcpIpRegKeyName, regItemName, value);
                
return true;
            }

            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }

        }


    }

}

使用方法如下:

 

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

 

转载于:https://www.cnblogs.com/shangfc/archive/2009/06/05/1497093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值