stringdelphinullshfileoperation

In delphi how do I create a string with null separator


I am using SHFileOperation and the source can be a pwidechar with filenames separated by a null and the whole thing terminated with a double null.

I can create an array of widechar and it holds the information I need but when I try to convert to pwidechar for the function, it only copies to the first null.

What am I doing wrong?


Solution

  • Like this:

    const
      null = #0;
    
    var
      FileNames: string;
    ...
    FileNames := FileName1 + null + FileName2 + null;
    

    Then pass PWideChar(FileNames) to SHFileOperation.

    Because Delphi strings are automatically null-terminated, PWideChar(FileNames) is double null-terminated. One null-terminator that we put on the end, and the automatically added one.

    Note that I am assuming that you are using a Unicode Delphi since you talk about wide characters. If you are on an older Delphi, and are calling SHFileOperationW, then declare FileNames to be WideString.

    More likely you have a list of files in, say, a string list. Treat them like this:

    var
      FileNames: string;
    ...
    if FileList.Count=0 then
      FileNames := null
    else
    begin
      FileNames := '';
      for i := 0 to FileList.Count-1 do
        FileNames := FileNames + FileList[i] + null;
    end;