VC写的COM接口函数为
AddItems (Count As Long, ItemIDs() As String, ClientHandles() As Long, ByRef  ServerHandles() As Long, ByRef Errors() As Long, Optional RequestedDataTypes As Variant, Optional AccessPaths As Variant)
参数说明如下:
Count: The number of items to be affected
ItemIDs:Array of Fully Qualified ItemID’s
ClientHandles:Array of client item handles for the items processed
ServerHandles:Array of server item handles for the items processed
Errors: Array of Long’s indicating the success of the individual items operation. 
RequestedDataTypes:Optional Variant containing an integer array of Requested DataTypes.
AccessPaths:Optional Variant containing a string array of Access Path’s.用Delphi 的Import Type Libaray转换城TLB后,上述接口函数成为
procedure AddItems(NumItems: Integer; var ItemIDs: PSafeArray; var ClientHandles: PSafeArray;out ServerHandles: PSafeArray; out Errors: PSafeArray;RequestedDataTypes: OleVariant; AccessPaths: OleVariant); safecall;我的调用代码如下:
procedure TForm1.Button4Click(Sender: TObject);
var
  MyItemIDs: PSafeArray;
  MyClientHandles: PSafeArray;
  MyServerHandles: PSafeArray;
  MyServerErrors: PSafeArray;
  DataTypes: OleVariant;
  AccessPaths: OleVariant;  ArrayBounds: SAFEARRAYBOUND;
  i: Integer;
  p1,p2: Pointer;type
  ItemIDsArray = Array of WideString;
  ClientHandlesArray = Array of LongInt;begin
  ArrayBounds.lLbound := 0;
  ArrayBounds.cElements := 3;  MyItemIDs := SafeArrayCreate(varOleStr,1,ArrayBounds);
  if SafeArrayAccessData(MyItemIDs,p1) = S_OK then
  begin
      ItemIDsArray(p1)[0] := 'TAG1';
      ItemIDsArray(p1)[1] := 'TAG51';
      ItemIDsArray(p1)[2] := 'TAG631';
  end;
  SafeArrayUnAccessData(MyItemIDs);  MyClientHandles := SafeArrayCreate(varInteger,1,ArrayBounds);
  if SafeArrayAccessData(MyClientHandles,p2) = S_OK then
  for i := 0 to 2 do
    ClientHandlesArray(p2)[i] := 100+i;
  SafeArrayUnAccessData(MyClientHandles);  MyServerHandles := SafeArrayCreate(varInteger,1,ArrayBounds);
  MyServerErrors:= SafeArrayCreate(varInteger,1,ArrayBounds);  DataTypes := varArrayCreate([0,2],varInteger);
  AccessPaths := varArrayCreate([0,2],varOleStr);
  for i := 0 to 2 do
  begin
    DataTypes[i] := VT_EMPTY;
    AccessPaths[i] := '';
  end;  MyGroup.OPCItems.AddItems(ItemCount,MyItemIDs,MyClientHandles,MyServerHandles,MyServerErrors,DataTypes,AccessPaths);
end;编译通过了,但是执行时出现class EOleException with Message "The parameter is incorrect"
问题出在哪里?是PsafeArray,还是OleVariant?请高手指点。

解决方案 »

  1.   

    将_TLB中的safecall改成stdcall,错误没了,但结果不对,没有添加成功
      

  2.   

    VC写的COM接口函数为
    AddItems (Count As Long, ItemIDs() As String, ClientHandles() As Long, ByRef  ServerHandles() As Long, ByRef Errors() As Long, Optional RequestedDataTypes As Variant, Optional AccessPaths As Variant)
    参数说明如下:
    VC?? VB吧。
      

  3.   

    对于com接口,我用的是要先创建一个ixxx类对象,然后才能调用它的函数。
      

  4.   

    对于用dll的com接口,先注册dll文件:regsur32 xxxx.dll,然后倒入delphi中生成tlb文件。在程序中定义一个接口对象:var mycom:ixxx;创建对象:mycom:=coxxx.create;就可以调用它的函数了。
      

  5.   

    idl文件是VC写的,上面给出的是VB和delphi的调用形式问题在最后两个OleVariant参数上我把DataTypes和AccessPaths的创建和附值语句去掉以后,问题就没有了请问是什么原因,有没有人能解释一下
      

  6.   

    optional 要赋初始值,你没有做?????
    Optional RequestedDataTypes As Variant='', Optional AccessPaths As Variant=''