Why can a WideString not be used as a function return value for interop?

后端 未结 2 525
南方客
南方客 2020-11-22 12:23

I have, on more than one occasion, advised people to use a return value of type WideString for interop purposes.

  • Accessing Delphi DLL throwing oc
2条回答
  •  再見小時候
    2020-11-22 12:54

    In regular Delphi functions, the function return is actually a parameter passed by reference, even though syntactically it looks and feels like an 'out' parameter. You can test this out like so (this may be version dependent):

    function DoNothing: IInterface;
    begin
      if Assigned(Result) then
        ShowMessage('result assigned before invocation')
      else
        ShowMessage('result NOT assigned before invocation');
    end;
    
    procedure TestParameterPassingMechanismOfFunctions;
    var
      X: IInterface;
    begin
      X := TInterfaceObject.Create;
      X := DoNothing; 
    end;
    

    To demonstrate call TestParameterPassingMechanismOfFunctions()

    Your code is failing because of a mismatch between Delphi and C++'s understanding of the calling convention in relation to the passing mechanism for function results. In C++ a function return acts like the syntax suggests: an out parameter. But for Delphi it is a var parameter.

    To fix, try this:

    function TestWideString: WideString; stdcall;
    begin
      Pointer(Result) := nil;
      Result := 'TestWideString';
    end;
    

提交回复
热议问题