任何人都可以帮我翻译这个结构到delphi记录吗?

时间:2014-07-01 15:08:24

标签: c++ delphi struct

我需要帮助将具有另一个嵌套结构的结构转录到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值。似乎大小不正确,因为字符串被截断并且总是丢失名称的第一个字符。

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

根据雷米的建议,我这样做:

 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;

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

1 个答案:

答案 0 :(得分:3)

{$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;