?
Solved

how to empty the recycle bin?

Posted on 1998-05-18
30
Medium Priority
?
940 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 12
  • 3
  • +1
30 Comments
 
LVL 2

Expert Comment

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

connex


0
 
LVL 2

Expert Comment

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

connex


0
 

Author Comment

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

Don
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 3

Expert Comment

by:tma050898
ID: 1312045
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
ID: 1312046
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
ID: 1312047
>>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
ID: 1312048
Yet, the SHEmptyRecycleBin function must be used with the lastest shell32.dll and ie 4.
0
 

Author Comment

by:dmetzler
ID: 1312049
Any way to do it through COM/OLE?
0
 

Author Comment

by:dmetzler
ID: 1312050
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
ID: 1312051
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
ID: 1312052
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
ID: 1312053
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
ID: 1312054
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
ID: 1312055
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
ID: 1312056
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
 

Author Comment

by:dmetzler
ID: 1312057
Tom,

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

Don
0
 
LVL 3

Expert Comment

by:tma050898
ID: 1312058
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
ID: 1312059
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 1200 total points
ID: 1312060
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
ID: 1312061
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
ID: 1312062
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
ID: 1312063
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
ID: 1312064
Thanks, Tom.  I appreciate the effort!

Don
0
 

Author Comment

by:dmetzler
ID: 1312065
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
ID: 1312066
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
ID: 1312067
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
ID: 2792313
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
ID: 2792316
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
ID: 2794578
I think it did for me.  Let me check my code and get back to you.

don
0
 
LVL 2

Expert Comment

by:DarrinE
ID: 2795891
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 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