AutoIt Send() Not Working - autoit

Func OpenSHK()
global $Loginx = 0, $Loginy = 0, $Playx = 0, $Playy = 0
Run(#ProgramFilesDir & "\Stronghold Kingdoms\StrongholdKingdoms.exe")
WinWaitActive("Stronghold Kingdoms")
If WinExists("Stronghold Kingdoms") Then
SendKeepActive("Stronghold Kingdoms")
Else
WinWait("Stronghold Kingdoms")
SendKeepActive("Stronghold Kingdoms")
EndIf
Sleep(5000)
Send("testphrase")
Local $search = _ImageSearch('pics/LoginButton.bmp', 0, $Loginx, $Loginy, 0)
If $search = 1 Then
MouseMove($Loginx, $Loginy, 20)
MouseClick("left")
Else
Do
$search = _ImageSearch('pics/LoginButton.bmp', 0, $Loginx, $Loginy, 0)
sleep(5000)
Until $search = 1
MouseMove($Loginx, $Loginy, 20)
MouseClick("left")
EndIf
Sleep(5000)
Local $search = _ImageSearch('pics/PlayButton.bmp', 0, $Playx, $Playy, 0)
If $search = 1 Then
MouseMove($Playx, $Playy, 20)
MouseClick("left")
Else
Do
$search = _ImageSearch('pics/PlayButton.bmp', 0, $Playx, $Playy, 0)
sleep(5000)
Until $search = 1
MouseMove($Playx, $Playy, 20)
MouseClick("left")
EndIf
WinWaitActive("Stronghold Kingdoms - World 8")
sleep(10000)
EndFunc
The function works fine, until it reaches line 6. It does not send the test phrase, and I cannot seem to figure out why.
Thanks.

There are many errors in your code:
Run("#ProgramFilesDir\Stronghold Kingdoms\StrongholdKingdoms.exe")
should be Run(#ProgramFilesDir & "\Stronghold Kingdoms\StrongholdKingdoms.exe")
That's why winwaitactive probably gets never passed. Try a loop instead to see if the window exists, rather than if it's active.
If the window exists you can activate it then or use SendKeepActive (I think that's what it's called.)

When I was working with AutoIt and having a similar problem (for the same program :) ), I think I ended up using controlsend, sometimes, and send at other times.
I also ended up going into the handles, and telling autoit to make specific text boxes active.

Related

Object Shell.Application Open Folder In Same Explorer Window

I want to ask you a question regarding Shell.Application COM Object. The programming language I work in is AutoIT. I would like to know, how can I open a folder in the exact same Window a previous folder has been opened.
For instance, if I open the C:\Folder in a Window and it has a subfolder, then how can I make the Script open that subfolder in the same window the "parent folder" has been opened, without creating a new window for that. The only way I can open a Folder is like this:
global $ShellApplication = ObjCreate("Shell.Application")
$ShellApplication.Explore("C:\Folder")
Sleep(100)
$ShellApplication.Explore("C:\Folder\Subfolder")
but this way is the wrong way. I hope someone has got any idea about how this could be achieved.
I don't know whether this is what you want, but you can automate the current window like this:
#include <Array.au3>
Opt("SendKeyDelay", 1) ;5 milliseconds
Opt("SendKeyDownDelay", 1) ;1 millisecond
$pid = ShellExecute("explorer.exe ", #DesktopDir)
Sleep(300)
;~ $aWinList=WinList("[REGEXPCLASS:(Explore|Cabinet)WClass]")
;~ _ArrayDisplay($aWinList)
ConsoleWrite($pid & #CRLF)
$handle = _WinGetHandleByPID($pid)
Sleep(2000)
SendKeepActive($handle)
Send('{F4}')
Send('^a')
Send(#AppDataCommonDir & "{ENTER}")
Func _WinGetHandleByPID($vProcess, $nShow = 1)
; Get Window Handle by PID
; Author Hubertus
; derived from Smoke_N's script with the same name,
; but to handle more than one process with the same name
; and to return handle of newest window ($nShow = 2).
;
; $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
; $nShow = -1 "All (Visble or not)", $nShow = 0 "Not Visible Only", $nShow = 1 "Visible Only", $nShow = 2 "return handle of newest window "
; #error = 0 Returns array of matches. Array[0][0] and #extended shows number of matches.
; #error = 0 and $nShow = 2 return handle of newest window
; Array[n][0] shows windows title. Array[n][1] shows windows handle. n = 1 to #extended
; #error = 1 Process not found.
; #error = 2 Window not found.
If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
Local $iWinList, $aWinList = WinList()
Local $iResult, $aResult[UBound($aWinList)][2]
Local $iProcessList, $aProcessList = ProcessList($vProcess)
If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0], ["", $vProcess]]
For $iWinList = 1 To $aWinList[0][0]
For $iProcessList = 1 To $aProcessList[0][0]
If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
$iResult += 1
$aResult[$iResult][0] = $aWinList[$iWinList][0]
$aResult[$iResult][1] = $aWinList[$iWinList][1]
EndIf
Next
Next
If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
ReDim $aResult[$iResult + 1][2]
$aResult[0][0] = $iResult
If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
Return SetError(0, $iResult, $aResult)
EndFunc ;==>_WinGetHandleByPID

Post wall facebook Autoit

I'm learning Autoit and have searched for a UDF as below, expecting people to help
Func FB_Post($Handle, $Content, $Pri = 0, $UID = False)
Local $Post_pri[3] = ["300645083384735", "291667064279714", "286958161406148"], $data, $UserID = ($UID ? $UID : $Handle[1]), $Post_body, $Get_id
If not IsArray($Handle) then Return SetError(1, 0, False)
If $Pri < 0 Or $Pri > 2 or (not IsNumber($Pri)) Then $Pri = 0
$data = "privacyx=" & ($UID ? "" : $Post_pri[$Pri]) & "&xhpc_targetid=" & $UserID &"&xhpc_message=" & _URIEncode($Content) & "&fb_dtsg=" & $Handle[2]
$Post_body = _HttpRequest(2, "https://www.facebook.com/ajax/updatestatus.php",$data, $Handle[0], "https://www.facebook.com/profile.php?id=" & ($UID ? $UID :$Handle[1]))
$Get_id = StringRegExp($Post_body, "top_level_post_id":"(.*?)"",3)
If #error then Return SetError(2, 0, True)
Return $Get_id[0]
EndFunc
Click Full Source

winhttp.winhttprequest.5.1 with progress bar

It is possible, in AU3, use WinHttp.WinHttpRequest.5.1 with a progress bar? Actually what I need is to know how many MB have already been downloaded.
I need to use "WinHttp.WinHttpRequest.5.1" because I have to use the parameter "Referer" in the header.
Of course it is
#include "WinHttp.au3"
; Download some gif
;~ http://33.media.tumblr.com/dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif
; Initialize and get session handle
$hOpen = _WinHttpOpen()
; Get connection handle
$hConnect = _WinHttpConnect($hOpen, "http://33.media.tumblr.com")
; Specify the reguest
$hRequest = _WinHttpOpenRequest($hConnect, Default, "dd3ffab90cc338666f192fd86f6a4f8f/tumblr_n0pefhIpss1swyb6ao1_500.gif")
; Send request
_WinHttpSendRequest($hRequest)
; Wait for the response
_WinHttpReceiveResponse($hRequest)
;~ ConsoleWrite(_WinHttpQueryHeaders($hRequest) & #CRLF)
ProgressOn("Downloading", "In Progress...")
Progress(_WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH))
Local $sData
; Check if there is data available...
If _WinHttpQueryDataAvailable($hRequest) Then
While 1
$sChunk = _WinHttpReadData_Ex($hRequest, Default, Default, Default, Progress)
If #error Then ExitLoop
$sData &= $sChunk
Sleep(20)
WEnd
Else
MsgBox(48, "Error", "Site is experiencing problems (or you).")
EndIf
Sleep(1000)
ProgressOff()
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
; Do whatever with data, write to some file or whatnot. I'll just print it to console here:
ConsoleWrite($sData & #CRLF)
Local $hFile = FileOpen(#DesktopDir & "\test.gif", 26)
FileWrite($hFile, $sData)
FileClose($hFile)
Func Progress($iSizeAll, $iSizeChunk = 0)
Local Static $iMax, $iCurrentSize
If $iSizeAll Then $iMax = $iSizeAll
$iCurrentSize += $iSizeChunk
Local $iPercent = Round($iCurrentSize / $iMax * 100, 0)
ProgressSet($iPercent, $iPercent & " %")
EndFunc
Func _WinHttpReadData_Ex($hRequest, $iMode = Default, $iNumberOfBytesToRead = Default, $pBuffer = Default, $vFunc = Default)
__WinHttpDefault($iMode, 0)
__WinHttpDefault($iNumberOfBytesToRead, 8192)
__WinHttpDefault($vFunc, 0)
Local $tBuffer, $vOutOnError = ""
If $iMode = 2 Then $vOutOnError = Binary($vOutOnError)
Switch $iMode
Case 1, 2
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("byte[" & $iNumberOfBytesToRead & "]")
EndIf
Case Else
$iMode = 0
If $pBuffer And $pBuffer <> Default Then
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]", $pBuffer)
Else
$tBuffer = DllStructCreate("char[" & $iNumberOfBytesToRead & "]")
EndIf
EndSwitch
Local $sReadType = "dword*"
If BitAND(_WinHttpQueryOption(_WinHttpQueryOption(_WinHttpQueryOption($hRequest, $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_PARENT_HANDLE), $WINHTTP_OPTION_CONTEXT_VALUE), $WINHTTP_FLAG_ASYNC) Then $sReadType = "ptr"
Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpReadData", _
"handle", $hRequest, _
"struct*", $tBuffer, _
"dword", $iNumberOfBytesToRead, _
$sReadType, 0)
If #error Or Not $aCall[0] Then Return SetError(1, 0, "")
If Not $aCall[4] Then Return SetError(-1, 0, $vOutOnError)
If IsFunc($vFunc) Then $vFunc(0, $aCall[4])
If $aCall[4] < $iNumberOfBytesToRead Then
Switch $iMode
Case 0
Return SetExtended($aCall[4], StringLeft(DllStructGetData($tBuffer, 1), $aCall[4]))
Case 1
Return SetExtended($aCall[4], BinaryToString(BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]), 4))
Case 2
Return SetExtended($aCall[4], BinaryMid(DllStructGetData($tBuffer, 1), 1, $aCall[4]))
EndSwitch
Else
Switch $iMode
Case 0, 2
Return SetExtended($aCall[4], DllStructGetData($tBuffer, 1))
Case 1
Return SetExtended($aCall[4], BinaryToString(DllStructGetData($tBuffer, 1), 4))
EndSwitch
EndIf
EndFunc
You here find the WinHTTP.au3 here: https://github.com/dragana-r/autoit-winhttp/releases.
It is my favorite UDF!
I have comments all around the code to explain you the process.

Autoit Add data instead of replace data

I start a server with:
$BungeeServer = Run("java -Xmx512M -jar " & '"' & $file0 & "\BungeeCord.jar" & '"', $file0, $Hide, $STDERR_MERGED)
Then I get the data from the server with(It's in a while loop):
Assign("sOutput", StdoutRead($BungeeServer, False, False) & #CRLF)
GUICtrlSetData($Edit1, $sOutput)
The problem is that It only returns the latest string of information. I need it to add instead of replace the values. Anybody know an easy way to do that?
Edit: When a server starts, you normally get a cmd window. I want to be able to have it in a Gui instead. Also I only want it to appear when I click a button, still it should start getting data when the server starts. The lines of text also went outside the box. I tried:
Do
$msg1 = GUIGetMsg()
$line1 = StdoutRead($BungeeServer, True)
$totalOutput1 &= $line1 & #CRLF
GUICtrlSetData($Edit1, $totalOutput1)
$line2 = StdoutRead($1, True)
$totalOutput2 &= $line2 & #CRLF
GUICtrlSetData($Edit2, $totalOutput2)
$line3 = StdoutRead($2, True)
$totalOutput3 &= $line3 & #CRLF
Until $msg = $GUI_EVENT_CLOSE
The edit boxes were made with:
$Form2 = GUICreate("Servers", #DesktopWidth - 15, #DesktopHeight - _GetTaskBarHeight() - 35, -1, -1)
$Edit1 = GUICtrlCreateEdit("", 0, 0, 634, 334, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
Font()
$Edit2 = GUICtrlCreateEdit("", 635, 0, 634, 334, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
Font()
$Edit3 = GUICtrlCreateEdit("", 634 + 634, 0, 634, 334, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
Font()
GUISetState(#SW_SHOW)
Edit: I found a better way to do it where I made a child window and didn't need all the other stuff:
$GUI = GUICreate("Consoles", 1020, 600, 1282, 300, BitOR($WS_MINIMIZEBOX, $WS_SYSMENU, $WS_CAPTION, $WS_CLIPCHILDREN, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_BORDER, $WS_CLIPSIBLINGS))
If GUICtrlRead($Bungee) = 1 Then
$BungeeServer = Run("java -Xmx512M -jar " & '"' & $file0 & "\BungeeCord.jar" & '"', $file0, $Hide)
If Not ProcessWait($BungeeServer) = 0 Then
WinSetTitle("C:\Windows\system32\java.exe", "", "Bungee")
WinSetTitle("C:\WINDOWS\SYSTEM32\java.exe", "", "Bungee")
Global $hwnd0 = WinGetHandle("Bungee")
EndIf
EndIf
_WinAPI_SetWindowLong($hwnd0, $GWL_EXSTYLE, $WS_EX_MDICHILD)
_WinAPI_SetParent($hwnd0, $GUI)
WinMove($hwnd0, "", 0, 0, 340, 300)
Still need to find a way to use ControlSend to the child windows :/
Use StdoutRead($BungeeServer, True)
StdoutRead ( process_id [, peek = false [, binary = false]] )
peek [optional] If true the function does not remove the read characters from the stream.
OR
Consider storing the previous data before getting the new one. Instead of assigning the new data to the variable, you can just add it.
eg.
$totalOutput &= $newOutput & #LF
About StdoutRead: In order to get all data, it must be called in a loop.
From the help file:
; Demonstrates StdoutRead()
#include <Constants.au3>
Local $foo = Run(#ComSpec & " /c dir foo.bar", #SystemDir, #SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
$line = StdoutRead($foo)
If #error Then ExitLoop
MsgBox(0, "STDOUT read:", $line)
WEnd
While 1
$line = StderrRead($foo)
If #error Then ExitLoop
MsgBox(0, "STDERR read:", $line)
WEnd
MsgBox(0, "Debug", "Exiting...")
Considering all above, StdoutRead should be used like this:
Local $sOutput
While 1
$line = StdoutRead($foo, True)
If #error Then ExitLoop
$sOutput = $line
WEnd
OR
Local $sOutput
While 1
$line = StdoutRead($foo)
If #error Then ExitLoop
$sOutput &= $line & #LF
WEnd

MsiEnumRelatedProducts returns ERROR_INVALID_PARAMETER with iProductIndex > 0

I'm trying to write an AutoIt script that uninstalls all MSI packages with a specific Upgrade Code. This is my code so far:
$i = 0
Do
$buffer = DllStructCreate("wchar[39]")
$ret = DllCall("msi.dll", "UINT", "MsiEnumRelatedProductsW", _
"wstr", "{a1b6bfda-45b6-43cc-88de-d9d29dcafdca}", _ ; lpUpgradeCode
"dword", 0, _ ; dwReserved
"dword", $i, _ ; iProductIndex
"ptr", DllStructGetPtr($buffer)) ; lpProductBuf
$i = $i + 1
MsgBox(0, "", $ret[0] & " " & DllStructGetData($buffer, 1))
Until($ret[0] <> 0)
This works flawlessly to determine the Product Code for the first installed product, but it returns 87 (ERROR_INVALID_PARAMETER) as soon as iProductIndex is incremented to 1. Usually this error is returned when the input GUID is malformed, but if that would be the case, it shouldn't work with iProductIndex = 0 either...
What I expected from this code (when 2 packages with the same Upgrade Code are installed) is:
Print "0 <first Product Code>"
Print "0 <second Product Code>"
Print "259" (ERROR_NO_MORE_ITEMS)
What it currently does:
Print "0 <first Product Code>"
Print "87" (ERROR_INVALID_PARAMETER)
Any ideas?
(If you want to test this code on your own computer, you will need to have two MSI packages with the same UpgradeCode installed. Here are my WiX test packages: http://pastie.org/3022676 )
OK, I've found a simple workaround: I just remove every product I can find with iProductIndex = 0 in a loop.
Func GetProduct($UpgradeCode)
$buffer = DllStructCreate("wchar[39]")
$ret = DllCall("msi.dll", "UINT", "MsiEnumRelatedProductsW", _
"wstr", $UpgradeCode, _ ; lpUpgradeCode
"dword", 0, _ ; dwReserved
"dword", 0, _ ; iProductIndex
"ptr", DllStructGetPtr($buffer)) ; lpProductBuf
Return DllStructGetData($buffer, 1)
EndFunc
$Last = ""
$Product = ""
Do
$Last = $Product
$Product = GetProduct("{a1b6bfda-45b6-43cc-88de-d9d29dcafdca}")
If $Product = "" Then Exit
$Ret = RunWait("msiexec /qn /x " & $Product)
ConsoleWrite($Ret & " " & $Product & #CRLF)
If $Product = $Last Then Exit 1
Until($product = "")
This doesn't work because using DllCall() the DLL is not kept open. The function MsiEnumRelatedProducts propably has internal state that is required for the enumeration and is only initialized when the index is zero. When the DLL is closed, this state is lost.
To fix this, call DllOpen() before the loop. Keep the DLL open while the loop is running and pass the DLL handle instead of its filename to DllCall(). Close the DLL using DllClose() when the loop has been finished.
Here is a function that returns an array of ProductCodes for the given UpgradeCode. It returns Null in case the function didn't found any products.
Func GetRelatedProducts( $UpgradeCode )
Local $result[ 1 ] ; Can't declare empty array :/
Local $dll = DllOpen( "msi.dll" )
If #error Then Return SetError( 1, #error, Null )
Local $buffer = DllStructCreate( "wchar[39]" )
Local $index = 0
Local $success = False
Do
Local $ret = DllCall( $dll, "UINT", "MsiEnumRelatedProductsW", _
"wstr", $UpgradeCode, _ ; lpUpgradeCode
"dword", 0, _ ; dwReserved
"dword", $index, _ ; iProductIndex
"ptr", DllStructGetPtr($buffer)) ; lpProductBuf
If #error Then
DllClose( $dll )
Return SetError( 1, #error, Null )
EndIf
$success = $ret[ 0 ] = 0 ; $ret[ 0 ] contains the DLL function's return value
If( $success ) Then
Local $productCode = DllStructGetData( $buffer, 1 )
Redim $result[ $index + 1 ]
$result[ $index ] = $productCode
$index += 1
EndIf
Until( Not $Success )
DllClose( $dll )
if( $index ) Then
Return $result
Else
Return Null
EndIf
EndFunc
Usage:
Local $productCodes = GetRelatedProducts( "{insert-upgradecode-here}" )
If( IsArray( $productCodes ) ) Then
MsgBox( 0, "Success!", "Found products:" & #CRLF & _ArrayToString( $productCodes, #CRLF ) )
EndIf

Resources