Spin Down Hard Disk Programmatically on Windows?

主宰稳场 提交于 2019-12-03 05:46:55

问题


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 make this happen?

I've tried making a program to send an ATA STANDBY command directly to the hard disk, but the problem is that this method doesn't inform the system, and hence whenever the system needs to flush the cache, it'll wake up the hard disk again. How do I tell the system to do this for me? (If the system does it, it'll save up the cache and "burst" the data when it gets too large, instead of writing in small increments.)

(The entire point here is to do this directly, not by changing the system-wide spin-down timeout to a 1-second period and waiting for the disk to spin down. I need a function I can call at a specific moment in time when I'm using my laptop, not something generic that doesn't suit 95% of situations.)


How far I've gotten so far:

I have a feeling that PoCallDriver and IRP_MJ_POWER might be useful for this, but I have very limited kernel-mode programming experience (and pretty much zero driver experience) so I really have no idea.


Please read:

Update:

People seem to be repeatedly mentioning the solutions that I have already mentioned do not work. Like I said above, I've already tried "hacky" solutions that change the timeout value or that directly issue the drive a command, and the entire reason I've asked this question here is that those did not do what I needed. Please read the entire question (especially paragraphs 2 and 3) before repeating what I've already said inside your answers -- that's the entire difficulty in the question.


More info:

I've found this document about Disk Idle Detection to be useful, but my answer isn't in there. It states that the Power Manager sends an IRP to the disk driver (hence why I suspect IRP_MJ_POWER to be useful), but I have no idea how to use the information.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/5367065/spin-down-hard-disk-programmatically-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!