您好我想將C/C++ Strcut轉換爲C#以及如何用C#中另一個結構的地址填充結構成員?如何在C#中對NDISUIO_QUERY_OID結構進行編組以執行以下任務
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);
獲取IoctlResult成功在C++;
我的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?
任何建議或指導方針,將有助於.. :)
謝謝:)
[C/C++結構轉換爲C#.Net CF WinCE]的可能重複(http://stackoverflow.com/questions/22560958/conversion-of-cc-struct-to-c-net-cf-wince ) –
@HansPassant 喜先生.. 結構轉換沒有問題 在這裏,我要在C#中工作的C++ 我添加的代碼在C++中執行任務。 – arya2arya
@HansPassant 在C++中,我使用memcpy將一個結構複製到另一個結構的Data成員中。 我在C#中嘗試的同樣的任務 – arya2arya