473,320 Members | 2,020 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,320 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 4522
>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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.