Delphi PWideChar returned by Chilkat Functions

For Chilkat methods that return a PWideChar, the memory referenced by the returned PWideChar is managed by the Chilkat library, not your application. This means you cannot be sure that this memory will stay valid for a long time. For example, if you delete the Chilkat object that provided the PWideChar, the memory for the PWideChar will also be deleted. For example:

var
mailman: HCkMailMan;
ftp: HCkFtp2;
sessionLog: PWideChar;
xmlDirListing: PWideChar;

begin
mailman := CkMailMan_Create();
// ....
sessionLog := CkMailMan__smtpSessionLog(mailman);

ftp := CkFtp2_Create();
// ...
xmlDirListing := CkFtp2__getXmlDirListing(ftp,'*.*');

CkMailMan_Dispose(mailman);
CkFtp2_Dispose(ftp);

// ---------------------------------------------------------------------------
// After disposing the Chilkat objects, the memory pointed to by the PWideChar
// variables is no longer valid.
// ---------------------------------------------------------------------------

Max of 10 PWideChar's per Chilkat Object Instance

Also, Chilkat retains in memory only the last 10 returned PWideChar's. The PWideChar memory will become invalid, or can change, after 10 Chilkat method calls returning a PWideChar on the same object instance. For example:

var
sftp: HCkSFtp;
handle1: PWideChar;
handle2: PWideChar;
...
handle10: PWideChar;
handle11: PWideChar;
handle12: PWideChar;

begin

sftp := CkSFtp_Create();
...

handle1 := CkSFtp__openDir(sftp,'dir1');
handle2 := CkSFtp__openDir(sftp,'dir2');
...
...
handle10 := CkSFtp__openDir(sftp,'dir10');
// --------------------------------------------------
// handle1 becomes invalid after the following call.
// --------------------------------------------------
handle11 := CkSFtp__openDir(sftp,'dir11');

// --------------------------------------------------
// handle2 becomes invalid after the following call.
// --------------------------------------------------
handle12 := CkSFtp__openDir(sftp,'dir12');

...

CkSFtp_Dispose(sftp);

Make a Copy of the PWideChar to Keep it in Memory

If you wish to keep the PWideChar memory for longer, then make a copy and delete it when finished. For example:

function CopyPWideChar(src: PWideChar): PWideChar;
var
  len: Integer;
begin
  if src = nil then
  begin
    Result := nil;
    Exit;
  end;

  len := StrLen(src); // Get the length of the PWideChar (number of wide characters)

  // Allocate memory for the copy, including space for the null terminator
  GetMem(Result, (len + 1) * SizeOf(WideChar));
  
  // Copy the wide characters from src to Result
  StrLCopy(Result, src, len);

  // Ensure the destination string is null-terminated
  Result[len] := #0;
end;


var
  sftp: HCkSFtp;
  chilkatOwnedHandle: PWideChar;
  appOwnedHandle: PWideChar;
begin


  sftp := CkSFtp_Create();
...
...
  chilkatOwnedHandle := CkSFtp__openDir(sftp,'dir1');

  // Make a copy for your app to hold for a longer time.
  appOwnedHandle := CopyPWideChar(chilkatOwnedHandle);
...
...
  // When your app is finished with the copy, delete it.
  // (Do not delete the PWideChar owned by Chilkat)
  FreeMem(appOwnedHandle);
...
...

end.