简体   繁体   中英

Passing array of string from Delphi 2 to COM Visible C# .net DLL

I'm trying to call a method in a COM DLL (written in C#) from a Delphi 2.0 client application. One of the parameters of the method I'm trying to call is a string array. Looking at the unit created by importing the library into a later version of Delphi, I see the string array parameter is defined as a PSafeArray.

This code works in Delphi 2007:

  stringToEcho := VarArrayCreate([0, 0], varVariant);
  stringToEcho[0] := 'Hello World!';

  oResponse := iface.RequestResponse('EXTests', 'Echo', PSafeArray(VarArrayAsPSafeArray(stringToEcho)), 30, '', true);

This Delphi 2 code causes a "The parameter is incorrect" error at runtime when calling the RequestResponse method:

  stringToEcho := VarArrayCreate([0, 0], varVariant);
  stringToEcho[0] := 'Hello World!';

  oResponse := iface.RequestResponse('EXTests', 'Echo', stringToEcho, 30, '', true);

Clearly a variant array is not a safearray and I need some way of converting or extracting the SafeArray from the variant array as I do in the Delphi 2007 example.

I have looked at the OLE2 unit & System units. I can see some Variant array support routines in System.pas but no variant array <-> SafeArray conversion routines.

How can I pass a PSafeArray to a COM automation server in Delphi 2?

Note that another point of difference is that I'm using early binding in Delphi 2007, and late binding in Delphi 2.

Thanks all.
I understand a Delphi Variant Array is not a SafeArray. But they are close:) Delphi's VarArrayCreate actually calls Win32 SafeArrayCreate.

I guess I needed some way of extracting the SafeArray from the VariantArray as I did in the Delphi 2007 example. I did try to backport VarArrayAsPSafeArray to Delphi 2 but was unsuccessful. However, I was able to backport another helper function from Delphi 2007 VarArrayRef which is actually more useful in the late bound context of Delphi 2. So all good now. BTW - I did try to create a VarArray of varOLEStr but that gives me a "Type Mismatch" error when calling the RequestResponse method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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