What are the C# equivalent of these C++ structs

前端 未结 4 2050
感情败类
感情败类 2020-12-30 16:52
typedef union _Value {
    signed char    c; 
    unsigned char  b; 
    signed short   s; 
    unsigned short w; 
    signed long    l; 
    unsigned long  u; 
             


        
4条回答
  •  情歌与酒
    2020-12-30 17:13

    I'm assuming the 'char' is being used as an 8 bit number, if so, then here are you're mappings:

    signed char    c; -> SByte    c; 
    unsigned char  b; -> Byte     b;
    signed short   s; -> Int16    s;
    unsigned short w; -> UInt16   w;
    signed long    l; -> Int32    l;
    unsigned long  u; -> UInt32   u;
    float          f; -> Single   f; (though 'float' still works)
    double        *d; -> Double   d; (was this meant to be a pointer???)
    char          *p; -> String   s; (assuming its a string here, in the marshaling you can tell it whether it is ASCII or wide char format)
    

    With this info it should be relatively easy to translate those strucutres (just make sure you keep them as a struct and give it the attribute "[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]", which will make sure that the marshaller keeps all the data in the same order.

    Also, I recommend looking through both the classes and attributes in System.Runtime.InteropServices as they do provide quite a few methods for automating marshaling of data to c/c++ code (and it doesn't require "unsafe" c# code either).

提交回复
热议问题