如何做到的memcpy在C#.NET CF与以下任务 [英] How to do memcpy in C# .Net CF with the following task

查看:156
本文介绍了如何做到的memcpy在C#.NET CF与以下任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想转换成C / C ++ Strcut到C#和如何填写结构成员与其他结构的地址,在C#?

Hi I am trying to convert the C/C++ Strcut to C# and how to fill the structure member with address of another structure in C#?

C / C ++结构体是这样的:

C/C++ Struct looks like:

         typedef struct _NDISUIO_QUERY_OID
         {
           NDIS_OID        Oid;
           PTCHAR          ptcDeviceName;  
           UCHAR           Data[sizeof(ULONG)];
         } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID;

         typedef struct My_Struct
         {
           //les have 2 variables...  
            UINT    a;
            UINT    b;
         }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));

我的C#结构是:

My C# Struct is:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    public struct _NDISUIO_QUERY_OID
    {
        public uint        Oid;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string          ptcDeviceName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = sizeof(uint))]
        public string Data;
    };

问题:如何统计结构复制到数据阵列在C#?

Problem: How to copy the Statistics structure to Data array in C#??

感谢:)

推荐答案

下面是我的实现(仅供参考,自卫队包含了所有的这code和更多)

Here's my implementation (FYI, the SDF contains all of this code and a lot more)

internal class NDISQueryOid
{
    protected const int NDISUIO_QUERY_OID_SIZE = 12;

    protected byte[] m_data;
    public int Size { get; private set; }

    public NDISQueryOid(byte[] data)
    {
        int extrasize = data.Length;
        Size = 8 + extrasize;
        m_data = new byte[Size];
        Buffer.BlockCopy(data, 0, m_data, DataOffset, data.Length);
    }

    public NDISQueryOid(int extrasize)
    {
       Size = NDISUIO_QUERY_OID_SIZE + extrasize;
        m_data = new byte[Size];
    }

    protected const int OidOffset = 0;
    public uint Oid
    {
        get { return BitConverter.ToUInt32(m_data, OidOffset); }
        set
        {
            byte[] bytes = BitConverter.GetBytes(value);
            Buffer.BlockCopy(bytes, 0, m_data, OidOffset, 4);
        }
    }

    protected const int ptcDeviceNameOffset = OidOffset + 4;
    public unsafe byte* ptcDeviceName
    {
        get
        {
            return (byte*)BitConverter.ToUInt32(m_data, ptcDeviceNameOffset);
        }
        set
        {
            byte[] bytes = BitConverter.GetBytes((UInt32)value);
            Buffer.BlockCopy(bytes, 0, m_data, ptcDeviceNameOffset, 4);
        }
    }

    protected const int DataOffset = ptcDeviceNameOffset + 4;
    public byte[] Data
    {
        get
        {
            byte[] b = new byte[Size - DataOffset];
            Array.Copy(m_data, DataOffset, b, 0, Size - DataOffset);
            return b;
        }
        set
        {
            Size = 8 + value.Length;
            m_data = new byte[Size];
            Buffer.BlockCopy(value, 0, m_data, DataOffset, value.Length);
        }
    }

    public byte[] getBytes()
    {
        return m_data;
    }

    public static implicit operator byte[](NDISQueryOid qoid)
    {
        return qoid.m_data;
    }
}

请注意,在我的使用中,NDIS IOCT接受一个指针(我的大部分编程工作全部完成为不安全的),所以你必须做一些调整那里。

Note that in my usage, the NDIS IOCT takes in a pointer (most of my NDIS work is all done as unsafe) so you'd have to do some adjustment there.

所以,举个例子,你查询BSSID,我知道BSSID的数据是36个字节,所以我想创造的东西是这样的:

So if, for example, you're querying the BSSID, I know the BSSID data is 36 bytes, so I'd create something like this:

var queryOID = new NDISQueryOid(36);

然后分配名称和调用NDIS(生产code的比这多很多检查):

then allocate the name and call NDIS (the production code has a lot more checking than this):

byte[] nameBytes = System.Text.Encoding.Unicode.GetBytes(adapterName + '\0');

fixed (byte* pName = &nameBytes[0])
{
    queryOID.ptcDeviceName = pName;
    queryOID.Oid = (uint)oid;

    var bytes = queryOID.getBytes();
    ndis.DeviceIoControl(IOCTL_NDISUIO_QUERY_OID_VALUE, bytes, bytes);

    var result = new byte[queryOID.Data.Length];
    Buffer.BlockCopy(queryOID.Data, 0, result, 0, result.Length);
}

修改

因此​​,结果成员的结果查询的字节数组。这意味着什么,以及如何跨preT这取决于什么OID你质疑了。例如,如果你查询当前连接的SSID(即 NDIS_OID.SSID ),则该回来为4个字节的长度后跟ASCII-CN codeD的名字,所以你破译它是这样的:

So the result member above is a byte array of the "result" of the query. What it means and how you interpret it depends on what the OID you queried was. For example, if you were querying the currently connected SSID (i.e. NDIS_OID.SSID), then that comes back as a 4-byte length followed by the ASCII-encoded name, so you'd decipher it like this:

int len = BitConverter.ToInt32(data, 0);
if (len > 0)
{
    ssid = System.Text.Encoding.ASCII.GetString(data, 4, len);
}

但同样,这是仅适用于一个特定的OID。你必须处理每一个案件的回报为你决定支持每一个进来的OID。

But again, this is only for one specific OID. You have to handle every return case for every incoming OID you decide to support.

这篇关于如何做到的memcpy在C#.NET CF与以下任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆