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.

3
  • A SafeArray is not the same as a variant array. 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. Commented May 25, 2020 at 3: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: stackoverflow.com/a/10803453/403671 Commented May 25, 2020 at 7:24
  • Did you try varOleStr instead of varVariant? Commented May 25, 2020 at 11:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.