Is it possible to edit AutoHotkey array in memory? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Is it possible to edit AutoHotkey array in memory?

30 Apr 2017, 17:49

I have been working on a StringBuilder similar to .net StringBuilder class for Mini-Framework.

I am wondering if it is possible to edit an AutoHotkey array of byte values directly in memory.
for instance in C# to edit text as a char array directly in memory it would be something like this.

Code: Select all

fixed (char* ptr = &value[startIndex])
{
	this.Append(ptr, charCount);
}
Where value is a char[] array. and startIndex is an index within the array.
ptr becomes the memory address to access the chars in memory.
For instanceptr + 1 would be then next char from startIndex

I know I can copy bytes in and out of memory using NumGet and NumPut but am wondering if there is even a more efficient way for an array.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

30 Apr 2017, 18:51

StrGet/StrPut?

EDIT:
Try this.

Code: Select all

Class string {
  __New(size){
    this.SetCapacity("",size)
    DllCall("RtlFillMemory","PTR",this.GetAddress(""),"PTR",size,"UCHAR",0)
  }
  Get(offset:=0,count:=0){
    if count
      return StrGet(this.GetAddress("")+offset*(A_IsUnicode?2:1),count)
    else return StrGet(this.GetAddress("")+offset*(A_IsUnicode?2:1))
  }
  Ptr(offset:=0){
    return this.GetAddress("")+offset*(A_IsUnicode?2:1)
  }
  Append(string,offset:=0,encoding:=""){
    return StrPut(string, this.GetAddress("")+offset*(A_IsUnicode?2:1),encoding?encoding:A_IsUnicode?"UTF-16":"CP0")
  }
}
s:=new string(100)
s.Append("text")
s.Append("TEXT",4)
MsgBox % s.Get() "`n" s.Ptr() "`n" s.Get(4,2)
Another approach:

Code: Select all

Class string {
  __New(size){
    this.SetCapacity("",size)
    DllCall("RtlFillMemory","PTR",this.GetAddress(""),"PTR",size,"UCHAR",0)
  }
  __Get(offset:=0,count:=0){
    if count
      return StrGet(this.GetAddress("")+offset*(A_IsUnicode?2:1),count)
    else return StrGet(this.GetAddress("")+offset*(A_IsUnicode?2:1))
  }
  Ptr(offset:=0){
    return this.GetAddress("")+offset*(A_IsUnicode?2:1)
  }
  __Set(offset:=0,string:=""){
    return StrPut(string, this.GetAddress("")+offset*(A_IsUnicode?2:1),A_IsUnicode?"UTF-16":"CP0")
  }
}
s:=new string(100)
s[0]:="text"
s[4]:="TEXT"
s[10]:="more"
MsgBox % s.0 "`n" s.Ptr() "`n" s[4,2] "`n" s[10]
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is it possible to edit AutoHotkey array in memory?

30 Apr 2017, 19:40

So can StrGet and StrPut, get and set (move) a specific number of bytes unchanged? (E.g. AHK Unicode gets and sets in UTF-16, and AHK ANSI get and sets in ANSI, would that work?) Can AHK Unicode handle an odd number of bytes/odd starting offset for data?

[I was asking about this here:]
binary data: copy ('move'), compare, search, replace nulls - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30869

Thanks.

Otherwise I suppose RtlMoveMemory or memmove could move the data.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: Is it possible to edit AutoHotkey array in memory?

30 Apr 2017, 22:33

I have already done much of the work on My StringBullder Class. I opted to use a MfMemoryString class I constructed.
the MfMemoryString class uses a lot of numput, numGet, strget, strput and DllCall RtlMoveMemory

I was wondering about accessing the contents of byte array directly in memory but I have thing working well without that feature so far.

the finished code will be on GitHub and requires Mini-Framework
Currently you can find this code in the topic-uLong Branch but that branch will get merged into the master.

here is a pastebin link to my current version of MfMemoryString
here is a pastebin link to my current version of MfStringBuilder

The Classes were too long to post on the forum.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
Helgef
Posts: 4462
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 03:36

@ HotKeyIt, is the memory allocated by ObjSetCapacity guaranteed to be zero-filled?
Cheers.
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 09:50

Helgef wrote:@ HotKeyIt, is the memory allocated by ObjSetCapacity guaranteed to be zero-filled?
Cheers.
For the MfStringBuilder class yes Zero filled and set by ObjSetCapacity , this is done by the default by the MfMemoryString Class.
The MfMemoryString Class However is intended to be an internal class and has some other options that can be set then just the default of ObjSetCapacityand zero fill.

The MfStringBuilder class uses one or more instances of MfMemoryString class internally. When the MfStringBuilder internal buffer becomes full it adds another instance of MfMemoryString class for the next chunk of text.
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 09:58

jeeswg wrote:So can StrGet and StrPut, get and set (move) a specific number of bytes unchanged? (E.g. AHK Unicode gets and sets in UTF-16, and AHK ANSI get and sets in ANSI, would that work?) Can AHK Unicode handle an odd number of bytes/odd starting offset for data?

[I was asking about this here:]
binary data: copy ('move'), compare, search, replace nulls - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30869

Thanks.

Otherwise I suppose RtlMoveMemory or memmove could move the data.
StrGet/StrPut is only for strings, not bytes!
Also StrPut will apply ending null character, you would need RtlMoveMemory otherwise.
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 10:01

Helgef wrote:@ HotKeyIt, is the memory allocated by ObjSetCapacity guaranteed to be zero-filled?
Cheers.
AFAIK, yes. See https://autohotkey.com/docs/objects/Obj ... etCapacity
Helgef
Posts: 4462
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 10:29

Thanks for the response HotKeyIt.
As far as I understand the documentation, it doesn't suggest any guarantee of zero-filled memory. :think:
HotKeyIt
Posts: 2144
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 10:41

Some test show it does not so we need DllCall("RtlFillMemory","PTR",this.GetAddress(""),"PTR",size,"UCHAR",0).
I have added above.
Helgef
Posts: 4462
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is it possible to edit AutoHotkey array in memory?  Topic is solved

01 May 2017, 11:25

@HotKeyIt, ok, good to know.
AmourSpirit wrote:
Helgef wrote:@ HotKeyIt, is the memory allocated by ObjSetCapacity guaranteed to be zero-filled?
Cheers.
For the MfStringBuilder class yes Zero filled and set by ObjSetCapacity , this is done by the default by the MfMemoryString Class.
The MfMemoryString Class However is intended to be an internal class and has some other options that can be set then just the default of ObjSetCapacityand zero fill.

The MfStringBuilder class uses one or more instances of MfMemoryString class internally. When the MfStringBuilder internal buffer becomes full it adds another instance of MfMemoryString class for the next chunk of text.
That is a very extensive piece of code AmourSpirit, there is nothing mini about it :thumbup:
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 12:12

HotKeyIt wrote:Some test show it does not so we need DllCall("RtlFillMemory","PTR",this.GetAddress(""),"PTR",size,"UCHAR",0).
I have added above.
I am not clear on this part!
Should we call RtlFillMemory to zero fill after using ObjSetCapacity or not?
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut
Helgef
Posts: 4462
Joined: 17 Jul 2016, 01:02
Contact:

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 12:23

If you want to be sure the memory is zero filled, yes.
AmourSpirit
Posts: 28
Joined: 01 Mar 2016, 11:35

Re: Is it possible to edit AutoHotkey array in memory?

01 May 2017, 12:58

Helgef wrote:If you want to be sure the memory is zero filled, yes.
Thanks :wave:
Recent AutoHotkey projects.
Mini-Framework
AutoHotkey Snippit
xsdOut

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, mikeyww, robertmiller and 49 guests