Jump to content
Sign in to follow this  

Help with get DNS informations!

Recommended Posts

This is my code. The problem is my _getDNS () doesn't work properly, it always return old data from the first call. How I can had it "refresh"? Please help me!

Opt ('TrayMenuMode',1)
#include <Array.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>
#include <Constants.au3>
#Include <GuiListView.au3>
Dim $pTitle = 'DNS Changer', $pVersion = '1.0', $pDate = '30-06-2010', $sDNS1, $sDNS2, $sName
TraySetToolTip ($pTitle & ' ' & $pVersion)
$sString = _getDNS ()
If UBound ($sString) = 1 Then
    $sDefaultDNS1 = $sString[0]
    $sDefaultDNS2 = ''
    $sDefaultDNS1 = $sString[0]
    $sDefaultDNS2 = $sString[1]

;$answer1 = StringRegExp($sResult,'((?:\d+)(?:\.\d+){3})',3)

;ConsoleWrite ('Default DNS : ' & $sDefaultDNS1 & '|' & $sDefaultDNS2 & @CRLF)
#Region ### START Koda GUI section ### Form=c:\documents and settings\administrator\my documents\downloads\form1.kxf
$hGUI = GUICreate($pTitle & ' ' & $pVersion, 585, 262, 193, 125)
GUISetFont(10, 400, 0, "Arial")
$Label = GUICtrlCreateLabel("Choose a DNS server :", 8, 8, 136, 20)
$List= GUICtrlCreateListView("Provider|Preferred DNS Server|Alternate DNS Server|Information", 8, 32, 570, 198)
$hList = GUICtrlGetHandle ($List)
GUICtrlSendMsg(-1, 0x101E, 0, 125)
GUICtrlSendMsg(-1, 0x101E, 1, 150)
GUICtrlSendMsg(-1, 0x101E, 2, 150)
GUICtrlSendMsg(-1, 0x101E, 3, 100)
GUICtrlCreateListViewItem ('Default DNS Server|' & $sDefaultDNS1 & '|' & $sDefaultDNS2 & '|',$List)
GUICtrlCreateListViewItem ('Google|||Recommended',$List)
GUICtrlCreateListViewItem ('OpenDNS|||Recommended',$List)
GUICtrlCreateListViewItem ('DNSAdvantage|||',$List)
GUICtrlCreateListViewItem ('Norton|||',$List)
GUICtrlCreateListViewItem ('GTEI DNS|||',$List)
GUICtrlCreateListViewItem ('GTEI DNS|||',$List)
GUICtrlCreateListViewItem ('GTEI DNS|||',$List)
GUICtrlCreateListViewItem ('ScrubIt|||',$List)
GUICtrlCreateListViewItem ('OpenNIC|||Queensland, Australia',$List)
GUICtrlCreateListViewItem ('OpenNIC|||New Jersey, United States',$List)
GUICtrlCreateListViewItem ('Public Name|||',$List)
GUICtrlCreateListViewItem ('Cisco|||',$List)
GUICtrlCreateListViewItem ('Sprintlink|||',$List)
$bSet = GUICtrlCreateButton("&Set", 264, 232, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
$bHide = GUICtrlCreateButton("&Hide", 344, 232, 75, 25, 0)
$Label = GUICtrlCreateLabel("DNS Changer 1.0", 8, 232, 105, 20)
GUICtrlSetColor(-1, 0x808080)
$bUpdate = GUICtrlCreateButton("&Update", 424, 232, 75, 25, 0)
$bAbout = GUICtrlCreateButton("&About", 504, 232, 75, 25, 0)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $bSet
            $aSelected = _GUICtrlListView_GetSelectedIndices ($hList,True)
            ConsoleWrite ('Position : ' & $aSelected[0] & @CRLF)
            If $aSelected[0] <> 0 Then
                $sName = _GUICtrlListView_GetItemText($hList,$aSelected[1])
                $sDNS1 = _GUICtrlListView_GetItemText($hList,$aSelected[1],1)
                $sDNS2 = _GUICtrlListView_GetItemText($hList,$aSelected[1],2)
                ConsoleWrite ('DNS 1 : ' & $sDNS1 & @CRLF & 'DNS 2 : ' & $sDNS2 & @CRLF)
            $pSetDNS = RunWait ( @ComSpec & " /c netsh interface ip set dns Local static " & $sDNS1,'',@SW_HIDE)
            Sleep (1000)
            $AddDNS = RunWait ( @ComSpec & " /c netsh interface ip add dns local " & $sDNS2,'',@SW_HIDE)

            If Not @error Then
                $getDNS = _getDNS ()
                ConsoleWrite ('DNS reread : ' & $getDNS[0] & ' ' & $getDNS[1])
                If $getDNS[0] = $sDNS1 And $getDNS[1] = $sDNS2 Then
                    MsgBox (64,$pTitle,'DNS Server Address changed!','',$hGUI)
                    MsgBox (32,$pTitle,'Error occured! Please try again!','',$hGUI)


Func _getDNS ()
    Local $sResult, $line
    $hRun = RunWait(@ComSpec & " /c netsh interface ip show dns", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $line = StdoutRead($hRun)
        If @error Then ExitLoop
        $sResult &= $line
    ;$answer1 = StringRegExp($sResult,'((?:\d+)(?:\.\d+){3})',3)
    $sString = StringRegExp($sResult,'((?:[1-2]?[0-9]?[0-9]\.){3}(?:[1-2]?[0-9]?[0-9]))',3)
    Return $sString

And this is the console error code :

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\Administrator\Desktop\dnschanger.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams 
+>17:34:43 Starting AutoIt3Wrapper v.   Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X64 OS:X86)
>Running AU3Check (  from:C:\Program Files\AutoIt3
+>17:34:44 AU3Check ended.rc:0
>Running:(\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\Administrator\Desktop\dnschanger.au3"    
C:\Documents and Settings\Administrator\Desktop\dnschanger.au3 (21) : ==> Subscript used with non-Array variable.:
$sDefaultDNS1 = $sString[0]
$sDefaultDNS1 = $sString^ ERROR
->17:34:44 AutoIT3.exe ended.rc:1
>Exit code: 1   Time: 2.227

Thanks you first! :-D

Share this post

Link to post
Share on other sites

What is the return value from this line $sString = StringRegExp($sResult,'((?:[1-2]?[0-9]?[0-9]\.){3}(?:[1-2]?[0-9]?[0-9]))',3) ? If the return isn't what you're expecting, an array with the DNS information, then the next check will fail. You need to add in some error checking for that pattern to make sure it's returning what you need/want before you're acting upon what you think is there. Either add an _ArrayDisplay to the _getDNS after that line to see if the array is created, or a consolewrite to tell you if there's an error.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...