GetClassNN()

Post your working scripts, libraries and tools
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

GetClassNN()

05 Dec 2019, 21:18

Here is a function that will determine the ClassNN for a control from its hWnd. Unlike another version I have posted, this version won't fail if it is interrupted, and then called again by the interrupting thread.

It sets ErrorLevel. The codes are:

0: Success
1: No window with the specified hWnd could be found.
2: The specified window is a top-level window.
3: The specified control's class name could not be retrieved.
4: The specified control's class atom could not be retrieved.
5: The control could not be found by EnumChildWindows().

Code: Select all

GetClassNN(hWnd) {
	static FindNN := RegisterCallback("[email protected]", "Fast", 2)
	If !DllCall("IsWindow", "Ptr", hWnd)
		return "", ErrorLevel := 1
	If (Main := DllCall("GetAncestor", "Ptr", hWnd, "Int", 2, "UPtr")) == hWnd
		return "", ErrorLevel := 2
	If not Atom := DllCall("GetClassWord", "Ptr", hWnd, "Int", -32, "UShort")
		return "", ErrorLevel := 4
	VarSetCapacity(Data, 2 * A_PtrSize + 2, 0), NumPut(Atom, Data, "UShort"), NumPut(hWnd, Data, 2)
	If DllCall("EnumChildWindows", "Ptr", Main, "Ptr", FindNN, "Str", Data)
		return "", ErrorLevel := 5
	VarSetCapacity(Class, 257 << (A_IsUnicode = true), 0)
	return DllCall("GetClassName", "Ptr", hWnd, "Str", Class, "Int", 257) ? (Class NumGet(Data, A_PtrSize + 2), ErrorLevel := 0) : ("", ErrorLevel := 3)
}

[email protected](Ctrl, DataAddress) {
	If DllCall("GetClassWord", "Ptr", Ctrl, "Int", -32, "UShort") == NumGet(DataAddress + 0, "UShort")
		NumPut(NumGet(DataAddress + 0, A_PtrSize + 2) + 1, DataAddress + 0, A_PtrSize + 2)
	return Ctrl <> NumGet(DataAddress + 0, 2)
}
guest3456
Posts: 3145
Joined: 09 Oct 2013, 10:31

Re: GetClassNN()

05 Dec 2019, 21:29

can you give some examples of when would we want a ClassNN rather than a hwnd?

DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: GetClassNN()

05 Dec 2019, 22:02

Nice I needed it yesterday!
Thank you!
1100++
Posts: 78
Joined: 10 Feb 2018, 19:05

Re: GetClassNN()

05 Dec 2019, 22:21

guest3456 wrote:
05 Dec 2019, 21:29
can you give some examples of when would we want a ClassNN rather than a hwnd?
The hWnd of a control varies with its host application instance, but the ClassNN is often (but not always) consistent between instances, making it suitable for working with applications whose control makeup is consistent. If one couldn't find a control with MouseGetPos, but could find it with the Windows API function GetGUIThreadInfo, this function could be used to get its ClassNN.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Spikea and 29 guests