Skip to content

Instantly share code, notes, and snippets.

@FreeSlave
Last active September 19, 2020 20:45
Show Gist options
  • Save FreeSlave/6c7d8221498380cc8049b5a7443b682b to your computer and use it in GitHub Desktop.
Save FreeSlave/6c7d8221498380cc8049b5a7443b682b to your computer and use it in GitHub Desktop.
// cl /EHsc Ole32.lib Shell32.lib Shlwapi.lib eraseitem.cpp
#include <iostream>
#include <windows.h>
#include <Ole2.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <string>
int main()
{
OleInitialize(0);
IShellFolder* desktop;
if (FAILED(SHGetDesktopFolder(&desktop))) {
std::cerr << "Failed to get desktop fodler" << std::endl;
return -1;
}
LPITEMIDLIST pidlRecycleBin;
if (FAILED(SHGetSpecialFolderLocation(0, CSIDL_BITBUCKET, &pidlRecycleBin))) {
std::cerr << "Failed to get recyble bin location" << std::endl;
desktop->Release();
return -1;
}
IShellFolder* recycleBin;
HRESULT res = desktop->BindToObject(pidlRecycleBin, 0, IID_IShellFolder, (LPVOID *)&recycleBin);
ILFree(pidlRecycleBin);
desktop->Release();
if (FAILED(res)) {
std::cerr << "Could not bind recycle bin location to shellfolder object" << std::endl;
return -1;
}
IEnumIDList* enumFiles;
if (FAILED(recycleBin->EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &enumFiles)))
{
std::cerr << "Could not enumerate recycle bin objects" << std::endl;
recycleBin->Release();
return -1;
}
LPITEMIDLIST pidl = 0;
while (enumFiles->Next(1, &pidl, 0) != S_FALSE) {
STRRET strRet;
if(SUCCEEDED(recycleBin->GetDisplayNameOf(pidl,SHGDN_NORMAL,&strRet)))
{
wchar_t* displayName;
if (SUCCEEDED(StrRetToStrW(&strRet, pidl, &displayName)))
{
std::wcout << L"Do you want to delete " << displayName << " from recycle bin?" << std::endl;
CoTaskMemFree(displayName);
std::string str;
std::cin >> str;
if (str == "yes")
{
IContextMenu* contextMenu;
recycleBin->GetUIObjectOf(0, 1, (LPCITEMIDLIST*)(&pidl), IID_IContextMenu, 0, (LPVOID *)&contextMenu);
CMINVOKECOMMANDINFO ci;
memset(&ci, 0, sizeof(ci));
ci.fMask = CMIC_MASK_FLAG_NO_UI;
ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
ci.lpVerb = "delete";
contextMenu->InvokeCommand(&ci);
contextMenu->Release();
}
}
}
CoTaskMemFree(pidl);
}
enumFiles->Release();
recycleBin->Release();
OleUninitialize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment