I am trying to get the memory address of a structure and can’t seem to figure it out. Here is what I have to demonstrate:
A simple structure:
Try and show the address in memory of the structure:
The error I am getting is ‘Type mismatch error’.
The reason for retrieving the address of a structure is that I have a needed declare that requires the addresses of three structures. If I can get the address of one structure then I can figure out the remaining three.
Does anyone have any helpful suggestions? Thanks
Edit: Oops, posted the correct example now…
Sorry for being a little pedantic, but technically this is getting a pointer to a copy of the structure. It makes a difference if the declare is changing the values of the structure. You would have to copy the contents of the memoryblock back into the structure after the declare.
I thought the same, but looks like that in Xojo, not.
Structure TestStruct
x as Int32
End Structure
Var abc as TestStruct
abc.x = 111
Var m as MemoryBlock = abc.StringValue(True)
Var p as Ptr = m
p.Int32(0) = 222
Var r As int32 = abc.x
Break // look at r
I would love if we had something more direct like:
Structure TestStruct
x as Int32
End Structure
Var abc as TestStruct
abc.x = 111
Var p as Ptr = AddressOf abc
p.Int32(0) = 222
Var r As int32 = abc.x
Break // look at r
I could easily be wrong for needing this @Tim_Hare . When I build the SendInput declare in C++, it appears to add an array of the four key commands. Feel free to look at the post: Creating a working SendInput Declare on Windows
Var abc as TestStruct
abc.x = 111
Var m as MemoryBlock = abc.StringValue(True)
Var p as Ptr = m
//p.Int32(0) = 222
Var r as Int32 = abc.x
Var b as MemoryBlock = p
b.Size = 8
Var p1 as Ptr = b
Break // look at r
Still curious on how can we achieve the equivalent to something like:
Structure TestStruct
x As Int32
y As Int32
End Structure
Var abc as TestStruct
Var p as Ptr = AddressOf abc
p.Int32(0) = 111
p.Int32(4) = 222 // y is 4 bytes after x
Break // abc.x should be 111, and y, 222.
The language must have a way, but it seems kind of unknown for many.
Do you happen to have a suggestion as to how I can retrieve the structure memory address, or possibly create a duplicate of the structure data and place it into a pointer so that I can retrieve the address of the copy?
It would be nice to have an AddressOf command work like @Rick_Araujo suggested.
This code allows you to essentially overlay a structure over MemoryBlock (which can be used with pointers) without copying…
Structure TestStruct
i As int32
End Structure
Dim M as New MemoryBlock(4)
M.Int32Value(0) = 111
Dim P as Ptr= M
Dim i1 as Integer = P.TestStruct.i
P.TestStruct.i = 222
Dim i2 as Integer = M.Int32Value(0)
Break
Just use a MemoryBlock the size of the structure instead of the structure directly which can ne assigned to a Ptr…
You can use the Ptr or the memoryblock.Datatype(offset) for the elements ofter structure… But remember a Structure IS a datatype…
So for convenience and readability you could use the Ptr.StructureName method as in my previous post, to get and set the data in the structure in the MemoryBlock