Question about local, global variables: Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LazarouJoinery
Posts: 22
Joined: 02 Jan 2022, 21:41

Question about local, global variables:

23 Nov 2022, 01:47

Hi all;

I am using a CAD Library via DllCall().
I am loading a list of files, using a function.
I have to save to memory, each of these files, so I can access them through selection on a ListView.
Spoiler
Each file needs to be saved to memory of a specific size (using VarSetCapacity) and I need to be able to access that specific memory address later, (upon selection in the ListView) so I am storing that (unique?) memory address in v.ptr

Below is an (abridged) function to load the files.

Code: Select all

LoadFileList()
{
for k, v in oList
  {
    idx := A_Index
    szFilePath := v.FilePath
    hDrw := lcCreateDrawing()
    lcDrwLoad(hDrw,szFilePath,0)
    bytes := lcDrwSaveMem(hDrw,0,0)
    VarSetCapacity(Mem%idx%,bytes,0)
    lcDrwSaveMem(hDrw,&Mem%idx%,bytes)
    v.ptr := &Mem%idx%
  }
return
}
This is an (abridged) function, to load the files.

Code: Select all

;i: index of Listview, hence file
OnChange(i)
{
global oList, hLcWnd
mem := oList[i].ptr
hDrw := lcCreateDrawing()
lcDrwLoadMem(hDrw,mem,hLcWnd)
return
}
My challenge: I can make this work, if I use global variables for the dynamically created mem%idx%.
[ie. subroutines, not functions]. I'd like to clean up the code, so that I have far fewer global variables.
Problem is, I don't know to then correctly define, store, these dynamically created memory addresses mem%idx%

What I have tried:
! Assume-global mode:
> Works. (But creates individual global variables!)
! store the memory in the object oTSV.
> I don't believe this can be done. I don't believe VarSetCapacity() takes an object reference as a parameter?
! define the memory mem%idx%, dynamically, as a global, inside the function: like global mem%idx%
> this fails. "The following variable name contains an illegal character"

Is there another way I should be doing this?
  • a.) If these DXF files were represented as strings (not blobs of binary memory) I'd store them in the object oList, as text, and that would be my desired solution.
    That's a request I am making for the developer.
    However, what's the best way to store a bunch of memory addresses like this, in AHK?
  • b.) Interestingly, when I write the memory addresses in assume-global mode, my function onChange() can actually read the (global) memory addresses, as stored by oList.ptr
    I figure this is because the CAD library DLL, operates on the memory address (As I am passing pointers to memory addresses.)
    Obviously then, these memory addresses need to be... global? But is there a way to store them, more protected? A little more OOP? Maybe even... not showing up in my ListVars?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Question about local, global variables:  Topic is solved

23 Nov 2022, 03:11

use

Code: Select all

...
v.SetCapacity("ptr", bytes)
lcDrwSaveMem(hDrw, v.GetAddress("ptr"), bytes)
...
LazarouJoinery
Posts: 22
Joined: 02 Jan 2022, 21:41

Re: Question about local, global variables:

23 Nov 2022, 05:46

@@swagfag - wow! I've never seen that before. Thank you! I'll give it a go :-)

Edit: That's fantastic, will free up my code so much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 99 guests