[Mono-devel-list] convert char[] in c struct to byte[] in C# struct??

Jonathan Pryor jonpryor at vt.edu
Sat Oct 2 21:09:40 EDT 2004


On Sat, 2004-10-02 at 20:52, Michael J. Ryan wrote:
> How would I define the following in a C# struct..
>    typedef struct {
>      char alias[26];
>    } x;

Is "alias" a string, or just binary data?

If it's a string, use this:

	struct MyStruct
	{
		[MarshalAs (UnmanagedType.ByValTStr, SizeConst=26)]
		public string alias;
	}

If it's binary data, use this:

	struct MyStruct
	{
		[MarshalAs (UnmanagedType.ByValArray, SizeConst=26)]
		public byte[] alias;
	}

	void UseMyStruct ()
	{
		MyStruct ms = new MyStruct ();
		ms.alias = new byte[26];
		// P/Invoke with native code...
	}

See also: http://jprl.com/~jon/interop.html#marshaling-arrays

 - Jon





More information about the Mono-devel-list mailing list