Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C++
Article

Windows 2003 Defrag interface IFsuDefrag

Rate me:
Please Sign up or sign in to vote.
4.60/5 (6 votes)
20 Jun 20032 min read 73.2K   780   12   11
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) 
    <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 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIFsuAsync Pin
GavinF6-Jun-05 7:20
GavinF6-Jun-05 7:20 
GeneralRe: IFsuAsync Pin
TeeBee3036-Jun-05 20:28
TeeBee3036-Jun-05 20:28 
GeneralRe: IFsuAsync Pin
GavinF7-Jun-05 7:12
GavinF7-Jun-05 7:12 
GeneralDEFRAG_REPORT Pin
Vlad Vissoultchev1-May-05 7:19
Vlad Vissoultchev1-May-05 7:19 
GeneralRe: DEFRAG_REPORT Pin
TeeBee3031-May-05 20:06
TeeBee3031-May-05 20:06 
GeneralIs there any way to continue the process Pin
Member 4433369-Dec-03 10:21
Member 4433369-Dec-03 10:21 
GeneralRe: Is there any way to continue the process Pin
TeeBee30310-Dec-03 0:35
TeeBee30310-Dec-03 0:35 
GeneralGreat article! Pin
eslea20-Jun-03 20:55
eslea20-Jun-03 20:55 
GeneralRe: Great article! Pin
TeeBee30320-Jun-03 22:20
TeeBee30320-Jun-03 22:20 
GeneralRe: Great article! Pin
John M. Drescher22-Jun-03 15:52
John M. Drescher22-Jun-03 15:52 
GeneralRe: Great article! Pin
TeeBee30322-Jun-03 18:57
TeeBee30322-Jun-03 18:57 
thx Smile | :)

i know this API functions, but it is a little bit hard to create a defrag algorithm. Wink | ;)

the build-in defragger in Windows 2003 server is now much more powerful than the previous versions. e.g. now he can also defrag the MFT.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.