Windows 2003 Defrag interface IFsuDefrag






4.60/5 (6 votes)
Jun 21, 2003
2 min read

73865

781
Using the Windows 2003 Server COM Defragmentation Interface
Introduction
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.
Background
The build-in defragmenter was only usable through the defrag MMC snap in. In Windows 2003 Server, I found new COM interfaces called IFsuDefrag
, IFsuAsync
and IFsuFormat
. IFsuDefrag
and 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).
Interface IFsuDefrag
This is the primary interface to initiate a defrag process. It contains the following methods:
HRESULT Defrag(LPWSTR pwszVolume, long fForce, IFsuAsync **ppAsyncOut)
. Use 1 if the defragmentation is forced (ignore if there is less than 15 % free diskspace). Otherwise 0.Starts the defragmentation of the volume specified by <CODE>pwszVolume
HRESULT DefragAnalysis(LPWSTR pwszVolume, IFsuAsync **ppAsyncOut)
instanceAnalyses the disk fragmentation.
</LI></UL> <H3>Remarks</H3> <UL> <LI>Both methods are giving back a pointer to an <CODE>IFsuAsync- Use the following notation for
pwszVolume
\\.\[DRIVE_LETTER]:\
e.g: "\\.\C:\"
Interface IFsuAsync
Calling 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:
HRESULT Cancel()
Used to stop the currentry running defragmentation task.
HRESULT Wait(HRESULT *pHrResult)
Used to wait for the process completion. E.g. after calling
Cancel()
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:.
Important note
The IFsuDefrag
and IFsuAsync
interfaces are only available on Windows 2003 Server.
Unknown stuff
There are still a few unknown things like:
- Possible error codes of
IFsuDefrag::Defrag()
. - The members of
DEFRAG_REPORT
are not yet known.
I defined it as:
struct DEFRAG_REPORT { ULONG ulThereIsSomeStuffHere[1000]; };
What I found out are the first 2 things:
struct DEFRAG_REPORT { WCHAR szVolumename[51]; WCHAR szVolumelabel[100]; };
So if you find out more information about DEFRAG_REPORT
or return values, please let me know.
Limitations
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.
History
- June, 16. 2002 - Initial Version 1.0