开发者

How to enable "Better Performance" on an External USB HD programatically in C/C++

开发者 https://www.devze.com 2023-04-04 17:37 出处:网络
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 Wi

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.

How to enable "Better Performance" on an External USB HD programatically in C/C++


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消