How can I perform a CopyFile in unbuffered mode?
A customer was copying a file with CopyFile, but they wanted the file handles to be opened as FILE_FLAG_NO_BUFFERING.
We saw some time ago that you can use the progress callback to CopyFileEx or CopyFile2 to flush the output handle. Maybe we can use the progress callback to open the handle as unbuffered?
Nope, that doesn't work because the progress callback gives you the already-opened handle. You can't change its buffering flag after the fact.
But that's okay, because CopyFileEx and CopyFile2 also have a flags parameter, and one of the flags is COPY_FILE_NO_BUFFERING, which means that the handle should be opened as FILE_FLAG_NO_BUFFERING.
BOOL success = CopyFileEx(
sourceFilePath, destinationFilePath,
nullptr, nullptr, nullptr,
COPY_FILE_NO_BUFFERING);
You can do the same with CopyFile2, but the flags are in the options structure.
COPYFILE2_EXTENDED_PARAMETERS parameters{};
parameters.dwSize = sizeof(parameters);
parameters.dwCopyFlags = COPY_FILE_NO_BUFFERING;
HRESULT hr = CopyFile2(sourceFilePath, destinationFilePath, ¶meters);
评论
?
参与讨论