Sign in to follow this  
Followers 0
dtouzeau

IpToLong and LongToIP

4 posts in this topic

Hi all,

I would like to find an example in order to conver a long to an IP address in string format.

To convert an IP to long i have found the way :

Local $vaDllCall1 = DllCall("ws2_32.dll", "ulong", "inet_addr", "str", "192.168.1.1")
If @error Then Exit

Local $vbinIP = $vaDllCall1[0]

This function will correct convert IP in string format to a integer long format..

How can do the opposit ?

I have tried

Local $vaDllCall = DllCall("ws2_32.dll", "str", "inet_addr", "ulong", $vbinIP)

But it crash autoit...

Thanks to help !

 

Share this post


Link to post
Share on other sites



Search JohnOne and int IP, as they have created a function similar to what you're asking.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Global Const $tagIN_ADDR = "ulong S_addr;"

Global $sIP = "192.168.1.1"
Global $longIP = _inet_addr($sIP)

ConsoleWrite($longIP & @LF)

Global $tIN_ADDR = DllStructCreate($tagIN_ADDR)

Global $sIPfromLong = _inet_ntoa($longIP)
ConsoleWrite($sIPfromLong & @LF)

DllStructSetData($tIN_ADDR, "S_addr", $longIP)
Global $sIPfromLongNew = _inet_ntop(DllStructGetPtr($tIN_ADDR))
ConsoleWrite($sIPfromLongNew & @LF)

Func _inet_addr($sIP)
    Local $aRet = DllCall("ws2_32.dll", "ULONG", "inet_addr", "str", $sIP)
    Return $aRet[0]
EndFunc   ;==>_inet_addr

Func _inet_ntoa($In_Addr) ;only IPv4 (deprecated)   --> use _inet_ntop() instead
    Local $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "long", $In_Addr)
    Return $aRet[0]
EndFunc   ;==>_inet_ntoa

Func _inet_ntop($pIn_Addr, $iFamily = 2) ;IPv4 and IPv6 --> Vista and above, 2 = $AF_INET, 23 = AF_INET6
    Local $tBuffer = DllStructCreate("char[46]") ;16 for IPv4, 46 for IPv6
    Local $aRet = DllCall("ws2_32.dll", "str", "inet_ntop", "int", $iFamily, "ptr", $pIn_Addr, "struct*", $tBuffer, "int", DllStructGetSize($tBuffer))
    Return DllStructGetData($tBuffer, 1)
EndFunc   ;==>_inet_ntop

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

I'm not necessarily advocating against using the DLL, but the algorithm for converting to a number and back to an address isn't particularly complicated...

Func IpToInt($addr)
    If Not IsArray($addr) Then
        ConsoleWriteError("IpToInt: $addr was not an array" & @LF)
        Return SetError(-1, 0, 0)
    EndIf
    If Not UBound($addr) == 4 Then
        ConsoleWriteError("IpToInt: $addr was a " & UBound($addr) & " element array" & @LF)
        Return SetError(-1, 0, 0)
    EndIf

    Local $ret = 0
    $ret += BitShift($addr[0], -24)
    $ret += BitShift($addr[1], -16)
    $ret += BitShift($addr[2], -8)
    $ret += $addr[3]
    Return $ret
EndFunc

Func IntToIp($addr)
    If Not IsInt($addr) Then
        ConsoleWriteError("IntToIp: " & $addr & " is not an integer" & @LF)
        Return SetError(-1, 0, 0)
    EndIf

    Local $ret[4]
    $ret[0] = BitShift($addr, 24)
    $ret[0] = BitAND($ret[0], 0xFF)
    $ret[1] = BitShift($addr, 16)
    $ret[1] = BitAND($ret[1], 0xFF)
    $ret[2] = BitShift($addr, 8)
    $ret[2] = BitAND($ret[2], 0xFF)
    $ret[3] = BitAND($addr, 0xFF)
    Return $ret
EndFunc
1 person likes this

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

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  
Followers 0