首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

Windows 檔案保護 (WFP) 應用:列舉受保護檔案與檢測單一檔案

     編輯:VC 教學

背景

Windows File Protection (SFC) 防止關鍵系統檔案被覆寫。開發安裝程式、修復工具時常需查詢某檔案是否受保護、或列舉所有受保護檔案。

核心 API (sfc.h / sfc.lib)

  • SfcGetNextProtectedFile:迭代列舉。
  • SfcIsFileProtected:檢測單一檔案。

完整封裝函式


#include 
// 需專案加入 sfc.lib

// 列舉所有受保護檔案
void __fastcall ListAllProtectedFile(TStrings *pList)
{
    PROTECTED_FILE_DATA data = {0};
    while (SfcGetNextProtectedFile(NULL, &data)) {
        if (data.FileNumber != 0)
            pList->Add(data.FileName);
    }
}

// 檢測單一檔案
bool __fastcall IsFileProtected(AnsiString strFile)
{
    WCHAR wszFileName[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, strFile.c_str(), -1, wszFileName, MAX_PATH);
    return SfcIsFileProtected(NULL, wszFileName);
}

// 使用範例
if (IsFileProtected("E:\\Winnt\\system32\\subst1.exe"))
    ShowMessage("受保護");
else
    ShowMessage("未受保護");

注意

  • 需管理員權限。
  • Windows XP/2003 後 WFP 整合為 Windows Resource Protection (WRP),API 仍相容但行為略有不同。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved