Search code examples
c#delphicom

COM Server sends empty string which is converted to a NULL pointer


i'm define in C# this interface for a COM-Server:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("58C77969-0E7D-3778-9999-B7716E4E1111")]
public interface IMyInterface    
{
    string MyName { get; }
}

This interface is imported and implemented in a Delphi XE5 program.

The import looks like this:

IMyInterface = interface(IUnknown)
  ['{58C77969-0E7D-3778-9999-B7716E4E1111}']
  function Get_MyName (out pRetVal: WideString): HResult; stdcall;
end;

The implementation like this:

type
  TMyImpl = class(TInterfacedObject, IMyInterface)
  public
    function Get_MyName (out pRetVal: WideString): HResult; stdcall;    
 end;

 function TMyImpl.Get_MyName (out pRetVal: WideString): HResult;
 var
  s: string;
 begin
   s:=''; // empty!
   pRetVal:=s;
   result:=S_OK;
 end;

When i call that server from c# like this:

var server = new Server();
string s = server.MyName;

Then s is NULL and not an empty string as excepted.

How i can force that empty strings are transferred in COM as empty string and not replace by marshaling to NULL?


Solution

  • Delphi implements empty strings as nil pointers (see System._NewUnicodeString). You can allocate an empty COM-compatible string manually:

    function TMyImpl.Get_MyName(out pRetVal: WideString): HResult;
    var
      BStr: TBstr;
    begin
      BStr := SysAllocString('');
      if Assigned(BStr) then
      begin
        Pointer(pRetVal) := BStr;
        Result := S_OK;
      end
      else
        Result := E_FAIL;
    end;
    

    or you could create a helper function:

    function EmptyWideString: WideString;
    begin
      Pointer(Result) := SysAllocString('');
    end;