Delete SHell forlder contents using IFileOperation
I am trying to get the contents of Recycle Bin and delete them manually,
but I have 2 problems: 1. It doesn't work, it cracks on
SHCreateShellItemArrayFromIDLists call, probably the second parameter that
I'm giving it isn't what he expects 2. Even if it would work this way..
I'm not ok with the fact that I have to declare a constant size vector of
type PIDLIST_ABSOLUTE .. the is no possible way I could know in advance
how many files there are in Recycle bin, so I 'm guessing that this
approach isn't the right one. The code follows, it's a quick wrap up of
what I've found on the web:
void WINAPI DeleteRBinData( )
{ LPITEMIDLIST pidlRecycleBin = NULL; LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfFirstFolder = NULL; IShellFolder *psfDeskTop = NULL;
IShellFolder *psfRecycleBin = NULL; LPENUMIDLIST ppenum = NULL; ULONG
celtFetched; HRESULT hr;
ULONG uAttr;
uAttr = SFGAO_FOLDER;
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, NULL, &pidlRecycleBin);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlRecycleBin, NULL, IID_IShellFolder,
(LPVOID *) &psfRecycleBin);
psfDeskTop->Release();
hr = psfRecycleBin->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
&ppenum);
PIDLIST_ABSOLUTE pIdlArray[10] ;
UINT cidl = 0;
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK &&
(celtFetched) == 1)
{
pIdlArray[cidl++] = pidlItems;
CoTaskMemFree(pidlItems);
}
IShellItemArray *aItems;
hr = SHCreateShellItemArrayFromIDLists(cidl, (PCIDLIST_ABSOLUTE_ARRAY
)pIdlArray, &aItems);
DeleteMyItems((IUnknown*)aItems);
}
and :
HRESULT DeleteMyItems(__in IUnknown* files)
{
IFileOperation *pfo;
hr = CoCreateInstance(CLSID_FileOperation,
NULL,
CLSCTX_ALL,
IID_PPV_ARGS(&pfo));
if (SUCCEEDED(hr))
{
hr = pfo->SetOperationFlags(FOF_NO_UI);
if (SUCCEEDED(hr))
{
// IFileOperation::DeleteItems()
if (SUCCEEDED(hr))
{
hr = pfo->PerformOperations();
}
}
pfo->Release();
}
return hr;
}
No comments:
Post a Comment