如何将结构复制到另一个 [英] how to copy structure to another one

查看:119
本文介绍了如何将结构复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是C ++的新手,有人可以告诉我如何将结构复制到另一个结构(二进制)吗?

结构看起来像这样:

Hi,

I am quite new to C++.Could anyone tell me how to copy structure to another structure (binary)?

structure looks like this:

typedef unsigned char DES_cblock[8];
#ifndef DES_LONG
#define DES_LONG unsigned long
#endif

typedef struct DES_ks
    {
    union
    {
    DES_cblock cblock;
    /* make sure things are correct size on machines with
     * 8 byte longs */
    DES_LONG deslong[2];
    } ks[16];
    } DES_key_schedule;



类中的声明:



declaration in class:

DES_key_schedule ks1;
DES_key_schedule ks2;



现在,我需要将两个结构都复制到另一个结构,然后再传递给VB.net(非托管代码).

使用简单的方法Assignem可以工作,但是在vb中返回错误"System.ExecutionEngineException":



Now I need to copy both structure to another structure which is later passed to VB.net (unmanaged code).

Using simple method assignem works but returns an error ''System.ExecutionEngineException'' in vb:

void cWrapper::ScheduleKeyRun(myStruct *cd)
{
    memcpy(desKey , cd->desKey, 16);
    //do things
    int sz = sizeof(DES_key_schedule);
    DES_key_schedule mks1=cd->ks1;
    cd->ks1 = ks1;
    cd->ks2 = ks2;
}



谁能告诉我是否将结构移植到了VB.NET



Could anyone check tell me if transfared structure corectly to VB.NET

<StructLayout(LayoutKind.Sequential)> _
Structure DES_key_schedule
    Dim block As DES_cblock
    <MarshalAs(UnmanagedType.I2)> Dim dlong As Short
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure DES_cblock
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Dim cblock() As Byte
End Structure


或在C#中将是:


or in c# would be:

[StructLayout(LayoutKind.Sequential)]
struct DES_key_schedule
{
    public DES_cblock block;
    [MarshalAs(UnmanagedType.I2)]
    public short dlong;
}
[StructLayout(LayoutKind.Sequential)]
struct DES_cblock
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] cblock;
}

推荐答案

有了结构,您就可以进行内存复制.
请注意,两个结构必须具有相同的布局.
With a structure, you can just do a memory copy.
Be aware that both structures must have the same layout.
memcpy(&cd->ks1, &ks1, sizeof(cd->ks1));
memcpy(&cd->ks2, &ks2, sizeof(cd->ks2));




如果您对memcpy不满意,可以做一些稍微复杂的事情.




If you''re not happy with the memcpy, you can do something slightly more complicated.

typedef struct DES_ks
{
    union
    {
        DES_cblock cblock;
        /* make sure things are correct size on machines with
         * 8 byte longs */
        DES_LONG deslong[2];
    }   ks[16];


public:
    const DES_ks & operator = (const DES_ks & rhs);

public:
    DES_ks(void)    { /* your choice to put in initialization 
                         The original struct didn't have it.*/ }
    DES_ks(const DES_ks & rhs);

} DES_key_schedule;

inline const DES_ks & DES_ks::operator = (const DES_ks & rhs)
{
    for (size_t Index = 0; Index < (sizeof(ks) / sizeof(ks[0])); Index++)
    {
        ks[Index].deslong[0] = rhs.ks[Index].deslong[0];
        ks[Index].deslong[1] = rhs.ks[Index].deslong[1];
    }

    return *this;
}

inline DES_ks::DES_ks(const DES_ks & rhs)
{
    *this = rhs;
}



通过运算符重载,您可以使用:



With the operator overloading you can use:

cd->ks1 = ks1;
cd->ks2 = ks2;


这只是一个有根据的猜测,因为我不熟悉编组,但这是:
This is just an educated guess, since I''m not familiar with marshalling, but this:
[StructLayout(LayoutKind.Sequential)]
struct DES_key_schedule
{
    public DES_cblock block;
    [MarshalAs(UnmanagedType.I2)]
    public short dlong;
}


...看起来像一个对我有两个成员的结构,这不是您在C ++中声明的内容.

咨询 http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.layoutkind.aspx [ ^ ]我假设联合的定义应如下所示:


...looks like a struct with two members to me, and that is not what you declared in C++.

After consulting http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.layoutkind.aspx[^] I would assume a definition of a union should look like that:

[StructLayout(LayoutKind.Explicit)]
struct DES_key_schedule
{
    [FieldOffset(0)] public DES_cblock block;
    [FieldOffset(0)] public int dlong[2];
}


经过大量测试,我决定尝试通过二进制数组进行存储(我只需要使用此数组即可重复使用).使用memcpy函数可以正常工作!

尝试了其他解决方案,但是VB.NET不允许我使用结构的零字段偏移来编译项目.在其他情况下,修改结构在C ++中不起作用(正在破坏内存),因为我想这太复杂了,所以我会坚持我的解决方案.

感谢您的帮助
After lots of testing I decided to try to pass binary array for storing (I need only this for reuse only). Using memcpy function this worked!

Tried other solutions, but VB.NET does not let me compile the project with zero field ofsets in structure. In other cases modifying structure did not worked in C++ (was corrupting memory) because I guess this is too complex, so I am staying with my solution.

Thanks for help


这篇关于如何将结构复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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