ASP.NET web app calling Delphi DLL on IIS webserver, locks up when returning PChar string

前端 未结 3 1203
無奈伤痛
無奈伤痛 2021-01-13 19:43

Works fine if I don\'t return anything, or I return an integer. But if I try to return a PChar, ie..

result := PChar(\'\')  or   result:= PChar(\'Hello\')
         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 20:09

    You cannot return a string like that, the string is local to the function and will be freed as soon as he function returns leaving the returned PChar pointing to an invalid location.

    you need to pass in a pointer to be filled within the DLL, dynamically create the string and free it back in the c# code or create a static buffer in yout DLL and return that.

    By far the safest way is to pass a pointer into the function ie

    function SomeFunction( Buffer: PChar; MaxLength: PInteger ): wordbool; stdcall;
    {
      // fill in the buffer and set MaxLength to length of data
    }
    

    you should set MaxLength to the sixe of the buffer before calling your dll so that the dll can check there is enough space for the data to be returned.

提交回复
热议问题