Loading MemoryBlock into Structure

I have a MemoryBlock returned from a declare which contains information about a connected device. I know the format of the data so I wanted to create a Structure and pass the device information directly into it, instead of getting the data using pointer offsets.

Using pointer offset works, I get a readable name in sDeviceName:

dim mbDeviceList as MemoryBlock = deviceListPointer.ptr(0) //get the device list
dim mbDevice as MemoryBlock = mbDeviceList.ptr(2) //device starts here
dim mbDeviceName as memoryblock = mbDevice.ptr(0) //get the name from offset 0 into a memoryblock
dim sDeviceName as string = mbDeviceName.CString(0) //convert memoryblock to string

Trying to parse it directly into my structure does not work, I get a bunch of gibberish in the structure:

Structure structTestName
  szDeviceName as string * 8
End Structure

dim mbDeviceList as MemoryBlock = deviceListPointer.ptr(0) //get the device list
dim mbDevice as MemoryBlock = mbDeviceList.ptr(2) //device starts here
dim myStruct as App.structTestName
myStruct.StringValue(mbDevice.LittleEndian) = mbDevice

The bit of code I used for trying to parse a MemoryBlock into a Structure was from this thread.

Any idea what I’m doing wrong? Thank you!

The results you are seeing are accurate, based on your posted code.

The declare is not returning the device name, it’s returning Ptr (or a memory address) where another structure, mbDeviceList, can be found. That address contains a Ptr to where the device information, mbDevice, can be found. Finally, that contains a Ptr to where the device name can be found.

In short, when you assign mbDevice to the structure, you are assigning the address of the name, not the name itself, so you are seeing gibberish.

Keep doing it the way you are doing it.

I am.out in the field, and my declare book shows how to take a pointer and populate a structure with it

I am going by memory and it goes something like this.

Dim p as ptr // populated with your data
Dim s as yourstructure
S=p.yourstructure

The s variable should then be populated with the data from the pointer.

[quote=490171:@Eugene Dakin]
S=p.yourstructure

The s variable should then be populated with the data from the pointer.[/quote]

Eugene,

Thanks for your reply. This looks like it is going work. When I type p.you it autofills the rest of the structure name as p.yourstructure.

But then when I try to compile I get Type “Ptr” has not member named “yourstructure”. Very odd.

Kem is correct, that the code should work.

Here is one way to explain how to make this work. I created a simple structure called StructTestName:

Structure StructTestName szDeviceName as CString*8 End Structure

This is a Cstring (ANSI) structure that is able to hold 8 bytes.

Below is code that is used to assign a cstring to a memoryblock, get the pointer location, and then create a structure and update the memoryblock data to the structure:

[code]Sub Action() Handles Action
//Both structure and memoryblock have
//The same size
Var mbDeviceList as New MemoryBlock(8)
mbDeviceList.CString(0) = “Mike G”

//Make a pointer to get the memoryblock
//location in memory
Var p as Ptr
//Assign the memoryblock location to the pointer
p = mbDeviceList
//Create a new structure
Var MyStruct as StructTestName
//Load memoryblock data to structure
MyStruct = p.StructTestName
//Show structure data
TextArea1.Text = MyStruct.szDeviceName
End Sub
[/code]

Does this help?