Declares and MemoryBlock vs Ptr

I was under the impression that you could use memory blocks and ptr interchangeably in a soft declare.
I have a case where I build a structure and If I pass it as a memory block it fails, but if I enter memoryBlock.ptr(0), it succeeds.

Can someone explain what’s going on?

Declares can only accept Ptrs, and although a MemoryBlock converts silently into a Ptr correct usage depends on exactly what the declare expects the Ptr to point to. It usually boils down to whether you or the library is responsible for allocating the memory (and later freeing it), and also whether the data is being copied (and in which direction) or directly pointed to.

Judging from your description I infer that the declare is expecting you to pass the structure by reference:

Soft Declare Sub FillStructWithData Lib "MyLib" (ByRef MyStructure As SomeStructure) Dim struct As SomeStructure FillStructWithData(struct)

Thanks Andrew… apparently the more I learn, the less I know…
I’m guessing it is expecting byVal not byRef because I can pass a structure and it behaves right.
The memory block needing a ptr(0) is what I found strange. I hadn’t seen that before.

It’s hard to be sure without seeing the function declaration and reading the documentation because the semantics of the Ptr type can be just about anything. If you’re allocating a New MemoryBlock, passing it to the declare, and then reading the Ptr(0) from it then this is equivalent to passing the Ptr ByRef.

Be careful here - the pointer to a memoryBlock is definitely not the same as MemoryBlock.Ptr(0) - the first is the address of the memoryBlock itself, whereas the second treats the first 4 bytes of the memoryBlock as a pointer. These are conceptually very different.

What is the declare statement when you use a structure? Are you passing data? Or receiving it back? And when you say it doesn’t work with a memoryblock, what do you mean exactly? Does it crash? Return bad data? If so, how are you extracting the data from the memoryblock?

Also be careful if you’re using pointers to structures in 64bit Xojo in Windows because there’s a bug <https://xojo.com/issue/52211>.

[quote]Ptr(offset As Integer) As Ptr
Gets or sets a Ptr value at the specified offset (in bytes).[/quote]

So please be aware that passing memory block or memory block.ptr(0) is not the same!

Christian, is there sometime inherently different between a 32 bit and 64 bit pointer?

the bit number is different…
Otherwise no.