摘 要 介紹了Windows磁盤清理工具二次開發的擴展接口
關鍵詞 磁盤清理工具
引言
Windows磁盤清理工具(Disk CleanUp)是一個實用快捷並擁有簡單易用界面的系統清理軟件
技術討論
微軟的COM技術廣泛地運用在Windows的模塊化設計中
IEmptyVolumeCache接口由五個函數組成
virtual /* [local] */ HRESULT STDMETHODCALLTYPE initialize(
/* [in] */ HKEY hkRegKey
/* [in] */ LPCWSTR pcwszVolume
/* [out] */ LPWSTR *ppwszDisplayName
/* [out] */ LPWSTR *ppwszDescription
/* [out] */ DWORD *pdwFlags) =
virtual HRESULT STDMETHODCALLTYPE getspaceused(
/* [out] */ DWORDLONG *pdwlSpaceUsed
/* [in] */ IEmptyVolumeCacheCallBack *picb) =
virtual HRESULT STDMETHODCALLTYPE showproperties(
/* [in] */ HWND hwnd) =
virtual HRESULT STDMETHODCALLTYPE purge(
/* [in] */ DWORDLONG dwlSpaceToFree
/* [in] */ IEmptyVolumeCacheCallBack *picb) =
virtual HRESULT STDMETHODCALLTYPE deactivate(
/* [out] */ DWORD *pdwFlags) =
清理工具在正常執行時
運用於Windows
virtual /* [local] */ HRESULT STDMETHODCALLTYPE initializeex(
/* [in] */ HKEY hkRegKey
/* [in] */ LPCWSTR pcwszVolume
/* [in] */ LPCWSTR pcwszKeyName
/* [out] */ LPWSTR *ppwszDisplayName
/* [out] */ LPWSTR *ppwszDescription
/* [out] */ LPWSTR *ppwszBtnText
/* [out] */ DWORD *pdwFlags) =
InitializeEx增加了更嚴格的本地化語言要求
EVCF_OUTOFDISKSPACE
EVCF_SETTINGSMODE
EVCF_DONTSHOWIFZERO
EVCF_ENABLEBYDEFAULT
EVCF_ENABLEBYDEFAULT_AUTO
EVCF_HASSETTINGS
EVCF_REMOVEFROMLIST
EVCF_OUTOFDISKSPACE與EVCF_SETTINGSMODE用於工具傳遞給插件的設定
圖
實現方法
我們開發一個新的清理工具插件
我們在Visual Studio
// 儲存掃描出文件的大小
DWORDLONG m_dwlFileSize;
// 儲存根目錄
WCHAR m_strRootDir[MAX_PATH];
// 儲存掃描出文件列表
std::vector<WCHAR *> m_lstFilesToDel;
然後
HRESULT CCleanSimpleHandler::InitializeEx (HKEY hKey
LPCWSTR pcwszKeyName
LPWSTR *ppwszBtnText
{
HRESULT hr = Initialize (hKey
ppwszDescription
*ppwszBtnText = (LPWSTR) CoTaskMemAlloc (
StrCpyW(*ppwszBtnText
return hr;
}
HRESULT CCleanSimpleHandler::Initialize (HKEY hKey
LPWSTR *ppwszDisplayName
{
StrCpyW(m_strRootDir
*ppwszDisplayName = (LPWSTR) CoTaskMemAlloc(
StrCpyW(*ppwszDisplayName
*ppwszDescription = (LPWSTR) CoTaskMemAlloc (
StrCpyW(*ppwszDescription
*pdwFlags = EVCF_HASSETTINGS | EVCF_ENABLEBYDEFAULT;
m_dwlFileSize =
return S_OK;
}
在GetSpaceUsed中
HRESULT ScanProgress(DWORDLONG dwlSpaceUsed
其中dwFlags正常應設為零
HRESULT CCleanSimpleHandler::GetSpaceUsed (DWORDLONG *pdwSpaceUsed
IEmptyVolumeCacheCallBack *picb)
{
m_dwlFileSize =
ScanDir(m_strRootDir
picb
*pdwSpaceUsed = m_dwlFileSize;
return S_OK;
}
bool CCleanSimpleHandler::ScanDir(WCHAR * szDir
*pcib)
{
WCHAR strPath[MAX_PATH];
WCHAR* pchPathFileName;
bool cancelled = false;
WIN
HANDLE hFind;
if (cancelled = FAILED(pcib
return false;
StrCpyW(strPath
PathAppendW(strPath
pchPathFileName = strPath+lstrlenW(strPath)
hFind = FindFirstFileW(strPath
if (hFind == INVALID_HANDLE_VALUE) // E
return true;
do {
StrCpyW(pchPathFileName
if ((fd
if (fd
if (cancelled = !ScanDir(strPath
}
} else {
WCHAR* pchExt = PathFindExtensionW(strPath);
if ( StrCmpIW(pchExt
m_dwlFileSize += ((DWORDLONG)fd
fd
WCHAR* filename = (WCHAR *)CoTaskMemAlloc((lstrlenW(strPath)+
sizeof(WCHAR));
StrCpyW(filename
m_lstFilesToDel
}
}
} while (FindNextFileW(hFind
FindClose(hFind);
return !cancelled;
}
其他的函數很簡單
HRESULT CCleanSimpleHandler::Purge (DWORDLONG dwSpaceToFree
IEmptyVolumeCacheCallBack *picb)
{
for (unsigned int i=
DeleteFileW(m_lstFilesToDel[i]);
return S_OK;
}
HRESULT CCleanSimpleHandler::ShowProperties (HWND hWnd)
{
for (unsigned int i=
if (MessageBoxW(hWnd
MB_OKCANCEL|MB_ICONINFORMATION)==IDCANCEL) break;
return S_OK;
}
HRESULT CCleanSimpleHandler::Deactivate (LPDWORD pdwFlags)
{
for (unsigned int i=
CoTaskMemFree(m_lstFilesToDel[i]);
m_lstFilesToDel
*pdwFlags =
return S_OK;
結論和建議
通過實例分解
From:http://tw.wingwit.com/Article/program/net/201311/13334.html