i get error 6 the handle is invalid autohotkey Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

i get error 6 the handle is invalid autohotkey

Post by abdohhh » 08 Jun 2023, 21:47

Hi everyone, i got this error when i lock the pc or run any thing as adminstrator but the script work fine but i don't know why i got this error when those situations like i told happen
the script is :

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	IF !WinExist(Win1 := "ahk_exe onexcui.exe")
	Or !WinExist(Win2 := "YouTube - Google Chrome")
		Exit
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}
}
the error :
image.png
image.png (13.02 KiB) Viewed 1877 times

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 08 Jun 2023, 22:17

When you lock the PC, then the values received from the WinGetPos function are probably null since the window isn’t actually being displayed, so then you’re passing null values as the search rectangle to the PixelSearch function.

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 08 Jun 2023, 22:30

boiler wrote:
08 Jun 2023, 22:17
When you lock the PC, then the values received from the WinGetPos function are probably null since the window isn’t actually being displayed, so then you’re passing null values as the search rectangle to the PixelSearch function.
so is there any way to fix this problem ?

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 09 Jun 2023, 03:38

You could put another If statement after the WinGetPos and then Return or Exit if the coordinate variables are empty. You probably only have to check one because if one is empty, they probably all are empty.

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 11 Jun 2023, 03:44

boiler wrote:
09 Jun 2023, 03:38
You could put another If statement after the WinGetPos and then Return or Exit if the coordinate variables are empty. You probably only have to check one because if one is empty, they probably all are empty.
would you edit the script for me with those edits like you told and rewrite because i don't have enough knowledge in autohotkey ?

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 11 Jun 2023, 05:22

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	if !WinExist(Win1 := "ahk_exe onexcui.exe")
	or !WinExist(Win2 := "YouTube - Google Chrome")
		return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	if (X1 = "")
		return
	CoordMode "Pixel", "Screen"
	if PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}

The code you posted had an extra closing brace for some reason.

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 11 Jun 2023, 10:18

boiler wrote:
11 Jun 2023, 05:22

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	if !WinExist(Win1 := "ahk_exe onexcui.exe")
	or !WinExist(Win2 := "YouTube - Google Chrome")
		return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	if (X1 = "")
		return
	CoordMode "Pixel", "Screen"
	if PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}

The code you posted had an extra closing brace for some reason.
it stil gives same error
image.png
image.png (11.79 KiB) Viewed 1730 times

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 11 Jun 2023, 10:29

Run this version and see what is in the WindowValues.ini file after it produces the error. They must not be blank values but maybe something else that can be used to determine when this condition occurs.

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	if !WinExist(Win1 := "ahk_exe onexcui.exe")
	or !WinExist(Win2 := "YouTube - Google Chrome")
		return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	IniWrite X1, "WindowValues.ini", "Main", "X1"
	IniWrite Y1, "WindowValues.ini", "Main", "Y1"
	IniWrite W1, "WindowValues.ini", "Main", "W1"
	IniWrite H1, "WindowValues.ini", "Main", "H1"
	CoordMode "Pixel", "Screen"
	if PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 11 Jun 2023, 13:05

boiler wrote:
11 Jun 2023, 10:29
Run this version and see what is in the WindowValues.ini file after it produces the error. They must not be blank values but maybe something else that can be used to determine when this condition occurs.

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	if !WinExist(Win1 := "ahk_exe onexcui.exe")
	or !WinExist(Win2 := "YouTube - Google Chrome")
		return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	IniWrite X1, "WindowValues.ini", "Main", "X1"
	IniWrite Y1, "WindowValues.ini", "Main", "Y1"
	IniWrite W1, "WindowValues.ini", "Main", "W1"
	IniWrite H1, "WindowValues.ini", "Main", "H1"
	CoordMode "Pixel", "Screen"
	if PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}
still got this error when i run any thing as adminstrator or lock pc

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 11 Jun 2023, 13:52

Read my post again. I said see what the last values that were written to the .ini file are after it produces the error. It was expected to produce the error, and this identifies values to help determine how to change the actual working code. Please read my posts or it’s not worth the time to try to step you through how to troubleshoot this.

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 09 Jul 2023, 11:37

boiler wrote:
11 Jun 2023, 13:52
Read my post again. I said see what the last values that were written to the .ini file are after it produces the error. It was expected to produce the error, and this identifies values to help determine how to change the actual working code. Please read my posts or it’s not worth the time to try to step you through how to troubleshoot this.
Oky these are the values : [Main]
X1=190
Y1=656
W1=482
H1=299
i changed it rarther than windows value.ini in script but id dosen't work so where i should to change it ?

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 09 Jul 2023, 13:32

It must just be that it's going to produce that error when the desktop is locked, so try this version:

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	IF !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1) ; desktop is locked
		return
	IF !WinExist(Win1 := "ahk_exe onexcui.exe")
	Or !WinExist(Win2 := "YouTube - Google Chrome")
		Exit
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 09 Jul 2023, 14:02

boiler wrote:
09 Jul 2023, 13:32
It must just be that it's going to produce that error when the desktop is locked, so try this version:

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	IF !DllCall("User32\OpenInputDesktop","int",0*0,"int",0*0,"int",0x0001L*1) ; desktop is locked
		return
	IF !WinExist(Win1 := "ahk_exe onexcui.exe")
	Or !WinExist(Win2 := "YouTube - Google Chrome")
		Exit
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}
i am really sorry i put alot of hard work on you but thank you for your help, but it gives me this error when i open the script
image.png
image.png (14.71 KiB) Viewed 1554 times

User avatar
boiler
Posts: 17042
Joined: 21 Dec 2014, 02:44

Re: i get error 6 the handle is invalid autohotkey

Post by boiler » 09 Jul 2023, 15:13

Oh, sorry. I posted v2 code fromhere, which is v1 code, so it needs to be updated for v2.

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 09 Jul 2023, 15:45

boiler wrote:
09 Jul 2023, 15:13
Oh, sorry. I posted v2 code fromhere, which is v1 code, so it needs to be updated for v2.
can you update it to me please ?

lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: i get error 6 the handle is invalid autohotkey

Post by lexikos » 09 Jul 2023, 21:59

The problem isn't that it is v1 code, but that it is bad code. ;)

0x0001L*1 would ordinarily produce an empty string on v1, because 0x0001L is not purely numeric and therefore is treated as a variable name, the variable is not defined, and multiplying an empty string by anything will produce an empty string.

For DllCall, it would be the same as passing 0. If tank's code ever worked verbatim, you could just replace 0x0001L*1 with 0.

The constant 0x0001L would probably have been copied from the Windows SDK, a C/C++ header file, or similar. In C, the L suffix denotes a "long integer" (32-bit on Windows). So you could replace 0x0001L*1 with 1.

Multiplying 0 by 0 is obviously unnecessary, so *0 can be removed.

Edit: You should probably also close the handle.
If the function succeeds, the return value is a handle to the desktop that receives user input. When you are finished using the handle, call the CloseDesktop function to close it.

Code: Select all

CanOpenInputDesktop() {
	if !h := DllCall("User32\OpenInputDesktop","int",0,"int",0,"int",1,"ptr")
		return false
	DllCall("User32\CloseDesktop","ptr",h)
	return true
}
Use it like

Code: Select all

if !CanOpenInputDesktop()
    return

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 10 Jul 2023, 10:18

lexikos wrote:
09 Jul 2023, 21:59
The problem isn't that it is v1 code, but that it is bad code. ;)

0x0001L*1 would ordinarily produce an empty string on v1, because 0x0001L is not purely numeric and therefore is treated as a variable name, the variable is not defined, and multiplying an empty string by anything will produce an empty string.

For DllCall, it would be the same as passing 0. If tank's code ever worked verbatim, you could just replace 0x0001L*1 with 0.

The constant 0x0001L would probably have been copied from the Windows SDK, a C/C++ header file, or similar. In C, the L suffix denotes a "long integer" (32-bit on Windows). So you could replace 0x0001L*1 with 1.

Multiplying 0 by 0 is obviously unnecessary, so *0 can be removed.

Edit: You should probably also close the handle.
If the function succeeds, the return value is a handle to the desktop that receives user input. When you are finished using the handle, call the CloseDesktop function to close it.

Code: Select all

CanOpenInputDesktop() {
	if !h := DllCall("User32\OpenInputDesktop","int",0,"int",0,"int",1,"ptr")
		return false
	DllCall("User32\CloseDesktop","ptr",h)
	return true
}
Use it like

Code: Select all

if !CanOpenInputDesktop()
    return
it still gives me the erorr the code is :

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	CanOpenInputDesktop() {
	if !h := DllCall("User32\OpenInputDesktop","int",0,"int",0,"int",1,"ptr")
		return false
	DllCall("User32\CloseDesktop","ptr",h)
	return true
}
	IF !WinExist(Win1 := "ahk_exe onexcui.exe")
	Or !WinExist(Win2 := "OnStar - Google Chrome")
		Exit
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}

just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: i get error 6 the handle is invalid autohotkey

Post by just me » 11 Jul 2023, 03:40

Your code does not call the function, it just declares it:

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	If	!CanOpenInputDesktop() Or
		!WinExist(Win1 := "ahk_exe onexcui.exe") Or
		!WinExist(Win2 := "OnStar - Google Chrome")
	Return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}
CanOpenInputDesktop() {
	if !h := DllCall("User32\OpenInputDesktop","int",0,"int",0,"int",1,"ptr")
		return false
	DllCall("User32\CloseDesktop","ptr",h)
	return true
}
?

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 13 Jul 2023, 17:39

just me wrote:
11 Jul 2023, 03:40
Your code does not call the function, it just declares it:

Code: Select all

SetTimer(MaxWin2, 100)
MaxWin2() {
	If	!CanOpenInputDesktop() Or
		!WinExist(Win1 := "ahk_exe onexcui.exe") Or
		!WinExist(Win2 := "OnStar - Google Chrome")
	Return
	WinGetPos(&X1, &Y1, &W1, &H1, Win1)
	CoordMode "Pixel", "Screen"
	IF PixelSearch(&X2, &Y2, X1, Y1, X1+W1, Y1+H1, 0xBED8EC)
		WinMaximize Win2
}
CanOpenInputDesktop() {
	if !h := DllCall("User32\OpenInputDesktop","int",0,"int",0,"int",1,"ptr")
		return false
	DllCall("User32\CloseDesktop","ptr",h)
	return true
}
?
So how can i fix this problem it stell get me same erorr

abdohhh
Posts: 113
Joined: 25 Apr 2023, 05:50

Re: i get error 6 the handle is invalid autohotkey

Post by abdohhh » 02 Aug 2023, 21:32

can any one help me ?

Post Reply

Return to “Ask for Help (v2)”