2

I have a struct like this:

public struct SERVER_DF_PARAMETERS

    {
        public bool bRunState;          
        public bool bWideband;          
        public double dFrequencyRF;
        public double dFrequencyStartScan;
        public double dFrequencyStopScan;

        public double dBandwidthIF;     
        public double dBandwidthDF; 

        public bool bNormalModeDF;      
        public double dThresholdLevelDF;
        public double dThresholdQualityDF;
        public int iAverageTimeDF;      

        public bool bPreAmplifierRF;
        public bool bLongAntennaRF;
        public int iTunerModeRF;
        public int iGainRF; 

        public bool bAutoGainIF;        
        public int iManualGainIF;
        public int iAutoGainCtrlTime;   
        public double dMaxGainAgc;      
        public bool bAFC;
        public bool bVoiceState;        //true -> On , false -> off
        public bool bSquelchState;      //true -> On , false -> off
        public bool bStateDenoising;    //true -> On , false -> off
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
        public String strDemodulation;  //AM,FM,SSB,CW,...
        public double dBandwidthVoice;  //in KHz
        public int iBeatFreqOffset; //in Hz (BFO)
        public double dGainVoice;           //int dBm

        public bool bClassifier;        //true -> Run , false -> Stop
        public int iTimeHistoryCL;      //in millisecond


    };

but when the program runs the order of fields is changed to this(All booleans are in top and so on):

    (I added to watch to see its value )


    bAFC    false   bool
    bAutoGainIF false   bool
    bClassifier false   bool
    bLongAntennaRF  false   bool
    bNormalModeDF   true    bool
    bPreAmplifierRF false   bool
    bRunState   true    bool
    bSquelchState   false   bool
    bStateDenoising false   bool
    bVoiceState false   bool
    bWideband   false   bool
    dBandwidthDF    20.0    double
    dBandwidthIF    20.0    double
    dBandwidthVoice 100.0   double
    dFrequencyRF    100.0   double
    dFrequencyStartScan 100.0   double
    dFrequencyStopScan  200.0   double
    dGainVoice  1.0 double
    dMaxGainAgc -30.0   double
    dThresholdLevelDF   -130.0  double
    dThresholdQualityDF 50.0    double
    iAutoGainCtrlTime   1000    int
    iAverageTimeDF  1000    int
    iBeatFreqOffset 0   int
    iGainRF 0   int
    iManualGainIF   0   int
    iTimeHistoryCL  1000    int
    iTunerModeRF    0   int
    strDemodulation "FM"    string

the problem is when I want to send this struct to the server using socket and and the server is in c++ and gives the bytes of this struct. So the order of fields should'nt change.

4
  • 3
    have you tried [StructLayout(LayoutKind.Sequential)] attribute on the deceleration of your struct?
    – Zafar
    Mar 1, 2014 at 11:11
  • are you sure the order of fields in memory is changing? The debug inspector is just alphabetizing the field names when it lists them out. Mar 1, 2014 at 11:55
  • @Zafar :Yes I put it but I had forgotten the(Pack=1) after it.The [StructLayout(LayoutKind.Sequential, Pack=1)] worked well
    – Fateme
    Mar 1, 2014 at 12:04
  • @MichaelEdenfield: You are right it seems this order is just Alphabetical!
    – Fateme
    Mar 1, 2014 at 12:14

2 Answers 2

3

put

[StructLayout(LayoutKind.Sequential, Pack=1)]

before your

public struct SERVER_DF_PARAMETERS

forces the compiler to assign the structure sequentially as listed in the definition, which is what it does by default. if you specify Pack=1 then the struct will be organised so that each field is on a byte boundary and can be read a byte at a time – i.e. no packing is necessary.

Refer to Mastering C# structs, for more explanations ^^

2
  • I had put The [StructLayout(LayoutKind.Sequential)] but I had forgotten to put (Pack=1) .My problem solved. Thank You.
    – Fateme
    Mar 1, 2014 at 12:07
  • Every problem solved lead to more understanding, I learn also from question:-)
    – V-SHY
    Mar 1, 2014 at 12:09
0

You can use the [StructureLayout] Attribute:

Reference is here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.