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

590 Views Asked by At

I have a C# DLL (.NET 2.0 framework) which is setup as 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.

I tested this DLL in VB6 and it works okay.

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).

I use Tlbimp to generate the required files. It looks like I can already reference the DLL in Delphi, but it is not working:

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):

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

How should I pass the correct parameters into this DLL in Delphi?

2

There are 2 best solutions below

1
On

At the COM level, the DLL is expecting you to pass in a VARIANT containing a SAFEARRAY of BSTR strings. VB and C# hide that detail from you using higher level syntaxes.

In Delphi, WideString is a wrapper for BSTR, and you can use the RTL's VarArrayCreate() function to create OLE/COM compatible arrays.

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;
2
On

As you are using Delphi 7, string <> widestring.

Did you include the explicit casts to widestring as shown in Remy's example code?