How to get structure memory address?

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 :slight_smile:
Edit: Oops, posted the correct example now…

Var abc as TestStruct

Var m as MemoryBlock = abc.StringValue(True)
Var p as Ptr = m
print str(p)

PS:Declare will translate the structure ptr automatically.

3 Likes

Thank you @Vigia_Lin! It works. :slight_smile:

This. If you pass a structure to a declare, it automatically passes the address. Just like a memoryblock.

2 Likes

yes. My English is poor. :hot_face:

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.

1 Like

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

r comes back as 111 here. I’m not on a current version, though.

Weird. Here too. But I saw a different result earlier. :confused:

Confused but tired. Won’t try to understand what happened.

The program that I am working with uses the SendInput function, through a declare. I will create a new topic, as this is just for fun :slight_smile:

The modification that I made for one of the structures to return the hexadecimal value as a UInt64 goes like this:

I’ll start a new topic and feel free to join in!

A. I’m not sure I understand why your XOJO code would need to know this information.

B. This function doesn’t do what you think it does.

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
:slight_smile:

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

p=p1

Of course it does. But neither equal abc. They both point to a copy of its contents.

1 Like

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.

1 Like

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

I did not figure this out!!! The is basically from Norm’s blog:
https://www.great-white-software.com/blog/2019/07/16/c-unions/

I did not know you could use Structures with pointers like that.

  • Karen
2 Likes

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

-Karen

1 Like