在 Windows CE 的 Struct 中编组 char 数组

发布于 2024-10-24 18:09:28 字数 1692 浏览 1 评论 0原文

如何在以下结构定义中编组 char 数组?在 .Net CompactFramework (For Windows CE)

//Struct of request for transaction

typedef struct _VXN_REQUEST
{

    char        DID [33]; 
    char        MID [33]; 
    char        TID [33];  
    char        ClientRef [33]; 
    char        Payload [8192];
    ULONG       PayloadLength;
} VXN_REQUEST, *LPVXN_REQUEST;

我尝试像这样封送它,但它似乎不起作用

        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
        public struct VXN_REQUEST
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string DID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string MID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string TID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string ClientRef;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string Payload;
            public uint PayloadLength;

        }

也尝试过这样,但它们都不起作用,本机代码将其视为改为单个字符。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct VXN_REQUEST
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] DID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] MID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] TID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] ClientRef;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] Payload;
    public uint PayloadLength;
}

How would one marshal the char arrays in the following struct definition? in the .Net CompactFramework (For Windows CE)

//Struct of request for transaction

typedef struct _VXN_REQUEST
{

    char        DID [33]; 
    char        MID [33]; 
    char        TID [33];  
    char        ClientRef [33]; 
    char        Payload [8192];
    ULONG       PayloadLength;
} VXN_REQUEST, *LPVXN_REQUEST;

I tried to Marshal it like this but it doesn't seem to work

        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
        public struct VXN_REQUEST
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string DID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string MID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string TID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string ClientRef;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
            public string Payload;
            public uint PayloadLength;

        }

Also Tried like This but none of them works, the native code is taking it as a single char instead.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct VXN_REQUEST
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] DID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] MID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] TID;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] ClientRef;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 33)]

    public char[] Payload;
    public uint PayloadLength;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2024-10-31 18:09:28

.NET char 占用两个字节,它存储以 utf-16 编码的 Unicode 代码点。在声明中使用字节而不是字符。如果您确实需要这些字段为字符串,请使用 Encoding.ASCII.GetBytes() 填充 byte[]。

A .NET char takes two bytes, it stores a Unicode codepoint encoded in utf-16. Use byte instead of char in the declaration. Use Encoding.ASCII.GetBytes() to fill the byte[] if you actually need these fields to be strings.

前事休说 2024-10-31 18:09:28

更改 P/Invoke 声明以采用 byte[] 而不是结构,然后按如下方式定义它:

public class VXN_REQUEST 
{
    private byte[] m_data;

    public const int Size = 8328;

    public VXN_REQUEST()
    {
        m_data = new byte[Size];
    }

    public static implicit operator byte[](VXN_REQUEST req)
    {
        return req.m_data;
    }

    public string DID
    {
        get { return Encoding.ASCII.GetString(m_data, 0, 33).Trim('\0'); }
        set 
        { 
            // TODO: verify that 'value' isn't too long
            // first clear the contents
            var empty = new byte[33];
            Buffer.BlockCopy(empty, 0, m_data, 0, empty.Length);
            // copy data
            Encoding.ASCII.GetBytes(value).CopyTo(m_data, 0); 
        }
    }

    public string MID
    {
        get { return Encoding.ASCII.GetString(m_data, 33, 33).Trim('\0'); }
    }

    public string TID
    {
        get { return Encoding.ASCII.GetString(m_data, 66, 33).Trim('\0'); }
    }

    public string ClientRef
    {
        get { return Encoding.ASCII.GetString(m_data, 99, 33).Trim('\0'); }
    }

    public string Payload
    {
        get { return Encoding.ASCII.GetString(m_data, 132, PayloadLength).Trim('\0'); }
    }

    public int PayloadLength
    {
        get { return BitConverter.ToInt32(m_data, 8324); }
    }
}

郑重声明,33 个字符的长度对我来说似乎很奇怪。

Change your P/Invoke declaration to take in a byte[] instead of the struct, then define it like this:

public class VXN_REQUEST 
{
    private byte[] m_data;

    public const int Size = 8328;

    public VXN_REQUEST()
    {
        m_data = new byte[Size];
    }

    public static implicit operator byte[](VXN_REQUEST req)
    {
        return req.m_data;
    }

    public string DID
    {
        get { return Encoding.ASCII.GetString(m_data, 0, 33).Trim('\0'); }
        set 
        { 
            // TODO: verify that 'value' isn't too long
            // first clear the contents
            var empty = new byte[33];
            Buffer.BlockCopy(empty, 0, m_data, 0, empty.Length);
            // copy data
            Encoding.ASCII.GetBytes(value).CopyTo(m_data, 0); 
        }
    }

    public string MID
    {
        get { return Encoding.ASCII.GetString(m_data, 33, 33).Trim('\0'); }
    }

    public string TID
    {
        get { return Encoding.ASCII.GetString(m_data, 66, 33).Trim('\0'); }
    }

    public string ClientRef
    {
        get { return Encoding.ASCII.GetString(m_data, 99, 33).Trim('\0'); }
    }

    public string Payload
    {
        get { return Encoding.ASCII.GetString(m_data, 132, PayloadLength).Trim('\0'); }
    }

    public int PayloadLength
    {
        get { return BitConverter.ToInt32(m_data, 8324); }
    }
}

And for the record, 33-charater lengths seems really odd to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文