2010-09-23 08:49:43 +00:00
|
|
|
/*
|
2012-01-23 06:36:15 +00:00
|
|
|
Copyright (C) 2005 Kimmo Pekkola, 2009 Greg Schoppe
|
2010-09-23 08:49:43 +00:00
|
|
|
|
2012-01-23 06:36:15 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2010-09-23 08:49:43 +00:00
|
|
|
|
2012-01-23 06:36:15 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2010-09-23 08:49:43 +00:00
|
|
|
|
2012-01-23 06:36:15 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2010-09-23 08:49:43 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
2012-02-14 19:34:00 +00:00
|
|
|
#include <Sddl.h>
|
2012-02-13 16:45:11 +00:00
|
|
|
#include <ShellAPI.h>
|
|
|
|
#include <ShlObj.h>
|
|
|
|
#include <process.h>
|
|
|
|
#include <vector>
|
|
|
|
#include "../../Library/Export.h" // Rainmeter's exported functions
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
2011-01-30 09:31:41 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
struct MeasureData
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
bool count;
|
|
|
|
|
|
|
|
MeasureData() : count(false) {}
|
2010-09-23 08:49:43 +00:00
|
|
|
};
|
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
struct BinData
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
ULONGLONG lastWrite;
|
2012-02-13 16:45:11 +00:00
|
|
|
HANDLE directory;
|
|
|
|
WCHAR drive;
|
2012-02-14 19:34:00 +00:00
|
|
|
bool isFAT;
|
2012-01-08 17:35:29 +00:00
|
|
|
};
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
unsigned int __stdcall QueryRecycleBinThreadProc(void* pParam);
|
|
|
|
HRESULT GetFolderCLSID(LPCWSTR pszPath, CLSID* pathCLSID);
|
2012-02-14 19:36:54 +00:00
|
|
|
LPWSTR GetCurrentUserSid();
|
2012-02-14 19:34:00 +00:00
|
|
|
HANDLE GetRecycleBinHandle(WCHAR drive, bool& isFAT);
|
2012-02-13 16:45:11 +00:00
|
|
|
|
|
|
|
std::vector<BinData> g_BinData;
|
|
|
|
double g_BinCount = 0;
|
|
|
|
double g_BinSize = 0;
|
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
bool g_IsXP = false;
|
|
|
|
|
|
|
|
int g_UpdateCount = 0;
|
|
|
|
int g_InstanceCount = 0;
|
2012-02-13 16:45:11 +00:00
|
|
|
HANDLE g_Thread = NULL;
|
|
|
|
|
2012-02-02 07:39:14 +00:00
|
|
|
PLUGIN_EXPORT void Initialize(void** data, void* rm)
|
2012-01-08 17:35:29 +00:00
|
|
|
{
|
|
|
|
MeasureData* measure = new MeasureData;
|
|
|
|
*data = measure;
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
if (g_InstanceCount == 0)
|
|
|
|
{
|
|
|
|
OSVERSIONINFOEX osvi = {sizeof(OSVERSIONINFOEX)};
|
|
|
|
if (GetVersionEx((OSVERSIONINFO*)&osvi))
|
|
|
|
{
|
|
|
|
if (osvi.dwMajorVersion == 5)
|
|
|
|
{
|
|
|
|
// Not checking for osvi.dwMinorVersion >= 1 because we won't run on pre-XP
|
|
|
|
g_IsXP = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
++g_InstanceCount;
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
LPCWSTR value = RmReadString(rm, L"RecycleType", L"COUNT");
|
|
|
|
if (_wcsicmp(L"COUNT", value) == 0)
|
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
measure->count = true;
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
else if (_wcsicmp(L"SIZE", value) == 0)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
measure->count = false;
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
WCHAR buffer[256];
|
|
|
|
_snwprintf_s(buffer, _TRUNCATE, L"RecycleManager.dll: RecycleType=%s is not valid in [%s]", value, RmGetMeasureName(rm));
|
|
|
|
RmLog(LOG_ERROR, buffer);
|
|
|
|
}
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT double Update(void* data)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2012-02-13 19:01:17 +00:00
|
|
|
++g_UpdateCount;
|
|
|
|
if (g_UpdateCount > g_InstanceCount && !g_Thread)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 19:01:17 +00:00
|
|
|
g_UpdateCount = 0;
|
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
WCHAR buffer[128];
|
|
|
|
DWORD len = GetLogicalDriveStrings(128, buffer);
|
|
|
|
|
|
|
|
// Convert "A:\\\0B:\\\0" -> "AB\0"
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < len; i += 4)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
buffer[index] = buffer[i];
|
|
|
|
++index;
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
buffer[index] = L'\0';
|
|
|
|
|
|
|
|
const WCHAR DRIVE_HANDLED = 1;
|
|
|
|
bool changed = false;
|
|
|
|
auto iter = g_BinData.begin();
|
|
|
|
while (iter != g_BinData.end())
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
BinData& data = (*iter);
|
|
|
|
|
|
|
|
WCHAR* pos = wcschr(buffer, data.drive);
|
|
|
|
if (pos != NULL)
|
2011-10-12 13:34:04 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
if (data.lastWrite != -1)
|
2011-10-12 13:34:04 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
if (data.isFAT)
|
2012-02-13 16:45:11 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
// FAT/FAT32 doesn't update directory last write time.
|
|
|
|
// TODO: Fix me?
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ULONGLONG lastWrite;
|
|
|
|
GetFileTime(data.directory, NULL, NULL, (FILETIME*)&lastWrite);
|
|
|
|
if (data.lastWrite != lastWrite)
|
|
|
|
{
|
|
|
|
data.lastWrite = lastWrite;
|
|
|
|
changed = true;
|
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|
2011-10-12 13:34:04 +00:00
|
|
|
}
|
2012-02-14 19:34:00 +00:00
|
|
|
|
|
|
|
*pos = DRIVE_HANDLED;
|
|
|
|
++iter;
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Drive removed
|
|
|
|
if (data.directory)
|
2011-10-12 13:34:04 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
CloseHandle(data.directory);
|
2011-10-12 13:34:04 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
|
|
|
|
iter = g_BinData.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < index; ++i)
|
|
|
|
{
|
|
|
|
if (buffer[i] != DRIVE_HANDLED)
|
|
|
|
{
|
|
|
|
// New drive
|
|
|
|
BinData data = {0};
|
|
|
|
data.drive = buffer[i];
|
2012-02-13 19:01:17 +00:00
|
|
|
|
|
|
|
WCHAR drive[] = L"\0:\\";
|
|
|
|
drive[0] = buffer[i];
|
|
|
|
if (GetDriveType(drive) == DRIVE_FIXED)
|
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
data.directory = GetRecycleBinHandle(buffer[i], data.isFAT);
|
2012-02-13 19:01:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
data.lastWrite = (data.directory || data.isFAT) ? 0 : -1;
|
2012-02-13 19:01:17 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
g_BinData.push_back(data);
|
2011-10-12 13:34:04 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
if (changed)
|
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
g_UpdateCount = -8;
|
2012-02-13 19:01:17 +00:00
|
|
|
g_Thread = (HANDLE)_beginthreadex(NULL, 0, QueryRecycleBinThreadProc, NULL, 0, NULL);
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
return measure->count ? g_BinCount : g_BinSize;
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Finalize(void* data)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
delete measure;
|
2010-09-23 08:49:43 +00:00
|
|
|
|
2012-02-13 16:45:11 +00:00
|
|
|
--g_InstanceCount;
|
|
|
|
if (g_InstanceCount == 0)
|
2011-10-12 13:34:04 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
for (auto iter = g_BinData.cbegin(); iter != g_BinData.cend(); ++iter)
|
2010-09-23 08:49:43 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
if ((*iter).directory)
|
2011-10-12 13:34:04 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
CloseHandle((*iter).directory);
|
2011-10-12 13:34:04 +00:00
|
|
|
}
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
|
|
|
|
WaitForSingleObject(g_Thread, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PLUGIN_EXPORT void ExecuteBang(void* data, LPCWSTR args)
|
|
|
|
{
|
|
|
|
MeasureData* measure = (MeasureData*)data;
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
if (_wcsicmp(args, L"EmptyBin") == 0)
|
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
SHEmptyRecycleBin(NULL, NULL, 0);
|
2011-03-29 19:21:57 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
else if (_wcsicmp(args, L"EmptyBinSilent") == 0)
|
2011-03-29 19:21:57 +00:00
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
SHEmptyRecycleBin(NULL, NULL, SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI | SHERB_NOSOUND);
|
2012-01-08 17:35:29 +00:00
|
|
|
}
|
|
|
|
else if (_wcsicmp(args, L"OpenBin") == 0)
|
|
|
|
{
|
|
|
|
ShellExecute(NULL, L"open", L"explorer.exe", L"/N,::{645FF040-5081-101B-9F08-00AA002F954E}", NULL, SW_SHOW);
|
2011-03-29 19:21:57 +00:00
|
|
|
}
|
2010-09-23 08:49:43 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
|
|
|
|
unsigned int __stdcall QueryRecycleBinThreadProc(void* pParam)
|
|
|
|
{
|
|
|
|
SHQUERYRBINFO rbi = {0};
|
|
|
|
rbi.cbSize = sizeof(SHQUERYRBINFO);
|
|
|
|
SHQueryRecycleBin(NULL, &rbi);
|
|
|
|
g_BinCount = (double)rbi.i64NumItems;
|
|
|
|
g_BinSize = (double)rbi.i64Size;
|
|
|
|
|
|
|
|
g_Thread = NULL;
|
|
|
|
|
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT GetFolderCLSID(LPCWSTR path, CLSID* clsid)
|
|
|
|
{
|
|
|
|
LPITEMIDLIST pidl;
|
|
|
|
HRESULT hr = SHParseDisplayName(path, NULL, &pidl, 0, NULL);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
IShellFolder* sf;
|
|
|
|
LPCITEMIDLIST pidlLast;
|
|
|
|
hr = SHBindToParent(pidl, IID_IShellFolder, (void**)&sf, &pidlLast);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
SHDESCRIPTIONID did;
|
|
|
|
hr = SHGetDataFromIDList(sf, pidlLast, SHGDFIL_DESCRIPTIONID, &did, sizeof(did));
|
|
|
|
*clsid = did.clsid;
|
|
|
|
|
|
|
|
sf->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
CoTaskMemFree(pidl);
|
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
// Return value must be freed with LocalFree
|
|
|
|
LPWSTR GetCurrentUserSid()
|
2012-02-13 16:45:11 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
LPWSTR sidStr = NULL;
|
|
|
|
HANDLE hToken;
|
|
|
|
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
|
2012-02-13 16:45:11 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
DWORD dwBufSize = 0;
|
|
|
|
GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufSize);
|
|
|
|
BYTE* buf = new BYTE[dwBufSize];
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
if (GetTokenInformation(hToken, TokenUser, buf, dwBufSize, &dwBufSize))
|
2012-02-13 16:45:11 +00:00
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
TOKEN_USER* tu = (TOKEN_USER*)buf;
|
|
|
|
if (ConvertSidToStringSid(tu->User.Sid, &sidStr))
|
2012-02-13 16:45:11 +00:00
|
|
|
{
|
|
|
|
}
|
2012-02-14 19:34:00 +00:00
|
|
|
}
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
delete [] buf;
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
CloseHandle(hToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sidStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE GetRecycleBinHandle(WCHAR drive, bool& isFAT)
|
|
|
|
{
|
|
|
|
WCHAR search[] = L"\0:\\";
|
|
|
|
search[0] = drive;
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
WCHAR filesystem[16];
|
|
|
|
if (!GetVolumeInformation(search, NULL, 0, NULL, NULL, NULL, filesystem, _countof(filesystem)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFAT = true;
|
|
|
|
if (wcscmp(filesystem, L"NTFS") == 0)
|
|
|
|
{
|
|
|
|
isFAT = false;
|
|
|
|
}
|
|
|
|
else if (wcscmp(filesystem, L"FAT") != 0 && wcscmp(filesystem, L"FAT32"))
|
|
|
|
{
|
|
|
|
RmLog(LOG_ERROR, L"RecycleManager.dll: Unsupported filesystem");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WCHAR* binFolder;
|
|
|
|
if (g_IsXP)
|
|
|
|
{
|
|
|
|
binFolder = isFAT ? L"RECYCLED" : L"RECYCLER";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
binFolder = L"$RECYCLE.BIN";
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hDir = NULL;
|
|
|
|
WCHAR binPath[MAX_PATH];
|
|
|
|
_snwprintf_s(binPath, _TRUNCATE, L"%s%s\\", search, binFolder);
|
|
|
|
|
|
|
|
DWORD binAttributes = GetFileAttributes(binPath);
|
|
|
|
if (binAttributes != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
binAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
{
|
|
|
|
if (isFAT)
|
|
|
|
{
|
|
|
|
if (_waccess(binPath, 0) != -1)
|
|
|
|
{
|
|
|
|
isFAT = true;
|
|
|
|
return hDir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get the correct, SID-specific bin for NTFS
|
|
|
|
LPWSTR currentSid = GetCurrentUserSid();
|
|
|
|
if (currentSid)
|
|
|
|
{
|
|
|
|
wcscat(binPath, currentSid);
|
|
|
|
binAttributes = GetFileAttributes(binPath);
|
2012-02-13 16:45:11 +00:00
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
if (binAttributes != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
binAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
{
|
2012-02-13 16:45:11 +00:00
|
|
|
CLSID id;
|
2012-02-14 19:34:00 +00:00
|
|
|
HRESULT hr = GetFolderCLSID(binPath, &id);
|
2012-02-13 16:45:11 +00:00
|
|
|
if (SUCCEEDED(hr) && IsEqualGUID(CLSID_RecycleBin, id))
|
|
|
|
{
|
2012-02-14 19:34:00 +00:00
|
|
|
hDir = CreateFile(
|
|
|
|
binPath,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
NULL);
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
LocalFree(currentSid);
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:34:00 +00:00
|
|
|
if (!hDir) RmLog(LOG_ERROR, L"RecycleManager.dll: Unable to find bin");
|
|
|
|
return hDir;
|
2012-02-13 16:45:11 +00:00
|
|
|
}
|