473,385 Members | 1,734 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,385 software developers and data experts.

Structure to byte array using Marshalling

Me
I'm trying to get a structure into a byte array. I can't seem to
figure out how to get a non-fixed length null-terminated string into
the array (without rolling my own logic). For example, a struct like
(from another posting in this group):

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Private Structure SPECIFIC_SOCKET_MSG
Public Length As Integer
Public MsgId As Integer
Public FileName As String
End Structure

and initialized like this:

Dim msg As SPECIFIC_SOCKET_MSG

msg.FileName = "my file"
msg.Length = msg.FileName.Length
msg.MsgId = 1

I'd like to have two four byte Integers followed by "my file" in ANSI
followed by a null:

{1,0,0,0,7,0,0,0,109,121,32,102,105,108,101,0}

I've tried using code like this, but I can't find any UnmanagedType to
MarshalAs the FileName string to get it right:

' allocate the buffer
Dim pBuffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(msg))
Console.WriteLine("SizeOf msg: " & Marshal.SizeOf(msg))

' copy the structure into the buffer
Marshal.StructureToPtr(msg, pBuffer, True)

' allocate the byte array
Dim byteBuffer(Marshal.SizeOf(msg) - 1) As Byte

' copy the value of the memory buffer to the byte array
Marshal.Copy(pBuffer, byteBuffer, 0, byteBuffer.Length)

' free the memory
Marshal.FreeHGlobal(pBuffer)

UnmanagedType.ByValTStr gets the values right, but it has to have a
SizeConst set whereas I need variable sized.

Any ideas on how to get the string to marshal correctly? Maybe define
FileName as a char array and then to "my file".ToCharArray() when
initializing it? I couldn't get that to work either.

Thanks for any help!
Nov 20 '05 #1
1 4524
>I can't seem to
figure out how to get a non-fixed length null-terminated string into
the array (without rolling my own logic).


You'll have to take care of the string manually, there's no
UnmanagedType marshal setting that will give you the result you want.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2

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

Similar topics

7
by: Mick | last post by:
Does anyone know how to pass an array of structures to a DLL? Here's what I'm doing... the VB.Net error is at the end. *** C++ Structure Declaration: typedef struct _SOME_STRUCT { char...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
1
by: Betty | last post by:
When marshalling a char array in a structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
1
by: Zoser | last post by:
When marshalling structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does not match the...
10
by: Scott Townsend | last post by:
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.