August 10th, 2026
0 reactions

How can I perform a Copy­File&shy in unbuffered mode?

A customer was copying a file with Copy­File&shy, 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 Copy­File­Ex or Copy­File2 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 Copy­File­Ex and Copy­File2 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 Copy­File2, 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, &parameters);

Topics

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Sort by :
  • Harry Johnston 1 hour ago

    CopyFile&shy?

    • Jernej Simončič 1 minute ago · Edited

      Probably copy&paste error – ­ is soft hypen, but the semicolon got dropped.