Solved

how to empty the recycle bin?

Posted on 1998-05-18
30
903 Views
Last Modified: 2013-11-20
How can I pragmatically empty the recycle bin?

I have been able to count items in the bin by:

SHGetDesktopFolder() and SHGetSpecialFOlderLocation().  I then use BindToObject() to bind the desktop to the recycle bin and EnumObjects() with the IEnumIDList interface.

But how do I empty the recycle bin?
0
Comment
Question by:dmetzler
  • 13
  • 12
  • 3
  • +1
30 Comments
 
LVL 2

Expert Comment

by:connex
Comment Utility
Never did it myself, but i believe SHFileOperation() should
do it.

connex


0
 
LVL 2

Expert Comment

by:connex
Comment Utility
Never did it myself, but i believe SHFileOperation() should
do it.

connex


0
 

Author Comment

by:dmetzler
Comment Utility
I believe that will allow items to be placed in the recycle bin, but that will not handle emptying the recycle bin.

Don
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
You need to call the SHGetSpecialFolderPath function with CSIDL_BITBUCKET as the nFolder argument. This function will give you the actual recycle bin folder. Once you have the folder name, you can delete the files as you normally would. I personally use _unlink when deleting files, but there are also Win32 functions to do the same thing.

Tom
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
Ok. I've done some more research. I think the following api is what you need...

SHEmptyRecycleBin

You can find the documentation on how to use it at...

http://www.eu.microsoft.com/msdn/sdk/inetsdk/help/itt/Shell/Functions/SHEmptyRecycleBin.htm

HTH,
Tom
0
 

Author Comment

by:dmetzler
Comment Utility
>>You need to call the SHGetSpecialFolderPath function with CSIDL_BITBUCKET as the nFolder argument.

Is this available in all versions of SHELL32.DLL?  Or is this new only to IE 4.0?  I need a solution for any version of Win95/NT if possible.
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
Yet, the SHEmptyRecycleBin function must be used with the lastest shell32.dll and ie 4.
0
 

Author Comment

by:dmetzler
Comment Utility
Any way to do it through COM/OLE?
0
 

Author Comment

by:dmetzler
Comment Utility
The SHEmptyRecycleBin() is not guaranteed to be a part of all implementations of Windows/IE.  I need a solution which using the Shell functions (via COM/OLE) to do this.
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
COM is nothing more than a binary interface standard. It doesn't automatically make a given function exist on a given platform if that function is not implemented to begin with. In other words, SHEmptyRecycleBin is a shell function in that it exists in the latest version of shell32.dll. Whether you use this exported function in the standard manner of implicitly loading the dll and calling its function or whether you call a com interface that wraps the same function, the point is that the underlying shell32.dll has to implement the function.

0
 

Author Comment

by:dmetzler
Comment Utility
Are you certain that is the case?  I believe there is a lot of Windows functionality is available via COM where there are no direct exported API calls.

Due to the fact that Windows is able to access the recycle bin and empty it and that the recycle bin is nothing more than an ActiveX shell extension, I would presume that COM would work over SHEmptyRecycleBin() no matter which version of Windows95 is being used.

What I am really looking for is how to get at the IContextMenu for the recycle bin via COM to the shell and issue a command 0x72.

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
FINALLY!!! I think I just tracked down what you need...

1. Call CoCreateInstance using the recycle bin's
CLSID
2. Use the IShellExtInit interface to initialize it with the recycle bin's pidl
3. Obtain an IContextMenu interface and invoke command 0x72.

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
I guess it would have helped had I mentioned what the clsid was,huh ;)

HKEY_CLASSES_ROOT\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}

0
 

Author Comment

by:dmetzler
Comment Utility
What does this code look like, exactly?  I found something similar through DejaNews, but nothing specific as far as implementation.

1. Call CoCreateInstance using the recycle bin's
CLSID
2. Use the IShellExtInit interface to initialize it with the recycle bin's pidl
3. Obtain an IContextMenu interface and invoke command 0x72.
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
dmetzler,

Sorry I couldn't be more specific, but I've never done it either. However, I've played with it for a while and have done the following. I found the recycle bin's clsid, figured out how to create the pidl for the recycle bin and was able to get the IShellExtInit::Initialize to work. Now, i'm trying to get the IContextMenu so that I can call its InvokeCommand. However, its 2 and I have to be up by 5:30 so I'll try this some more tomorrow.

Tom

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dmetzler
Comment Utility
Tom,

Thanks - I'll double the points for you if you can this.

Don
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
BTW, if I ever do get this 72 command to work, won't the end-user see a delete confirmation dialog? Is that not a problem?
0
 

Author Comment

by:dmetzler
Comment Utility
Hmmm...I don't know about that - you could be correct.  However, I think I can turn that off (at least temporarily) in the registry.
0
 
LVL 3

Accepted Solution

by:
tma050898 earned 400 total points
Comment Utility
CHA-CHING, BABY!!! Try this and lemme know what results you get. (crossing fingers...)

#include "objbase.h"
#include "shlobj.h"

// GUID for Recyle Bin
// {645FF040-5081-101B-9F08-00AA002F954E}
static const GUID CLSID_RecyleBin =
{ 0x645ff040, 0x5081, 0x101b, { 0x9f, 0x08, 0x00, 0xaa, 0x00, 0x2f, 0x95, 0x4e } };

void CEmptyRecycleBinDlg::OnOk()
{
 BOOL bSuccess = FALSE;

 ::CoInitialize(NULL);

 IShellExtInit* pIShellExtInit;
 HRESULT hr = ::CoCreateInstance(CLSID_RecyleBin,
  NULL,
  CLSCTX_INPROC_SERVER,
  IID_IShellExtInit,
  (void**)&pIShellExtInit
 );

 if (SUCCEEDED(hr))
 {
  LPITEMIDLIST psyspidl = NULL;
  hr = SHGetSpecialFolderLocation(NULL, CSIDL_BITBUCKET, &psyspidl);
  if (SUCCEEDED(hr))
  {
   LPDATAOBJECT pdataObject;
   hr = pIShellExtInit->Initialize(psyspidl, NULL, HKEY_CLASSES_ROOT);
   if (SUCCEEDED(hr))
   {
    IContextMenu* pIContextMenu;
    HRESULT hr = ::CoCreateInstance(CLSID_RecyleBin,
     NULL,
     CLSCTX_INPROC_SERVER,
     IID_IContextMenu,
     (void**)&pIContextMenu
    );
    if (SUCCEEDED(hr))
    {
     CMINVOKECOMMANDINFO cmi;
     ::ZeroMemory(&cmi, sizeof(CMINVOKECOMMANDINFO));

     cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
     cmi.fMask = 0;
     cmi.hwnd = m_hWnd;
     cmi.lpParameters = NULL;
     cmi.lpDirectory = NULL;
     cmi.nShow = SW_SHOWNORMAL;
     cmi.dwHotKey = 0;
     cmi.hIcon = NULL;
     cmi.lpVerb = MAKEINTRESOURCE(0x72);

     hr = pIContextMenu->InvokeCommand(&cmi);
     if (SUCCEEDED(hr))
     {
      bSuccess = TRUE;
     }
    }
   }
  }
 }
 ::CoUninitialize();

 AfxMessageBox((TRUE == bSuccess ? "SUCCESS" : "FAILURE"));
}

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
I cleaned up the code a little bit. I still had a bunch of "orphan code" from the different approaches I had tried ;)

Although this works to some extent, I still have 2 bugs that I am chasing down...

void CEmptyRecycleBinDlg::OnOk()
{
 BOOL bSuccess = FALSE;
 HRESULT hr;

 ::CoInitialize(NULL);

 // Get recycle bin's pidl
 LPITEMIDLIST psyspidl = NULL;
 hr = SHGetSpecialFolderLocation(NULL, CSIDL_BITBUCKET, &psyspidl);
 if (SUCCEEDED(hr))
 {
  // Get a IContextMenu ptr interface for the Recyle Bin
  IContextMenu* pIContextMenu = NULL;
  HRESULT hr = ::CoCreateInstance(CLSID_RecyleBin,
   NULL,
   CLSCTX_INPROC_SERVER,
   IID_IContextMenu,
   (void**)&pIContextMenu
  );

  if (SUCCEEDED(hr))
  {
   // Send the "Empty Recyle Bin" command
   CMINVOKECOMMANDINFO cmi;
   ::ZeroMemory(&cmi, sizeof(CMINVOKECOMMANDINFO));

   cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
   cmi.fMask = 0;
   cmi.hwnd = GetSafeHwnd();
   cmi.lpParameters = NULL;
   cmi.lpDirectory = NULL;
   cmi.nShow = SW_SHOWNORMAL;
   cmi.dwHotKey = 0;
   cmi.hIcon = NULL;
   cmi.lpVerb = MAKEINTRESOURCE(0x72);

   hr = pIContextMenu->InvokeCommand(&cmi);
   if (SUCCEEDED(hr))
   {
    bSuccess = TRUE;
   }
  }
 }
 ::CoUninitialize();
 AfxMessageBox((TRUE == bSuccess ? "SUCCESS" : "FAILURE"));
}

Tom

0
 

Author Comment

by:dmetzler
Comment Utility
That worked!  Now I need to have it happen without asking the user (ie they have already confirmed in my program).

Thanks!

Don

0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
Don,

Actually, I should also thank you. Thank you for not letting me slide by with a "you kinda do this and then you kinda do that" answer. Because you insisted on a real answer, I put in the extra effort necessary and learned something new in the process.

As I said, my "resolution" is not perfect, but when I get all the kinks worked out and wrap it with a C++ class, I'll send you the new version.

Tom

0
 

Author Comment

by:dmetzler
Comment Utility
Thanks, Tom.  I appreciate the effort!

Don
0
 

Author Comment

by:dmetzler
Comment Utility
Tom,

Were you going to put together this into a C++ class?  Also - any idea on how to turn off recycle bin emptying notification?

Thanks,

Don
0
 
LVL 3

Expert Comment

by:tma050898
Comment Utility
Sorry. We're in deadline mode on our latest book (Visual C++ 6 Bible) and I haven't had any more time to devote to it. This weekend I will be finished with my last chapter and I planned on finally uploading a lot of my code to my web site. I'll post when its up (probably by Monday).

Tom

0
 

Author Comment

by:dmetzler
Comment Utility
Thanks - if you would send an email to me directly (barefoot@pobox.com) I'd appreciate it (off topic for this).

Don
0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
Sorry for butting in guys (like its two years ago since you all spoke - but does this code work on ALL versions of windows9? ??
0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
Sorry for butting in guys (like its two years ago since you all spoke - but does this code work on ALL versions of windows9? ??
0
 

Author Comment

by:dmetzler
Comment Utility
I think it did for me.  Let me check my code and get back to you.

don
0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
Greatly appreciated if you can - I've managed to get the code to work in C with a little help from NickRepin - Many thanks for the response

DarrinE
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now