某项目需要调用设备厂家提供的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);
问题解决。