   [.net] To read C++ written binary file in C#
  Hi All,I have a bin file written in C++the C++ strucuture isstruct s{byte* var;}Now in C# how do I read this, please help   
  BinaryReader   ;  Using Binary reader but not getting correct value - My C# code is  [StructLayout(LayoutKind.Sequential, Pack = 1)]    public struct STRUCT    {   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]    public byte[] var;    }and to readprivate STRUCT_STRUCT Reader()   {   FileStream posSecFile = File.OpenRead(filename);  STRUCT posSec = new STRUCT();  int count = Marshal.SizeOf(typeof(STRUCT));  byte[] readBuffer = new byte[200];  BinaryReader reader = new BinaryReader(posSecFile);  reader.Read(readBuffer, 0, 200);   GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);  posSec = (STRUCT)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(STRUCT));  handle.Free();  posSecFile.Close();   }but not gatting correct values   ;  Can you post the C++ code which is writing the data to the file?   ;  int SysSecTbl::save(){if (!level) return -1;int status = -1;unsigned short savecoders[8];getcoder( savecoders );setcoder( newcoders, 8 );CS_BFILE bf;if ( bf.Open( SecTableName, PATH_DB, FM_OPEN ) ) {bf.Close();WriteFileAttrib( SecTableName, PATH_DB, FA_NORMAL ); }if ( bf.Open( SecTableName, PATH_DB, FM_DENYWR|FM_TRUNCATE) ) {int bytes = sizeof(*level) * tot;encrypt2( (unsigned char *)level, bytes );bf.Write( level, sizeof(*level)*tot );bf.Close();decrypt2( (unsigned char *)level, bytes );#if 0// WriteFileAttrib( SecTableName, PATH_DB, FA_HIDDEN );#endif }setcoder( savecoders, 8 );status = 0;Exit:return status;}   ;  Well, the only thing I see that's obviously going to act different is the encrypt2/decrypt2 calls on the C++ side.Other than that, is your byte array always the same size?  If not, you should write out the # of bytes as an integer to the file beforehand so that the reader knows how much data to read.As far as your C# code goes, it's got all of the necessary code but you probably don't need to use any of the marshalling stuff.  You could just trim it down to this:public struct STRUCT{  public byte[] var = new byte[200];}FileStream posSecFile = File.OpenRead(filename);STRUCT posSec = new STRUCT();BinaryReader reader = new BinaryReader(posSecFile);reader.Read(posSec.var, 0, posSec.var.Length); posSecFile.Close(); ;  Usevar MyData = System.IO.File.ReadAllBytes("MyFile.dat");To read all (binary) data in that file into MyData, which is a byte[].   