将C ++ Union Struct转换为VB6

时间:2012-12-02 19:00:14

标签: c++ vb6 struct unions

我的C ++应用程序中有这个结构:

struct textField
{
        //0
        union nameField
        {
                void* ptr;
                char cstring[16];
        } text;
        //16
        uint8_t textLength;
        //17
        char unknown1[3];
        //20
        uint8_t fieldType;
        //21
        char unknown2[3];
        //24
        uint32_t unknown3;
        //28
};

我知道在VB6中,它看起来像这样:

Private Type textField        ' 0
    cstring(0 To 15) As Byte  ' 16
    textLength       As Byte  ' 17
    unknown1(0 To 2) As Byte  ' 20
    fieldType        As Byte  ' 21
    unknown2(0 To 2) As Byte  ' 24
    unknown3         As Long  ' 28
End Type

但结构中的联合呢?如何实现?

2 个答案:

答案 0 :(得分:0)

最简单的方法是使用变体类型 - 这可以包含任何其他类型。

请注意,可能没有void *直接映射到VB中,因此您需要研究如何使用联合而不仅仅是快速转换。

答案 1 :(得分:0)

使用LSET命令。

它没有声明与结构符合,而是在声明对象的实例后声明。

Example

Private Type MyType1
    x as Integer
    y as Integer
    z as Integer
    w as Integer
End Type

Private Type MyType2
    a as Long
    b as Long
End Type

Private Sub Form_Load()
    Dim t1 as MyType1
    Dim t2 as MyType2
    t1.x = 0
    t1.y = 0
    t1.z = -1
    t1.w = -1

    LSet t2 = t1
    ....

这与两种类型之间的未命名联合具有相同的效果。