Help me decipher the meaning of this code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alfa-Zeta
Posts: 1
Joined: 22 May 2018, 07:25

Help me decipher the meaning of this code

22 May 2018, 07:51

Code: Select all

ReadMemory(MADDRESS=0,PROGRAM="")
{
	Static OLDPROC, ProcessHandle
	VarSetCapacity(MVALUE,4,0)
	If PROGRAM != %OLDPROC%
	{
		WinGet, pid, pid, % OLDPROC := PROGRAM
		;QUESTION #0, On the line above, is it the old value of OLDPROC which is used in the winget command, and the 	;OLDPROC is set to the value of PROGRAM, first after the winget command is executed?
		
		;QUESTION #1, What does the question mark after "processHandle" below mean?
		;QUESTION  #2, What does the 0* mean?
		
		ProcessHandle := ( ProcessHandle ? 0*(closed:=DllCall("CloseHandle"
		,"UInt",ProcessHandle)) : 0 )+(pid ? DllCall("OpenProcess"
		,"Int",16,"Int",0,"UInt",pid) : 0)
		
		;QUESTION #3  What is the ": 0" doing in the lines above?
	}
	If (ProcessHandle) && DllCall("ReadProcessMemory","UInt"
	,ProcessHandle,"UInt",MADDRESS,"Str",MVALUE,"UInt",4,"UInt *",0)
	return *(&MVALUE+3)<<24 | *(&MVALUE+2)<<16 | *(&MVALUE+1)<<8 | *(&MVALUE)
	;QUESTION #6 Please explain how the line above works, for example why is there an & before MVALUE, and what does << ;mean?
	return !ProcessHandle ? "Handle Closed: " closed : "Fail"
	
	;QUESTION #4  Does the two return lines above mean that the function returns two different values? 
	;QUESTION #5  What is the meaning of the exclamation mark before "ProcessHandle"?

User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Help me decipher the meaning of this code

22 May 2018, 14:14

At a quick glance:
Answer #0: OLDPROC is assigned the value of PROGRAM before WinGet is executed or the command would fail in this case.
Answer #1: The ? operator is a shorthand replacement for the `if-else` statement. See ternary for more info.
Answer #2: `0*` is `multiply by zero`
Answer #3: `: 0` is assigning the value of 0 to the ProcessHandle variable if the ProcessHandle or pid evaluate false
Answer #4: The 1st return is contingent apon whether ProcessHandle and the call to ReadProcessMemory return true
Answer #4.1: The 2nd return is a conditional statement that returns either a `handle closed` ( if true ) or `fail` ( if false ) string
Answer #5: The exclamation mark before the ProcessHandle variable is the `logical not` operator.
Answer #6: If ProcessHandle is true AND the ReadProcessMemory call returns true, return the value for MVALUE.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, RandomBoy and 359 guests