Sign in to follow this  
Followers 0
dbzfanatic

Help with storing WinGetText

7 posts in this topic

What I want to do is store all the window's text to a variable. That I can do, but I want that variable to KEEP the original text even if the window is cleared. This is for creating a log file and not some stupid keylogger. I am running a F.E.A.R. Multiplayer server and have received complaints about people cursing but it seems that after so many messages are entered the server clears a few to make room for displaying the newer ones. I need to keep track of ALL of the messages so I can confirm this, otherwise it's just a matter of "he said/she said". I've tried tweaking my code and moving bits before others to no avail. Here is the code.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=..\..\..\Program Files\Sierra\FEARCombat\server log.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <GUIConstants.au3>
#include <Constants.au3>

$line2 = "TabItemContainer" & "&Clear"

$FEAR = Run("FEARServer.exe","C:\Program Files\Sierra\FEARCombat",@SW_SHOW,2)
If @error <> 0 Then
    
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("FEAR Server Log", 303, 235, 193, 125)
$edtLine = GUICtrlCreateEdit("", 8, 16, 289, 209, 0x50210004)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(250)
    If ProcessExists("FEARServer.exe") Then
        $line = WinGetText("FEAR - Dedicated Server")
        If $line2 <> $line Then
            $line = $line2 & $line          
            $line = StringReplace($line,$line2,"")
            GUICtrlSetData($edtLine,$line)
            $line2 = $line
        EndIf
    Else
        $write = GUICtrlRead($edtLine)
        $write = StringReplace($write,"TabItemContainer","")
        $write = StringReplace($write,"&Clear","")
        $File = FileOpen("server.log",9)
        FileWrite($File,$write & @CRLF & "****************************************************" & @CRLF & @CRLF)
        MsgBox(64,"Server Log","Log File Saved.")
        Exit
    EndIf
WEnd

Note: I have tried simply Diming $line2 but then NO window text is displayed or stored.

Note 2: The file write operation works perfectly for the data that is stored in the variable, what you see in the edit box is what is written to the file. That's why I need all the text to be stored and not just what's currently there.

Share this post


Link to post
Share on other sites



You need to write changes to the log file continually. Your script only appears to record the last text read and then when the program ends you save that. Also, you have 2 lines which do nothing

$line = $line2 & $line;prefix $line2 
            $line = StringReplace($line,$line2,"");remove $line2 so why prefix it?

Maybe something like this would work

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\..\..\Program Files\Sierra\FEARCombat\server log.exe
#AutoIt3Wrapper_Compression=4
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <GUIConstants.au3>
#include <Constants.au3>

$line2 = "TabItemContainer" & "&Clear"

$FEAR = Run("FEARServer.exe", "C:\Program Files\Sierra\FEARCombat", @SW_SHOW, 2)
If @error <> 0 Then
    
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("FEAR Server Log", 303, 235, 193, 125)
$edtLine = GUICtrlCreateEdit("", 8, 16, 289, 209, 0x50210004)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
global $lastwritten, $newstart
$File = FileOpen("server.log", 9)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(250)
    If ProcessExists("FEARServer.exe") Then
        $line = stringreplace(WinGetText("FEAR - Dedicated Server"),@LF,'')
        $lineA = StringSplit($line, @CR)
        $newstart = 1
        For $n = $lineA[0] To 1 Step -1
            If $lineA[$n] = $lastwritten Then
                $newstart = $n + 1
                ExitLoop
            EndIf
        Next
        for $n = $newstart to $lineA[0]
            FileWriteLine($File,$limneA[$n])
        Next
        $lastwritten = $limneA[$lineA[0]]
        GUICtrlSetData($edtLine, $line)
        
    Else
        FileWrite($File, $write & @CRLF & "****************************************************" & @CRLF & @CRLF)
        MsgBox(64, "Server Log", "Log File Saved.")
        Exit
    EndIf
WEnd
FileClose($File)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The "prefix" lines if I did them properly shouldn't actually prefix the lines, they should add the text currently read to the stored text and remove any redundant text. I'll test your code and edit this post with results.

Edit: The script writes all the text to the file properly but your script has "$limneA, Undeclared global variable". I switched that to $lineA and it writes properly (or seems to) but it doesn't DISPLAY like I need it to. The log is great but I want to be able to see everything as well. If I open the logfile while the program is still running I don't know if it will still write or if it will encounter an error because the file is open. If that's the case I really need it to display properly as well,even if I clear the text from the server console.

Edited by dbzfanatic

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The "prefix" lines if I did them properly shouldn't actually prefix the lines, they should add the text currently read to the stored text and remove any redundant text. I'll test your code and edit this post with results.

Edit: The script writes all the text to the file properly but your script has "$limneA, Undeclared global variable". I switched that to $lineA and it writes properly (or seems to) but it doesn't DISPLAY like I need it to. The log is great but I want to be able to see everything as well. If I open the logfile while the program is still running I don't know if it will still write or if it will encounter an error because the file is open. If that's the case I really need it to display properly as well,even if I clear the text from the server console.

when you say "the log file is great" does that mean it does what you want?

If so then to get the same information in the edit just add a line after the FileWriteLn line

for $n = $newstart to $lineA[0]
            FileWriteLine($File,$limneA[$n])
            _GUICtrlEdit_AppendText($Edline,$limneA[$n])
        Next

and remove the line

GUICtrlSetData($edtLine, $line)

You need to include GuiEdit.au3 as well.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Well the log seemed to do what I needed but when I looked closer everything was rewritten, which is exactly what I DON'T want. I added the display lines like you suggested and it was easier to catch because as I typed my server commands the lines were continually rewritten. I want the text stored but I only want it stored once, no redundancies. That is exactly what my code was trying to do with the $line and $line2 StringReplace code (as I've already explained). What I want is to read the text from the window, store that to a variable ($line), compare the newly stored text to the text that was stored already ($line2). If it's different remove the part of the text that was already stored from the newly read text, then store THAT text to the previously stored text variable.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\..\..\Program Files\Sierra\FEARCombat\server log.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <GUIConstants.au3>
#include <Constants.au3>
#include <GuiEdit.au3>

$line2 = "TabItemContainer" & "&Clear"

$FEAR = Run("FEARServer.exe", "C:\Program Files\Sierra\FEARCombat", @SW_SHOW, 2)
If @error <> 0 Then
    
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("FEAR Server Log", 303, 235, 193, 125)
$edtLine = GUICtrlCreateEdit("", 8, 16, 289, 209, 0x50210004)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(250)
    If ProcessExists("FEARServer.exe") Then
        $line = WinGetText("FEAR - Dedicated Server")
        If $line2 <> $line Then
            $line = $line2 & $line
            $line = StringReplace($line, $line2, "")
            _GUICtrlEdit_AppendText($edtLine,$line)
            $line2 = $line2 & $line
        EndIf
    Else
        $write = GUICtrlRead($edtLine)
        $write = StringReplace($write, "TabItemContainer", "")
        $write = StringReplace($write, "&Clear", "")
        $File = FileOpen("server.log", 9)
        FileWrite($File, $write & @CRLF & "****************************************************" & @CRLF & @CRLF)
        MsgBox(64, "Server Log", "Log File Saved.")
        Exit
    EndIf
WEnd

Edit: Yes I've tried it without the first $line = $line2 & $line but to no avail. The same problem repeats itself.

Edited by dbzfanatic

Share this post


Link to post
Share on other sites

OK. I think my script was close. I've changed it a bit and tested it and I think it does what you want. The trick is to only copy the lines up to the one before the last one because you don't know what that line will be until the next one is started.

This is the script which I think works

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\..\..\Program Files\Sierra\FEARCombat\server log.exe
#AutoIt3Wrapper_Compression=4
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Misc.au3>
#include <GUIConstants.au3>
#include <Constants.au3>
#include <guiedit.au3>

$line2 = "TabItemContainer" & "&Clear"

$FEAR = Run("FEARServer.exe", "C:\Program Files\Sierra\FEARCombat", @SW_SHOW, 2)
If @error <> 0 Then

EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("FEAR Server Log", 303, 235, 193, 125)
$edtLine = GUICtrlCreateEdit("", 8, 16, 289, 209, 0x50210004)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $lastwritten, $newstart
$File = FileOpen("server.log", 9)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(250)
    If ProcessExists("FEARServer.exe") Then
        $line = StringReplace(WinGetText("FEAR - Dedicated Server"), @LF, '')
        $lineA = StringSplit($line, @CR)
        $newstart = 1
        If $lineA[0] > 1 Then
            For $n = $lineA[0] - 1 To 1 Step -1
                If $lineA[$n] = $lastwritten Then
                    $newstart = $n + 1
                    ExitLoop
                EndIf
            Next
            For $n = $newstart To $lineA[0]-1
                FileWriteLine($File, $lineA[$n])
                _GUICtrlEdit_AppendText($edtLine, $lineA[$n] & @CRLF)
            Next
            $lastwritten = $lineA[$lineA[0]-1]
        EndIf
    Else
        FileWrite($File, @CRLF & "****************************************************" & @CRLF & @CRLF)
        MsgBox(64, "Server Log", "Log File Saved.")
        Exit
    EndIf
WEnd
FileClose($File)

and this is the script I compiled and ran to test it

;save as fearserver.au3 
GUICreate("FEAR - Dedicated Server")
GUICtrlCreateEdit("",0,0,300,300)
GUISetState()
while GUIGetMsg() <> -3
    sleep(90)
WEnd

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thank you very much. The script works just as I need. I'm very appreciative of your efforts to help me. Now I will be able to check for sure if there is any inappropriate behavior happening on my server so thank you once again. :P

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