Using VarSetCapacity inside classes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mynameisjohn
Posts: 32
Joined: 27 Mar 2018, 19:38

Using VarSetCapacity inside classes

17 Oct 2021, 15:49

The objective is to avoid running VarSetCapacity everytime CursorInfo.Get() is called when triggering CTRL+LButton, like in the code below:

Code: Select all

class CursorInfo {
   static size := A_PtrSize + 16

   Get() {
      VarSetCapacity(tagCURSORINFO, this.size, 0)
      NumPut(this.size, tagCURSORINFO, 0, "UInt")
      DllCall("GetCursorInfo", "Ptr", &tagCURSORINFO)
      return NumGet(tagCURSORINFO, 4, "UInt")
   }
}

^LButton::
msgbox, % CursorInfo.Get()
return
I know I can just set everything global and call it a day:

Code: Select all

global size := A_PtrSize + 16
global tagCURSORINFO := VarSetCapacity(tagCURSORINFO, size, 0)

class CursorInfo {
   Get() {
      NumPut(size, tagCURSORINFO, 0, "UInt")
      DllCall("GetCursorInfo", "Ptr", &tagCURSORINFO)
      return NumGet(tagCURSORINFO, 4, "UInt")
   }
}

^LButton::
msgbox, % CursorInfo.Get()
return
But how do I run VarSetCapacity inside the class to keep everything local (if its possible)?
Last edited by mynameisjohn on 18 Oct 2021, 10:26, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Using VarSetCapacity inside classes

17 Oct 2021, 17:24

viewtopic.php?f=76&t=84784&hilit=globalalloc
or assign to static variables and check them every time
or use v2
User avatar
mynameisjohn
Posts: 32
Joined: 27 Mar 2018, 19:38

Re: Using VarSetCapacity inside classes

18 Oct 2021, 10:27

I found it's best to make a mix of the two.

Code: Select all

global tagCURSORINFO := VarSetCapacity(tagCURSORINFO, CursorInfo.size, 0)

class CursorInfo {
   static size := A_PtrSize + 16

   Get() {
      NumPut(this.size, tagCURSORINFO, 0, "UInt")
      DllCall("GetCursorInfo", "Ptr", &tagCURSORINFO)
      return NumGet(tagCURSORINFO, 4, "UInt") ; flags
   }
}

^LButton::
msgbox, % CursorInfo.Get()
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Rohwedder and 376 guests