如何在c#应用程序中转换C结构

本文关键字:转换 结构 应用程序 | 更新日期: 2023-09-27 17:54:36

我试图在c#应用程序中使用C DLL的结构。我无法得到我正在处理的结构的等价数据类型。我正在处理来自C:

的这些数据类型
struct teststruct {
    unsigned short protocol_type;       //c#'s UInt16 ?
    unsigned char hardwareSize;         //c#'s byte?
    UCHAR dest[6];                      //unsigned char and thus byte in c#?
    unsigned char ip_version : 4;       //How to get the exact same thing in c#?!!
    unsigned char Nonce_Sum_Flag: 1;    //the same as above
    unsigned char ip[4];                // !!
    unsigned short  in6_addr_src[8];    // !!
    char* astring;                      //string!?    
    char  anarray[10];                  //string? or char[]?
    const char* conststring;            //string? or array[] or char[]?   
};

对于结构本身,我认为把

放在前面
[StructLayout(LayoutKind.Sequential)]

就足够了。

这是我的c#结构;很明显,我被中间两个和最后三个数据类型困住了。我完全不知道该怎么做!

[StructLayout(LayoutKind.Sequential)]
public struct teststruct
{
    public UInt16 protocol_type;                     //c's unsigned short ?
    public byte hardwareSize;                        //c's unsigned char?
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public byte[] dest;                              //c's unsigned char ?
    unsigned char ip_version : 4;                    //How to get the exact same thing in c#?!!
    unsigned char Nonce_Sum_Flag : 1;                //the same as above
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public byte[] ip;                                 // right?!
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public UInt16[]   in6_addr_src;                   // !!
    char* astring;                                    // !?    
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public byte[] anarray;                           //string? or char[]?
    const char* conststring;                         //string? or array[] or char[]?   
}

会有人特别建议我在C唯一的概念上的位分配?我指的是这一行:

unsigned char Nonce_Sum_Flag: 1;

和最后3个!

如何在c#应用程序中转换C结构

use [MarshalAs(UnmanagedType.LPStr)]string for char*

对于位域unsigned char ip_version : 4;, c#中没有直接的支持,你可以自己实现这个链接可能会有所帮助。

在c#中,您必须将所有位字段作为单个字节或int读取,并使用位掩码函数打包/解包它们。

大多数数据类型都猜对了。

查看下面的表,这是一个C++数据类型的表,应该是C#数据类型,大小也包括在内。

列顺序:c++数据类型,c#数据类型,Size

BOOL                            bool                    1 byte
BYTE                            byte                    1 byte
CHAR                            byte                    1 byte
DECIMAL                         Decimal                 16 bytes
DOUBLE                          double                  8 bytes
DWORD                           uint, UInt32            4 bytes
FLOAT                           float, single           4 bytes
INT, signed int                 int, Int32              4 bytes
INT16, signed short int         short, Int16            2 bytes
INT32, signed int               int, Int32              4 bytes
INT64                           long, Int64             8 bytes
LONG                            int, Int32              4 bytes
LONG32, signed int              int, Int32              4 bytes
LONG64                          long, Int64             8 bytes
LONGLONG                        long, Int64             8 bytes
SHORT, signed short int         short, Int16            2 bytes
UCHAR, unsigned char            byte                    1 byte
UINT, unsigned int              uint, UInt32            4 bytes
UINT16  ushort,                 UInt16                  2 bytes
UINT32, unsigned int            uint, UInt32            4 bytes
UINT64  ulong,                  UInt64                  8 bytes
ULONG, unsigned long            uint, UInt32            4 bytes
ULONG32 uint,                   UInt32                  4 bytes
ULONG64 ulong,                  UInt64                  8 bytes
ULONGLONG   ulong,              UInt64                  8 bytes
void*, pointers                 IntPtr                  x86=4 bytes, x64=8 bytes
<<p> <子>来源/订阅>