嗨,我正在尝试将 C/C++ Strcut 转换为 C#,以及如何用 C# 中另一个结构的地址填充结构成员?
C/C++ 结构看起来像:
typedef struct _NDISUIO_QUERY_OID
{
NDIS_OID Oid;
PTCHAR ptcDeviceName;
UCHAR Data[sizeof(ULONG)];
} NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;
typedef struct My_Struct
{
//leT's have 2 variables...
UINT a;
UINT b;
//sTRUCT may have many no.of variables depending upon the requirement
}My_STATS, *PMy_STATS;
PNDISUIO_QUERY_OID pQueryOid = NULL;
pQueryOid = (PNDISUIO_QUERY_OID)malloc(sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS)) ;
PMy_STATS Statistics;
pQueryOid->Oid = ulOIDCode;//Required OID
pQueryOid->ptcDeviceName = AUB_NAME;//REquired STRING
memcpy(pQueryOid->Data, Statistics, sizeof(My_STATS));
IoctlResult = DeviceIoControl(
handle,
IOCTL_NDISUIO_QUERY_OID_VALUE,
pQueryOid,
sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
pQueryOid,
sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
&dwReturnedBytes,
NULL);
在 C++ 中获得 IoctlResult 成功;
我的 C# 结构是:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct _NDISUIO_QUERY_OID
{
public uint Oid;
[MarshalAs(UnmanagedType.LPWStr)]
public string ptcDeviceName;
[MarshalAs(UnmanagedType.ByValArray,SizeConst = sizeof(uint))]
public byte[] Data;
};
My_STATS Sta_Conn_Info = new My_STATS();
_NDISUIO_QUERY_OID QueryOid = new _NDISUIO_QUERY_OID();
QueryOid.Oid = ulOIDCode; // required OID
QueryOid.ptcDeviceName = STRING;//Required String
//Imported coredll.dll with required prototype for this.
IoctlResult = DeviceIoControl(
handle,
IOCTL_NDISUIO_QUERY_OID_VALUE,
ref QueryOid,
sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
ref QueryOid,
sizeof(NDISUIO_QUERY_OID)+ sizeof(My_STATS),
ref dwReturnedBytes,
NULL
);
问题:如何编组 NDISUIO_QUERY 结构以将另一个结构复制到其 C# 中的数据成员?以及如何像在 C++ 中一样替换 memcpy?
任何建议或指南都会有所帮助.. :)
谢谢 :)