Obtaining DNS Primary and Secondary Address

Posted on 2002-03-30
Last Modified: 2007-11-27
I need to know how to obtain both the primary and secondary DNS addresses from a machine currently connected to the net.

Assume that I will already know if a connection is present, I just need to obtain the DNS numbers and assign them to a String variable in the proper Quad format.
Question by:quantum2
LVL 14

Expert Comment

ID: 6909712
I don't know if this is the proper format, but if you just need to obtain it from the registry, here goes: (Win2K)

Navigate to HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Adapters

Loop through the name of the keys to obtain the network interfaces you have (they are enclosed in braces, {}, eg {E0149FCC-34D2-4158-884B-A06F3B6F0F8E})

Let's say you managed to get {E0149FCC-34D2-4158-884B-A06F3B6F0F8E} (that is, HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Adapters{E0149FCC-34D2-4158-884B-A06F3B6F0F8E}), remember the key, then navigate to HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\YourKeyName, eg. HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\Interfaces\{E0149FCC-34D2-4158-884B-A06F3B6F0F8E}

Open the key, then read the value for NameServer. The DNS servers are separated by a comma.

LVL 11

Accepted Solution

shaneholmes earned 100 total points
ID: 6911405
Try this function "GetDNSIP"

 function GetDnsIp : string;
   Output        : TStringList;
   CmdLine       : string;
   CmdLine := GetIpCfgExePath;
   if CmdLine <> '' then
     Output := TStringList.Create;
       case GetBasicOsType of
           if ExecAndGetConsoleOutput (CmdLine, Output) then
             Output.LoadFromFile (GetIpCfg9xOutPath);
           ExecAndGetConsoleOutput (CmdLine, Output);
       if GetDnsIpFromIpCfgOut (Output, DnsIp) then
         Result := DnsIp
         // Attempt to locate via registry
         Result := GetDnsIpFromReg;


function GetDnsIpFromReg : string;
   OpenKey       : HKEY;
   SubKey        : PChar;
   DataSize      : integer;
   Temp          : array [0..2048] of char;
   Result := '';
   SubKey := '';
   Vn := '';
   case GetBasicOsType of
       SubKey := REG_9X_NAMESERVER_PATH;
      Vn := REG_9X_NAMESERVER;
     VER_PLATFORM_WIN32_NT       :
     KEY_READ, OpenKey) = ERROR_SUCCESS then
     DataType := REG_SZ;
     DataSize := SizeOf(Temp);
     if RegQueryValueEx (OpenKey, Vn, nil, @DataType, @Temp,
       @DataSize) = ERROR_SUCCESS then
       Result := string(Temp);
     RegCloseKey (OpenKey);

function GetDnsIpFromIpCfgOut (const Output : TStringList;
   var DnsIp : string) : boolean;
   i     : integer;
   Result := FALSE;
   if Output.Count >= 1 then
     for i := 0 to Output.Count - 1 do
       if Pos(IPCFG_DNS_SERVER_LINE, Output[i]) > 0 then
         DnsIp := Trim(Copy (Output[i], Pos(':', Output[i])+1,
         Result := LooksLikeIp (DnsIp);

Author Comment

ID: 6942364
Thanks a bunch...
Just what I was looking for. I really apprecaite the help.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 79
oracle global variables 4 76
how to resize animated Gif image in delphi ? 1 34
DBGrid or StringGrid ? 6 103
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question