How to empty recycle bin programmatically?

21 06 2008


While processing or writing huge files to disk, its quite possible that disk will go out of space. To Squeeze and to get more disk space, its a good idea to clean the recycle bin. But how to do it programmatically?


You can use the api – SHEmptyRecycleBin(). See the code snippet below.

// Empty the recycle bin.
SHEmptyRecycleBin( NULL,
                   NULL,
                   SHERB_NOCONFIRMATION |
                   SHERB_NOPROGRESSUI   |
                   SHERB_NOSOUND );


By modifying the options, you can show progress UI, Show confirmation dialog and play sound on finishing task. Just remove the unwanted flags.


Targeted Audience – Beginners.

Advertisements




How to get the icon of a file?

3 06 2008


If you try to delete files in Vista, you can see the following dialog.

Did you noticed that, for every file, it will display its corresponding file icon. That’s pretty nice thing. If you are writing your own file browser tree or list, it will be nice to display the files with their own icons. But how can you get the file icon of a file?


You can use the call – SHGetFileInfo(). While calling the function, pass SHGFI_ICON as parameter. It will return handle for File icon. See the following code snippet.

// The classic FooBar. ;)
CString FilePath = _T("C:\\FooBar.zip");

// Get the file icon.
SHFILEINFO FileInfo = { 0 };
SHGetFileInfo( FilePath,
               0,
               &FileInfo,
               sizeof( FileInfo ),
               SHGFI_ICON );

// FileInfo.hIcon contains Icon handle.


There are more constants such as – SHGFI_ICONLOCATION, SHGFI_LARGEICON, SHGFI_SMALLICON etc. Seems they are self explanatory. ūüėČ


Targeted Audience – Intermediate.





How to get the path of Special Folders in windows.

1 05 2008


Windows have a number of special folder such as my documents, desktop folder etc. They are special because, their path can be different in system to system. So how can you get the path of special folder in windows?


You can use the api – SHGetSpecialFolderPath(). For each special folder there is a predefined ID called CSIDL. For instance, for windows system directory the CSIDL is CSIDL_WINDOWS. While calling the function, you’ve to provide the string buffer and the CSIDL and the requested special folder path is retuned in specified string buffer. See code snippet which fetches the path of Desktop directory.

// String buffer for holding the path.
TCHAR strPath[ MAX_PATH ];

// Get the special folder path.
SHGetSpecialFolderPath(
    0,       // Hwnd
    strPath, // String buffer.
    CSIDL_DESKTOPDIRECTORY, // CSLID of folder
    FALSE ); // Create if doesn't exists?


Wanna to see the list of CSLID of special folders? have a look at them. Their names are self explanatory.

CSIDL_ADMINTOOLS
CSIDL_ALTSTARTUP
CSIDL_APPDATA
CSIDL_BITBUCKET
CSIDL_COMMON_ADMINTOOLS
CSIDL_COMMON_ALTSTARTUP
CSIDL_COMMON_APPDATA
CSIDL_COMMON_DESKTOPDIRECTORY
CSIDL_COMMON_DOCUMENTS
CSIDL_COMMON_FAVORITES
CSIDL_COMMON_PROGRAMS
CSIDL_COMMON_STARTMENU
CSIDL_COMMON_STARTUP
CSIDL_COMMON_TEMPLATES
CSIDL_CONTROLS
CSIDL_COOKIES
CSIDL_DESKTOP
CSIDL_DESKTOPDIRECTORY
CSIDL_DRIVES
CSIDL_FAVORITES
CSIDL_FONTS
CSIDL_HISTORY
CSIDL_INTERNET
CSIDL_INTERNET_CACHE
CSIDL_LOCAL_APPDATA
CSIDL_MYMUSIC
CSIDL_MYPICTURES
CSIDL_NETHOOD
CSIDL_NETWORK
CSIDL_PERSONAL
CSIDL_PRINTERS
CSIDL_PRINTHOOD
CSIDL_PROFILE
CSIDL_PROGRAM_FILES
CSIDL_PROGRAM_FILES_COMMON
CSIDL_PROGRAMS
CSIDL_RECENT
CSIDL_SENDTO
CSIDL_STARTMENU
CSIDL_STARTUP
CSIDL_SYSTEM
CSIDL_TEMPLATES
CSIDL_WINDOWS


Targeted Audience – Beginners.





How to change the desktop wallpaper, programmatically.

27 04 2008


I bet at least once in your lifetime, you’ve used desktop wallpaper changing applications like webshots – which sets beautiful wallpaper to the desktop automatically after certain time intervals. When i started leaning windows programming, i wonder – how this application changes the wallpaper. The answer is – IActiveDesktop.


The IActiveDesktop is a com interface exposed by windows shell. You’ve to call the SetWallpaper() function and then apply the changes by calling ApplyChanges(). See the code snippet below.

#include "shlobj.h"
...

HRESULT hr;
IActiveDesktop* pActiveDesktop = 0;

// Initialize COM.
::CoInitialize( 0 );

// Get the ActiveDesktop Interface.
hr = CoCreateInstance( CLSID_ActiveDesktop,
                       0,
                       CLSCTX_INPROC_SERVER,
                       IID_IActiveDesktop,
                       (void**) &pActiveDesktop );

// Check whether CoCreateInstance is success.
if( FAILED( hr ))
{
    // Creating ActiveDesktop interface pointer failed.
    AfxMessageBox( _T("Error Occurred!"));
}

// SetWallpaper() accepts the wallpaper path only as WideChar.
LPCWSTR strWallPaper = L"C:\\Autumn Leaves.jpg";

// Set the new wallpaper.
pActiveDesktop->SetWallpaper( strWallPaper, 0 );

// Apply changes to refresh desktop.
pActiveDesktop->ApplyChanges( AD_APPLY_ALL );

// Release the interface pointer.
pActiveDesktop->Release();

// Uninitialize COM.
::CoUninitialize();


While compiling this, certainly you will hit the following error.

error C2065: 'IActiveDesktop' : undeclared identifier
error C2065: 'pActiveDesktop' : undeclared identifier
error C2106: '=' : left operand must be l-value

Don’t worry. Its a known issue. For solving this error, take StdAfx.h and include wininet.h header just before afxdisp.h. I assume you’re using MFC. See the sample below.

#include <afxwin.h>
#include <afxext.h>
#include "wininet.h"
#include <afxdisp.h>
#include <afxdtctl.h>

Keen to know more? See KB196342 for more details.


Trargeted Audience – Intermediate.