How to pass Delphi empty string to com visible .net assembly

626 Views Asked by At

I have a C# assembly that contains a ComVisible class with a method taking 2 string parameters. I have registered the assembly and imported it using Delphi XE2's "Import Component" command.

The C# interface looks like this:

[ComVisible(true),
 Guid("132D7742-D86A-49E4-81FD-D804E6872475"),
 InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IResourceManager
{        
    int AddResource(string guid, string address);
}

The imported Delphi interface looks like this:

IResourceManager= dispinterface
['{132D7742-D86A-49E4-81FD-D804E6872475}']
function AddResource(const Guid: WideString; const address: WideString): Integer; dispid 1610743809;
end;

Here is an example of how the code is called from Delphi:

procedure TResourceManager.AddResource(const Guid, Address: String);
var
  ResourceManager: IResourceManager;
begin
  ResourceManager := CoResourceManager.Create;
  ResourceManager.AddResource(Guid, Address);
end;

Everything works 100% as long as I don't pass empty strings to the method from my Delphi application. When I do I get a the following error on the .Net side:

InvalidVariant was detected Message: An invalid VARIANT was detected during a conversion from an unmanaged VARIANT to a managed object. Passing invalid VARIANTs to the CLR can cause unexpected exceptions, corruption or data loss.

Ideally I want to be able to call the method from the Delphi side with an empty string '' and on the .Net side do a string.IsNullOrEmpty(guid) to check for this condition. So far all I have been able to do is to pass #0 to the method instead of '' from Delphi but on the .Net side the value becomes "\0" which is neither null nor empty.

What is the correct approach to deal with this?

Thanks.

0

There are 0 best solutions below