如何创建 PSafeArray 类型的参数?
我从 C# COM 库中获取以下错误:
SafeArray with range 65262 transfered to the method that requires array with range 1
Delphi XE2
应该使用带有 PSafeArray 类型参数的类型库调用C# COM library
过程。Generated RIDL
德尔福 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);