Marshalling stucts with bit-fields in C#

前端 未结 4 1951
死守一世寂寞
死守一世寂寞 2020-12-16 05:58

Is it possible to marshal a C-style struct containing bit-fields to a C# struct, or would you have to marshal it to a basic type and then do bit-masks?

E.g. I would

4条回答
  •  清酒与你
    2020-12-16 06:40

    I've marshaled bitfields like:

    public struct Rgb16 {
        public ushort Value; // two byte value that internally contain structure R(4):G(5):B(4)
    
        public Rgb16BitField GetBitField
        {
            get; set;
        }
    }
    

    where the property creates new structure like you mentioned by dividing Value to bits.

    Aren't the best way to do that, but haven't found anything else that worked for me. I can provide the code for GetBitField if you want (it isn't very compact)

    Upd: The link provided by Tony in the comments to your question uses the same idea but seems more accurate then mine, so use his solution if you cannot find any better

提交回复
热议问题