StandBy rewriting for v2 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

StandBy rewriting for v2

10 Sep 2022, 05:29

There is a useful function for v1.1L to put system in standby mode:

Code: Select all

StandBy() {
	;enable SeShutdownPrivilege token
	Process, Exist  ; Sets ErrorLevel to the PID of this running script.
	; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):
	h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ErrorLevel, "Ptr")
	; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):
	DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "PtrP", t)
	VarSetCapacity(ti, 16, 0)  ; structure of privileges
	NumPut(1, ti, 0, "UInt")  ; one entry in the privileges array...
	; Retrieves the locally unique identifier of the privilege:
	DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeShutdownPrivilege"
	        , "Int64P", luid)
	NumPut(luid, ti, 4, "Int64")
	NumPut(2, ti, 12, "UInt")  ; Enable this privilege: SE_PRIVILEGE_ENABLED = 2
	; Update the privileges of this process with the new access token:
	r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", &ti
	             , "UInt", 0, "Ptr", 0, "Ptr", 0)
	DllCall("CloseHandle", "Ptr", t)  ; Close this access token handle to save memory.
	DllCall("CloseHandle", "Ptr", h)  ; Close this process handle to save memory.

	;PC standby (S3)
	DllCall("ntdll\ZwInitiatePowerAction", "int", 2, "int", 4, "int", 0x80000000, "int", 1)
}
Trying to rewrite it to v2, so replaced "PtrP" to "Ptr*", VarSetCapacity to Buffer, but still miss something:

Code: Select all

StandBy() {
	;enable SeShutdownPrivilege token
	PID := ProcessExist()
	; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):
	h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", PID, "Ptr")
	; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):
	DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "Ptr*", &t)
	; VarSetCapacity(ti, 16, 0)  ; structure of privileges
	ti := Buffer(16, 0)  ; structure of privileges
	NumPut(1, ti, 0, "UInt")  ; one entry in the privileges array...
	; Retrieves the locally unique identifier of the privilege:
	DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeShutdownPrivilege"
	        , "Int64*", &luid)
	NumPut(luid, ti, 4, "Int64")
	NumPut(2, ti, 12, "UInt")  ; Enable this privilege: SE_PRIVILEGE_ENABLED = 2
	; Update the privileges of this process with the new access token:
	r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", ti
	             , "UInt", 0, "Ptr", 0, "Ptr", 0)
	DllCall("CloseHandle", "Ptr", t)  ; Close this access token handle to save memory.
	DllCall("CloseHandle", "Ptr", h)  ; Close this process handle to save memory.

	;PC standby (S3)
	DllCall("ntdll\ZwInitiatePowerAction", "Int", 2, "Int", 4, "Int", 0x80000000, "Int", 1)
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: StandBy rewriting for v2

10 Sep 2022, 08:23

"Ptr*", &t) and the likes should be "Ptr*", &t := 0)
also, read the docs for NumPut()

if u miss anything else, its probably all the DllCall return val error checking that no one's bothered to implement. u could be the first to do so!
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: StandBy rewriting for v2

10 Sep 2022, 09:24

@stealzy: When converting v1 to v2, you can use the tool QuickConvertorV2 It will probably not fix everything, but does already the most part.

If you discover a wrong conversion, please report it and explain the way of thinking behind it.

For DllCalls I also use AHK DllCall Terminator that helps with the correct types, unfortunately it is not yet available for V2 ;) .
Descolada
Posts: 1184
Joined: 23 Dec 2021, 02:30

Re: StandBy rewriting for v2

10 Sep 2022, 09:52

swagfag wrote:
10 Sep 2022, 08:23
if u miss anything else, its probably all the DllCall return val error checking that no one's bothered to implement. u could be the first to do so!
Nope, fixing those problems (passing an unset variable into Ptr*, and changing the order of NumPut arguments) works. As a reward my computer got suspended :P
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: StandBy rewriting for v2

10 Sep 2022, 14:02

@swagfag
Thanks, NumPut arguments order fixed.

@AHK_user
Converter is cool, I manually replaced after only `PtrP, var` to `Prt*, &var`, but still miss smth.
Descolada wrote:
10 Sep 2022, 09:52
fixing those problems (passing an unset variable into Ptr*, and changing the order of NumPut arguments) works.
Could you share, please? Still stuck here...

Code: Select all

StandBy() {
	;enable SeShutdownPrivilege token
	PID := ProcessExist()  ; Sets ErrorLevel to the PID of this running script.
	; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):
	h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", PID, "Ptr")
	; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):
	DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "Ptr*", &t:=0)
	ti := Buffer(16, 0)  ; structure of privileges ; V1toV2: if 'ti' is a UTF-16 string, use 'VarSetStrCapacity(&ti, 16)'
	NumPut("UInt", 1, ti, 0)  ; one entry in the privileges array...
	; Retrieves the locally unique identifier of the privilege:
	DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeShutdownPrivilege", "Int64*", &luid:=0)
	NumPut("Int64", luid, ti, 4)
	NumPut("UInt", 2, ti, 12)  ; Enable this privilege: SE_PRIVILEGE_ENABLED = 2
	; Update the privileges of this process with the new access token:
	r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
	DllCall("CloseHandle", "Ptr", t)  ; Close this access token handle to save memory.
	DllCall("CloseHandle", "Ptr", h)  ; Close this process handle to save memory.

	;PC standby (S3)
	DllCall("ntdll\ZwInitiatePowerAction", "Int", 2, "Int", 4, "Int", 0x80000000, "Int", 1)
}
Descolada
Posts: 1184
Joined: 23 Dec 2021, 02:30

Re: StandBy rewriting for v2  Topic is solved

10 Sep 2022, 14:31

@stealzy, the code you provided worked in my computer (of course after actually calling the function):

Code: Select all

StandBy()
StandBy() {
	;enable SeShutdownPrivilege token
	PID := ProcessExist()  ; Sets ErrorLevel to the PID of this running script.
	; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):
	h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", PID, "Ptr")
	; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):
	DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "Ptr*", &t:=0)
	ti := Buffer(16, 0)  ; structure of privileges ; V1toV2: if 'ti' is a UTF-16 string, use 'VarSetStrCapacity(&ti, 16)'
	NumPut("UInt", 1, ti, 0)  ; one entry in the privileges array...
	; Retrieves the locally unique identifier of the privilege:
	DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeShutdownPrivilege", "Int64*", &luid:=0)
	NumPut("Int64", luid, ti, 4)
	NumPut("UInt", 2, ti, 12)  ; Enable this privilege: SE_PRIVILEGE_ENABLED = 2
	; Update the privileges of this process with the new access token:
	r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
	DllCall("CloseHandle", "Ptr", t)  ; Close this access token handle to save memory.
	DllCall("CloseHandle", "Ptr", h)  ; Close this process handle to save memory.

	;PC standby (S3)
	DllCall("ntdll\ZwInitiatePowerAction", "Int", 2, "Int", 4, "Int", 0x80000000, "Int", 1)
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, vmech and 37 guests