I first came across this little trick via KaFu (couple of years ago) who was using it in his example for an alternative of _Singleton(). I couldn't understand why he was reading the text in an edit box until I decided to delve a little deeper. Turns out the hidden AutoIt window contains an edit box, so with that in mind I decided to post an example of how useful it could be. In this example I add data from the first instance of the program e.g. PID, location, working directory & then grab this data in the second instance. Just to prove I'm not cheating I've also included data of the second instance too so you can see the PID's are totally different.
Example 1: Compile this example and run it! Once the hidden window is shown run the executable again. MsgBox's will be displayed during the process explaining what is happening.
#include <GUIConstantsEx.au3> Example() Func Example() If @Compiled = 0 Then Return MsgBox(4096, 'AutoIt Hidden Window', 'This example needs to be compiled before you can run it.') ; Show an error if the script isn't compiled. EndIf Local $sData = 'PID: ' & @AutoItPID & @CRLF & _ ; Create a data string about the running executable. 'Version: ' & @AutoItVersion & @CRLF & _ 'Script Directory: ' & @ScriptDir & @CRLF & _ 'Working Directory: ' & @WorkingDir Local $hAutoIt = WinGetHandle('AutoIt_Hidden_Window') ; Check if the AutoIt Hidden Window exists and if it does then read the details set in the edit box. If IsHWnd($hAutoIt) And @error = 0 Then Local $sRead = AutoItWinGetText($hAutoIt) ; Retrieve the information of the inital AutoIt Hidden Window. AutoItWinSetText('Now you can close this window.') ; Set the text of the edit box to show it can now be closed. Return MsgBox(4096, 'AutoIt Hidden Window', 'Previous PID:-' & @CRLF & _ $sRead & @CRLF & @CRLF & _ 'Current PID:-' & @CRLF & _ $sData & @CRLF & @CRLF & _ 'This will now Exit. Thanks.') EndIf AutoItWinSetTitle('AutoIt_Hidden_Window') ; Set the title of the AutoIt Hidden Window with some custom text, the more unqiue the better. AutoItWinShow() AutoItWinSetText($sData) ; Set the text of the edit box using the data string created at the start of the script. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ; Just hit the close button to Exit the application. ExitLoop EndSwitch WEnd EndFunc ;==>Example ; Retrieve the text in AutoIt's Hidden Window. Func AutoItWinGetText($hWnd = Default) If IsHWnd($hWnd) = 0 Or $hWnd = Default Then $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. EndIf Return ControlGetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1')) EndFunc ;==>AutoItWinGetText ; Add text to AutoIt's Hidden Window. Func AutoItWinSetText($sString, $hWnd = Default) If IsHWnd($hWnd) = 0 Or $hWnd = Default Then $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. EndIf Return ControlSetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1'), AutoItWinGetText() & $sString) EndFunc ;==>AutoItWinSetText ; Display AutoIt's Hidden Window. Returns the handle of the window. Func AutoItWinShow() Local $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. WinMove($hWnd, '', (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ; Move the AutoIt Hidden Window and re-size for a better view of the data that will be set. WinSetState($hWnd, '', @SW_SHOW) ; Show the AutoIt Hidden Window, normally this is hidden, but in the interest of this example I'm displaying it. Return $hWnd EndFunc ;==>AutoItWinShow
Example 2: Compile this example and run it.
#include <GUIConstantsEx.au3> Example() Func Example() ; Display AutoIt's Hidden Window. See AutoItWinGetTitle and AutoItWinSetTitle for more details. AutoItWinShow() ; Add a text string to AutoIt's Hidden Window. Compile to see the text in AutoIt's Hidden Window. AutoItWinSetText('Welcome to AutoIt V' & @AutoItVersion & @CRLF) ; Add a text string to AutoIt's Hidden Window. Compile to see the text in AutoIt's Hidden Window. AutoItWinSetText('Windows Type: ' & @OSType & @CRLF) ; Display the text stored in AutoIt's Hidden Window. MsgBox(4096, '', AutoItWinGetText()) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd EndFunc ;==>Example ; Retrieve the text in AutoIt's Hidden Window. Func AutoItWinGetText() Local $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. Return ControlGetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1')) EndFunc ;==>AutoItWinGetText ; Add text to AutoIt's Hidden Window. Func AutoItWinSetText($sString) Local $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. Return ControlSetText($hWnd, '', ControlGetHandle($hWnd, '', 'Edit1'), AutoItWinGetText() & $sString) EndFunc ;==>AutoItWinSetText ; Display AutoIt's Hidden Window. Returns the handle of the window. Func AutoItWinShow() Local $hWnd = WinGetHandle(AutoItWinGetTitle()) ; Get the handle of the AutoIt Hidden Window by finding out the title of the AutoIt Hidden Window. WinMove($hWnd, '', (@DesktopWidth / 2) - 250, (@DesktopHeight / 2) - 250, 500, 500) ; Move the AutoIt Hidden Window and re-size for a better view of the data that will be set. WinSetState($hWnd, '', @SW_SHOW) ; Show the AutoIt Hidden Window, normally this is hidden, but in the interest of this example I'm displaying it. Return $hWnd EndFunc ;==>AutoItWinShow
Example 3: Send text to a 2nd instance.
Edited by guinness, 08 October 2012 - 02:50 PM.




