S
Scott Townsend
So I need to talk to a devices that expects all of the bits and bytes I sent
it to be in specific places (not yet 100% defined).
I wanted to create a structure/class with all of the data in it and then
convert that to a byte array, pass it to the device, then get a reply and
then convert that to a structure.
I'm having issues with making sure what I've coded is correct. Cant figure
out how to define an array in structure that is a Fixed length.
Could someone tell me if I'm way off in what I'm trying to do?
Public Sub initpacketData(ByRef myPacketData As PacketData)
myPacketData.MaxCmdLength = 254
myPacketData.CmdLength = 0
System.Array.Resize(CType(myPacketData.SQLCmd, Char()), 254)
System.Array.Resize(CType(myPacketData.padding, Char()), 3840)
'myPacketData.SQLCmd = New String(" ", 254)
'myPacketData.padding = New String(" ", 3840)
End Sub
<StructLayout(LayoutKind.Explicit, Size:=4096, CharSet:=CharSet.Ansi)> _
Structure PacketData
<FieldOffset(0)> Dim MaxCmdLength As Byte
<FieldOffset(1)> Dim CmdLength As Byte
<FieldOffset(2)> Dim SQLCmd As String
<FieldOffset(25)> Dim padding As String
End Structure
Dim DataToSend As PacketData
Dim DataToReceieve As PacketData
Dim DataReceived As String
Dim iSizeOfStruct As Integer
initpacketData(DataToSend)
initpacketData(DataToReceieve)
DataToSend.MaxCmdLength = 254
DataToSend.CmdLength = message.Length
DataToSend.SQLCmd = message
'Set Up Our Pointer to Some Memory
Dim ptrDataToSend As IntPtr = Marshal.AllocHGlobal(4096)
'Copy the structure to our memory location @ pointer
Marshal.StructureToPtr(DataToSend, ptrDataToSend, True)
iSizeOfStruct = Marshal.SizeOf(DataToSend)
DataReceived = Marshal.PtrToStringUni(ptrDataToSend, 4096)
Dim ptrDataToReceieve As IntPtr = Marshal.AllocHGlobal(4096)
ptrDataToSend = Marshal.StringToHGlobalUni(DataReceived)
DataToReceieve = CType(Marshal.PtrToStructure(ptrDataToSend,
GetType(PacketData)), PacketData)
iSizeOfStruct = Marshal.SizeOf(DataToReceieve)
------
DataReceived does not seem to be what I expected, though converting it back
to a struct seemed to work.
The Size is always 12 too?
Thanks,
Scott<-
it to be in specific places (not yet 100% defined).
I wanted to create a structure/class with all of the data in it and then
convert that to a byte array, pass it to the device, then get a reply and
then convert that to a structure.
I'm having issues with making sure what I've coded is correct. Cant figure
out how to define an array in structure that is a Fixed length.
Could someone tell me if I'm way off in what I'm trying to do?
Public Sub initpacketData(ByRef myPacketData As PacketData)
myPacketData.MaxCmdLength = 254
myPacketData.CmdLength = 0
System.Array.Resize(CType(myPacketData.SQLCmd, Char()), 254)
System.Array.Resize(CType(myPacketData.padding, Char()), 3840)
'myPacketData.SQLCmd = New String(" ", 254)
'myPacketData.padding = New String(" ", 3840)
End Sub
<StructLayout(LayoutKind.Explicit, Size:=4096, CharSet:=CharSet.Ansi)> _
Structure PacketData
<FieldOffset(0)> Dim MaxCmdLength As Byte
<FieldOffset(1)> Dim CmdLength As Byte
<FieldOffset(2)> Dim SQLCmd As String
<FieldOffset(25)> Dim padding As String
End Structure
Dim DataToSend As PacketData
Dim DataToReceieve As PacketData
Dim DataReceived As String
Dim iSizeOfStruct As Integer
initpacketData(DataToSend)
initpacketData(DataToReceieve)
DataToSend.MaxCmdLength = 254
DataToSend.CmdLength = message.Length
DataToSend.SQLCmd = message
'Set Up Our Pointer to Some Memory
Dim ptrDataToSend As IntPtr = Marshal.AllocHGlobal(4096)
'Copy the structure to our memory location @ pointer
Marshal.StructureToPtr(DataToSend, ptrDataToSend, True)
iSizeOfStruct = Marshal.SizeOf(DataToSend)
DataReceived = Marshal.PtrToStringUni(ptrDataToSend, 4096)
Dim ptrDataToReceieve As IntPtr = Marshal.AllocHGlobal(4096)
ptrDataToSend = Marshal.StringToHGlobalUni(DataReceived)
DataToReceieve = CType(Marshal.PtrToStructure(ptrDataToSend,
GetType(PacketData)), PacketData)
iSizeOfStruct = Marshal.SizeOf(DataToReceieve)
------
DataReceived does not seem to be what I expected, though converting it back
to a struct seemed to work.
The Size is always 12 too?
Thanks,
Scott<-