473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Structure to ByteArray Problems

Hi,

I am having problems converting from a Structure to a ByteArray. The
main problem being that the structure contains a variable length array,
which cannot be defined within the structure definition. A example of
my code is shown below and when this is run, an error is produced
within the BuildByteArray function due to the structure size not being
set.

Any help would be greatly appreciated.

CODE
----

Public g_by2DOpBuffer(1023) As Byte
Public Structure Struct1
Public Field1 As Integer
Public Array1() As Integer
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim message As New Struct1()

message.Array1 = 2
Redim message.Array1(message.Field1)
BuildByteArray(message, g_by2DOpBuffer)

End Sub

Public Function BuildByteArray(ByVal UserData As Object, ByVal
ByteArray() As Byte) As Byte()

Dim Ptr As IntPtr =
Marshal.AllocHGlobal(Marshal.SizeOf(UserData))

'now copy strcutre to Ptr pointer
Marshal.StructureToPtr(UserData, Ptr, False)
Marshal.Copy(Ptr, ByteArray, 0, Marshal.SizeOf(UserData))

Marshal.FreeHGlobal(Ptr)

End Function

Nov 21 '05 #1
2 1062
>I am having problems converting from a Structure to a ByteArray. The
main problem being that the structure contains a variable length array,
which cannot be defined within the structure definition. A example of
my code is shown below and when this is run, an error is produced
within the BuildByteArray function due to the structure size not being
set.


Wouldn't it be easier to forget about the structure and just use a
BinaryWriter on top of a MemoryStream?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
I have no knowledge/experience of using BinaryWriter or MemoryStream
but it may be an option. Would it be an easy job to replace the
structures ? An example would help. The reason I ask is that I have
over 50 structures within my code which are used extensively.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kristina Engdahl | last post by:
Hello, I'm working with dynamic classloading and have encountered a problem when it comes to defining an URL for a file that is available through a bytearray representing a jar-file. The file in...
1
by: Vince | last post by:
Hi, I would like to associate a couple of value(SFID and nRecNo) with a byte array. So I tried the following structure : typedef std::vector<BYTE> ByteArray; class CardIndex {
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
2
by: James | last post by:
Hi, I need help with a couple of questions, I have create and structure like: Structure byteArray Public byteA() As Byte End Structure Public ptrArray() As byteArray
1
by: gijamie911 | last post by:
int nBytes = 256; byte ByteArray = new byte; int nBytesRead = inputStream.Read(ByteArray, 0 , nBytes); recorderID = ""; for (int i...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
11
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte...
2
by: =?Utf-8?B?QXNzaWRv?= | last post by:
Hello @all i need help with the following problem: Im calling an unmanaged C++ DLL (TAPI32.dll) function (lineGetCallInfo) like this: Declare Function lineGetCallInfo Lib "tapi32.dll"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.