Marshalling strings as fixed length WITHOUT null terminator.

From: Tajmiester (tristan_me_at_homail.com)
Date: 02/21/05


Date: Mon, 21 Feb 2005 16:26:28 GMT

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/articles/csharp/1431/C_and_advanced_binary_files-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



Relevant Pages

  • Re: [Lit.] Buffer overruns
    ... >> As has been pointed out, C strings are unrelated to C buffer overflow ... > is a infrastructure source length available based on nul-terminated, ... > data-pattern based length paradigm ... ...
    (sci.crypt)
  • Re: null terminated strings
    ... terminated strings are blamed because they were being used in the programs ... involved with the overrun situation. ... maximum length for that buffer and make sure you don't exceed it. ... Languages such as COBOL would automaically pad or truncate fields during ...
    (comp.os.vms)
  • Re: [Lit.] Buffer overruns
    ... > As has been pointed out, C strings are unrelated to C buffer overflow ... is a infrastructure source length available based on nul-terminated, ... data-pattern based length paradigm ... ...
    (sci.crypt)
  • Re: String concatenation function, request for comments.
    ... usefulness, efficiency, and most importantly the correctness of this small piece of code. ... as the responsibility of freeing the memory would be left to the client. ... If the resultant buffer is needed beyond the second call to the function, it can be copied to another buffer at the clients digression. ... If the strings specified for concatenation exceed the buffer available, ...
    (comp.lang.c)
  • Re: String concatenation function, request for comments.
    ... >> left to the client. ... If the resultant buffer is needed beyond the second ... >> If the strings specified for concatenation exceed the buffer available, ... > This demonstrates a fragility of the interface. ...
    (comp.lang.c)