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?
At the COM level, the DLL is expecting you to pass in a
VARIANT
containing aSAFEARRAY
ofBSTR
strings. VB and C# hide that detail from you using higher level syntaxes.In Delphi,
WideString
is a wrapper forBSTR
, and you can use the RTL'sVarArrayCreate()
function to create OLE/COM compatible arrays.Try this: