How to call a .NET COM method with an array from delphi using PSafeArray?

烈酒焚心 提交于 2019-12-14 03:23:04

问题


I have an .NET (4.0) interface which is implemented with a ServicedComponent COM+ class:

interface DotNetIface
{
    void MethodRef(var System.Guid guid);
    void MethodArray(System.Guid[] guids, params object[] parameters);
    void MethodCStyle([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct, SizeConst=5)]System.Guid[] guids);
}

Now I used the Delphi 2007 import wizard to import the type library, and as expected I get the following signatures:

procedure MethodRef(var guid : TGuid);
procedure MethodArray(guids : PSafeArray);
procedure MethodCStyle(var guids : ClrGuid /* from mscorlib_TLB */);

If i now call the "ref" method like this it works fine:

procedure CallByRef(guid : TGuid);
var
    test : TGuid;
begin
    test := ...
    comRef.MethodRef(guid);
end;

Now I also need the array method

procedure CallArray();
var
    localGuid : TGuid;
    arrayVariant : OleVariant;
begin
    arrayVariant := VarArrayCreate([0,4], varVariant /* dont know here */);
    arrayVariant[0] := localGuid; /* compile error, cannot cast implicitly */

    comRef.MethodArray(PSafeArray(TVarData(arrayVariant.VArray)), /* here this object... PSafeArray works actually*/);
end;

Now lastly i tried with a c array

procedure CallCStyle();
var
    localGuid : TGuid;
    arrayOfGuid : array [0..4] of ClrGuid;
begin
    arrayOfGuid[0] := ClrGuid(localGuid);

    comRef.MethodCStyle(PSafeArray(/* now i dont know put it*/, /* here this object... PSafeArray works actually*/);
end;

I seriously dont know how to make this work. I hope someone has more experience with COM marshalling thx

Side node:

I found VT_CLSID which i think can be passed for SafeArrayCreate, but I am not sure how to sue that


回答1:


I have never tried what you need but a quick search found the following articles:

  • Scripting Your Delphi Applications (Shows a Good example of converting a variant array to PSafeArray
  • How to do a SafeArray Access with a DLL for Variants (OLE)
  • The mysteries of PSafeArray


来源:https://stackoverflow.com/questions/2876320/how-to-call-a-net-com-method-with-an-array-from-delphi-using-psafearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!