【问题标题】:Change file owner in Windows在 Windows 中更改文件所有者
【发布时间】:2011-01-14 06:55:21
【问题描述】:

Windows 中是否有类似于 Linux 的 chown 的 API?

【问题讨论】:

  • Windows 安全模型不像 UNIX 文件系统那样真正基于文件所有权,因此这并不是一个经常需要的工具。

标签: c windows filesystems file-management


【解决方案1】:

您可能会发现 cacls or icacls commands 很有用...虽然它们使用起来并不简单

你能提供更多关于你想要做什么的信息吗?

【讨论】:

    【解决方案2】:

    取自这里:http://www.perlmonks.org/?node_id=70562

        // #includes omitted for the sake of sanity
        HANDLE token;
        char *filename = "somefile.txt";
        char *newuser = "someuser";
        DWORD len;
        PSECURITY_DESCRIPTOR security = NULL;
        PSID sidPtr = NULL;
        int retValue = 1;
    
        // Get the privileges you need
        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
            SetPrivilege(token, "SeTakeOwnershipPrivilege", 1);
            SetPrivilege(token, "SeSecurityPrivilege", 1);
            SetPrivilege(token, "SeBackupPrivilege", 1);
            SetPrivilege(token, "SeRestorePrivilege", 1);
        } else retValue = 0;
    
        // Create the security descriptor
        if (retValue) {
            GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security, 0, &len);
            security = (PSECURITY_DESCRIPTOR)malloc(len);
            if (!InitializeSecurityDescriptor(security, SECURITY_DESCRIPTOR_REVISION))
                retValue = 0;
        }
    
        // Get the sid for the username
        if (retValue) {
            char domainbuf[4096];
            DWORD sidSize = 0;
            DWORD bufSize = 4096;
            SID_NAME_USE sidUse;
            LookupAccountName(NULL, newuser, sidPtr, &sidSize, domainbuf, &bufSize, &sidUse);
            sid = (PSID)malloc(sidSize);
            if (!LookupAccountName(NULL, string, (PSID)sid, &sidSize, domainbuf, &bufSize, &sidUse))
                retValue = 0;
            }
        }
    
        // Set the sid to be the new owner
        if (retValue && !SetSecurityDescriptorOwner(security, sidPtr, 0))
            retValue = 0;
    
        // Save the security descriptor
        if (retValue)
            retValue = SetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security);
        if (security) free(security);
        if (sid) free(sid);
        return retValue;
    

    `

    【讨论】:

    • 天哪,这一切都取代了一个简单的chown 电话!
    • 是否为了模糊而省略了函数返回类型、名称和参数?
    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多