使用 C# 将非托管结构编组为托管代码
当我从外部应用程序获取字节时,我需要处理字节[]。 外部应用程序也是用 C# 完成的,它们通过 UDP 发送字节。 他们正在发送从结构转换而来的字节,如下所示:
public struct DISPATCH_MESSAGE
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] federation_name; // Units: nil Range: nil
}
因此,当我获取字节时,我需要取出其中的 char[] , 并从 char[] 中取出字符串。
我对这种非托管编码很陌生。
I need to process the bytes[] when I get from external application. The external application is also done in C# and they send the bytes thru UDP. They are sending the bytes converted from struct which is stated below :
public struct DISPATCH_MESSAGE
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] federation_name; // Units: nil Range: nil
}
So, when I get the bytes, I need to take out the char[] inside that,
and get the string out of that char[].
I am new to this kind of unmanaged coding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您应该将其声明为
ByValTStr
(根据字符串的性质,它可能会有所不同):更新: 如果它已经给出了
char[]
,它可能正确地进行了必要的转换(包括处理编码),所以我认为您只需要:Probably you should declare it as
ByValTStr
(depending on the nature of the string, it might be different):UPDATE: If it's already giving out a
char[]
, it's probably doing the necessary conversion (includes handling encoding) correctly, so I think you'd just need: