Page 1 of 1

Using VarSetCapacity inside classes

Posted: 17 Oct 2021, 15:49
by mynameisjohn
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)?

Re: Using VarSetCapacity inside classes

Posted: 17 Oct 2021, 17:24
by swagfag
viewtopic.php?f=76&t=84784&hilit=globalalloc
or assign to static variables and check them every time
or use v2

Re: Using VarSetCapacity inside classes

Posted: 18 Oct 2021, 10:27
by mynameisjohn
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