[2.0.4] Bug related to StrPut() and Buffer objects Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

[2.0.4] Bug related to StrPut() and Buffer objects

23 Jul 2023, 05:12

The following code:

Code: Select all

Test := "12345678901234567890123456789012345678901234567890"
DevName := Buffer(64, 0)
StrPut(Test, DevName, 3)
MsgBox("Done!")
throws the following error message:

Code: Select all

Error: Parameter #2 of StrPut is invalid.

Specifically: Buffer

	001: Test := "12345678901234567890123456789012345678901234567890"
	002: DevName := Buffer(64, 0)
▶	003: StrPut(Test, DevName, 3)
	004: MsgBox("Done!")
	005: Exit

Show call stack »
The following code:

Code: Select all

Test := "1234567890"
DevName := Buffer(64, 0)
StrPut(Test, DevName, 8)
MsgBox("Done!")
throws the following error message:

Code: Select all

Error: Invalid Length.

	001: Test := "1234567890"
	002: DevName := Buffer(64, 0)
▶	003: StrPut(Test, DevName, 8)
	004: MsgBox("Done!")
	005: Exit

Show call stack »
User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: [2.0.4] Bug related to StrPut() and Buffer objects

23 Jul 2023, 07:44

If the 3-parameter mode of StrPut is used, the third parameter must be Encoding.

You can use one of the following:

Code: Select all

StrPut(Test, DevName, "UTF-16") ; 3 parameters
StrPut(Test, DevName, StrLen(Test), "UTF-16") ; 4 parameters
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [2.0.4] Bug related to StrPut() and Buffer objects

23 Jul 2023, 16:04

Moin @Ragnar,

even if the docs would require Encoding as the 4th parameter if Length is specified it absolutely makes no sense.
If unset or omitted, the string is simply copied or measured without any conversion taking place.
User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: [2.0.4] Bug related to StrPut() and Buffer objects

23 Jul 2023, 17:29

The StrPut/StrGet docs were not 100% correct. I have revised them slightly, maybe it is a bit more understandable now.
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [2.0.4] Bug related to StrPut() and Buffer objects

24 Jul 2023, 03:46

Hi @Ragnar, did you test your suggestion?
After adding "UTF-16" as the fourth parameter to my code I'm still getting the same errors.
User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: [2.0.4] Bug related to StrPut() and Buffer objects

24 Jul 2023, 04:49

With the second example, it should work. You also need to make sure the buffer has enough capacity. UTF-16 needs StrLen*2. In the second example it works with a value of 64 because the string's length is less than that (10*2), but in the first example it must be at least 100 (50*2). I chose UTF-16 because AutoHotkey v2 uses UTF-16 natively and I used StrGet in my test script:

Code: Select all

#Requires AutoHotkey v2
Test := "1234567890"
; Test := "12345678901234567890123456789012345678901234567890"
DevName := Buffer(StrLen(Test)*2, 0)
StrPut(Test, DevName, "UTF-16")
; StrPut(Test, DevName, StrLen(Test), "UTF-16")
MsgBox StrGet(DevName)
Of course, you can also use e.g. UTF-8, in which case the double length is not needed, but StrGet has to be adjusted accordingly.

Code: Select all

#Requires AutoHotkey v2
Test := "1234567890"
; Test := "12345678901234567890123456789012345678901234567890"
DevName := Buffer(StrLen(Test), 0)
StrPut(Test, DevName, "UTF-8")
; StrPut(Test, DevName, StrLen(Test), "UTF-8")
MsgBox StrGet(DevName, "UTF-8")
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: [2.0.4] Bug related to StrPut() and Buffer objects  Topic is solved

24 Jul 2023, 05:17

Parameter #2 - Buffer(64, 0) - is too small for a UTF-16 string of 50 characters, hence the error.

It is not too small for a string of 10 characters, therefore parameter #2 is valid, and execution proceeds to validation of the Length parameter.

The 2.0.4 version of the documentation says:
If Length is zero or less than the projected length after conversion (or the length of the source string if conversion is not required), an exception is thrown.
Hence the error.

I suppose neither 3 nor 8 make sense as Length, and you probably meant for those to be codepage identifiers. "Invalid Length" is telling you that you have specified an invalid Length, not Encoding.
For numeric identifiers, the prefix "CP" can be omitted only if Length is specified.
It means literally what it says. Providing an upper bound via the Buffer is not sufficient.

If numeric, the third parameter is Length.

So, what was the bug exactly? You didn't explain what the parameters mean or what result you expect.
Ragnar wrote: If the 3-parameter mode of StrPut is used, the third parameter must be Encoding.
That is not true.
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [2.0.4] Bug related to StrPut() and Buffer objects

24 Jul 2023, 07:39

lexikos wrote: The 2.0.4 version of the documentation says:
If Length is zero or less than the projected length after conversion (or the length of the source string if conversion is not required), an exception is thrown.
I missed that.
lexikos wrote: I suppose neither 3 nor 8 make sense as Length, and you probably meant for those to be codepage identifiers.
No, I tried to specify a maximum length for StrPut().

What's the intended use of Length if it cannot be less than the length of the source string? Would you please provide an example?
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: [2.0.4] Bug related to StrPut() and Buffer objects

24 Jul 2023, 19:41

Length is intended for use with an address. It serves the same purpose as Buffer.Size, though in characters, not bytes.
Note: When Encoding is specified, Length should be the size of the buffer (in characters), not the length of String or a substring, as conversion may increase its length.
(Even when Encoding is not specified, Length should be the size of the buffer (in characters).)
just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [2.0.4] Bug related to StrPut() and Buffer objects

25 Jul 2023, 05:39

Well, I think I got it (though I don't like it). It's no bug, so this post can be closed or moved.

Thanks for the explanations.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: emp00, Google [Bot], songdg and 78 guests