计算机文档所有者怎么修改,在Windows中更改文件所有者

// #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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值