With Windows NT4, Executive Software introduced the defragmentation API in Windows. Later, Windows 2000 was the first Windows version that included a built-in defragmenter. Now with Windows 2003 Server, Microsoft introduced the
IFsuDefrag interface which is very easy to use.
The build-in defragmenter was only usable through the defrag MMC snap in. In Windows 2003 Server, I found new COM interfaces called
IFsuAsync are giving us the ability to:
- Start and stop defragmentation
- Analyze volume fragmentation
It took a while until I found out how to use these interfaces, but there are still some unknown things... See below (Unknown Stuff).
This is the primary interface to initiate a defrag process. It contains the following methods:
HRESULT Defrag(LPWSTR pwszVolume, long fForce, IFsuAsync **ppAsyncOut)
<P>Starts the defragmentation of the volume specified by <CODE>pwszVolume. Use 1 if the defragmentation is forced (ignore if there is less than 15 % free diskspace). Otherwise 0.
HRESULT DefragAnalysis(LPWSTR pwszVolume, IFsuAsync **ppAsyncOut)
<P>Analyses the disk fragmentation.</P></LI></UL> <H3>Remarks</H3> <UL> <LI>Both methods are giving back a pointer to an <CODE>IFsuAsync instance
- Use the following notation for
IFsuDefrag::Defrag ( , , IFsuAsync **ppAsyncOut) gives back an instance of
IFsuAsync which can be used to:
- Stop the defragmentation
- Get the status of the task
IFsuDefrag contains the following methods:
Used to stop the currentry running defragmentation task.
HRESULT Wait(HRESULT *pHrResult)
Used to wait for the process completion. E.g. after calling
HRESULT QueryStatus(HRESULT *pHrResult, unsigned long *pulPercentCompleted)
Get the completion status of the current task.
HRESULT GetDefragReport(_DEFRAG_REPORT *pAsyncOut)
Gets detailed information about disk defragmentation (the members of
_DEFRAG_REPORT are unknown)
Using the code
Download the source and demo project to get an overview and a small console app which demonstrates the interruptable defragmentation of the drive C:.
IFsuAsync interfaces are only available on Windows 2003 Server.
There are still a few unknown things like:
- Possible error codes of
- The members of
DEFRAG_REPORT are not yet known.
I defined it as:
What I found out are the first 2 things:
So if you find out more information about
DEFRAG_REPORT or return values, please let me know.
Only one instance of the defrag engine can run at the same time!
Other ways of starting the defrag process
With Windows 2003 Server, Microsoft also introduced the WMI class
Win32_Volume. This WMI class can also be used to start the defragmentation, but there is no possibility to stop the defragmentation.
- June, 16. 2002 - Initial Version 1.0