Help with ScrCmp() please Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: Help with ScrCmp() please

Post by mikeyww » 09 Dec 2022, 04:46

You have attributed a section of code to me, but I didn't write it. I wrote and tested only the code that I posted. A different script may yield different results-- like it certainly did in this case. You have posted a script fragment, so you might be the only person who can debug the problem that you are having after the script has run for a while.

In light of this, you could try to debug the script by logging activity to a file. You can then examine the file closely. You can log the time, along with the values of your variables, returned function calls, and conditional statements. If you create a CSV file, you can easily open it in Microsoft Excel.

I have nothing more to add, but at least demonstrated how the "screen compare" function works. Conceptually, it is fairly basic: the function checks the screenshots at the specified intervals, and returns when the screenshot changes. Thus, your task now is to understand when that is happening, and how it relates to the rest of the script. The log file can help you to see which conditions are met, which code is executing, and why. You are not currently doing that, so the added knowledge may help you to solve the puzzle.

Best of luck!

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 09 Dec 2022, 20:31

mikeyww wrote:
09 Dec 2022, 04:46
You have attributed a section of code to me, but I didn't write it. I wrote and tested only the code that I posted. A different script may yield different results-- like it certainly did in this case. You have posted a script fragment, so you might be the only person who can debug the problem that you are having after the script has run for a while.

In light of this, you could try to debug the script by logging activity to a file. You can then examine the file closely. You can log the time, along with the values of your variables, returned function calls, and conditional statements. If you create a CSV file, you can easily open it in Microsoft Excel.

I have nothing more to add, but at least demonstrated how the "screen compare" function works. Conceptually, it is fairly basic: the function checks the screenshots at the specified intervals, and returns when the screenshot changes. Thus, your task now is to understand when that is happening, and how it relates to the rest of the script. The log file can help you to see which conditions are met, which code is executing, and why. You are not currently doing that, so the added knowledge may help you to solve the puzzle.

Best of luck!
right, sir
the whole code for debug is here:

Code: Select all

#Persistent
#NoEnv
#Warn
#SingleInstance, Force
Menu,Tray,Icon,imageres,273

x1=601
y1=670
x2=700
y2=920
between=10
timeout=100
width:= x2-x1
height:= y2-y1
between:= between*1000
timeout:= timeout*1000

Loop {
 FormatTime, TimeString,, hh:mm:ss
 FileAppend, % "loop start at " TimeString "`n", debug-without-msgbox.log
 SetTimer, NoChange, % timeout
 ScrCmp(x1, y1, width, height,, between, 200)
 SetTimer, NoChange, Off
 ;Msgbox, 48, Changed, just keep watching, 0.1
}

NoChange:
try
{
  FormatTime, TimeString,, hh:mm:ss
  FileAppend, % "no change at " TimeString "`n", debug-without-msgbox.log
}

ScrCmp( X, Y, W, H, Hwnd:=0x0, Sleep* )  {                                        ; v0.66 By SKAN on D3B3/D3B6 @ tiny.cc/scrcmp
Local
Global A_Args
  Sleep[1] := Sleep[1]="" ? 100 : Format("{:d}", Sleep[1])
  Sleep[2] := Sleep[2]="" ? 100 : Format("{:d}", Sleep[2])

  VarSetCapacity(BITMAPINFO, 40, 0)
  NumPut(32, NumPut(1, NumPut(0-H*2, NumPut(W, NumPut(40,BITMAPINFO,"Int"),"Int"),"Int"),"Short"),"Short")

  hBM := DllCall("Gdi32.dll\CreateDIBSection", "Ptr",0, "Ptr",&BITMAPINFO, "Int",0, "PtrP",pBits := 0, "Ptr",0, "Int",0, "Ptr")
  sDC := DllCall("User32.dll\GetDC", "Ptr",(Hwnd := WinExist("ahk_id" . Hwnd)), "Ptr")
  mDC := DllCall("Gdi32.dll\CreateCompatibleDC", "Ptr",sDC, "Ptr")
  DllCall("Gdi32.dll\SaveDC", "Ptr",mDC)
  DllCall("Gdi32.dll\SelectObject", "Ptr",mDC, "Ptr",hBM)
  DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",H, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)

  A_Args.ScrCmp := {"Wait": 1},   Bytes := W*H*4,  Count := 0
  hMod := DllCall("Kernel32.dll\LoadLibrary", "Str","ntdll.dll", "Ptr")
  While ( A_Args.ScrCmp.Wait && (Count<2) )
  {
      DllCall("Gdi32.dll\BitBlt", "Ptr",mDC, "Int",0, "Int",0, "Int",W, "Int",H, "Ptr",sDC, "Int",X, "Int",Y, "Int",0x40CC0020)
      Count := ( (Byte := DllCall("ntdll.dll\RtlCompareMemory", "Ptr",pBits, "Ptr",pBits+Bytes, "Ptr",Bytes) ) != Bytes )
               ? (Count + 1) : 0
      Sleep % (Count ? Sleep[2] : Sleep[1])
  }   Byte +=1
  DllCall("Kernel32.dll\FreeLibrary", "Ptr",hMod)

  SX := (CX := Mod((Byte-1)//4, W) + X),    SY := (CY := (Byte-1) // (W*4)   + Y)
  If (Hwnd)
    VarsetCapacity(POINT,8,0), NumPut(CX,POINT,"Int"), NumPut(CY,POINT,"Int")
  , DllCall("User32.dll\ClientToScreen", "Ptr",Hwnd, "Ptr",&POINT)
  , SX := NumGet(POINT,0,"Int"),  SY := NumGet(POINT,4,"Int")

  If (Wait := A_Args.ScrCmp.Wait)
      A_Args.ScrCmp := { "Wait":0, "CX":CX, "CY":CY, "SX":SX, "SY":SY }
  DllCall("Gdi32.dll\RestoreDC", "Ptr",mDC, "Int",-1)
  DllCall("Gdi32.dll\DeleteDC", "Ptr",mDC)
  DllCall("User32.dll\ReleaseDC", "Ptr",Hwnd, "Ptr",sDC)
  DllCall("Gdi32.dll\DeleteObject", "Ptr",hBM)
Return ( !!Wait )
}

}
and the "debug-without-msgbox.log" here:
(the part of ellipsis is just repeat previous line)

Code: Select all

loop start at 12:44:43
loop start at 12:44:53
loop start at 12:45:04
……
loop start at 01:10:32
loop start at 01:10:42
loop start at 01:10:53
no change at 01:12:33
no change at 01:14:13
no change at 01:15:53
……
no change at 09:07:33
no change at 09:09:13
no change at 09:10:53
……
as i said, it works right at first about 26min, then seems the scrcmp function doesnt work any more,
it shouldnt be like this consider the specific area is keeping changing about per 10 seconds.

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: Help with ScrCmp() please

Post by wer » 10 Dec 2022, 05:52

based on a long period of test,
seems like the msgbox command could "keep screen activated",
so the scrcmp function could detected screen changed,
without msgbox, even if screen changed actually, scrcmp could not detect it
btw, the automation action made by ahk, could not "keep screen activated"
so doesnt other methods could by far, just miraculous and mysterious "msgbox" could

Post Reply

Return to “Ask for Help (v1)”