Click here to Skip to main content
15,893,486 members
Articles / Desktop Programming / MFC

CFormatDriveDialog - A wrapper class for the undocumented SHFormatDrive API function

Rate me:
Please Sign up or sign in to vote.
5.00/5 (20 votes)
27 Aug 20023 min read 189K   3K   36  
A wrapper class for SHFormatDrive (XP/2K only). Corrects some errors in KB article Q173688
/*
	Author - Nishant S
	EMail - nishforever@vsnl.com
*/

#include "StdAfx.h"
#include "formatdrive.h"

HHOOK CFormatDriveDialog::m_hHook=NULL;
LPCTSTR CFormatDriveDialog::m_vol_label = NULL;

CFormatDriveDialog::CFormatDriveDialog(void)
{
}

CFormatDriveDialog::~CFormatDriveDialog(void)
{
}

int CFormatDriveDialog::DoModal(HWND hWnd, UINT Drive, bool bQuickFormat,
								LPCTSTR vol_label)
{
	ASSERT(hWnd);
	m_vol_label = vol_label;
	int rv = 0;
	UINT Options = bQuickFormat ? SHFMT_OPT_QUICK : 0;
	FMTDRIVEFUNC* pshfd;
	HMODULE  hMod = LoadLibrary("shell32.dll");
	if(hMod)
	{
		pshfd = reinterpret_cast<FMTDRIVEFUNC*>(GetProcAddress(hMod,"SHFormatDrive"));
		m_hHook = SetWindowsHookEx(WH_CBT,CBTProc,
			AfxGetApp()->m_hInstance,AfxGetApp()->m_nThreadID);
		rv = ((*pshfd)(hWnd,Drive,SHFMT_ID_DEFAULT,Options)==SHFMT_FMTSUCCESS) ? IDOK : IDCANCEL;
		FreeLibrary(hMod);
	}
	return rv;
}

LRESULT CALLBACK CFormatDriveDialog::CBTProc(int nCode,WPARAM wParam, LPARAM lParam)
{
	if (nCode == HCBT_ACTIVATE )
	{	
		HWND hWnd = reinterpret_cast<HWND>(wParam);
		
		EnumChildWindows(hWnd,EnumChildProc,NULL);	
		
		UnhookWindowsHookEx(m_hHook);
		CFormatDriveDialog::m_hHook = NULL;		
	}
	return FALSE;
}

BOOL CALLBACK CFormatDriveDialog::EnumChildProc( HWND hwnd, LPARAM lParam )
{	
	char buff[256];
	GetClassName(hwnd,buff,255);
	if(strcmp(buff,"Edit")==0)
	{
		SetWindowText(hwnd,m_vol_label);
		return FALSE;
	}
	return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and 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 experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions