简体   繁体   English

如何在 C++ 中制作与 Delphi 中类似的结构

[英]How to make similar structure in C++ to this in Delphi

My program is receiving messages by WM_COPYDATA containing a pointer to structure which looks in Delphi like this:我的程序通过WM_COPYDATA接收消息,其中包含一个指向在 Delphi 中看起来像这样的结构的指针:

type
  arr_geo_t = array[0..5000] of packed record
    nr : string[15];
    x,y,h : double;
    k : string[10];
  end;
  parr_geo_t = ^arr_geo_t;

The question is, how to make a similar one in C++?问题是,如何在 C++ 中制作一个类似的?

When I define something like this:当我定义这样的东西时:

typedef struct
{
    char nr[5000][15];
    double x[5000];
    double y[5000];
    double h[5000];
    char k[5000][10];
} arr_geo_t, *parr_geo_t;

and try to retrieve data, I get nothing.并尝试检索数据,我一无所获。

In the Delphi code, arr_geo_t is an array of records of anonymous type, not a record of arrays, like you declared in your C++ code.在 Delphi 代码中, arr_geo_t是匿名类型的记录数组,而不是数组的记录,就像您在 C++ 代码中声明的那样。 The correct C++ translation of the Delphi declarations would look more like this: Delphi 声明的正确 C++ 翻译看起来更像这样:

#include <pshpack1.h> // #pragma pack(push, 1), etc
typedef struct
{
    char nr[16];
    double x,y,h;
    char k[11];
} arr_geo_t[5001];
#include <poppack.h> // #pragma pack(pop), etc

typedef arr_geo_t *parr_geo_t;

Note that Delphi array ranges are inclusive, so 0..5000 denotes 5001 elements indexed from index 0 to index 5000, inclusive.请注意,Delphi 数组范围是包括在内的,因此0..5000表示从索引 0 到索引 5000(包括端点)索引的 5001 个元素。

Also note that the char[] arrays have 1 more char in them that you are not taking into account.另请注意, char[]数组中还有 1 个您没有考虑的char That is because in Delphi, a string[N] is a ShortString , an AnsiChar -based fixed length string type where N is the maximum number of AnsiChar s allowed, and is prefixed by 1 AnsiChar specifying how many actual AnsiChar s are in the string.这是因为在 Delphi 中, string[N]ShortString ,一种基于AnsiChar的固定长度字符串类型,其中N是允许的AnsiChar最大数量,并以 1 AnsiChar为前缀,指定字符串中有多少实际AnsiChar . The string length is at index 0, and the character data begins at index 1.字符串长度在索引 0 处,字符数据从索引 1 开始。

If you are translating the Delphi code to C++Builder specifically, you can use the SmallString template instead of char[] arrays:如果您要专门将 Delphi 代码转换为 C++Builder,则可以使用SmallString模板而不是char[]数组:

#include <System.hpp>

#include <pshpack1.h> // #pragma pack(push, 1), etc
typedef struct
{
    System::SmallString<15> nr;
    double x,y,h;
    System::SmallString<10> k;
} arr_geo_t[5001];
#include <poppack.h> // #pragma pack(pop), etc

typedef arr_geo_t *parr_geo_t;

If you translate to C++11 or later, you can clean up the typedef s via using statements instead:如果您转换为 C++11 或更高版本,则可以通过using语句来清理typedef s:

#include <array>
//#include <System.hpp>

#include <pshpack1.h> // #pragma pack(push, 1), etc
struct arr_geo_data_t
{
    std::array<char, 16> nr; // or: System::SmallString<15> nr;
    double x,y,h;
    std::array<char, 11> k; // or: System::SmallString<10> k;
};
#include <poppack.h> // #pragma pack(pop), etc

using arr_geo_t = std::array<arr_geo_data_t, 5001>;
using parr_geo_t = arr_geo_t*;

There is an ordering issue here.这里有一个排序问题。 You have an array of a structure, not a number of arrays of data members.您有一个结构数组,而不是多个数据成员数组。

I think you may find that the following is a closer match, but I'm sorry delphi internal types are no longer important to me!我想你可能会发现以下是更接近的匹配,但很抱歉,delphi 内部类型对我来说不再重要!

struct arr_geo_t 
{
  struct record 
  {
    char nr[15];
    double x,y,h;
    char k[10];
  } array[5001];

};

Note also that I don't know how well packed record works in delphi.另请注意,我不知道在 delphi 中打包记录的效果如何。 c/c++ will ensure that the doubles are aligned on 8 byte boundaries, effectively expanding nr to 16 characters, for example.例如,c/c++ 将确保双精度数在 8 字节边界上对齐,从而有效地将 nr 扩展到 16 个字符。 The whole structure will also be aligned to 8 byte boundaries, so k will have an effective size of 16 as well.整个结构也将与 8 字节边界对齐,因此 k 的有效大小也将为 16。 On some computers, and with some compiler settings, doubles simply cannot be accessed from unaligned addresses.在某些计算机上,使用某些编译器设置,根本无法从未对齐的地址访问双精度值。

That is assuming delphi strings are char based and not unicode based.那是假设 delphi 字符串是基于字符而不是基于 unicode 的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM