2

How do I enable "Better Performance" on an External USB HD programatically in C/C++.

Specifically I am talking about the device properties pane in the control panel in Microsoft Windows. That enables a form of OS level write caching.

Better Performance Policy

Puppy
  • 144,682
  • 38
  • 256
  • 465
unixman83
  • 9,421
  • 10
  • 68
  • 102
  • 2
    Take a look @ http://blogs.msdn.com/b/dhawan/archive/2009/10/05/enable-or-disable-enable-write-caching-on-disk-behavior-on-disk.aspx (there is a caveat RE vista+ in the comments) – Alex K. Sep 13 '11 at 11:20
  • @Alex K. I bet a windows service gets past these just fine. – unixman83 Sep 13 '11 at 11:30
  • For the record: That link contains a great snippet of code showing just how to apply the IOCTL. Kudos. (@unixman83: a service is not magic; it is the account that you configure the service to run as which makes the difference) – sehe Sep 13 '11 at 17:17
  • I realize that, but since most services by default run as Admin I doubt it's an issue. Also my app requires `SeBackupPrivledge` so it would work – unixman83 Sep 13 '11 at 17:35

2 Answers2

3

You need to send the IOCTL_DISK_SET_CACHE_INFORMATION Control Code using DeviceIoControl.


I suggest you use the Dskcache.exe tool to configure the "Power Protected" Write Cache option.

With W2K SP3 MS introduced the "Power Protected" Write Cache Option in additon to the "Write Caching" option. Basically, to have the FS driver to issue Flush/Write-Through commands you will need to set "Write Caching" option to Enabled and the "Power Protected" option to Disabled (see more info here: http://support.microsoft.com/?kbid=332023).1

.

1 source

sehe
  • 374,641
  • 47
  • 450
  • 633
2

This link Provided by Alex K. is my accepted answer: It deals with the IOCTL_DISK_SET_CACHE_INFORMATION DeviceIoControl()

http://blogs.msdn.com/b/dhawan/archive/2009/10/05/enable-or-disable-enable-write-caching-on-disk-behavior-on-disk.aspx

#define _WIN32_WINNT 0x0503

#include <windows.h>

DISK_CACHE_INFORMATION info;
DISK_CACHE_INFORMATION rinfo;


void main(void)
{
    DWORD rr;
    HANDLE hDevice;
    DWORD err;
    DWORD returned;

    hDevice = CreateFile("\\\\.\\C:", // drive to open
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_WRITE | FILE_SHARE_READ, 
                // share mode
                NULL, // default security attributes
                OPEN_EXISTING, // disposition
                FILE_ATTRIBUTE_SYSTEM, // file attributes
                NULL); // do not copy file attributes
    if(hDevice==INVALID_HANDLE_VALUE)
    {
        return;
    }

    rr = DeviceIoControl(hDevice,IOCTL_DISK_GET_CACHE_INFORMATION,NULL,
                        0,(LPVOID)&info,(DWORD)sizeof(info),(LPDWORD)&returned,    (LPOVERLAPPED)NULL);
    if (!rr)
    {
        err = GetLastError();
        return;
    }

    info.WriteCacheEnabled = true;
    info.ReadCacheEnabled = false;
    info.DisablePrefetchTransferLength = 1;

    rr = DeviceIoControl(hDevice,IOCTL_DISK_SET_CACHE_INFORMATION,(LPVOID)&info,(DWORD)sizeof(info),
                        NULL,0,(LPDWORD)&returned,(LPOVERLAPPED)NULL);
    if (!rr)
    {
        err = GetLastError();
        return;
    }

    rr = DeviceIoControl(hDevice,IOCTL_DISK_GET_CACHE_INFORMATION,NULL,0,
                        (LPVOID)&rinfo,(DWORD)sizeof(rinfo),(LPDWORD)&returned,(LPOVERLAPPED)NULL);
    if (!rr)
    {
        err = GetLastError();
        return;
    }

    CloseHandle(hDevice);
}

Old Information:
Windows 2K did contain a "Power Protected" Write Cache Option, but it was never carried over to Windows XP. Which makes a comment about using Dskcache.exe invalid. Was "Power Protected Mode" ever put back into e.g. Windows Vista? I do not know.

unixman83
  • 9,421
  • 10
  • 68
  • 102
  • 1
    unixman83, I understand that some of the information I presented was outdated/not applicable. However, I thought I'd present information that I stumbled upon while looking up the IOCTL. **PS** It's ok to accept your own answer** (IIRC) – sehe Sep 13 '11 at 17:33
  • 1
    -1 This is not a correct answer to the original question. `IOCTL_DISK_SET_CACHE_INFORMATION` will set the disk cache property and not the Windows drive cache property. It will NOT set the setting in the screenshot in your question, but instead, it will set the setting in the screenshot in the original article. The best article I have found is http://winntfs.com/2012/11/29/windows-write-caching-part-2-an-overview-for-application-developers/ is is very thorough, but also missing an option to query/set the filesystem write cache status. I haven't found any API documentation for the FS cache. – Felix Dombek Jul 11 '13 at 11:30