Script crash Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Script crash

23 Jan 2019, 07:51

afe wrote:
23 Jan 2019, 07:43
I still can't understand why strLength := StrLen(str) must be multiplied by 2.
If ur AHK version is Unicode, all strings r stored in 2 bytes per character (widechar) format
U store a binary data as a string and use StrLen to count number of 2-byte tokens in it:
strLength := StrLen(str) * 2
it might fail if ur binary data has a sequence of 2 or more zero bytes. AHK doesnt have a function to count binary array's length.
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script crash

23 Jan 2019, 07:57

AHK doesnt have a function to count binary array's length.
Please tell me, what does that mean?
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Script crash

23 Jan 2019, 08:04

Helgef wrote:
23 Jan 2019, 07:57
AHK doesnt have a function to count binary array's length.
Please tell me, what does that mean?
AHK unlike Python and some other languages, doesnt have a "binary array" data type. there is no way to count and store size of an arbitrary binary buffer that we declared earlier.
StrLen/StrPut try to work with binary array as if its a zero terminated string so it might prematurely end up. Strlen works for some binary data and fails for others. We use VarSetCapacity to set binary array size but its up to us to store its size to re-use it later.

In the OP script StrLen is prone to fail on some data.
Last edited by vvhitevvizard on 23 Jan 2019, 08:15, edited 3 times in total.
afe
Posts: 616
Joined: 06 Dec 2018, 04:36

Re: Script crash

23 Jan 2019, 08:05

Helgef wrote:
23 Jan 2019, 07:48
the input length is in bytes, if you run your script on unicode build, it is 2 bytes per character.
Thank you very much, I see. pszString is the number of characters.

Code: Select all

BOOL CryptBinaryToStringW(
  const BYTE *pbBinary,
  DWORD      cbBinary,
  DWORD      dwFlags,
  LPWSTR     pszString,
  DWORD      *pcchString
);
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script crash

23 Jan 2019, 08:14

We use VarSetCapacity to set binary array size but its up to us to store its size to re-use it later.
No, you call varsetcapacity(var), as documented. The length of the buffer is stored internally.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Script crash

23 Jan 2019, 08:19

Helgef wrote:
23 Jan 2019, 08:14
We use VarSetCapacity to set binary array size but its up to us to store its size to re-use it later.
No, you call varsetcapacity(var), as documented. The length of the buffer is stored internally.
U r right here. its stored. It couldn't work other way. :) But the point is I see many (including OP) try to emulate a binary array counting by resorting to Strlen.

PS: Fun stuff is we lack some string length math function to directly cut/extend string w/o copy/concatenation (as if its an array of tokens) yet we have such functionality for binary fields.
Last edited by vvhitevvizard on 23 Jan 2019, 09:05, edited 2 times in total.
User avatar
vvhitevvizard
Posts: 405
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: Script crash

23 Jan 2019, 08:42

After some thinking, StrLen doesnt calculate string size on the spot but just retrieves its field size which was previously saved by AHK internally. So it works in case it was a real string or was properly initiated with VarSetCapacity for binary fields by the caller. But once we skip ByRef keyword for this argument, AHK will try to copy that "string" field to a function's local var with the same name (shadowed) and prematurely cut it at first zero token. Correct me if Im wrong.

In the OP example its a string not a real binary data to be converted so everything works as expected.
Helgef wrote:
23 Jan 2019, 08:14
No, you call varsetcapacity(var), as documented. The length of the buffer is stored internally.
Btw main question is how to retrieve it back.
ofc, a correct way for binary fields would be to use its counterpart VarGetCapacity but... hey there is no such a function for regular vars. Its analogue exists for object's fields only... object.GetCapacity("field_name") :D
Helgef
Posts: 4479
Joined: 17 Jul 2016, 01:02
Contact:

Re: Script crash

23 Jan 2019, 09:31

Sorry I'm lost, you should probably start a new thread if you have any questions. This seems off topic

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], hotkeyuser1234, OPK, viv, WatsonEnterprises and 65 guests