1

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.

Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
MarkW
  • 61
  • 7
  • A `SafeArray` is not the same as a `variant array`. https://stackoverflow.com/a/2878492/62576 has some links that might help. You could also search this site for `[delphi] safearray` for some other posts that are relevant. – Ken White May 25 '20 at 03:55
  • I don't know about Delphi, but COM has API to create and populate a SAFEARRAY (which is a type of its own and seems to be the type RequestResponse expects): SafeArrayCreate, SafeArrayPutElement, SafeArrayDestroy, etc. Maybe this helps: https://stackoverflow.com/a/10803453/403671 – Simon Mourier May 25 '20 at 07:24
  • Did you try varOleStr instead of varVariant? – R. Hoek May 25 '20 at 11:15

1 Answers1

1

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.

MarkW
  • 61
  • 7