繁体   English   中英

如何创建PSafeArray类型的参数?

[英]How to create a parameter of type PSafeArray?

如何创建PSafeArray类型的参数?

我从C#COM库中收到以下错误:

  SafeArray with range 65262 transfered to the method that requires array with range 1

Delphi XE2应该使用带有参数Generated RIDL类型的Generated RIDL类型库来调用C# COM library过程。

Delphi XE2代码:

  function GetObjects: PSafeArray;
  var
    aObjects: Variant;
  begin
    aObjects := VarArrayCreate([0, 2], varVariant);
    aObjects[0] := ADOConnection.ConnectionObject;
    aObjects[1] := CashConnection;
    aObjects[2] := Self as IDispatch;
    Result := PSafeArray(TVarData(aObjects).VArray);
  end;

  ICompiler.Execute('MainNameSpace', 'MainClass', 'MainMethod', GetObjects);

C#COM库代码:

void Execute(string Namespace, string ClassName, string MethodName, Object[] Objects);

void ICSCompiler.Execute(string Namespace, string ClassName, string MethodName, Object[] Objects)
{
  System.Type _type = cr.CompiledAssembly.GetType(Namespace + "." + ClassName);
  System.Object obj = Activator.CreateInstance(_type);
  System.Reflection.MethodInfo mi = obj.GetType().GetMethod(MethodName);
  mi.Invoke(obj, new Object[] { Objects });
}

生成的RIDL代码:

HRESULT _stdcall Execute([in] BSTR Namespace, [in] BSTR ClassName, [in] BSTR MethodName, [in] SAFEARRAY(VARIANT) Objects);

这段代码可以正常工作:

  function GetObjects: PSafeArray;
  var
    aBounds: array [0..0] of TSafeArrayBound;
    aObjects: PSafeArray;
    aIndex: Integer;
    aConnectionObject: OleVariant;
    aCashConnection: OleVariant;
    aScript: OleVariant;
  begin
    aBounds[0].lLbound   := 0;
    aBounds[0].cElements := 3;
    aObjects := SafeArrayCreate(varVariant, 1, @aBounds);
    aIndex := 0;
    aConnectionObject := ADOConnection.ConnectionObject;
    OleCheck(SafeArrayPutElement(aObjects, aIndex, aConnectionObject));
    aIndex := 1;
    aCashConnection := CashConnection;
    OleCheck(SafeArrayPutElement(aObjects, aIndex, aCashConnection));
    aIndex := 2;
    aScript := Self as IDispatch;
    OleCheck(SafeArrayPutElement(aObjects, aIndex, aScript));
    Result := aObjects;
  end;

我记得的第一件事是SafeArrayCreate 看看“ PSafeArray奥秘

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM