Empty string becomes null when passed from Delphi to C# as a function argument

后端 未结 4 376
南笙
南笙 2021-01-14 14:43

I have a native Delphi exe which calls into C# dll via COM interop. Here\'s the simplest case which demonstrate this issue:

Delphi:

IClass1 = interfa         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 15:14

    In Delphi, a null (i.e., nil) and empty string are treated as equivalent. As such, passing '' for a string (or WideString) parameter passes nil internally -

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    procedure Foo(const S: WideString);
    begin
      WriteLn(Pointer(S) = nil);
    end;
    
    begin
      Foo('Something');  //FALSE
      Foo('');  //TRUE
      ReadLn;
    end.
    

    The equation of null and empty strings was in fact copied from COM... so a COM library isn't really the place to insist on a C#-style distinction between the two.

提交回复
热议问题