Click here to Skip to main content
12,242,812 members (43,464 online)
Click here to Skip to main content
Add your own
alternative version

Stats

65.5K views
738 downloads
11 bookmarked
Posted

Windows 2003 Defrag interface IFsuDefrag

, 20 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

TeeBee303
Web Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralIFsuAsync Pin
GavinF6-Jun-05 8:20
memberGavinF6-Jun-05 8:20 
GeneralRe: IFsuAsync Pin
TeeBee3036-Jun-05 21:28
memberTeeBee3036-Jun-05 21:28 
GeneralRe: IFsuAsync Pin
GavinF7-Jun-05 8:12
memberGavinF7-Jun-05 8:12 
GeneralDEFRAG_REPORT Pin
Vlad Vissoultchev1-May-05 8:19
memberVlad Vissoultchev1-May-05 8:19 
GeneralRe: DEFRAG_REPORT Pin
TeeBee3031-May-05 21:06
memberTeeBee3031-May-05 21:06 
GeneralIs there any way to continue the process Pin
PMorrone9-Dec-03 11:21
memberPMorrone9-Dec-03 11:21 
GeneralRe: Is there any way to continue the process Pin
TeeBee30310-Dec-03 1:35
memberTeeBee30310-Dec-03 1:35 
GeneralGreat article! Pin
eslea20-Jun-03 21:55
membereslea20-Jun-03 21:55 
GeneralRe: Great article! Pin
TeeBee30320-Jun-03 23:20
memberTeeBee30320-Jun-03 23:20 
GeneralRe: Great article! Pin
John M. Drescher22-Jun-03 16:52
memberJohn M. Drescher22-Jun-03 16:52 
GeneralRe: Great article! Pin
TeeBee30322-Jun-03 19:57
memberTeeBee30322-Jun-03 19:57 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 21 Jun 2003
Article Copyright 2003 by TeeBee303
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid