打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Pascal(Delphi)] delphi版不重启改ip、dns、网关
  • program MASK;
  • uses
  •    windows, SysUtils;

  • const
  •    MAX_ADAPTER_NAME_LENGTH = 256;
  •    MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
  •    MAX_ADAPTER_ADDRESS_LENGTH = 8;
  •    IPHelper = 'iphlpapi.dll';


  • type
  •    USHORT = WORD;
  •    ULONG = DWORD;
  •    time_t = Longint;

  •    IP_ADDRESS_STRING = record
  •      S: array[0..15] of Char;
  •   end;

  •    IP_MASK_STRING = IP_ADDRESS_STRING;
  •    PIP_MASK_STRING = ^IP_MASK_STRING;

  •    PIP_ADDR_STRING = ^IP_ADDR_STRING;
  •    IP_ADDR_STRING = record
  •      Next: PIP_ADDR_STRING;
  •      IpAddress: IP_ADDRESS_STRING;
  •      IpMask: IP_MASK_STRING;
  •      Context: DWORD;
  •   end;

  •    PIP_ADAPTER_INFO = ^IP_ADAPTER_INFO;
  •    IP_ADAPTER_INFO = record
  •      Next: PIP_ADAPTER_INFO;
  •      ComboIndex: DWORD;
  •      AdapterName: array[0..MAX_ADAPTER_NAME_LENGTH + 3] of Char;
  •      Description: array[0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char;
  •      AddressLength: UINT;
  •      Address: array[0..MAX_ADAPTER_ADDRESS_LENGTH - 1] of BYTE;
  •      Index: DWORD;
  •      Type_: UINT;
  •      DhcpEnabled: UINT;
  •      CurrentIpAddress: PIP_ADDR_STRING;
  •      IpAddressList: IP_ADDR_STRING;
  •      GatewayList: IP_ADDR_STRING;
  •      DhcpServer: IP_ADDR_STRING;
  •      HaveWins: BOOL;
  •      PrimaryWinsServer: IP_ADDR_STRING;
  •      SecondaryWinsServer: IP_ADDR_STRING;
  •      LeaseObtained: time_t;
  •      LeaseExpires: time_t;
  •   end;

  • function GetAdaptersInfo(pAdapterInfo: PIP_ADAPTER_INFO; var pOutBufLen: ULONG): DWORD; stdcall; external IPHelper;

  • function StringToWideString(const S: string): WideString;
  • var
  •    InputLength, OutputLength: Integer;
  • begin
  •    InputLength := Length(S);
  •    OutputLength := MultiByteToWideChar(CP_ACP, 0, PChar(S), InputLength, nil, 0);
  •    SetLength(Result, OutputLength);
  •    MultiByteToWideChar(CP_ACP, 0, PChar(S), InputLength, PWideChar(Result), OutputLength);
  • end;



  • function NotifyIPChange(lpszAdapterName: string): bool;
  • type TDhcpNotifyConfigChange = function(lpwszServerName: PWideChar; // 本地机器为NULL
  •      lpwszAdapterName: PWideChar; // 适配器名称
  •      bNewIpAddress: BOOL; // TRUE表示更改IP
  •      dwIpIndex: DWORD; // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0
  •      dwIpAddress: DWORD; // IP地址
  •      dwSubNetMask: DWORD; // 子网掩码
  •      nDhcpAction: Integer): BOOL; stdcall;
  • var
  •    hDhcpDll: dword;
  •    MyDhcpNotifyConfigChange: TDhcpNotifyConfigChange;
  • begin
  •    hDhcpDll := LoadLibrary('dhcpcsvc.dll');
  •   if hDhcpDll <> 0 then
  •   begin
  •      MyDhcpNotifyConfigChange := GetProcAddress(hDhcpDll, 'DhcpNotifyConfigChange');
  •      MyDhcpNotifyConfigChange(nil, pwidechar(StringToWideString(lpszAdapterName)), FALSE, 0, 0, 0, 0)
  •   end;
  •    CloseHandle(hDhcpDll);
  • end;

  • function RegIp(lpszAdapterName, pIPAddress, pNetMask, pNetGate, pDNSServer1, pDNSServer2: string): bool;
  • var
  •    hkRoot: HKEY;
  •    mszDNSServer, mszIPAddress, mszNetMask, mszNetGate: string;
  •    strKeyName: string;
  • begin
  •    strKeyName := 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\' + lpszAdapterName;
  •    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pchar(strKeyName), 0, KEY_WRITE, hkRoot) <> ERROR_SUCCESS)
  •      then exit;
  •    mszDNSServer := pDNSServer1 + ',' + pDNSServer2;
  •    mszIPAddress := pIPAddress + #0#0;
  •    mszNetMask := pNetMask + #0#0;
  •    mszNetGate := pNetGate + #0#0;
  •    RegSetValueEx(hkRoot, 'IPAddress', 0, REG_MULTI_SZ, pchar(mszIPAddress), length(mszIPAddress));
  •    RegSetValueEx(hkRoot, 'SubnetMask', 0, REG_MULTI_SZ, pchar(mszNetMask), length(mszNetMask));
  •    RegSetValueEx(hkRoot, 'DefaultGateway', 0, REG_MULTI_SZ, pchar(mszNetGate), length(mszNetGate));
  •    RegSetValueEx(hkRoot, 'NameServer', 0, REG_SZ, pchar(mszDNSServer), length(mszDNSServer));
  •    RegCloseKey(hkRoot);
  • end;


  • function GetLanAdapterName: string;
  • var
  •    InterfaceInfo, TmpPointer: PIP_ADAPTER_INFO;
  •    IP: PIP_ADDR_STRING;
  •    Len: ULONG;
  • begin
  •    result := '';
  •    if GetAdaptersInfo(nil, Len) = ERROR_BUFFER_OVERFLOW then
  •    begin
  •      GetMem(InterfaceInfo, Len);
  •      try
  •        if GetAdaptersInfo(InterfaceInfo, Len) = ERROR_SUCCESS then
  •        begin
  •          TmpPointer := InterfaceInfo;
  •          result := TmpPointer.AdapterName;
  •        end;
  •      finally
  •        FreeMem(InterfaceInfo);
  •      end;
  •    end;
  • end;

  • var
  •    AdapterName: string;
  • begin
  •    AdapterName := GetLanAdapterName;
  •    RegIp(AdapterName, '192.168.0.5', '255.255.255.0', 11.10.10.1', 111.111.11.111', '111.111.11.112');
  •    NotifyIPChange(AdapterName);
  • end.
  • 本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    Delphi用Socket API隐藏自己的真实IP
    Delphi网络函数
    C++ 中通过GetAdaptersInfo获取网卡配置和Ip地址信息
    Delphi中PING的实现
    CIPAddressCtrl类的使用(IP地址与CString的互相转化
    Delphi 获取Windows服务状态、启动和停止服务_delphi 启动服务
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服