﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
806	DllStructGetData fails in compiled script with a name longer than 12/13 characters	autoit@…		"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)

}}}
"	Bug	closed		Aut2Exe	3.3.0.0	None	No Bug		
