Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 951
  • Last Modified:

how to empty the recycle bin?

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
dmetzler
Asked:
dmetzler
  • 13
  • 12
  • 3
  • +1
1 Solution
 
connexCommented:
Never did it myself, but i believe SHFileOperation() should
do it.

connex


0
 
connexCommented:
Never did it myself, but i believe SHFileOperation() should
do it.

connex


0
 
dmetzlerAuthor Commented:
I believe that will allow items to be placed in the recycle bin, but that will not handle emptying the recycle bin.

Don
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tma050898Commented:
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
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
>>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
 
tma050898Commented:
Yet, the SHEmptyRecycleBin function must be used with the lastest shell32.dll and ie 4.
0
 
dmetzlerAuthor Commented:
Any way to do it through COM/OLE?
0
 
dmetzlerAuthor Commented:
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
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
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
 
tma050898Commented:
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
 
tma050898Commented:
I guess it would have helped had I mentioned what the clsid was,huh ;)

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

0
 
dmetzlerAuthor Commented:
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
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
Tom,

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

Don
0
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
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
 
tma050898Commented:
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
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
That worked!  Now I need to have it happen without asking the user (ie they have already confirmed in my program).

Thanks!

Don

0
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
Thanks, Tom.  I appreciate the effort!

Don
0
 
dmetzlerAuthor Commented:
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
 
tma050898Commented:
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
 
dmetzlerAuthor Commented:
Thanks - if you would send an email to me directly (barefoot@pobox.com) I'd appreciate it (off topic for this).

Don
0
 
DarrinECommented:
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
 
DarrinECommented:
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
 
dmetzlerAuthor Commented:
I think it did for me.  Let me check my code and get back to you.

don
0
 
DarrinECommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 12
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now