简体   繁体   English

将字符串数组从Delphi 7传递到COM Visible C#.net DLL

[英]Passing array of string from Delphi 7 to COM Visible C# .net DLL

I have a C# DLL (.NET 2.0 framework) which is setup as COM Visible. 我有一个C#DLL(.NET 2.0框架),它设置为COM Visible。

The Interface is declared as 接口声明为

[Guid("....")]
[InterfaceType(ComInterfaceType.InterfaceIDispatch)]
public interface IMyClass
{
   [DispId(1)]
   string myFunc(string url, ref string[] keys);
}

[Guid("...")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("MyClass.Lib")]
public class MyClass:IMyClass
{
   public string myFunc(string url, ref string[] keys)
   {
      string retString = "";
      // do my stuff
      return retString;
   }
}

This class is registered by calling RegAsm.exe . 此类通过调用RegAsm.exe注册。

I tested this DLL in VB6 and it works okay. 我在VB6中测试了此DLL,它可以正常工作。

Dim myClassInstance As MyClass
Set myClassInstance As New MyClass

Dim keys(0 to 1) As String
keys(0) = "a"
keys(1) = "b"

Dim url As String
url = "https://10.0.0.2"

Dim response As String
response = myClassInstance.myFunc(url, keys)

Now I need to use this DLL in Delphi 7 (I have no prior knowledge with Delphi). 现在,我需要在Delphi 7中使用此DLL(我对Delphi并不了解)。

I use Tlbimp to generate the required files. 我使用Tlbimp生成所需的文件。 It looks like I can already reference the DLL in Delphi, but it is not working: 看来我已经可以在Delphi中引用该DLL,但无法正常工作:

var
  myClassInstance : IMyClass;
  url : WideString
  keys : array[0..1] of WideString;
begin
    url := 'https://10.0.0.2';
    keys[0] := 'a';
    keys[1] := 'b';

    myClassInstance.MyFunc(url, keys); // Incompatible types error
end;

The function signature is (from Delphi 7 Editor): 函数签名是(来自Delphi 7编辑器):

function(const url: WideString; var keys: OleVariant): WideString;

How should I pass the correct parameters into this DLL in Delphi? 如何将正确的参数传递到Delphi中的此DLL中?

At the COM level, the DLL is expecting you to pass in a VARIANT containing a SAFEARRAY of BSTR strings. 在COM级别,DLL期望您传递包含BSTR字符串SAFEARRAYVARIANT VB and C# hide that detail from you using higher level syntaxes. VB和C#使用更高级的语法向您隐藏了这些细节。

In Delphi, WideString is a wrapper for BSTR , and you can use the RTL's VarArrayCreate() function to create OLE/COM compatible arrays. 在Delphi中, WideStringBSTR的包装,您可以使用RTL的VarArrayCreate()函数来创建OLE / COM兼容的数组。

Try this: 尝试这个:

var
  myClassInstance : IMyClass;
  url : WideString;
  keys : OleVariant;
begin
  url := 'https://10.0.0.2';
  keys := VarArrayCreate([0, 1], varOleStr);
  keys[0] := WideString('a');
  keys[1] := WideString('b');
  // don't forget to create the COM object instance
  // before trying to call any of its methods... 
  MyClassInstance := TMyClass.Create;
  myClassInstance.MyFunc(url, keys);
end;

As you are using Delphi 7, string <> widestring. 当您使用Delphi 7时,字符串<> widestring。

Did you include the explicit casts to widestring as shown in Remy's example code? 如雷米(Remy)的示例代码所示,您是否包括对字符串的显式转换?

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

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