简体   繁体   English

通过 UDP 连接读取 C# 中的 C++ 编组数据

[英]Reading this C++ Marshalled Data in C# over UDP connection

This structured data is being sent over a UDP connection.此结构化数据通过 UDP 连接发送。 I can can read the UDP packets but I am having trouble with parcing the data that comes from this C++ Struct.我可以读取 UDP 数据包,但我无法解析来自这个 C++ 结构的数据。

here is the Struct:这是结构:

struct scanner_status{
    // ---
    u_short UDP_STATUS;
    u_short TCP_COMMAND;
    u_short TCP_LOGGING;
    // scanner attributes
    EPlatformModelNumber ModelNo;
    string SerialNo;
    bool bHasCT;
    bool bHasPET;
    bool bHasSPECT;
    // physical
    EBedType_mMM _eCurrBedType;
    int_16 _CurrPallet;
    bool _bTunnelPresent;
    //
    scanner_status()
      : UDP_STATUS(u_short(~0))
      , TCP_COMMAND(u_short(~0))
      , TCP_LOGGING(u_short(~0))
      , ModelNo(eUnknownPlatformModelNumber)
      , bHasCT(false)
      , bHasPET(false)
      , bHasSPECT(false)
      , _eCurrBedType(emMM_BedType_Unknown)
      , _CurrPallet(-1)
      , _bTunnelPresent(false)
    { }
};
decl_marshalling(scanner_status);

i would like to get help in reading this in C# and I am not really familiar with C++ and with marshalling, ao any assistance is greatly appreciated.我想在 C# 阅读这篇文章时获得帮助,我对 C++ 和编组并不十分熟悉,非常感谢任何帮助。

You need to create a similar structure in c# with equivalent .net types.您需要在 c# 中使用等效的 .net 类型创建类似的结构。 You also need to decorate your c# structure as follows.您还需要按如下方式装饰 c# 结构。 Makes sure that the order is the same as in the c++ structure as you need to make sure that the memory layout is the same.确保顺序与 c++ 结构中的顺序相同,因为您需要确保 memory 布局相同。

[StructLayout(LayoutKind.Sequential)]
struct scanner_status{
// ---
[MarshalAs(UnmanagedType.U1)]
u_short UDP_STATUS;
[MarshalAs(UnmanagedType.U1)]
u_short TCP_COMMAND;
[MarshalAs(UnmanagedType.U1)]
u_short TCP_LOGGING;

Here is some help from Microsoft.这是 Microsoft 的一些帮助。 Link关联

Let me know if you have still trouble.如果您还有问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM