我想創建一個檔案:
- 所有本地用戶都可以只讀訪問。
- 僅當應用程式以提升的權限運行時才可讀寫。
我在
但是由于某種原因,即使具有提升的權限,寫入檔案也會失敗。(我嘗試使用 PowerShell、NotePad 等)
我究竟做錯了什么?
uj5u.com熱心網友回復:
正如@RbMm和@RaymondChen在評論中顯示的那樣,這可以非常干凈地完成:
#include <Windows.h>
#include <Accctrl.h>
#include <Aclapi.h>
#include <sddl.h>
#include <filesystem>
enum class FileAccess { ReadOnly, ReadWrite };
void grantAllAccess(const std::filesystem::path &file, const FileAccess access)
{
const auto sidString = (access == FileAccess::ReadWrite) ?
L"D:PAI(A;;0x12019f;;;WD)(A;;FA;;;BA)" : // Everyone Read/Write, Admin full access
L"D:P(A;;FA;;;BA)(A;;FR;;;WD)"; // Everyone Read only, Admin full access
PSID pSid = nullptr;
if(ConvertStringSecurityDescriptorToSecurityDescriptorW(
sidString, SDDL_REVISION_1, &pSid, nullptr) == 0)
{
throw std::runtime_error("Failed to create SID");
}
if(SetFileSecurityW(file.c_str(), DACL_SECURITY_INFORMATION, pSid) == 0)
{
LocalFree(pSid);
throw std::runtime_error("Failed to set SID to file");
}
LocalFree(pSid);
}
有關如何生成這些神秘字串的資訊,請參閱此答案。請參閱此 repo以將這些字串轉換為可讀文本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527223.html
標籤:C 温纳皮
下一篇:我的表包含不應為空的可為空值
