How to call a .NET COM method with an array from delphi using PSafeArray?
- by Sebastian Godelet
Hello. 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