Click here to Skip to main content
Licence 
First Posted 20 Jun 2003
Views 58,668
Bookmarked 11 times

Windows 2003 Defrag interface IFsuDefrag

By | 20 Jun 2003 | Article
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)

    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)

    Analyses the disk fragmentation.

    </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

About the Author

TeeBee303

Web Developer

Switzerland Switzerland

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIFsuAsync PinmemberGavinF7:20 6 Jun '05  
GeneralRe: IFsuAsync PinmemberTeeBee30320:28 6 Jun '05  
GeneralRe: IFsuAsync PinmemberGavinF7:12 7 Jun '05  
GeneralDEFRAG_REPORT PinmemberVlad Vissoultchev7:19 1 May '05  
GeneralRe: DEFRAG_REPORT PinmemberTeeBee30320:06 1 May '05  
GeneralIs there any way to continue the process PinmemberPMorrone10:21 9 Dec '03  
GeneralRe: Is there any way to continue the process PinmemberTeeBee3030:35 10 Dec '03  
GeneralGreat article! Pinmembereslea20:55 20 Jun '03  
GeneralRe: Great article! PinmemberTeeBee30322:20 20 Jun '03  
GeneralRe: Great article! PinmemberJohn M. Drescher15:52 22 Jun '03  
GeneralRe: Great article! PinmemberTeeBee30318:57 22 Jun '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 21 Jun 2003
Article Copyright 2003 by TeeBee303
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid