Question about StrGet() Topic is solved

Discuss the future of the AutoHotkey language
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Question about StrGet()

09 Feb 2016, 12:17

The code below behaves differently under v1.1 and v2. In v2 the last "|" doesn't show up.

Code: Select all

nSize := VarSetCapacity(lpFilename, 512, 0)
DllCall("GetModuleFileName", "Ptr", 0, "Ptr", &lpFilename, "UInt", nSize)
MsgBox % "|" StrGet(&lpFilename) "|`n|" StrGet(&lpFilename, 256) "|"
I am wondering if I missed something. Any insights would be appreciated.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Question about StrGet()

09 Feb 2016, 16:17

In v2, the string can and does contain null characters. MsgBox only shows up to the first null character.

You can pass the return value of GetModuleFileName to StrGet instead of the buffer size.
If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters, not including the terminating null character.
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: Question about StrGet()

09 Feb 2016, 17:47

Ok. Thank you. This seems inconsistent with the current documentation for StrGet():
Length
The maximum number of characters to read/write, including the null-terminator if required. See Return Value.
As implemented, it seems that Length represents the actual number of characters. That confused me.

Code: Select all

nSize := VarSetCapacity(lpFilename, 512, 0)
nSize := DllCall("GetModuleFileName", "Ptr", 0, "Ptr", &lpFilename, "UInt", nSize)
String1 := StrGet(&lpFilename, nSize)   ; C:\Program Files\AutoHotkey\AutoHotkey.exe
String2 := StrGet(&lpFilename, 256)
MsgBox % StrLen(String1) " vs " StrLen(String2)   ; 42 vs 256
Making Length the maximum number of characters would make StrGet() compatible with v1.1 and save people some work.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Question about StrGet()

09 Feb 2016, 21:57

"Full documentation is very much outdated, with the exception of v2-changes"
http://lexikos.github.io/v2/
https://autohotkey.com/v2/

The current behaviour is a direct result of adding support for binary zero in strings and wasn't originally intended.
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: Question about StrGet()

10 Feb 2016, 00:08

Got it. Thank you. I figured there was a reason. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Question about StrGet()  Topic is solved

03 Jun 2016, 23:11

v2.0-a075-02fab78

Changed StrGet to truncate the string at the first binary zero (null terminator), as in v1. Specifying a negative Length now causes it to extract exactly Abs(Length) characters (as previous builds did for positive values), utilizing v2's support for binary zero in strings.
iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: Question about StrGet()

21 Jan 2017, 05:43

Thank you! I appreciate the change.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 46 guests