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

The Old New Thing (Raymond Chen) Tools

Summary

Explains how to use the COPY_FILE_NO_BUFFERING flag with CopyFileEx and CopyFile2 to copy files using unbuffered handles, and why the progress callback cannot be used to change buffering after the handles are opened.

<p>A customer was copying a file with <code>Copy­File</code>, but they wanted the file handles to be opened as <code>FILE_<wbr />FLAG_<wbr />NO_<wbr />BUFFERING</code>.</p> <p>We saw some time ago that <a title="How can I perform a CopyFile, but also flush the file buffers before the destination handle is closed?" href="https://devblogs.microsoft.com/oldnewthing/20221007-00/?p=107261"> you can use the progress callback to <code>Copy­File­Ex</code> or <code>Copy­File2</code> to flush the output handle</a>. Maybe we can use the progress callback to open the handle as unbuffered?</p> <p>Nope, that doesn&#8217;t work because the progress callback gives you the already-opened handle. You can&#8217;t change its buffering flag after the fact.</p> <p>But that&#8217;s okay, because <code>Copy­File­Ex</code> and <code>Copy­File2</code> also have a flags parameter, and one of the flags is <code>COPY_<wbr />FILE_<wbr />NO_<wbr />BUFFERING</code>, which means that the handle should be opened as <code>FILE_<wbr />FLAG_<wbr />NO_<wbr />BUFFERING</code>.</p> <pre>BOOL success = CopyFileEx( sourceFilePath, destinationFilePath, nullptr, nullptr, nullptr, COPY_FILE_NO_BUFFERING); </pre> <p>You can do the same with <code>Copy­File2</code>, but the flags are in the options structure.</p> <pre>COPYFILE2_EXTENDED_PARAMETERS parameters{}; parameters.dwSize = sizeof(parameters); parameters.dwCopyFlags = COPY_FILE_NO_BUFFERING; HRESULT hr = CopyFile2(sourceFilePath, destinationFilePath, &amp;parameters); </pre> <p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20260810-00/?p=112600">How can I perform a &lt;CODE&gt;Copy&shy;File&lt;/CODE&gt; in unbuffered mode?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
Original Article
View Cached Full Text

Cached at: 08/12/26, 08:26 AM

# How can I perform a Copy­File in unbuffered mode? - The Old New Thing Source: [https://devblogs.microsoft.com/oldnewthing/20260810-00?p=112600](https://devblogs.microsoft.com/oldnewthing/20260810-00?p=112600) A customer was copying a file with`Copy­File`, 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](https://devblogs.microsoft.com/oldnewthing/20221007-00/?p=107261)\. 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); ``` ### Category ### Topics ## Author ![Raymond Chen](https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2019/02/RaymondChen_5in-150x150.jpg) 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\.

Similar Articles

Zero-copy in Go: sendfile, splice, and the cost of io.Copy

Hacker News Top

This article explains how Go's io.Copy automatically uses sendfile and splice for zero-copy file transfers over TCP, and demonstrates how wrapping a file in a plain io.Reader—e.g., for logging—quietly disables this optimization, causing a significant performance drop. Benchmarks and strace output illustrate the impact.

Interconverting std::function with copyable_function

Lobsters Hottest

Technical analysis of the interconversion between std::function and std::copyable_function in C++26, discussing implementation approaches and performance trade-offs, with specific note that libstdc++ currently uses a less optimal approach.

Retrofitting the WM_COPY­DATA message onto Windows 3.1

The Old New Thing (Raymond Chen)

This article explains how the WM_COPYDATA message, introduced for 32-bit Windows, was retrofitted onto 16-bit Windows 3.1 by taking advantage of the shared address space, requiring no changes for inter-16-bit communication.

How do I inform Windows that I’m writing a binary file?

The Old New Thing (Raymond Chen)

This article explains that Windows does not have a built-in concept of binary vs. text mode at the OS level; such distinctions are handled by runtime libraries like the C runtime. It clarifies that all files are treated as bytes by Windows and that content transformations must be done manually or via libraries.