Click here to Skip to main content
11,649,582 members (77,755 online)
Click here to Skip to main content

CFormatDriveDialog - A wrapper class for the undocumented SHFormatDrive API function

, 27 Aug 2002 139.4K 2.5K 34
Rate this:
Please Sign up or sign in to vote.
A wrapper class for SHFormatDrive (XP/2K only). Corrects some errors in KB article Q173688

Overview

This class basically brings up the Windows Disk-Format dialog box. You'd have thought it would have become part of the common dialogs by now. What's worse is that some of the information in KB article Q173688 is now incorrect as far as Windows XP and Windows 2000 are concerned. I presume that the KB article was written during the 9x days and they forgot to remove or update it later. What confused me was that the KB article  states that the information in it applies to XP and 2K. The first shock for me was to discover that the Quick-Format option behaved exactly opposite to how it was described in the KB article. In addition the Make-system-disk option was not even working properly. Anyway I've written a class - CFormatDriveDialog that basically wraps the stuff for you. I have stuck to the MS recommended constant definitions where they work, but in other cases I've either discarded them or added my own.

Strong warning

The information in KB article Q173688 is partially incorrect. Do not follow it blindly if you intend to build on this class. Also note that this class should only be used on Win2K/XP systems. The KB article is to be followed for older OS versions like 9x and ME.

Class Reference

There's just one public method here (of course, in addition to the parameter-less constructor).

DoModal

int CFormatDriveDialog::DoModal(HWND hWnd, UINT Drive, bool bQuickFormat, LPCTSTR vol_label);

  • hWnd - The window handle of the dialog's parent. This cannot be NULL.
  • Drive - The drive code (0 for A, 1 for B, 2 for C etc.) I've defined constants like DRIVEA, DRIVEB, DRIVEC etc. which you may use instead of raw numbers. They are defined in the header file.
  • bQuickFormat - If true then the Quick-Format check box is ticked by default.
  • vol_label - The default value for the volume label to be used.

Return Value

IDOK if a successful format was completed and IDCANCEL if the format operation was cancelled or if some error occurred.

Sample Usage

void Cnish_testDlg::OnBnClickedButton1()
{
    UpdateData(true);
    CFormatDriveDialog dlg;
    int d=dlg.DoModal(AfxGetMainWnd()->m_hWnd,DRIVEA,
        bQuickFormat,m_vollab);
    if(d==IDOK)
        MessageBox("yeah"); 
}

Tech Details

We call the SHFormatDrive function from shell32.dll. I use LoadLibrary and load shell32.dll and then call GetProcAddress to get SHFormatDrive's address. I also use SetWindowsHookEx to set a CBT hook because we need to set the volume label. The hook proc calls EnumChildWindows and when we encounter a child window with "Edit" class, we know this is the volume label edit box, because that's the only edit box in the dialog. Refer the source code for full details. A snipped partial listing is given below with comments not found in the actual source code.

int CFormatDriveDialog::DoModal(...)
{

    //...

    // Function pointer to hold SHFormatDrive
    FMTDRIVEFUNC* pshfd;

    // Load the dll
    HMODULE hMod = LoadLibrary("shell32.dll");

    // Chk for error - just in case
    if(hMod)
    {
        // Assign function pointer
        pshfd = reinterpret_cast<FMTDRIVEFUNC*>
            (GetProcAddress(hMod,"SHFormatDrive"));

        // Set a WH_CBT hook
        m_hHook = SetWindowsHookEx(WH_CBT,CBTProc,
            AfxGetApp()->m_hInstance,AfxGetApp()->m_nThreadID);

        // Call the function via the function pointer
        rv = ((*pshfd)(hWnd,Drive,SHFMT_ID_DEFAULT,Options)==
            SHFMT_FMTSUCCESS) ? IDOK : IDCANCEL;

        // Free the dll
        FreeLibrary(hMod);
    }
    
    //...
}

LRESULT CALLBACK CFormatDriveDialog::CBTProc(...)
{
    if (nCode == HCBT_ACTIVATE )
    { 
        // Get handle of Format dialog
        HWND hWnd = reinterpret_cast<HWND>(wParam);

        // Enumerate all child windows to get 
        // volume label the edit box
        EnumChildWindows(hWnd,EnumChildProc,NULL); 

        // Finished our need for the hook
        UnhookWindowsHookEx(m_hHook);
        CFormatDriveDialog::m_hHook = NULL; 
    }
    return FALSE;
}

BOOL CALLBACK CFormatDriveDialog::EnumChildProc(...)
{ 
    char buff[256];
    GetClassName(hwnd,buff,255);

    // The vol label edit box is the only edit control
    if(strcmp(buff,"Edit")==0)
    {
        SetWindowText(hwnd,m_vol_label);
        return FALSE;
    }
    return TRUE;
}

Conclusion

There are several return codes and option codes mentioned in the above mentioned KB article. But none of them work as expected, and some of them don't work at all. In addition the code constant given for Quick-Format works in the exact opposite manner. Of course I base my results from trying out the code on my own machine which is an XP professional box. I also got James T Johnson to test it partially on his XP professional box and Smitha(Tweety) to test it on her Windows 2000 professional box. But I have no idea how this class will work on untested OS versions with or without service packs.

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

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

 
GeneralVista problem Pin
Kranggg22-Oct-09 10:25
memberKranggg22-Oct-09 10:25 
Generala problem Pin
qiqi552125-Dec-08 15:45
memberqiqi552125-Dec-08 15:45 
Generalhi Pin
qiqi552123-Dec-08 17:07
memberqiqi552123-Dec-08 17:07 
Generalthe bigger the disk space , the greater the effect. Pin
qiqi552123-Dec-08 19:15
memberqiqi552123-Dec-08 19:15 
GeneralUmounting the drive Pin
joelparker2-Apr-04 6:45
memberjoelparker2-Apr-04 6:45 
Generalformat disk without dialog Pin
poganka24-Jan-04 21:20
memberpoganka24-Jan-04 21:20 
GeneralRe: format disk without dialog Pin
zbilal12-Mar-04 12:00
memberzbilal12-Mar-04 12:00 
GeneralRe: format disk without dialog ( How can I do .. ) Pin
ka.kevinliu29-Mar-07 20:09
memberka.kevinliu29-Mar-07 20:09 
GeneralRe: format disk without dialog ( How can I do .. ) Pin
George Grimes6-Apr-07 18:22
memberGeorge Grimes6-Apr-07 18:22 
GeneralDialog Pin
jlechem14-May-03 6:05
memberjlechem14-May-03 6:05 
GeneralNot a graphic interface and Hi Rating!!! Great!!! Pin
Gabriel 229-Aug-02 9:29
memberGabriel 229-Aug-02 9:29 
GeneralRe: Not a graphic interface and Hi Rating!!! Great!!! Pin
Nishant S29-Aug-02 14:03
editorNishant S29-Aug-02 14:03 
GeneralVery good article Pin
Matt Newman29-Aug-02 4:05
memberMatt Newman29-Aug-02 4:05 
GeneralRe: Very good article Pin
Nishant S29-Aug-02 14:03
editorNishant S29-Aug-02 14:03 
GeneralGood work Pin
Kannan Kalyanaraman28-Aug-02 20:38
memberKannan Kalyanaraman28-Aug-02 20:38 
GeneralRe: Good work Pin
Nishant S28-Aug-02 20:45
editorNishant S28-Aug-02 20:45 
GeneralIt Is Documented Pin
Blake Coverett28-Aug-02 12:27
memberBlake Coverett28-Aug-02 12:27 
GeneralRe: It Is Documented Pin
Nishant S28-Aug-02 12:34
editorNishant S28-Aug-02 12:34 
GeneralRe: It Is Documented #2 Pin
Nishant S28-Aug-02 12:38
editorNishant S28-Aug-02 12:38 
QuestionCan you Pin
Chris Losinger28-Aug-02 6:11
memberChris Losinger28-Aug-02 6:11 
find a way to make this work over the internet? i'd like to format my brother's HD, as a joke. he lives in NYC. and i want it to run without a taskbar icon, and i don't want it to appear on the process list.

oh, and please give me the source.

-c


A man is like a rusty wheel on a rusty cart,
He sings his song as he rattles along and then he falls apart.
-- Richard Thompson

AnswerRe: Can you Pin
Jörgen Sigvardsson28-Aug-02 7:25
memberJörgen Sigvardsson28-Aug-02 7:25 
GeneralRe: Can you Pin
Chris Losinger28-Aug-02 7:38
memberChris Losinger28-Aug-02 7:38 
AnswerRe: Can you Pin
Nishant S28-Aug-02 11:51
editorNishant S28-Aug-02 11:51 
GeneralRe: Can you Pin
Chris Losinger28-Aug-02 11:49
memberChris Losinger28-Aug-02 11:49 
GeneralRe: Can you Pin
Anonymous29-Aug-02 20:02
sussAnonymous29-Aug-02 20:02 
GeneralRe: Can you Pin
Chris Losinger29-Aug-02 20:07
memberChris Losinger29-Aug-02 20:07 
GeneralRe: Can you Pin
Nishant S29-Aug-02 20:30
editorNishant S29-Aug-02 20:30 
GeneralRe: Can you Pin
Chris Losinger29-Aug-02 20:32
memberChris Losinger29-Aug-02 20:32 
GeneralRe: Can you Pin
Nishant S29-Aug-02 20:36
editorNishant S29-Aug-02 20:36 
GeneralRe: Can you Pin
Nishant S29-Aug-02 20:28
editorNishant S29-Aug-02 20:28 
AnswerRe: Can you Pin
Vagif Abilov30-Aug-02 3:25
memberVagif Abilov30-Aug-02 3:25 
GeneralNice! Pin
Ravi Bhavnani28-Aug-02 2:18
memberRavi Bhavnani28-Aug-02 2:18 
GeneralRe: Nice! Pin
Nishant S28-Aug-02 11:48
editorNishant S28-Aug-02 11:48 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.4 | Last Updated 28 Aug 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid