Sign in to follow this  
Followers 0
scu42

[solved] strange problem - script seems to run asynchron - if condition before variables are set correctly

8 posts in this topic

#1 ·  Posted (edited)

I have a really strange problem. My script seems to run asynchron.

The if-condition is tested before both of the variables which should be testet have been set to proper values.

Please have a look at my "test" source code and especially the console output to see which problem i am talking about.

If you want to reproduce it, you can download md5deep here: http://md5deep.sourceforge.net/#download

i added $whoami to the _FileHashMD5 function so that you can easilier see which function call produces which and when output.

Sourcecode:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Func _FileHashMD5($sFile, $whoami)
    Local $hPID, $sLine, $iFileNameLen, $sHash
    If Not FileExists($sFile) Or StringInStr(FileGetAttrib($sFile), "D") Or StringInStr($sFile, "*") Or StringInStr($sFile, "?") Or Not StringInStr($sFile, "\") Then
  MsgBox(16, "Error", "file does not exist or cannot be opened")
  Exit
EndIf
If @OSArch == "X64" Then
  $md5exe = "md5deep64.exe"
Else
  $md5exe = "md5deep.exe"
EndIf
$hPID = Run('"' & @ScriptDir & '\' & $md5exe & '" "' & $sFile & '"',@TempDir,@SW_HIDE,2)
$iError = @error
    If $iError Then
  MsgBox(16, "Error", "md5deep.exe not found")
  Exit
    EndIf
    While ProcessExists($hPID)
        $sLine = StdoutRead($hPID,True)
  If $sLine<>"" Then ConsoleWrite($whoami & ": " & $sLine)
    WEnd
    $iFileNameLen = StringLen($sFile)
    $sHash = StringStripWS(StringTrimRight($sLine, $iFileNameLen + 1),8)
    SetError(0)
    ProcessClose($hPID)
    Return $sHash
EndFunc  ;==>_FileHash
While 1
$hash1 = _FileHashMD5("C:\Windows\notepad.exe", "first")
$hash2 = _FileHashMD5("C:\Windows\notepad.exe", "second")

If StringCompare($hash1, $hash2) <> 0 Then
ConsoleWrite("StringCompare($hash1, $hash2) <> 0" & @CRLF)
; Files don't match - different md5 - should never happen because same file is hashed
Else
ConsoleWrite("StringCompare($hash1, $hash2) == 0" & @CRLF)
EndIf
WEnd

Console:

>"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\WTF\Documents\Autoit Tools\test.au3" /UserParams  
+>19:14:16 Starting AutoIt3Wrapper v.2.1.0.8    Environment(Language:0407  Keyboard:00000407  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64)
>Running AU3Check (1.54.22.0)  from:C:\Program Files (x86)\AutoIt3
+>19:14:16 AU3Check ended.rc:0
>Running:(3.3.8.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\WTF\Documents\Autoit Tools\test.au3"  
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) == 0
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
StringCompare($hash1, $hash2) <> 0
first: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
second: f2c7bb8acc97f92e987a2d4087d021b1  C:\Windows\notepad.exe
...
Edited by scu42

Share this post


Link to post
Share on other sites



You should either be using RunWait on the run command, or you should use the _Crypt_HashData function which comes in the standard installation of AutoIt.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Why should i do that?

With RunWait i cannot access stdout and with _Crypt_HashData i cannot calculate hashes of files which are bigger than 4GB.

The following snippet (which i already have implemented) should behave exactly like RunWait

While ProcessExists($hPID)
   $sLine = StdoutRead($hPID,True)
   If $sLine<>"" Then ConsoleWrite($whoami & ": " & $sLine)
WEnd

Can anyone please explain why my script does these weird things? Is this a bug or a feature?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

It isn't that strange. You need to check for EOF condition for this to work the way you want and predictably.

Func _FileHashMD5($sFile, $whoami)
Local $hPID, $sLine, $iFileNameLen, $sHash
If Not FileExists($sFile) Or StringInStr(FileGetAttrib($sFile), "D") Or StringInStr($sFile, "*") Or StringInStr($sFile, "?") Or Not StringInStr($sFile, "") Then
  MsgBox(16, "Error", "file does not exist or cannot be opened")
  Exit
EndIf
If @OSArch == "X64" Then
  $md5exe = "md5deep64.exe"
Else
  $md5exe = "md5.exe"    ; <<<< changed for me
EndIf
$hPID = Run('"' & @ScriptDir & '' & $md5exe & '" "' & $sFile & '"', @TempDir, @SW_HIDE, 2)
$iError = @error
If $iError Then
  MsgBox(16, "Error", "md5deep.exe not found")
  Exit
EndIf
; that's where we differ
While Not @error
  $sLine &= StdoutRead($hPID)
WEnd
ConsoleWrite($whoami & ": " & $sLine)
$iFileNameLen = StringLen($sFile)
$sHash = StringStripWS(StringTrimRight($sLine, $iFileNameLen + 1), 8)
Return $sHash
EndFunc   ;==>_FileHashMD5
While 1
  Local $hash1, $hash2
$hash1 = _FileHashMD5("C:Windowsnotepad.exe", "first ")
$hash2 = _FileHashMD5("C:Windowsnotepad.exe", "second")
If StringCompare($hash1, $hash2) <> 0 Then
  ConsoleWrite("StringCompare($hash1, $hash2) <> 0" & @CRLF)
  ; Files don't match - different md5 - should never happen because same file is hashed
Else
  ConsoleWrite("StringCompare($hash1, $hash2) == 0" & @CRLF)
EndIf
WEnd

Also ProcessClose() of a process which you just know doesn't exists anymore is dangerous (another process could have been launched meantime with [possibly] the same PID). Unlikely but possible.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

When i change my code according to your suggestion, i get the following output:

first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe
first: f2c7bb8acc97f92e987a2d4087d021b1  C:Windowsnotepad.exe

Share this post


Link to post
Share on other sites

When i do the following changes, it works but sometimes the "while 1" loop is not exited.

I think this happens when the md5deep.exe runs to fast.

While 1
   $sLine = StdoutRead($hPID,False)
   If @error Then ExitLoop
WEnd

Any ideas?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Any ideas?

Yeah, either use the standard UDF _Crypt_EncryptData() or Ward's excellent example for calculating the MD5 hash using OPCode Both are by some magnitudes faster and more reliable than using the md5deep.exe. Edited by KaFu
1 person likes this

Share this post


Link to post
Share on other sites

I'm using now _Crypt_HashFile($pathToFile, $CALG_MD5) and everything works fine.

I wonder why so much threads in this forum point to custom UDFs when there already exists a function for it.

Thanks for your help!

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