Click here to Skip to main content
Click here to Skip to main content

Clean Up Handler

By , 9 Jun 2002
 

Sample Image - cleanup.jpg

Introduction

Windows introduced a new concept when it let the user add new handlers to the shell, such as a context menu handler, icon handler and also for other applications such as the cleanup manager.

Generally all the handlers implement a certain interface (one or more) and register the component by adding some keys to the registry.

Implementation

This Application is a simple COM component that implement the IEmptyVolumeCache2 interface and its 6 methods.

//
//
Class CExtHandler : public IEmptyVolumeCache2 {
	
     //IUnknown interface 
    ...

    //IEmptyVolumeCache interface 
    STDMETHODIMP Initialize(
             HKEY hkRegKey,
             LPCWSTR pcwszVolume,
             LPWSTR *ppwszDisplayName,
             LPWSTR *ppwszDescription,
             DWORD *pdwFlags);

    STDMETHODIMP GetSpaceUsed( 
             DWORDLONG *pdwlSpaceUsed,
             IEmptyVolumeCacheCallBack *picb);
     
     STDMETHODIMP Purge( 				// Called when it's time to delete.
             DWORDLONG dwlSpaceToFree,
             IEmptyVolumeCacheCallBack *picb);
        
    STDMETHODIMP ShowProperties(HWND hwnd);
        
    STDMETHODIMP Deactivate(DWORD *pdwFlags);

    //IEmptyVolumeCache2 interface 
    STDMETHODIMP InitializeEx (HKEY hKey, LPCWSTR pcwszVolume, LPCWSTR pcwszKeyName,
                               LPWSTR *ppwszDisplayName, LPWSTR *ppwszDescription, 
                               LPWSTR *ppwszBtnText, DWORD *pdwFlags);

How it works:

  • You first register the server (through regsrv32.exe, or indirectly by reg.bat) and you'll be presented with
    a dialog box, where you could fill the fields appropriately or let those default values, if you want the clean
    handler to delete the intermediate visual c++ files.
  • Once the extension is registered, it can be accessed by starting CLEANMGR.EXE or by clicking the "Disk Cleanup" button on a drive's Properties dialog in Explorer. The Properties dialog can be accessed by right-clicking on a drive in Explorer and selecting "Properties" from the context menu or by clicking on a drive in Explorer and selecting File then Properties.
  • When you don't need it anymore simply unregister the server, and delete the DLL file.

Conclusion

When I first registered my component and ran the cleanup manager (cleanmgr.exe), thus launching my brand-new cleanup handler, I was amazed by the the size of the intermediate files that Visual C++ has generated for its need (about 200 Mb).

Though this handler was intended to delete the files that visual C++ has generated, it can also be used for other application temporary files or other purpose.

It has been tested only on windows XP, please tell me it work on your machine. If you found this application of help, let me know at blackhorus@caramail.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Black Horus
Software Developer (Senior)
Canada Canada
Member
Begin programming in pascal some 8 years ago, then to assembly and C, after seeing that pascal is out of fashion. play a little with Delphi and C++ Builder, to see that was enough.
 

A little mess with other technologies, HTML, CGI, DHTML, PHP.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDLLRegisterServermemberPeter Ritchie27 Jan '04 - 7:30 
It's not a good idea to "override" DLLRegisterServer with configuration UI. This DLL entry point is meant for automated OLE/COM registry entr(y|ies) creation. A setup program would typically call this entrypoint to install, not configure a component.
 
The DllInstall entry-point is typically used if the DLL performs other-than-COM self-setup/install. It can be given a command-line so the DLL can be installed in more than one way.
 
Like DllRegisterServer, DllInstall should not provide any GUI, simply a return code indicating success or failure. A seperate installation program would provide the UI to generate the command-line for the DLLInstall entrypoint.
GeneralUnicode BuildmemberBlake Miller29 Aug '03 - 9:23 
I ran into problem with the GetStringByteSizeUnicode function as it was in the source, with the compiler warnings at level 4.
 
I think you need to make the parameter like this:
GetStringByteSizeUnicode (LPCTSTR pszString)

Generalhelp !!!membermr_nishantpant6 Feb '03 - 14:38 
Hi,
Nice article. But I need 1 help. How do I schedule the Disk Cleaner to run in the night without popping up any UI.
 
Currently, it pops up a UI asking which drive I want to clean Frown | :( ..and keeps on asking questions. Isn't there any command line param which will make it run in silent mode ?
GeneralRe: help !!!memberDavidCrow30 May '03 - 7:21 
Do any of the command-line switches for Disk Cleanup (cleanmgr.exe) work for you?
GeneralRe: help !!!membermr_nishantpant30 May '03 - 8:08 
Yes both work for me.
First I run sageset. It pops up a UI and I select which all items I want to run with configuration 1
 
Later on when I want to run it. I simply use the sagerun option.
CLEANMGR /SAGESET:1
CLEANMGR /SAGERUN:1
 

GeneralRe: help !!!memberBlake Miller20 Aug '03 - 15:34 
The problem might be that the source does not follow the Initialize(Ex) API properly.
HRESULT InitializeEx(
HKEY hkRegKey,
LPCWSTR pcwszVolume,
LPCWSTR pcwszKeyName,
LPWSTR *ppwszDisplayName,
LPWSTR *ppwszDescription,
LPWSTR *ppwszBtnText,
DWORD *pdwFlags
);
 
According to the documentation, the dwFlags is an In/Out parameter.
 
These flags can be passed in to the object:
 
EVCF_OUTOFDISKSPACE
If this flag is set, the user is out of disk space on the drive. When this flag is received, the handler should be aggressive about freeing disk space, even if it results in a performance loss. The handler, however, should not delete files that would cause an application to fail or the user to lose data.
 
EVCF_SETTINGSMODE
If the disk cleanup manager is being run on a schedule, it will set the EVCF_SETTINGSMODE flag. You must assign values to the ppwszDisplayName and ppwszDescription parameters. If this flag is set, the disk cleanup manager will not call GetSpaceUsed, Purge, or ShowProperties . Because Purge will not be called, cleanup must be handled by InitializeEx. The handler should ignore the pcwszVolume parameter and clean up any unneeded files regardless of what drive they are on. Because there is no opportunity for user feedback, only those files that are extremely safe to clean up should be touched.
 
I can not see that these two flags are processed by the source file.
This could be why the module does not respond to a scheduled run very well.

GeneralError!!memberWREY20 Sep '02 - 15:46 
After registering the server, I ran "regsvr32.exe" (followed by directory in which the ".dll" is located) and obtained the dialog your article talks about. But when I clicked the 'Cancel' button, RegSvr32 produced the following error message.
 
"DllRegisterServer in (... name of directory & path shown here) failed."
 
Return code was: 0x80004004
 
==============================
 
Also I noticed if I were to click on the Close button in the upper right corner of the dialog, NOTHING happens!
 
Unsure | :~
 
William
GeneralRe: Error!!sussAnonymous21 Sep '02 - 2:39 
0x80004004 == Operation cancelled
GeneralNicememberAndreas Saurwein11 Jun '02 - 0:40 
Combine that with the DirClean Shell Extenstion from M.Dunn and you have a perfect tool that is really needed by all (VC) developers.
 
Nice article
 
Vote against software patents in europe
GeneralRe: NicememberBlack Horus13 Jun '02 - 13:57 
ThanksBig Grin | :-D

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 10 Jun 2002
Article Copyright 2002 by Black Horus
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid