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

Michael J. Ryan tracker1_lists at theroughnecks.com
Sun Oct 3 13:36:05 EDT 2004


Thanks, I just didn't "get" it... at this point, I
am reading the data directly from the file, I had wanted
to use the C api that was already written, but honestly
prefer all managed code... just kinda weird.. :)

Thanks a bunch though, will keep a note of this.
Beyond that, any idea why a char[]'s .ToString() would
keep giving me "System.Char[]" instead of the value?
Same went for Convert.ToString(char[]); (I'm using the
variable for the char[])

Jonathan Pryor wrote:

> 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

-- 
Michael J. Ryan - tracker1(at)theroughnecks(dot)com - www.theroughnecks.net
icq: 4935386  -  AIM/AOL: azTracker1  -  Y!: azTracker1  -  MSN/Win: (email)



More information about the Mono-devel-list mailing list