0

I am trying to use SHFileOperation() to copy a folder, but get this error:

a value of type "const char *" cannot be assigned to an entity of type "PCZZWSTR"

for both s.pTo and s.pFrom.

The code I'm using is:

SHFILEOPSTRUCT s = { 0 };
s.hwnd = hWnd;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = "C:\\Users\\styler\\Desktop\\Folder1\0";
s.pFrom = "C:\\Users\\styler\\Desktop\\Software\\Folder2\\Folder3\\*\0";
SHFileOperation(&s);

What am I doing wrong in s.pTo and s.pFrom? I am setting those equal to the target folder and the source folder, but why is this not working?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SA1234
  • 3
  • 2

1 Answers1

3

The compiler is telling you that you are trying to assign char string literals to wchar_t string pointers (PCZZWSTR = CONST WCHAR *). That means you must be compiling with UNICODE defined, where SHFileOperation() maps to SHFileOperationW() which expects wchar_t* string pointers instead of char* string pointers.

So, you need to prefix your string literals with the L prefix, eg:

SHFILEOPSTRUCT s = { 0 };
s.hwnd = hWnd;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = L"C:\\Users\\styler\\Desktop\\Folder1\0";
s.pFrom = L"C:\\Users\\styler\\Desktop\\Software\\Folder2\\Folder3\\*\0";
SHFileOperation(&s);

Or, since you are actually using the TCHAR version of SHFileOperation(), use the TEXT() macro to match your string literals to the actual character type used by TCHAR:

SHFILEOPSTRUCT s = { 0 };
s.hwnd = hWnd;
s.wFunc = FO_COPY;
s.fFlags = FOF_SILENT;
s.pTo = TEXT("C:\\Users\\styler\\Desktop\\Folder1\0");
s.pFrom = TEXT("C:\\Users\\styler\\Desktop\\Software\\Folder2\\Folder3\\*\0");
SHFileOperation(&s);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That got rid of the error, but for some reason the folder is not copying over to the location I specified. – SA1234 Jan 11 '21 at 21:09
  • That is a different issue. What is the actual error code that `SHFileOperation()` returns? And does your app have permissions to access the folders in question? – Remy Lebeau Jan 11 '21 at 21:12
  • It's saying "The file name you specified is not valid or too long. Specify a different file name." – SA1234 Jan 11 '21 at 21:18
  • That is not what I asked. What is the ERROR CODE (the `int` value) that `SHFileOperation()` returns? `int errCode = SHFileOperation(&s);` What is the value of `errCode`? But in any case, did you validate that the ```C:\Users\styler\Desktop\Software\Folder2\Folder3\``` folder actually exists before trying to copy from it? Are you trying to copy the `Folder3` folder itself, or the files inside of it? – Remy Lebeau Jan 11 '21 at 21:20
  • @SA1234 I read in the [documentation](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shfileopstructw): "Standard MS-DOS wildcard characters, such as "*", are permitted only in the file-name position. Using a wildcard character elsewhere in the string will lead to unpredictable results." – Paul Sanders Jan 11 '21 at 21:21
  • @PaulSanders the wildcard IS being used in the file-name position only – Remy Lebeau Jan 11 '21 at 21:22
  • @RemyLebeau Doesn't look that way to me. – Paul Sanders Jan 11 '21 at 21:22
  • @PaulSanders well, it is. The `\0` after the wildcard doesn't change that. `pFrom` is a double-null-terminated list. The explicit `\0` terminates the 1st string in the list, and the implicit compiler-provided null terminator ends the list. The `*` is in the file-name position of the 1st string of the list. – Remy Lebeau Jan 11 '21 at 21:23
  • @RemyLebeau I figured out what the issue was. I had switched the to and from folders. It is working now. Thank you so much! – SA1234 Jan 11 '21 at 21:42