有人可以帮助我将此结构转换为delphi记录吗? [英] Can anyone help me with translation of this struct to delphi record?
问题描述
我需要帮助将具有另一个嵌套结构的结构转录到Delphi。下面是一个结构:
I need help to transcribe a struct that has another nested struct to Delphi. Below is a struct:
#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的值:
In Borland C + + 6, the example uses the following code to read the value of 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
}
我希望我可以在德尔福我应该如何转录结构?
I wish I could do the same in Delphi. How should I transcribe structs?
非常抱歉,因为英语不是我的母语
Thanks and sorry because English is not my native language
编辑
我没有发布我在Delphi上所做的事情是错误的。跟随我的尝试:
I was wrong not to post what I had done on Delphi. Follow my attempt:
// 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值。似乎大小不正确,因为字符串被截断并且总是丢失名称的第一个字符。
I get the record, but I can not correctly read ChannelTag values. It seems that the size is incorrect, because the strings is truncated and always lose the first character of the name.
也许这可以澄清一些问题。再次感谢
Maybe this clarify a little the question. Thanks again
解决方案
根据雷米的建议,我这样做:
Following advice from Remy , i do this :
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;
目前,这可以解决我的问题。谢谢大家。
For now this resolves my problem. Thanks all.
推荐答案
{$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;
这篇关于有人可以帮助我将此结构转换为delphi记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!