发表于:2004-05-20 10:50:00
楼主
我用Delphi开发opc客户端,通过自动化接口,基本上已经完成,只有一处解决不了,请各位大侠帮忙,感激不尽!
问题主要是对OpcItems接口的Remove函数,它的说明文档如下:
Syntax Remove (Count As Long, ServerHandles() As Long, ByRef Errors() As Long)
Count: The number of items to be removed
ServerHandles: Array of server item handles for the items processed
Errors: Array of Long’s indicating the success of the individual items operation.
在VB中使用很简单,但在Delphi中就复杂一些,主要是因为SafeArray,下面是我的Delphi代码
procedure TFormMain.BtnDelClick(Sender: TObject);
var
i: integer;
Item: TListItem;
Handles, Errors: PSafeArray;
Bound: TSAFEARRAYBOUND;
Temp: ^integer;
begin
if ListGroup.SelCount > 0 then
begin
// Create SafeArray
Bound.lLbound := 1;
Bound.cElements := ListGroup.SelCount;
Handles := SafeArrayCreate(varInteger, 1, Bound);
// Fill SafeArray 以下是把要删除点的Serverhandle放入Handles数组
i := 0;
Item := ListGroup.Selected;
while Item <> nil do
begin
Inc(i);
New(Temp);
Temp^ := StrToInt(Item.SubItems.Strings[0]);
SafeArrayPutElement(Handles, i, Temp); //填入ServerHandle
Item := ListGroup.GetNextItem(Item, sdAll, [isSelected]);
end;
// Remove
try
Itms.Remove(ListGroup.SelCount, Handles, Errors); //调用这个函数老是出错,原因是内存访问异常
finally
// Free SafeArray
SafeArrayDestroy(Handles);
end;
end;
end;
不知我的用法对不对?