在Windows中更改文件所有者 [英] Change file owner in Windows

查看:1194
本文介绍了在Windows中更改文件所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

  // #includes为了理智而省略
HANDLE标记;
char * filename =somefile.txt;
char * newuser =someuser;
DWORD len;
PSECURITY_DESCRIPTOR security = NULL;
PSID sidPtr = NULL;
int retValue = 1;
$ b $ //获取你需要的权限
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,& token)){
SetPrivilege(token,SeTakeOwnershipPrivilege,1)
SetPrivilege(令牌,SeSecurityPrivilege,1);
SetPrivilege(令牌,SeBackupPrivilege,1);
SetPrivilege(令牌,SeRestorePrivilege,1);
} else retValue = 0;
$ b $ //创建安全描述符
if(retValue){
GetFileSecurity(filename,OWNER_SECURITY_INFORMATION,security,0,& len);
security =(PSECURITY_DESCRIPTOR)malloc(len);
if(!InitializeSecurityDescriptor(security,SECURITY_DESCRIPTOR_REVISION))
retValue = 0;


//获取用户名
的sid 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;



设置sid为新所有者
if(retValue&&!SetSecurityDescriptorOwner(security,sidPtr,0))
retValue = 0;
$ b $ //保存安全描述符
if(retValue)
retValue = SetFileSecurity(filename,OWNER_SECURITY_INFORMATION,security);
if(security)free(security);
if(sid)free(sid);
返回retValue;

`


Is there an API in Windows similar to Linux's chown?

解决方案

Taken from here: 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;

`

这篇关于在Windows中更改文件所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆