Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Obtaining DNS Primary and Secondary Address

Posted on 2002-03-30
3
Medium Priority
?
796 Views
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.
0
Comment
Question by:quantum2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
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.


HTH
DragonSlayer
0
 
LVL 11

Accepted Solution

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


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

 

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

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

Author Comment

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

Quantum2
0

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

705 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