如何用编程的方式获得系统DNS,有相应的API函数吗?

解决方案 »

  1.   

    读取注册表
    LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tcpip\parameters
    下的nameserver,多个dns以逗号分开.
      

  2.   

    读注册表
    在 LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tcpip\parameters
    \Interfaces\{17CE8728-AA4E-44BE-9F76-52C5F3B99842}\NameServer{17CE8728-AA4E-44BE-9F76-52C5F3B99842}字符串根据网卡的不同而不同,通过下列代码获得
    function GateWay:string;
    var
      Reg :TRegistry;
      MyStr : TStrings;
      buffer : array[0..1024] of byte;
      i:integer;
      info:TRegKeyInfo ;
      str:string;
    begin
      Result:='';
      Reg :=TRegistry.Create;
      Mystr:=Tstringlist.Create;
      Reg.RootKey :=HKEY_LOCAL_MACHINE;
      if Reg.OpenKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards',false) then
        begin
        if reg.GetKeyInfo(info) then
           begin
           reg.GetKeyNames(mystr);
           str:='SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\'+Mystr.Strings[0];
           end;
        end;
     Reg.CloseKey;
     Mystr.Free;
     Reg.RootKey :=HKEY_LOCAL_MACHINE;
     if reg.OpenKey(str,false) then
        str:=reg.ReadString('ServiceName');
      Reg.CloseKey;
      Reg.free;
      Result:=Str;
    end;
      

  3.   

    api
    DnsQueryConfig
    DnsQueryConfig
    The DnsQueryConfig function enables application programmers to query for the configuration of the local computer or a specific adapter.DNS_STATUS WINAPI DnsQueryConfig(
      DNS_CONFIG_TYPE Config,
      DWORD Flag,
      PWSTR pwsAdapterName,
      PVOID pReserved,
      PVOID pBuffer,
      PDWORD pBufferLength
    ); 
    Parameters
    Config 
    [in] Structure specifying query requests. The following parameters can be queried: 
    DnsConfigPrimaryDomainName_W, DnsConfigPrimaryDomainName_A, DnsConfigPrimaryDomainName_UTF8, DnsConfigAdapterDomainName_W, DnsConfigAdapterDomainName_A, DnsConfigAdapterDomainName_UTF8, DnsConfigDnsServerList, DnsConfigSearchList, DnsConfigAdapterInfo, DnsConfigPrimaryHostNameRegistrationEnabled, DnsConfigAdapterHostNameRegistrationEnabled, DnsConfigAddressRegistrationMaxCount Flag 
    [in] Specifies whether the configuration should be associated with a LocalAlloc function call. Set Flag to TRUE to associate the query. 
    pwsAdapterName 
    [in] Specifies the adapter name against which the query is run. 
    pReserved 
    Reserved for future use. 
    pBuffer 
    [out] Pointer to the buffer storing the query response. 
    pBufferLength 
    [in, out] Length of the buffer, in bytes. If the buffer provided is not sufficient, an error is returned and pBufferLength contains the minimum necessary buffer size. Ignored on input if Flag is set to TRUE. 
    Return Values
    Returns success confirmation upon successful completion. Otherwise, returns the appropriate DNS-specific error code as defined in Winerror.h.Requirements 
      Windows NT/2000: Requires Windows 2000.
      Header: Declared in Windns.h.
      Library: Use Dnsapi.lib.See Also
    DnsQuery
      

  4.   

    在dnsapi.dll中
    sdk的头文件是Windns.h如果用delphi的化只好手工再写个头了
    注意Requirements 
      Windows NT/2000: Requires Windows 2000.
    好像只能再2k下用