[英]Marshalling error on SNMP project with .net C#
I have to construct an SNMP project to communicate with various devices. 我必须构建一个SNMP项目以与各种设备进行通信。 Sadly I can't use any libraries other than the windows dlls for SNMP (mgmtapi.dll, snmpapi.dll, wsnmp32.dll).
遗憾的是,我无法使用Windows dll以外的任何其他SNMP库(mgmtapi.dll,snmpapi.dll,wsnmp32.dll)。 I have made the wrappers for those libraries and all the structures needed by the functions of thos libraries, but after the cycle:
我为这些库以及thos库的功能所需的所有结构做了包装,但是在循环之后:
When I want to see what's inside the SnmpVarBind inside the SnmpVarBindList using 当我想使用以下命令查看SnmpVarBindList中的SnmpVarBind内部内容时
Marshal.PtrToStringAnsi(varBind.value.asnValue.text.stream, (int)varBind.value.asnValue.text.length + 1)
I'm getting the error: Attempted to read or write protected memory. 我收到错误消息:尝试读取或写入受保护的内存。 This is often an indication that other memory is corrupt.
这通常表明其他内存已损坏。
The structures translated from the header snmp.h are: 从头文件snmp.h转换的结构是:
[StructLayout(LayoutKind.Sequential)]
public struct AsnOctetString
{
public uint length;
public int dynamic;
public IntPtr stream;
}
[StructLayout(LayoutKind.Sequential)]
public struct AsnObjectIdentifier
{
public uint idLength;
public IntPtr ids;
}
[StructLayout(LayoutKind.Explicit)]
public struct AsnUnion
{
[FieldOffset(0)]
public int number;
[FieldOffset(0)]
public uint unsigned32;
[FieldOffset(0)]
public ulong counter64;
[FieldOffset(0)]
public AsnOctetString text;
[FieldOffset(0)]
public AsnOctetString bits;
[FieldOffset(0)]
public AsnObjectIdentifier objectID;
[FieldOffset(0)]
public AsnOctetString sequence;
[FieldOffset(0)]
public AsnOctetString address;
[FieldOffset(0)]
public uint counter;
[FieldOffset(0)]
public uint gauge;
[FieldOffset(0)]
public uint ticks;
[FieldOffset(0)]
public AsnOctetString arbitrary;
}
[StructLayout(LayoutKind.Sequential)]
public struct AsnAny
{
public byte asnType;
public AsnUnion asnValue;
}
[StructLayout(LayoutKind.Sequential)]
public struct SnmpVarBind
{
public AsnObjectIdentifier name;
public AsnAny value;
}
[StructLayout(LayoutKind.Sequential)]
public struct SnmpVarBindList
{
public IntPtr list;
public uint len;
}
Any thoughts? 有什么想法吗? I can put the code that you want/need.
我可以输入您想要/需要的代码。
I know this answer is very late, but I've recently run into marshaling problems with the SNMP API. 我知道这个答案很晚,但是最近我遇到了SNMP API编组问题。 I had assumed that the structures are packed on byte boundaries, but that is wrong.
我以为结构打包在字节边界上,但这是错误的。 If you look at
Snmp.h
, you will see #include <pshpack4.h>
which turns on packing on 4 byte boundaries. 如果查看
Snmp.h
,则会看到#include <pshpack4.h>
,它将打开4字节边界上的打包。 This means that the field offsets are different. 这意味着场偏移是不同的。 This is how my
AsnAny
looks like: 这就是我的
AsnAny
样子:
[StructLayout(LayoutKind.Explicit)]
private struct AsnAny
{
[FieldOffset(0)]
public readonly byte Type;
[FieldOffset(4)]
public readonly Int32 Number;
[FieldOffset(4)]
public readonly UInt32 Unsigned32;
[FieldOffset(4)]
public readonly UInt64 Counter64;
[FieldOffset(4)]
public readonly AsnOctetString String;
[FieldOffset(4)]
public readonly AsnOctetString Bits;
[FieldOffset(4)]
public readonly AsnObjectIdentifier Object;
[FieldOffset(4)]
public readonly AsnOctetString Sequence;
[FieldOffset(4)]
public readonly AsnOctetString IPAddress;
[FieldOffset(4)]
public readonly UInt32 Counter32;
[FieldOffset(4)]
public readonly UInt32 Gauge32;
[FieldOffset(4)]
public readonly UInt32 Ticks;
[FieldOffset(4)]
public readonly AsnOctetString Arbitrary;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.