ObjCreateWithDataPtr [v2.1-alpha.36+]

Creates an object with an internal data pointer.

Obj := ObjCreateWithDataPtr(ClassObj, Address)

Parameters

ClassObj

Type: Object

A Class object which supports direct construction.

Address

Type: Integer

A number in the native integer range (32-bit or 64-bit). For a nonempty struct, specify zero or a valid address; values from 1 through 65535 are rejected. For other classes, any integer is permitted.

Return Value

Type: Object or Struct

Returns an instance based on the class's original prototype. Replacing ClassObj.Prototype does not change the prototype used by this function.

Error Handling

An exception is thrown if ClassObj is unsupported.

Passing an invalid non-zero address may cause program instability if ClassObj defines any typed properties.

Remarks

This function can be used for:

This function should not be used merely to bypass an overridden static Call method. Within the method, call super() or super.Call() to construct an instance. An overridden method can also be bypassed by explicitly invoking its superclass version, as in (Object.Call)(ClassObj).

Unlike ClassObj.Call, this function does not call __Init or __New. Instance variable initializers are not executed unless the script calls __Init manually.

Unlike StructClass.At, __Delete is called when the object is deleted. The runtime does not free the external memory; release it in __Delete if the object owns it. A Buffer or another object which owns the memory must be kept alive separately.

Typed properties can be declared only in a struct class. AutoHotkey also permits typed properties on ordinary classes.

The object's data pointer can be retrieved with ObjGetDataPtr and set with ObjSetDataPtr. If ClassObj is a struct class, it can also be retrieved with Obj.Ptr.

The object's Base cannot be changed. Its Clone method raises TypeError, even when the address is zero.

Examples

Views an integer stored in a Buffer. Keep Storage alive while Number is used.

#Requires AutoHotkey v2.1-alpha.36

Storage := Buffer(4, 0)
NumPut("Int", 42, Storage)
Number := ObjCreateWithDataPtr(StoredNumber, Storage.Ptr)
MsgBox Number.Value

struct StoredNumber {
    Value : Int32
}