代码之家  ›  专栏  ›  技术社区  ›  Kevin S. Miller

将字符串传入/传出Delphi COM服务器

  •  -1
  • Kevin S. Miller  · 技术社区  · 7 年前

    result:=RetVal; 指示

    我在客户端使用C#:

    var msg = delphi.GetMessage("My Message");

    这是米瑞德尔:

    HRESULT _stdcall GetMessage([in] BSTR msg, [out, retval] BSTR* RetVal);

    以下是我的实现:

    function TDelphiCom.GetMessage(msg:WideString; out RetVal:WideString):HRESULT;
    var
      tempString: string;
    begin
      tempString:=msg;
      RetVal:=WideString(tempString);
    end;
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Remy Lebeau    7 年前

    您的RIDL声明是正确的。

    您没有显示该方法的C#声明,因此我们无法查看您是否正确封送了参数。

    在Delphi方面,您的实现缺少 stdcall 调用约定(以匹配RIDL声明),以及异常处理,以便返回正确的 HRESULT

    function TDelphiCom.GetMessage(msg: WideString; out RetVal: WideString): HRESULT; stdcall;
    var
      tempString: string;
    begin
      try
        tempString := string(msg);
        RetVal := WideString(tempString);
        Result := S_OK;
      except
        // do something...
        Result := DISP_E_EXCEPTION;
      end;
    end;
    

    不过,您确实应该使用 safecall calling convention

    function TDelphiCom.GetMessage(msg: WideString): WideString; safecall;
    var
      tempString: string;
    begin
      tempString := string(msg);
      Result := WideString(tempString);
    end;