StrPtr

Returns the current memory address of a string.

Address := StrPtr(Value)

Parameters

Value

Type: String, VarRef or StringBuffer

In a direct call, a variable, or an assignment to one such as StrPtr(MyVar := "blue"), is passed as the variable itself, as is &MyVar, which an indirect call such as StrPtr.Call(&MyVar) needs. A built-in variable is passed as its value, also when an #Import names it. Otherwise Value is a string, such as a literal, the result of an expression or a property's value.

Return Value

Type: Integer

The address of the variable's own memory, of a StringBuffer's memory (its Ptr), or of a pinned copy of any other string.

Error Handling

A TypeError is thrown if Value is a number or an object other than a StringBuffer or a reference, or if the variable or reference passed as Value holds such a value.

Remarks

Ptr := StrPtr(MyVar)

A variable keeps memory of its own for native code: StrPtr returns its address, and a Str argument passes it. The first time a call or StrPtr needs the memory, it has room for 259 characters plus the null terminator (MAX_PATH), or for the variable's value if that is longer, unless VarSetStrCapacity sized it. The memory grows, and moves to a new address, only when a longer value must fit or VarSetStrCapacity asks for more room, so the address stays the same while the variable exists and its value fits.

StrPtr and a Str argument copy the variable's value into the memory only when the variable holds a different string than the one last copied in or taken out. What native code wrote there therefore survives another StrPtr(MyVar), and is what the next Str argument passes. Assigning the variable the very string it already holds does not count as assigning it. The most common case is MyVar := "" on a variable which is already empty, such as right after VarSetStrCapacity, and reassigning a literal the variable already holds counts the same way. In both cases the memory keeps what native code wrote, where AutoHotkey would overwrite it, so to discard such a write, assign a different value or call VarSetStrCapacity(&MyVar, 0).

What native code writes there, through DllCall, NumPut, StrPut or anything else, reaches the variable, up to the first null character, at VarSetStrCapacity(&MyVar, -1) or when the variable is next passed as a Str or WStr argument, unless the variable was assigned a different string in the meantime. AutoHotkey shows a write which keeps the length at once, such as the one DllCall("CharUpper", "Ptr", StrPtr(MyVar)) makes. Keysharp shows it only then, so pass the variable as Str instead, or call VarSetStrCapacity(&MyVar, -1) afterwards. To have a function write a longer string, give the variable room first with VarSetStrCapacity.

Ptr := StrPtr("literal string")

A string which has no memory of its own is copied to pinned memory. That is the case for a literal, the result of an expression, a property's value or the value of any other object with a __Value property. What native code writes to the copy reaches nothing. The copy stays at the same address as long as the string exists, so a literal's address never changes, and a temporary such as the result of an expression lasts at least until the statement using its address finishes, as in AutoHotkey.

StringBuffer, DllCall, SendMessage, Buffer object, NumPut, NumGet