Good day to all, please help determine the size of the structure in C #, there is a certain structure in C ++:

typedef struct { short int n; unsigned long adr; int nz,ms; TDateTime tr; TDateTime tz; TDateTime tv; TDateTime ts; short tm,sk,dop; float bz,hz,hzz,hdm,hdp,h0,h1; float l,ldm,ldp,lbm,lbp; float pf,pt,pdm,pdp,pbm,pbp,pu,pe; float l_1,l_2,l_3,l_4; float h1s,h2s,h3s,h4si,h1si; float t1s,t2s,t3s,t4s; float d1,d2,d3,d4; float gh4n,gh4z,gh4s,gh1z,gh1s; float l4; float d_rr,d_rv; int k_t,k_z; float f_rez[4]; int i_rez[4]; } ser; 

And another one :

 typedef struct { TDateTime dt; short int Nbr,Nsm,Nr; float pt,pf,pdm,pdp; float pbm,pbp,pu,pe; float ps[12]; float ph[8]; float phu[8]; //int prs,pr[8]; int prs; short pr[8],kr[8]; unsigned int m[16]; unsigned int s[16]; float frez[4]; int irez[4]; } str_sm; 

Next, in C ++, the sizes of these structures are determined:

 fser=CreateFile(AnsiString(S).c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (fser!=INVALID_HANDLE_VALUE) { long int ss=sizeof(str_sm)+((nrs.n.nr-1)*(sizeof(ser))); SetFilePointer(fser,ss,NULL,FILE_BEGIN); ReadFile(fser,&sr,sizeof(ser),&lread,NULL); CloseHandle(fser); flag=true; } 

How can I also find out the size of the structure? to make a shift in a binary file, I try this:

 struct test { int n; ulong adr; int nz, ms; double tr; double tz; double tv; double ts; short tm, sk, dop; float bz, hz, hzz, hdm, hdp, h0, h1; float l, ldm, ldp, lbm, lbp; float pf, pt, pdm, pdp, pbm, pbp, pu, pe; float l_1, l_2, l_3, l_4; float h1s, h2s, h3s, h4si, h1si; float t1s, t2s, t3s, t4s; float d1, d2, d3, d4; float gh4n, gh4z, gh4s, gh1z, gh1s; float l4; float d_rr, d_rv; int k_t, k_z; float[] f_redz1; float[] f_redz2; float[] f_redz3; float[] f_redz4; float[] f_redz5; int[] i_rez1; int[] i_rez2; int[] i_rez3; int[] i_rez4; int[] i_rez5; } test te = new test(); int abcd = System.Runtime.InteropServices.Marshal.SizeOf(typeof(test)); 

at the output I get 296 ... I did the same with the second structure and with this solution abcd + 0 * abcd2 = 296 (But I started the C ++ debugger and there = 360). Please help me understand, thank you very much.

  • Clarification: do you want to compare the same structure in two languages? - RusArt
  • @RuslanArtamonov One structure is written in C ++ I am rewriting a project in C # and I want to know how to correctly declare the structure and find out its size? - Ethernets
  • Dear Ethernets, if this relates to your yesterday's problem (one program writes another reads), then to solve it you need to know only the size and structure of the data block when writing. Further readable data you can easily convert everything you need. But this does not completely remove the value of your question. - Alexander Muksimov
  • 2
    Read about alignment in the structures of developerfusion.com/article/84519/mastering-structs-in-c - Primus Singularis
  • 2
    @Ethernets you define the size of the structure correctly, but you need to specify how the fields in c # should be aligned, for this there is the StructLayoutAttribute attribute. - Primus Singularis

1 answer 1

Sorry to write in response, but you are sure that the size of the ser structure in your project is exactly 360. Now I defined this structure in C ++ Builder 6. Its sizeof is 280. I also checked in MSVC, replacing TDataTime with double, there is also 280.

On C #, I aligned the structure so that its size is 280 bytes. You had problems with alignment of structure, and you not absolutely correctly transferred an array to a C # structure.

As a result, your structure looks like this:

 [StructLayout(LayoutKind.Sequential, Pack = 4)] struct test { int n; ulong adr; int nz, ms; double tr; double tz; double tv; double ts; short tm, sk, dop; float bz, hz, hzz, hdm, hdp, h0, h1; float l, ldm, ldp, lbm, lbp; float pf, pt, pdm, pdp, pbm, pbp, pu, pe; float l_1, l_2, l_3, l_4; float h1s, h2s, h3s, h4si, h1si; float t1s, t2s, t3s, t4s; float d1, d2, d3, d4; float gh4n, gh4z, gh4s, gh1z, gh1s; float l4; float d_rr, d_rv; int k_t, k_z; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] float[] f_redz; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] int[] i_rez; } 

Check again the compatibility of the C # structure when reading binary files. I also imported a namespace for C # code:

 using System.Runtime.InteropServices; 

Updated: Or you talked about the second structure. Its size is just 360. And in C # it looks like this:

 [StructLayout(LayoutKind.Sequential, Pack = 4)] struct str_sm { double dt; int Nbr, Nsm, Nr; float pt, pf, pdm, pdp; float pbm, pbp, pu, pe; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] float[] ps; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] float[] ph; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] float[] phu; int prs; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] short[] pr; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] short[] kr; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] uint[] m; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] uint[] s; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] float[] frez; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] int[] irez; 
  • Yes, that's right str_sm = 360 and ser is test = 280 . Thank you very much. - Ethernets