473,396 Members | 2,004 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,396 software developers and data experts.

Marshalling strings as fixed length WITHOUT null terminator.

Hi, And thanks for any help.

I am having trouble declaring a struct containing strings that can be
Serialized using the following functions. It works, but the strings wont
store the right number of characters, they store one less with a null at the
end! Whats the point in a marshalling as fixed length if it wastes space
with a null! How can i make it marshall the strings as "proper" fixed length
strings, where the full 8 (or 3) characters can be stored without having to
extend the length of the strings.
/* Generic serialization functions */

/* Thanks to Peter Petrov @
http://csharp.codenewbie.com/article...es-Page_1.html
*/

public class RawSerializer

{
public static byte[] RawSerialize( object anything )

{

int rawsize = Marshal.SizeOf( anything );

IntPtr buffer = Marshal.AllocHGlobal( rawsize );

Marshal.StructureToPtr( anything, buffer, false );

byte[] rawdatas = new byte[ rawsize ];

Marshal.Copy( buffer, rawdatas, 0, rawsize );

Marshal.FreeHGlobal( buffer );

return rawdatas;

}
public static object RawDeserialize( byte[] rawdatas, Type anytype )

{

int rawsize = Marshal.SizeOf( anytype );

if( rawsize > rawdatas.Length )

return null;

IntPtr buffer = Marshal.AllocHGlobal( rawsize );

Marshal.Copy( rawdatas, 0, buffer, rawsize );

object retobj = Marshal.PtrToStructure( buffer, anytype );

Marshal.FreeHGlobal( buffer );

return retobj;

}

}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]

public struct FileInfoRecord

{

public uint fileId;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]

public string fileName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]

public string fileType;

public uint fileDirectory;

public DateTime fileModified;

public DateTime fileCreated;

public uint fileSize;

}

Thanks

Tristan
Nov 16 '05 #1
1 5492
>How can i make it marshall the strings as "proper" fixed length
strings, where the full 8 (or 3) characters can be stored without having to
extend the length of the strings.


Replace the strings with char[] and change the marshaling setting to
ByValArray.

Mattias

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

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
1
by: Me | last post by:
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...
4
by: Martin Fletcher | last post by:
when I execute dim strTEMP_DATA as string strTEMP_DATA = Encoding.ASCII.GetString(rBuffer).ToString I alway get a string without the closing quote. I see this in the 'Autos' window, I also...
3
by: Carlo Stonebanks | last post by:
I need to read a binary file which has mixed data types embedded in it. Fixed length strings, 16-bit integers and zero (null) terminated ASCII strings. (akak ASCIIZ) The first two types are no...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
23
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in...
1
by: peary | last post by:
Hi, everyone, I'm writing a program to discover wireless network using Windows Native Wifi API & VB.net. I have to declare the windows API in my VB.net program. The original windows...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
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: 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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.