Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I never used marshaling before, but I did some c++ code and I wanted it's equivalence in c#. My code used a struct like this:

 

struct EnteteWave
{
char chunkID [4];
int chunkSize;
char format [4];
char subchunk1ID [4];
int subchunk1Size;
short audioFormat;
short numChannels;
int sampleRate;
int byteRate;
short blockAlign;
short bitsPerSample;
char subchunk2ID [4];
int subchunk2Size;
};

 

but in c# we cant fix the array in a declaration on struct. So i searched on these forum and fond how to use marshaling and did this:

 

[structLayout(LayoutKind.Sequential)]
public struct EnteteAvi
{
	[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	public byte[] chunkId;
	public Int32 fileSize;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	public byte[] fileType;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	public byte[] subChunk1ID;
	public Int32 subChunk1Size;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	public byte[] subChunk1Type;
	public Int32 cb;
	public Int32 MicroSecPerFrame;
	public Int32 MaxBytePerSec;
	public Int32 PaddingGranularity;
	public Int32 Flags;
	public Int32 TotalFrames;
	public Int32 InitialFrames;
	public Int32 Streams;
	public Int32 SuggestedBufferSize;
	public Int32 Width;
	public Int32 Height;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
	public Int32[] Reserved;
}

 

(it's not the same thing because my c++ code was to acess a wave ane the c# code is acessing an AVI file)

 

I find this Marshaling thing very confused but I think im using it right. the problem is, my compilator does not like it at all....

C:\Documents and Settings\Nicolas Dufour\Mes documents\Visual Studio Projects\ZoneMedia\AVI.cs(30): The type or namespace name 'StructLayout' could not be found (are you missing a using directive or an assembly reference?)

 

Maybe i'm just missing a namespace or using call,m anybody can help?

 

and while you're looking at my code, does it actually look right?

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...