问题描述
所以,正如标题所说,我正在努力翻译一些调用DLL的函数,我在C中有文档,但是我需要在Delphi中使用它。经过少量初始尝试,并在翻译后解决了一些编译器错误,借助于你们(),其中一些仍然不起作用。主要是变量作为参数的问题...
首先是主题:我买了一个由一些捷克公司制造的USB逻辑分析仪和示波器,和有限的软件,但是有一个关于如何在我们自己的应用程序中实现它的DLL和(差的)文档,以便与分析器通信。
现在,这个文档有几个函数,用C编写(代码段落1) 。我已将它们全部翻译成Delphi(代码段2),但只有大约一半工作。
我需要一些帮助才能确定问题的位置站在:
- 是否在我的功能翻译到Delphi,或者是
其他地方? - 甚至可以通过Delphi完成吗?
- 文档太差,无法在Delphi中执行,
还是足够了?
注意:所有功能和所有数据通信使用原始软件正常工作!因此,我相信可以安全地假设一切不起作用的是DLL或者我的Delphi代码,而不是设备本身的问题。
int SetAnalogChannels(int CH1Gain,int CH2Gain,bool CH1Couple,bool
CH2Couple,bool CH1Multiplier,bool CH2Multiplier)
在这里,发送int CH1Gain和int CH2Gain时遇到麻烦。设备总是返回0到功能,这意味着增益参数无效。没有一个线索为什么...
函数SetAnalogChannels(CH1Gain,CH2Gain:integer; CH1Couple,CH2Couple,
CH1Multiplier,CH2Multiplier:boolean):integer;
stdcall;外部'E_l80.dll';
long RetrieveDSOData(unsigned char whatchannels,double * DSOCH1,
double * DSOCH2,unsigned short * LADATA,unsigned char Nth_Sample)
这里,我不知道这些类型应该是什么,如果他们真的是数组的指针(尽管没有[]标记
编辑:我只是弄清了我的错误和浅浅的阅读文档。该函数的返回
给出了数组的确切大小!
function RetrieveDSOData(whatchannels:uchar; var DSOCH1,DSOCH2:TLAArray;
var LADATA:array of ushort; Nth_Sample:uchar):
longint; STDCALL;外部'E_l80.dll';
float SetUPPS1(float Voltage)
这里很奇怪。看起来我正确设置它,但是函数返回始终为0,尽管它应该是其他一些值。我使用扩展的浮动类型...我可以看到我正确设置,因为另一个功能给我当前的状态,这个变化与这个设置...
函数SetUPPS1(电压:扩展):扩展; STDCALL;外部'E_l80.dll';
long SetupAWG(double * AnalogVoltage,short * DigitalData,long
BufferSize,double DCOffsetVoltage,double SampleFrequency,long Use4xGain,double
OutputImpedance,long Repeat,long Triggered,long OverrideOtherClocks )
这里与数组相同的问题。但是,根据使用情况,我相信这个数组是要发送的数组,不是通过参数接收的,所以这里的var可能是错的?
函数SetupAWG(var AnalogVoltage:double; var DigitalData:short; BufferSize:longint;
DCOffsetVoltage,SampleFrequency:double; Use4xGain:longint;
OutputImpedance:double; Repeat_,Triggered: longint;
OverrideOtherClocks:longint):longint; STDCALL;外部'E_l80.dll';
现在,您可能会看到,我将类型更改为Delphi等效项,至少在我知道我做的正确的方式。
如果有人知道任何这些事情的答案,或看到任何错误,我很高兴看到在评论/答案
如果我定义了这个错误,请指出正确的方向,并给我一个提示,而不是对我的错误的评论。他们有些不帮助...:/
非常感谢。
查看DLL的实际 .h
文件而不是文档会更有用,因为文档往往不提及调用约定实际上是什么(和这种情况也不例外)。您正在将您的Delphi函数声明为 stdcall
,
Edit: I just figured out my mistake and shallow reading of the docs. The return
of the function gives the exact size of the arrays!
function RetrieveDSOData(whatchannels: uchar; var DSOCH1, DSOCH2: TLAArray;
var LADATA: array of ushort; Nth_Sample: uchar):
longint; stdcall; external 'E_l80.dll';
float SetUPPS1(float Voltage)
Here, it's strange. It seems like I set it correctly, however the function return is always 0, although it should be some other value. I used "extended" for the float type... I can see I set it correctly since another function gives me the current status, which changes with this setting...
function SetUPPS1(Voltage: extended): extended; stdcall; external 'E_l80.dll';
long SetupAWG(double *AnalogVoltage, short *DigitalData, long
BufferSize, double DCOffsetVoltage, double SampleFrequency, long Use4xGain, double
OutputImpedance, long Repeat,long Triggered, long OverrideOtherClocks)
Here, same issue as above with arrays. However, based on the usage, I believe this arrays are the one to be sent, not received via parameter, therefore the "var" here might be wrong?
function SetupAWG(var AnalogVoltage: double; var DigitalData: short; BufferSize: longint;
DCOffsetVoltage, SampleFrequency: double; Use4xGain: longint;
OutputImpedance: double; Repeat_, Triggered: longint;
OverrideOtherClocks: longint): longint; stdcall; external 'E_l80.dll';
Now, as you might see, I changed the types to Delphi equivalent, at least as far as I know I did it the right way.
If anyone knows answer to any of these things, or sees any mistake, I'd be glad to see that in comments/answers.
If I defined this bad, please point me to the right directions and give me a hint instead of comments about me being wrong. They kind of don't help... :/
Thanks a lot.
It would be more useful to see the DLL's actual .h
file instead of documentation, as documentation tends to not mention what the calling conventions actually are (and this case is no exception). You are declaring your Delphi functions as stdcall
, which may or may not be correct. cdecl
is another commonly used calling convention in DLLs, especially since it is C's default calling convention. So, if the functions are not declared with ANY explicit calling convention in the .h
file, assume cdecl
instead of stdcall
. For this discussion, I will assume cdecl
unless shown otherwise.
function SetAnalogChannels(CH1Gain, CH2Gain: Integer; CH1Couple, CH2Couple, CH1Multiplier, CH2Multiplier: Boolean): Integer; cdecl; external 'E_l80.dll';
As the documentation says, these are pointers to arrays. I would translate them as-is, don't try to convert them to any fancy Delphi syntax:
function RetrieveDSOData(whatchannels: Byte; DSOCH1, DSOCH2: PDouble; LADATA: PWord; Nth_Sample: Byte): longint; cdecl; external 'E_l80.dll';
You converted this to Extended
, which is wrong. Delphi's Single
is equivalent to C's float
:
function SetUPPS1(Voltage: Single): Single; cdecl; external 'E_l80.dll';
Same as above for the arrays:
function SetupAWG(AnalogVoltage: PDouble; DigitalData: PShort; BufferSize: Longint; DCOffsetVoltage, SampleFrequency: Double; Use4xGain: Longint; OutputImpedance: Double; Repeat, Triggered, OverrideOtherClocks: Longint): longint; external 'E_l80.dll';
这篇关于将DLL调用函数从C转换为Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!