Modify

Opened 15 years ago

Closed 15 years ago

#806 closed Bug (No Bug)

DllStructGetData fails in compiled script with a name longer than 12/13 characters

Reported by: autoit@… Owned by:
Milestone: Component: Aut2Exe
Version: 3.3.0.0 Severity: None
Keywords: Cc:

Description

The script below will work fine in AutoIT 3.3.0.0 as long as it is not compiled.
It requires two parameters given in the command line:

  1. IP address of the DHCP server
  2. IP address of the client to look for

It will query the DHCP server (at least 'DHCP User' permissions are required on the DHCP server) and return the name of the client that has the given IP address as a lease (the error might happen on other uses of DllStructGetData, but I've noticed it with this one).

Once it is compiled, the exe sometimes will end with an application error in the function "_DHCP_GetClientNameByIP":
$sClientName = DllStructGetData($tClientNameString, "ClientName")
but only if the name of the exe is

  • 12 characters or longer on Server 2003 SP2
  • 13 characters or longer on Windows XP SP3

If the file name is shorter, it seems to run without problems.
In some setups, it crashes "reliably" on some IP addresses, but works on others, in some it only crashes sometimes (while querying the same IP address).
In the case of the "reliable" crash, the crashes will stop as soon as the exe is renamed to something shorter than 12/13 characters.
The crashes happen on 3.2.12.1 as well, but I've only yet verified the solution on 3.3.0.0.

The function below is, for demonstration purposes, a stripped-down version of Alexander Melnichuk's _DHCP_GetClientInfo, found at
http://www.autoitscript.com/forum/index.php?showtopic=74295
In case it's needed, MS information for the DHCP query:
DhcpGetClientInfo Function
http://msdn.microsoft.com/en-us/library/aa363286(VS.85).aspx
DHCP_SEARCH_INFO Structure
http://msdn.microsoft.com/en-us/library/aa363370(VS.85).aspx
DHCP_CLIENT_INFO Structure
http://msdn.microsoft.com/en-us/library/aa363348(VS.85).aspx

;===============================================================================
;
; Description:      Get DHCP Client Attributes by IP Address
; Parameter(s):     $sDHCP - IP Address of DHCP Server
;                   $ClientID - Client IP(String)
; Return Value(s):  On Success - The name of the client
;                   On Failure - Null, @error set to
;                     1 - Client not found on DHCP server
;                     2 - Invalid ClientID parameter
;                     3 - Operating System not supported
;                     4 - Any Runtime Error, API Error Code set to @extended
; Author(s):         amel27 (Alexander Melnichuk)
; Note(s):           Client Name is case sensitive
;
;===============================================================================
Func _DHCP_GetClientNameByIP($sDHCP, $sClientID)
; Create DHCP_SEARCH_INFO Structure
Local $tSearchInfo
Local $tClientInfo
Local $iSearchInfoPtr
Local $tSearchInfo_IP
Local $tClientInfo_Ptr
Local $tClientNameString
Local $aOctets
Local $i, $aRet
Local $sClientName
Local $iClientIP
	$tSearchInfo = DllStructCreate("int SearchType;int DataLength;ptr DataPtr")
	$iSearchInfoPtr = DllStructGetPtr($tSearchInfo)
	If StringRegExp($sClientID, "^(\d+\.){3}\d+$") Then
		; Get IP DWord type from String
		$iClientIP = 0
		$aOctets = StringSplit($sClientID, ".")
		For $i = 1 To 4
			If BitAND($aOctets[$i], 0xFFFFFF00) Then Return SetError(2) ; ERR: Invalid Client IP Address
			$iClientIP = BitOR(BitRotate($iClientIP, 8, "D"), $aOctets[$i])
		Next
	Else
		Return SetError(2)
	EndIf
	; Route the filling of DHCP_SEARCH_INFO structure
	; Filling DHCP_SEARCH_INFO for search by client IP address
	$tSearchInfo_IP = DllStructCreate("int SearchType;dword ClientIPAddress", $iSearchInfoPtr)
	DllStructSetData($tSearchInfo_IP, "SearchType", 0)
	DllStructSetData($tSearchInfo_IP, "ClientIPAddress", $iClientIP)
	; Call  DhcpGetClientInfo API function
	$tClientInfo_Ptr = DllStructCreate("ptr")
	$aRet = DllCall("Dhcpsapi.dll", "int", "DhcpGetClientInfo", _
		"wstr", $sDHCP, _ 
		"ptr", $iSearchInfoPtr, _
		"ptr", DllStructGetPtr($tClientInfo_Ptr))
	If @error Then Return SetError(3, @error) ; ERR: Invalid DLL or Function Name
	If ($aRet[0] = 20013) Then Return SetError(1, $aRet[0]) ; ERR: Client not found
	If $aRet[0] Then Return SetError(4, $aRet[0]) ; ERR: Any runtime errors
	; DHCP_CLIENT_INFO structure
	$tClientInfo = DllStructCreate("dword ClientIpAddress;dword SubnetMask;int BinaryLen;ptr BinaryPtr;" & _
		"ptr ClientNamePtr;ptr ClientCommentPtr;ubyte ClientLeaseExpires[8];dword OwnerHostIPAddress;" & _
		"ptr OwnerHostNetBiosNamePtr;ptr OwnerHostNamePtr", DllStructGetData($tClientInfo_Ptr, 1))
	; Get Client Name
_FileWriteLog($strLogFile, "Creating DLL Structure for ClientName")
	$tClientNameString = DllStructCreate("wchar ClientName[255]", DllStructGetData($tClientInfo, "ClientNamePtr"))
_FileWriteLog($strLogFile, "Retrieving ClientName")
	$sClientName = DllStructGetData($tClientNameString, "ClientName")
_FileWriteLog($strLogFile, "Name: " & $sClientName & " ... releasing memory")
	DllCall("Dhcpsapi.dll", "none", "DhcpRpcFreeMemory", "ptr", DllStructGetData($tClientInfo_Ptr,1))
	Return $sClientName
EndFunc

; ========== Main ================================================================================

If ($CmdLine[0] < 2) Then
	MsgBox(0, "Syntax", "Syntax: " & @ScriptName & " <IP DHCP server> <IP DHCP Client>")
	Exit(1)
EndIf
Dim $strDHCPServerIP = $CmdLine[1]
Dim $strDHCPClientIP = $CmdLine[2]
Dim $strDHCPClientInfo[6]
Dim $strDHCPClientName
; If FileExists($strLogFile) Then FileDelete($strLogFile)
_FileWriteLog($strLogFile, "--------------------------------------------------------------------------------")
$strDHCPClientName = _DHCP_GetClientNameByIP($strDHCPServerIP, $strDHCPClientIP)
MsgBox(0, "Result", "Host name of DHCP client " & $strDHCPClientIP & ": " & $strDHCPClientName, 2)

Attachments (0)

Change History (3)

comment:1 Changed 15 years ago by Valik

Read WikiStart. Then re-write the code so that it's not a wall of text. Way too much code.

comment:2 Changed 15 years ago by Jpm

After correcting the script to have it working using my DCHPServerIP and my system IP
the

	$aRet = DllCall("Dhcpsapi.dll", "int", "DhcpGetClientInfo", _


never return under Vista/SP1

comment:3 Changed 15 years ago by Valik

  • Resolution set to No Bug
  • Status changed from new to closed

I entered my info, compiled the script and named it "SuperReallyLongNameAndStuff.exe". Works fine on Windows XP SP3. And I'm even more annoyed now having did that because the code, too long as it is, doesn't even run out of the box due to calls to functions that don't exist in the example. Just a poor example all around.

Anyway, as expected, NO BUG.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.