简体   繁体   English

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

[英]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.我正在尝试从 Delphi 2.0客户端应用程序调用 COM DLL (用 C# 编写)中的方法。 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.查看通过将库导入更高版本的 Delphi 创建的单元,我看到字符串数组参数定义为 PSafeArray。

This code works in Delphi 2007:此代码适用于 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:此 Delphi 2 代码在调用 RequestResponse 方法时在运行时导致“参数不正确”错误:

  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.显然,变体数组不是安全数组,我需要某种方式从变体数组中转换或提取 SafeArray,就像我在 Delphi 2007 示例中所做的那样。

I have looked at the OLE2 unit & System units.我看过 OLE2 单元和系统单元。 I can see some Variant array support routines in System.pas but no variant array <-> SafeArray conversion routines.我可以在 System.pas 中看到一些变体数组支持例程,但没有变体数组 <-> SafeArray 转换例程。

How can I pass a PSafeArray to a COM automation server in Delphi 2?如何将 PSafeArray 传递给 Delphi 2 中的 COM 自动化服务器?

Note that another point of difference is that I'm using early binding in Delphi 2007, and late binding in Delphi 2.请注意,另一个不同点是我在 Delphi 2007 中使用早期绑定,而在 Delphi 2 中使用后期绑定。

Thanks all.谢谢大家。
I understand a Delphi Variant Array is not a SafeArray.我了解 Delphi 变体数组不是 SafeArray。 But they are close:) Delphi's VarArrayCreate actually calls Win32 SafeArrayCreate.但它们很接近:) Delphi 的 VarArrayCreate 实际上调用了 Win32 SafeArrayCreate。

I guess I needed some way of extracting the SafeArray from the VariantArray as I did in the Delphi 2007 example.我想我需要某种从 VariantArray 中提取 SafeArray 的方法,就像我在 Delphi 2007 示例中所做的那样。 I did try to backport VarArrayAsPSafeArray to Delphi 2 but was unsuccessful.我确实尝试将 VarArrayAsPSafeArray 反向移植到 Delphi 2 但没有成功。 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.但是,我能够从 Delphi 2007 VarArrayRef 向后移植另一个助手 function,这实际上在 Delphi 2 的后期绑定上下文中更有用。 BTW - I did try to create a VarArray of varOLEStr but that gives me a "Type Mismatch" error when calling the RequestResponse method.顺便说一句 - 我确实尝试创建一个 varOLEStr 的 VarArray,但是在调用 RequestResponse 方法时这给了我一个“类型不匹配”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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