Spin Down Hard Disk Programmatically on Windows?

前端 未结 5 388
名媛妹妹
名媛妹妹 2021-02-01 19:44

How do you request Windows to spin down a hard disk programmatically? Is there any user-mode function I can call (or kernel-mode function to call or IRP to send) in order to mak

相关标签:
5条回答
  • 2021-02-01 20:07

    Have you tried WMI? Based on MSDN documentation, you should be able to send spindown command to HDD via WMI:

    http://msdn.microsoft.com/en-us/library/aa393493%28v=VS.85%29.aspx

    uint32 SetPowerState(
      [in]  uint16 PowerState,
      [in]  datetime Time
    );
    

    EDIT:

    This code lists all drives in system and drives that support this API:

    strServer = "."
    
    Set objWMI = GetObject("winmgmts://" & strServer & "/root\cimv2")
    rem Set objInstances = objWMI.InstancesOf("CIM_DiskDrive",48)
    Set objInstances = objWMI.ExecQuery("Select * from CIM_DiskDrive",,48) 
    On Error Resume Next
    For Each objInstance in objInstances
        With objInstance
            WScript.Echo Join(.Capabilities, ", ")
            WScript.Echo Join(.CapabilityDescriptions, ", ")
            WScript.Echo .Caption
            WScript.Echo .PNPDeviceID
            WScript.Echo "PowerManagementCapabilities: "  & .PowerManagementCapabilities
            WScript.Echo "PowerManagement Supported: " & .PowerManagementSupported
            WScript.Echo .Status
            WScript.Echo .StatusInfo
        End With
    On Error Goto 0
    Next
    

    Just save this code as a .vbs file and run that from command line.

    0 讨论(0)
  • 2021-02-01 20:11

    I believe the Devcon Command line utility should be able to accomplish what you need to do. If it does - the source code is available in the Windows Ddk.

    0 讨论(0)
  • 2021-02-01 20:13

    I do not have an answer to the specific question that Mehrdad asked.

    However, to help others who find this page when trying to figure out how to get their disk to standby when it should but doesn't:

    1. I found that on a USB disk, MS PwrTest claims that the disk is off, but actually it is still spinning. This occurs even with really short global disk timeouts in win 7. (This implies that even if the system thinks it has turned the disk off, it might not actually be off. Consequently, Mehrdad's original goal might not work even if the correct way to do it is found. This may relate to how various USB disk controllers implement power state.)

    2. I also found that the program HDDScan successfully can turn off the disk, and can successfully set a timeout value that the disk honors. Also, the disk spins up when it is accessed by the OS, a good thing if you need to use it, but not so good if you are worrying about it spinning up all the time to flush 1kB buffers. (I chose to set the idle timeout in HDDScan to 1 minute more than the system power manager timeout. This hopefully assures that the system will not think the disk is spun up when it is not.)

    I note that powercfg has an option to prevent the idle clock from restarting from small infrequent disk writes. (Called "burst ignore time.")

    You can get HDDScan here: HDDScan.com and PwrTest here: Windows Driver Kit. Unfortunately, the PwrTest thing forces you to have a lot of other MS stuff installed first, but it is all free if you can figure out how to download it from their confusing web pages.

    0 讨论(0)
  • 2021-02-01 20:19

    I hope this helps:

    This: http://msdn.microsoft.com/en-us/library/aa394173%28VS.85%29.aspx

    Leads to this: http://msdn.microsoft.com/en-us/library/aa394132%28VS.85%29.aspx#properties

    Then, you can browse to this: http://msdn.microsoft.com/en-us/library/aa393485(v=VS.85).aspx

    This documentation seems to outline what you are looking for I think.

    P.S. Just trying to help, don't shoot the messanger.

    0 讨论(0)
  • 2021-02-01 20:25

    While there is no apparent way to do what you're asking for (i.e. tell power management "act as if the timer for spinning down the disk has expired"), there may be a couple ways to simulate it:

    1. Call FlushFileBuffers on the drive (you need to be elevated to open \\.\C), then issue the STANDBY command to the drive.

    2. Make the API call that sets the timeout for spinning down the disk to 1 second, then increase it back to its former value after 1 second. Note that you may need to ramp up to the former value rather than immediately jump to it.

    0 讨论(0)
提交回复
热议问题