【问题标题】:Can anyone help me with translation of this struct to delphi record?谁能帮我把这个结构翻译成delphi记录?
【发布时间】:2014-07-01 15:08:24
【问题描述】:

我需要帮助将具有另一个嵌套结构的结构转录到 Delphi。下面是一个结构体:

#define CHANNEL_TAG_LENGTH 17
struct stChannelInfo
 {
  char ChannelTag[CHANNEL_TAG_LENGTH];  // Tag (máx 16 caracteres)
  char ChannelEnabled;  // Habilitado se diferente de "0"
 };

// Structure with information about channels
struct stChannel
 {
  int ChannelNumber;  // Número de canais no buffer
  struct stChannelInfo *ChannelInfo;  // Buffer com informações dos canais
 };

在Borland C++ 6中,示例使用如下代码读取ChannelTag的值:

 stChannels = this->deviceInterface->LookForAvailableChannels(EdDirectorySource->Text.c_str(), iSn, dateTimeStart, dateTimeEnd);
 for(int i = 0; i < stChannels.ChannelNumber; i++)
 {
  CLbChannels->Items->Add(stChannels.ChannelInfo[i].ChannelTag);  // Add to list the values found
 }

我希望我能在 Delphi 中做同样的事情。我应该如何转录结构?

感谢和抱歉,因为英语不是我的母语

编辑

我不发布我在 Delphi 上所做的事情是错误的。按照我的尝试:

// record who receive the values 
type stChannelInfo = record
    ChannelTag : string[16];    
    ChannelEnabled : char ; 
end;

type stChannel = record
    ChannelNumber:integer;  // Númber of buffer channels
    ChannelInfo : ^stChannelInfo ;
end;

所以我尝试阅读:

 Var DadosCanais : stChannel;  // defined in var section of procedure onclick Button.

 DadosCanais:=LookForAvailableChannels (Pwidechar(dirroot) , sn , datepickerinicial.DateTime,datepickerfinal.DateTime);
 for i := 0 to (DadosCanais.ChannelNumber-1) do  
  begin  
   Showmessage(String(DadosCanais.ChannelInfo^.ChannelTag));
   inc(DadosCanais.ChannelInfo);
  end;

我得到了记录,但是我无法正确读取ChannelTag的值。似乎大小不正确,因为字符串被截断,总是丢失名称的第一个字符。

也许这能澄清一点问题。再次感谢

解决方案

听从 Remy 的建议,我这样做了:

 sn:=strtoint(lstdirMaquinas.Items[lstdirMaquinas.Itemindex]);
 Dadoscanais := LookForAvailableChannels(PChar(dirroot) , sn , datepickerinicial.DateTime,datepickerfinal.DateTime);
 for i:=0 to DadosCanais.ChannelNumber-1 do
  begin
    ListboxChannel.Items.add(String(DadosCanais.ChannelInfo[i].ChannelTag));
  end;

现在这解决了我的问题。谢谢大家。

【问题讨论】:

  • 抱歉,本网站不适合其他人为您工作。您至少应该包括您尝试过的内容以及失败的地方。 en.wikipedia.org/wiki/Comparison_of_Pascal_and_C
  • 这实际上并不完全正确(请注意,第二个结构的第二部分是指向通道信息数组的指针!)
  • 您应该发布您尝试了什么以及遇到了哪些问题。
  • 我想首先你需要向我们展示一些你知道什么和不知道什么的证据。我们可以告诉您,您可能不知道如何在 Delphi 中声明记录或数组。这似乎很有可能,因为不可能有比这更简单的结构。
  • 对不起,我第一次在这里发帖。我现在将更新我的帖子以显示我所做的。

标签: c++ delphi struct


【解决方案1】:
{$POINTERMATH ON}

Type
  PstChannelInfo = ^stChannelInfo;
  stChannelInfo = record
    ChannelTag: array[0..CHANNEL_TAG_LENGTH-1] of AnsiChar;  // Tag (máx 16 caracteres)
    ChannelEnabled: AnsiChar;  // Habilitado se diferente de "0"
  end;

  // Structure with information about channels
  stChannel = record
    ChannelNumber: Integer;  // Número de canais no buffer
    ChannelInfo: PstChannelInfo;  // Buffer com informações dos canais
  end;

stChannels := Self.deviceInterface.LookForAvailableChannels(PChar(EdDirectorySource.Text), iSn, dateTimeStart, dateTimeEnd);
for i := 0 to stChannels.ChannelNumber-1 do begin
  CLbChannels.Items.Add(stChannels.ChannelInfo[i].ChannelTag);  // Add to list the values found
end;

或者:

Type
  PstChannelInfo = ^stChannelInfo;
  stChannelInfo = record
    ChannelTag: array[0..CHANNEL_TAG_LENGTH-1] of AnsiChar;  // Tag (máx 16 caracteres)
    ChannelEnabled: AnsiChar;  // Habilitado se diferente de "0"
  end;

  // Structure with information about channels
  stChannel = record
    ChannelNumber: Integer;  // Número de canais no buffer
    ChannelInfo: PstChannelInfo;  // Buffer com informações dos canais
  end;

  PstChannelInfoList = ^TstChannelInfoList;
  TstChannelInfoList = [0..(MaxInt div SizeOf(stChannelInfo))-1] of stChannelInfo;

stChannels := Self.deviceInterface.LookForAvailableChannels(PChar(EdDirectorySource.Text), iSn, dateTimeStart, dateTimeEnd);
for i := 0 to stChannels.ChannelNumber-1 do begin
  CLbChannels.Items.Add(PstChannelInfoList(stChannels.ChannelInfo)^[i].ChannelTag);  // Add to list the values found
end;

【讨论】:

  • 太棒了!像魅力一样工作......我会将我的代码发布给所有需要帮助的人。如果我犯了一些错误,请指出错误。我使用了第一个想法,因为第二个想法告诉我 TstChannelInfoList 有 2 GB 的大小并且无法编译。
  • TstChannelInfoList = array[0..(MaxInt div SizeOf(stChannelInfo))-1] of stChannelInfo;
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 2013-12-23
  • 2022-12-03
  • 2022-12-17
  • 1970-01-01
  • 2011-01-18
相关资源
最近更新 更多