开发者

memcpy in C# with different dataType

I am porting a c++ code into c#.There are 2 structures, as explained below. // Must be a total of 180 bytes

typedef struct MAINTENANCE_PARAM`
{
    BYTE bCommand;
    BYTE bSubCommand;
    USHORT usDataLength;
    union  // Must be no larger than 176 bytes
    {
        MyStruct1 myStruct1;
                MyStruct2 myStruct2;
        BYTE bDummy[176];
    } data;
} MAINTENANCE_PARAM;

typedef struct _OCI_PARAM {
    SHORT  sType;          // Size: 2
    LONG   lValue;         //  4
    SHORT  sScale;         //  2
    LONG   lValueTabIdx;   //  4
} OCI_PARAM[OCI_MAXPARAM];    //Size: OCI_MAXPARAM = 15  // 15 * 12 = 180

My code uses the memcpy in the following fashion.

MAINTENANCE_PARAM maintainanceParam;
    OCI_PARAM ociParam       

    // Copy recieved structure to correct format
    memcpy(开发者_C百科(void*)& maintainanceParam, (void*) ociParam, sizeof(OCI_PARAM));

As I know there is no code for memcpy in the C#. So how can I port the above code into the C#. I am new to C#. I don't know much regarding the C#. So can anybody tell me how exactly I can implement the above line of code in C#. I need to copy 180 bytes from one structure to another structure object with different datatype Thanks in Advance for any help. Regards, Ashish


Use the StructLayoutAttribute and FieldOffsetAttribute for your structs so that you can layout your fields explicitly, then import RtlMoveMemory (P/Invoke). You can modify the signature to take any pointer type (in unsafe context) or use the standard signature on PInvoke.net.

unsafe class Program {

    [System.Runtime.InteropServices.DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
    private static extern void MoveMemory(void* dest, void* src, int size);

    static  void Main(string[] args) {

        Maintenance_Param p1 = new Maintenance_Param();
        p1.bCommand = 2;
        p1.bSubCommand = 3;
        p1.usDataLength = 3;
        p1.myStruct1 = new MyStruct1();

        Maintenance_Param p2 = new Maintenance_Param();
        MoveMemory(&p2, &p1, sizeof(Maintenance_Param));
    }
}

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct Maintenance_Param {

    // fields should be private and be accessed over properties ...
    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte bCommand;
    [System.Runtime.InteropServices.FieldOffset(1)]
    public byte bSubCommand;
    [System.Runtime.InteropServices.FieldOffset(2)]
    public ushort usDataLength;
    [System.Runtime.InteropServices.FieldOffset(4)]
    public MyStruct1 myStruct1;
    [System.Runtime.InteropServices.FieldOffset(4)]
    public MyStruct2 myStruct2;
}

public struct MyStruct1 {
    int value;
}

public struct MyStruct2 {
    int value;
}


MemCpy DOES exist in .NET! Try googling for OpCodes.Cpblk. Here is an example: http://www.abstractpath.com/weblog/2009/04/memcpy-in-c.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜