Calling a Delphi DLL from a C# .NET application

前端 未结 4 810
南笙
南笙 2020-12-02 07:46

EDIT: I\'ve posted a better implementation of this, below. I left this here so the responses would make sense.

I\'ve done numerous searches for the

4条回答
  •  一个人的身影
    2020-12-02 08:30

    It is easier to retireve a string using PString:

    function DelphiFunction(inputString : PAnsiChar;
                        var outputStringBuffer : PString;
                        var errorMsgBuffer : PString)
                        : WordBool; stdcall; export;
    var 
      s : string;
    begin
      try
        s := inputString;
        outputStringBuffer:=PString(AnsiString(s));
        Result := true;
      except
        on e : exception do
        begin
          s:= 'error';
          errorMsgBuffer:=PString(AnsiString(e.Message));
          Result := false;
        end;
      end;
    end;
    

    In c# then:

        const int stringBufferSize = 1024;
    
        var  str = new    IntPtr(stringBufferSize);
    
        string loginResult = Marshal.PtrToStringAnsi(str);
    

提交回复
热议问题