How to Dynamically Change DNS Servers

When you connect to the Internet (any kind of connection) you use DNS (Domain Name Server) servers to resolve domain names. To put that in plain English - you know Borland’s site is www.borland.com, but that name is for humans to better remember it. The address that your computer would connect to if you wanted to visit their site would look like this: “207.105.75.31″. Now how does your computer figure out that address? Unless it already knows it (fro previous, cached visits) it queries a DNS server.
If for any given reason you want to change this you can use the code below:

uses
  Registry, Windows;
  ...
  with TRegistry.Create do
    try
      // For Windows NT/2000/XP
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('SYSTEMCurrentControlSetServicesTcpipParameters', True) then begin
        WriteString('NameServer', '1.1.1.1 2.2.2.2 3.3.3.3'); // The name server value are IP addresses separated with spaces
        CloseKey;
      end;

      // For Windows 9x/ME
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('SYSTEMCurrentControlSetServicesVxDMSTCP', True) then begin
        WriteString('NameServer', '1.1.1.1 2.2.2.2 3.3.3.3'); // The name server value are IP addresses separated with spaces
        CloseKey;
      end;
    finally
      Free;
    end;

Tags: , , , , , , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login