Vc. NET extends the capabilities of the Windows Disk Cleanup tool

Source: Internet
Author: User
Tags definition bool file size file system function definition new features tmp file versions
Introduction

The Windows Disk Cleanup tool (diskette CleanUp) is a practical, fast and Easy-to-use interface for System cleanup, and it is worth the attention of system development managers that this system cleanup software is built on the basis of COM technology and supports Third-party Plug-ins, And can be customized according to the needs of the function two development platform. Here, we delve into the development interface of the Windows Disk Cleanup tool and, on this basis, illustrate the ability to add a find "*.tmp" temporary file.

Technical discussion

Microsoft's COM technology is widely used in the modular design of Windows, resulting in support for two of times development. About COM Technology Foundation and application, can refer to. Here, we only decompose the Windows Disk Cleanup tool, the extension interface for the cleanup tool, for short. The cleanup tool first appeared on the Windows 98 operating system and was improved in later versions of Windows, adding new features. For example, under the NTFS file system, files that are infrequently accessed are automatically compressed. These new features are implemented through COM modules and invoked as Plug-ins in the cleanup tool. Earlier versions were invoked through the Iemptyvolumecache interface, and in later versions of Windows 2000, the IEmptyVolumeCache2 interface was added, adding smaller updates.

The Iemptyvolumecache interface consists of five functions, according to the order of the call, respectively:

Virtual/* [local]/HRESULT stdmethodcalltype Initialize (
/* [in] * * hkey Hkregkey,
/* [in] * * LPCWSTR Pcwszvolume,
/* [out] * * LPWSTR *ppwszdisplayname,
/* [out] * * LPWSTR *ppwszdescription,
/* [out] */DWORD *pdwflags) = 0;

Virtual HRESULT stdmethodcalltype getspaceused (
/* [out] * * Dwordlong *pdwlspaceused,
/* [in] * * iemptyvolumecachecallback *PICB) = 0;

Virtual HRESULT stdmethodcalltype showproperties (
/* [in] */hwnd hwnd) = 0;

Virtual HRESULT stdmethodcalltype Purge (
/* [in] * * Dwordlong Dwlspacetofree,
/* [in] * * iemptyvolumecachecallback *PICB) = 0;

Virtual HRESULT stdmethodcalltype Deactivate (
/* [out] */DWORD *pdwflags) = 0;

When the cleanup tool executes normally, it first invokes the initialize initialization plug-in and then executes the getspaceused to scan the file size that can be purged. Once the scan is complete, the main interface of the Cleanup tool appears as shown in Figure 1, where we have added the ability to clean the TMP file to browse different types of cleanup files. Each file type in the list is implemented by a COM plug-in. In addition to reading the file size can be cleaned, users can click on a customizable button to invoke the plugin's showproperties function to display more detailed information. If the user chooses OK, the cleanup tool calls the purge function to clean up the scanned files. Finally, the Deactivate function is invoked to terminate the application of the plug-in.

Plug-ins that apply to the Cleanup tool after Windows 2000 should also support Iemptyvolumecache interfaces. Iemptyvolumecache consists of only one function:

Virtual/* [local]/HRESULT stdmethodcalltype InitializeEx (

/* [in] * * hkey Hkregkey,
/* [in] * * LPCWSTR Pcwszvolume,
/* [in] * * LPCWSTR Pcwszkeyname,
/* [out] * * LPWSTR *ppwszdisplayname,
/* [out] * * LPWSTR *ppwszdescription,
/* [out] * * LPWSTR *ppwszbtntext,
/* [out] */DWORD *pdwflags) = 0;

InitializeEx adds more stringent localization language requirements, enhances internationalization support, and allows custom button display text. The pdwflags variable is used to pass information between tools and plug-ins to support the following flags:

Evcf_outofdiskspace
Evcf_settingsmode
Evcf_dontshowifzero
Evcf_enablebydefault
Evcf_enablebydefault_auto
Evcf_hassettings
Evcf_removefromlist

Evcf_outofdiskspace and Evcf_settingsmode are used for the settings that the tool passes to the plug-in. Evcf_outofdiskspace indicates that the current hard disk has very limited free space and needs to be cleaned as much as possible, even if the performance of the system is affected. Evcf_settingsmode represents an unattended mode that can be executed on a regular basis. In this mode, Getspaceused,purge, and showproperties will not be invoked, and all cleanup tasks should be performed InitializeEx. Other flags are used for different modes of operation that the plug-in passes to the tool. Evcf_dontshowifzero indicates that this type is not displayed when a removable file is not found, evcf_enablebydefault that this type of file can be safely deleted, evcf_enablebydefault_ Auto indicates that this type of file can be safely deleted, evcf_hassettings that this plugin supports showproperties features and can display detailed information. Evcf_removefromlist is a one-time cleanup task, and the cleanup tool automatically shuts down the plug-in after it is executed and is no longer executed.



Figure 1 The main interface of the cleanup tool

Implementation methods

We develop a new cleanup tool plugin that scans and cleans *. TMP file. There are a number of ways to program COM, and we chose the ATL library. About the application of the ATL library.

We built a new ATL DLL server project in Visual Studio. Net 2003 and joined the new ATL Simple Object control class Ccleansimplehandler with Add Class. In the definition, we let ccleansimplehandler inherit from IEmptyVolumeCache2. Also, we added the following variables:

Store the size of the scanned file

Dwordlong m_dwlfilesize;

Store root directory

WCHAR M_strrootdir[max_path];

Store scan out of file list

Std::vector<wchar *> M_lstfilestodel;

Then, we implement the functions of Iemptyvolumecache and IEMPTYVOLUMECACHE2 interfaces. In the following code list, the strict check error return value is not included. This is to shorten the length of the code to improve readability. In practical applications, it is essential to check for error return values. For different versions of Windows compatibility, we call initialize in InitializeEx.

HRESULT Ccleansimplehandler::initializeex (hkey hkey, Lpcwstr Pcwszvolume, LPCWSTR pcwszkeyname, LPWSTR * Ppwszdisplayname, LPWStr *ppwszdescription, LPWStr *ppwszbtntext, DWORD *pdwflags)
{
HRESULT hr = Initialize (hkey, Pcwszvolume, Ppwszdisplayname, Ppwszdescription, pdwflags);
*ppwszbtntext = (LPWSTR) cotaskmemalloc (sizeof (WCHAR));
Strcpyw (*ppwszbtntext, L "View files");
return HR;
}

HRESULT ccleansimplehandler::initialize (hkey hkey, Lpcwstr Pcwszvolume, LPWStr *ppwszdisplayname, LPWSTR * Ppwszdescription, DWORD *pdwflags)
{
Strcpyw (M_strrootdir, Pcwszvolume);
*ppwszdisplayname = (LPWSTR) cotaskmemalloc (256 * sizeof (WCHAR));
Strcpyw (*ppwszdisplayname, L "*.) TMP files ");
*ppwszdescription = (LPWSTR) cotaskmemalloc (256 * sizeof (WCHAR));
Strcpyw (*ppwszdescription, L "temporary files-*. TMP ");
*pdwflags = Evcf_hassettings | Evcf_enablebydefault;
m_dwlfilesize = 0;
return S_OK;
}

In getspaceused, we call Scandir to scan the *. TMP file, stored in M_lstfilestodel. The second parameter of the getspaceused is a pointer to the Iemptyvolumecachecallback interface, which is used to invoke its scanprogress function to report the progress of the scan. The Scanprogress function definition is:

HRESULT scanprogress (Dwordlong dwlspaceused, DWORD dwflags, Lpcwstr pwszreserved);

Where dwflags normal should be set to zero, at the end of the change to evccbf_lastnotification. The return value of the Scanprogress function is important because the user can interrupt the cleanup task in progress at any time. such as scanprogress return e_abort,getspaceused should be the fastest terminal scan, function return. Therefore, in the recursive directory scan function Scandir, we have added the function of breaking out immediately.

HRESULT ccleansimplehandler::getspaceused (Dwordlong *pdwspaceused, Iemptyvolumecachecallback *PICB)
{
m_dwlfilesize = 0;
Scandir (M_strrootdir, PICB);
Picb->scanprogress (M_dwlfilesize, evccbf_lastnotification, NULL);
*pdwspaceused = m_dwlfilesize;
return S_OK;
}

BOOL Ccleansimplehandler::scandir (WCHAR * szdir, Iemptyvolumecachecallback *PCIB)
{
WCHAR Strpath[max_path];
wchar* Pchpathfilename;
BOOL cancelled = false;
Win32_find_dataw FD;
HANDLE Hfind;

if (cancelled = FAILED (pcib->scanprogress (m_dwlfilesize, NULL, NULL)) return false;
Strcpyw (Strpath,szdir);
PATHAPPENDW (strpath, L "*");
Pchpathfilename = Strpath+lstrlenw (strpath)-1;
Hfind = Findfirstfilew (strpath, &AMP;FD);
if (hfind = = INVALID_HANDLE_VALUE)//e.g. Due to security issues
return true;
do {
Strcpyw (Pchpathfilename, fd.cfilename);
if ((Fd.dwfileattributes & File_attribute_directory)) {
if (fd.cfilename[0]!= '. ') {
if (cancelled =! Scandir (strpath, PCIB)) break;
}
} else {
wchar* Pchext = pathfindextensionw (strpath);
if (STRCMPIW (Pchext, L ". tmp") = = 0) {
M_dwlfilesize + = ((Dwordlong) fd.nfilesizehigh) *4294967295+fd.nfilesizelow;
wchar* filename = (WCHAR *) CoTaskMemAlloc ((Lstrlenw (strpath) +1) *sizeof (WCHAR));
STRCPYW (filename, strpath);
M_lstfilestodel.push_back (filename);
}
}

while (Findnextfilew (Hfind, &AMP;FD)!= NULL);
FindClose (hfind);
return!cancelled;
}

The other functions are simple. The Purge function deletes file one by one from the scanned list of files M_lstfilestodel. In ShowProperties, we show the scanned files. Finally, deactivate frees the allocated memory.

HRESULT Ccleansimplehandler::P urge (Dwordlong Dwspacetofree, Iemptyvolumecachecallback *PICB)
{
for (unsigned int i=0 i < m_lstfilestodel.size (); ++i)
Deletefilew (M_lstfilestodel[i]);
return S_OK;
}

HRESULT Ccleansimplehandler::showproperties (HWND hwnd)
{
for (unsigned int i=0 i < m_lstfilestodel.size (); ++i)
if (MessageBoxW (HWnd, m_lstfilestodel[i], L "View files",
mb_okcancel| mb_iconinformation) ==idcancel) break;
return S_OK;
}

HRESULT Ccleansimplehandler::D eactivate (Lpdword pdwflags)
{
for (unsigned int i=0 i < m_lstfilestodel.size (); ++i)
CoTaskMemFree (M_lstfilestodel[i]);
M_lstfilestodel.clear ();
*pdwflags = 0;
return S_OK;

Conclusions and recommendations

Through instance decomposition, we have studied the development interface of the Windows Disk Cleanup tool based on COM technology deeply. There are more development interfaces in the Windows shell, and the development ideas introduced in this article can be used in other extensions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.