Last active
September 15, 2023 13:04
-
-
Save im-infamou5/03487b5b05336fcc69a1 to your computer and use it in GitHub Desktop.
Allows to copy content of recycle bin to hardcoded folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <tchar.h> | |
#include <shlobj.h> | |
#include <shlwapi.h> | |
#include <propkey.h> | |
HRESULT BindToCsidl(int csidl, REFIID riid, void **ppv) | |
{ | |
HRESULT hr; | |
PIDLIST_ABSOLUTE pidl; | |
hr = SHGetSpecialFolderLocation(NULL, csidl, &pidl); | |
if (SUCCEEDED(hr)) | |
{ | |
IShellFolder *psfDesktop; | |
hr = SHGetDesktopFolder(&psfDesktop); | |
if (SUCCEEDED(hr)) | |
{ | |
if (pidl->mkid.cb) | |
{ | |
hr = psfDesktop->BindToObject(pidl, NULL, riid, ppv); | |
} | |
else | |
{ | |
hr = psfDesktop->QueryInterface(riid, ppv); | |
} | |
psfDesktop->Release(); | |
} | |
CoTaskMemFree(pidl); | |
} | |
return hr; | |
} | |
HRESULT GetUIObjectOfFile(HWND hwnd, LPCWSTR pszPath, REFIID riid, void **ppv) | |
{ | |
*ppv = NULL; | |
HRESULT hr; | |
LPITEMIDLIST pidl; | |
SFGAOF sfgao; | |
printf_s("%s",pszPath); | |
if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL, &pidl, 0, &sfgao))) | |
{ | |
IShellFolder *psf; | |
LPCITEMIDLIST pidlChild; | |
if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder, (void**)&psf, &pidlChild))) | |
{ | |
hr = psf->GetUIObjectOf(hwnd, 1, &pidlChild, riid, NULL, ppv); | |
psf->Release(); | |
} | |
CoTaskMemFree(pidl); | |
} | |
return hr; | |
} | |
void DropOnRestoreFolder(IDataObject *pdto) | |
{ | |
IDropTarget *pdt; | |
if(SUCCEEDED(GetUIObjectOfFile(NULL, L"C:\\Files", IID_PPV_ARGS(&pdt)))) | |
{ | |
POINTL pt = { 0, 0 }; | |
DWORD dwEffect = DROPEFFECT_MOVE; | |
if (SUCCEEDED(pdt->DragEnter(pdto, MK_LBUTTON, | |
pt, &dwEffect))) | |
{ | |
dwEffect &= DROPEFFECT_MOVE; | |
if (dwEffect) | |
{ | |
pdt->Drop(pdto, MK_LBUTTON, pt, &dwEffect); | |
} | |
else | |
{ | |
pdt->DragLeave(); | |
} | |
} | |
pdt->Release(); | |
} | |
} | |
int __cdecl _tmain(int argc, PTSTR *argv) | |
{ | |
HRESULT hr = CoInitialize(NULL); | |
if (SUCCEEDED(hr)) | |
{ | |
IShellFolder2 *psfRecycleBin; | |
hr = BindToCsidl(CSIDL_BITBUCKET, IID_PPV_ARGS(&psfRecycleBin)); | |
if (SUCCEEDED(hr)) | |
{ | |
IEnumIDList *peidl; | |
hr = psfRecycleBin->EnumObjects(NULL, | |
SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &peidl); | |
if (hr == S_OK) | |
{ | |
PITEMID_CHILD rgpidlItems[100]; | |
UINT cpidlItems = 0; | |
PITEMID_CHILD pidlItem; | |
while (peidl->Next(1, &pidlItem, NULL) == S_OK) | |
{ | |
if (cpidlItems < ARRAYSIZE(rgpidlItems)) | |
{ | |
rgpidlItems[cpidlItems++] = pidlItem; | |
} | |
else | |
{ | |
CoTaskMemFree(pidlItem); | |
} | |
} | |
if (cpidlItems) | |
{ | |
IDataObject *pdto; | |
hr = psfRecycleBin->GetUIObjectOf(NULL, cpidlItems, (PCUITEMID_CHILD_ARRAY)rgpidlItems, IID_IDataObject, NULL, (void**)&pdto); | |
if (SUCCEEDED(hr)) | |
{ | |
DropOnRestoreFolder(pdto); | |
pdto->Release(); | |
} | |
for (UINT i = 0; i < cpidlItems; i++) | |
{ | |
CoTaskMemFree(rgpidlItems[i]); | |
} | |
} | |
} | |
psfRecycleBin->Release(); | |
} | |
CoUninitialize(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment