1

我正在将我的程序移植到 C++,但我遇到了一些麻烦。我在 C# 中有这个代码创建一个 byte[] :

writer.New(0x484700C0);
    {
        writer += Functions.PadString("PTEmu", 0x10);
        writer += new byte[] { 0xC9, 0x32, 0xE0, 0x4D, 0xEB, 0x01 };
        writer.SkipBytes(6);
        writer += 1;
        writer += channels.Count; // sub count


        for (int i = 0; i < channels.Count; i++)
        {
            writer += Functions.PadString(channels[i].Item4, 0x20); // len = 0x20
            writer += Functions.PadString(channels[i].Item2, 0x14);
            writer += Functions.PadString(channels[i].Item2, 0x14);
            writer += Functions.PadString(channels[i].Item2, 0x14);


            writer += channels[i].Item3; // int, size = 4
            writer += channels[i].Item3;
            writer += channels[i].Item3;
            writer += 0;
        }


        writer += Functions.PadString("Clan", 32);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += 80;
        writer += 80;
        writer += 80;
        writer.SkipBytes(12);
    }

我想过做类似的事情:

struct PT_ServerList{
    int size;
    int opcode;
    char server[0x10];
    char bla[6];
    int reserved1;
    short reserved2;
    int unknow;
    int serverCount;
    ServerInfo servers[];
};

但是在服务器 [] 之后,我需要添加更多变量,但是编译器不接受该结构。我能做些什么来解决这个问题?

4

2 回答 2

2

使用向量

struct PT_ServerList {
    ...
    std::vector<ServerInfo> servers;
    ...
};
于 2012-05-19T17:28:34.503 回答
1

您可能想要使用:

ServerInfo *servers;

(在这种情况下,您需要使用“new[]”分配数组并使用“delete[]”解除分配);或者:

std::vector<ServerInfo> servers;

(在这种情况下,如果您以前没有使用过,您应该查阅std::vector 文档以获取有关如何使用它的信息std::vector

于 2012-05-19T17:29:31.290 回答