在 Windows CE 的 Struct 中编组 char 数组
如何在以下结构定义中编组 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.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.
更改 P/Invoke 声明以采用 byte[] 而不是结构,然后按如下方式定义它:
郑重声明,33 个字符的长度对我来说似乎很奇怪。
Change your P/Invoke declaration to take in a byte[] instead of the struct, then define it like this:
And for the record, 33-charater lengths seems really odd to me.