gpt4 book ai didi

delphi - 使用例程的结构和参数的另一个问题

转载 作者:行者123 更新时间:2023-12-03 18:18:39 25 4
gpt4 key购买 nike

之前我有 problems in converting C/C++ structs to Delphi records并得到帮助。幸运的是,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
};

在 Delphi 中,我成功地使用了这段代码来访问成员值:

{$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
Type
PstChannel = ^stChannel;
stChannel = record
ChannelNumber: Integer; // Número de canais no buffer
ChannelInfo: PstChannelInfo; // Buffer com informações dos canais
end;

var
DadosCanais: stChannel;

但是现在我遇到了另一个问题,就是使用这个DLL函数:

char GatherData ( const struct stChannel channelBuffer, int blockIndex);

DLL的使用说明如下:

char GatherData ( const struct stChannel channelBuffer, int blockIndex)

Gets the equipment data from the data base, in the interval specified in the last call of "LookForAvailableChannels".

Version: 1.00

Parameters:
channelBuffer inform in the structure which are the desired channels.
blockIndex inform the desired data block index.

Returns:
char Returns "0" in case of error and "1" if everything is OK.

通过调用上一行中完成的 LookForAvailableChannels() 例程来填充该结构。我正常看到DadosCanais中的值。

所以,我在 Delphi 中定义了 GatherData:

function GatherData(ChannelBuffer : stChannel ; blockindex:integer):char ; stdcall; external 'Reader.dll';

然后像这样使用:

GatherData(Dadoscanais,0)

并且...该函数仅返回 '0',表明该函数不起作用。我现在不知道该怎么办。

有人可以向我解释在 Delphi 中应该做什么才能使用此功能吗?

好心的人也可以给我一些关于这个主题的学习 Material ,这样我就可以更好地理解发生了什么

编辑1
我按照 Remy 所说更改了我的定义,现在我收到“0”作为返回。
查看 Borland 6 C++ Builder 中的源代码,我看到了这段代码:

iBlock = 1;
if(this->deviceInterface->GatherData(*this->stChannels, iBlock)){

我是这样写的:

iBlock:=1;
if ( GatherData(DadosCanais,1) = 1 ) then

解决方案

我研究了更多,发现除了类型转换的问题之外,dll 是有问题的。所以雷米的回答是对的。

最佳答案

您对 GatherData() 的声明是错误的。改用这个:

function GatherData(const channelBuffer: stChannel; blockIndex: Integer): AnsiChar; stdcall; external 'Reader.dll';

或者:

function GatherData(const channelBuffer: stChannel; blockIndex: Integer): AnsiChar; cdecl; external 'Reader.dll';

因为在 DLL 的描述中并不清楚 DLL 实际使用的调用约定。我怀疑 cdecl

关于delphi - 使用例程的结构和参数的另一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24521271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com