1

Is it safe to pass a Delphi (Unicode) string to a procedure as a parameter of type WideString?

procedure Test;
var
  S: string;
  Html: string;
begin
  S := '...'; // may contain anything a Unicode string accepts
  Html := WideStringToHTML(S);
end;

function WideStringToHTML(const AParam: WideString): string;
begin
  ...
end;
mjn
  • 36,362
  • 28
  • 176
  • 378
  • 2
    It is safe, however it is advised to avoid it, if not dealing with COM. See also [Is WideString identical to String in Delphi 2009](https://stackoverflow.com/questions/374446/is-widestring-identical-to-string-in-delphi-2009). Delphi handles the conversion from `string` to `WideString` for you. Behind the scenes it calls [`SysAllocStringLen`](https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-sysallocstringlen) and `SysFreeString`. You can further investigate in `System` unit - see `_WStrFromUStr`, `_WStrFromPWCharLen` and `_WStrClr`. – Peter Wolf Jun 17 '20 at 14:27
  • The real question is why you are using `WideString` – David Heffernan Jun 17 '20 at 17:14

0 Answers0