某项目需要调用设备厂家提供的DLL的函数,厂家给了一个VB的例子,有个参数是ByRef pBuffer As Single。于是在Delphi中用buffer:array of single代替:
function func(buffer:array of single;count:integer):integer;far;stdcall;external 'func.dll';
调用后buffer确实返回了正确的数据,但奇怪的是函数返回后原来的一些局部变量的值都变了。如:
i:=1;
func(buffer,count); //func是DLL中的一个函数,buffer是array of single,count是读进buffer的数据量
这时i就变成一个随机的数了。程序还很不稳定,经常出"Access violation..."
搜:
从
连到:
http://rvelthuis.de/articles/articles-convert.html#arrayparams
引用:
Array-of parameters are open array parameters. They may look like any array, and they do accept any array, but they get an extra (hidden) parameter, which holds the highest index in the array (the High value). Since this is only so in Delphi, and not in C or C++, you'd have a real problem. (See also my article on open arrays), since the real number of parameters wouldn't match.
于是定义改成:
type pSingle=^single;
function func(buffer:pSingle;count:integer):integer;far;stdcall;external 'func.dll';
调用改成:
func(@buffer[0],count);
问题解决。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步