Windows中是否有类似于Linux的chown的API?在Windows中更改文件所有者
5
A
回答
3
从这里摘自: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;
`
+3
圣地狱,所有这些来代替一个简单的'chown'调用! – 2010-02-08 08:44:52
+0
为了避免混淆,您是否忽略了函数返回类型,名称和参数? – 2010-02-08 08:51:58
1
您可能会发现cacls or icacls commands有用...他们不完全直接使用,虽然
你能提供一个更位信息你想要做什么?
相关问题
- 1. 在Node.js中更改文件所有者
- 2. 文件所有者无法在Linux中更改所有权
- 3. 如何在Amazon S3中更改多个文件的所有者?
- 4. 在java中更改目录所有者
- 5. 更改存储库中所有文档的所有者
- 6. redshift更改udf更改所有者
- 7. 更改所有者为ReentrantLock
- 8. 更改活动的事件所有者
- 9. 更改在服务器上传的文件的所有者
- 10. 如何更改服务器中文件的所有者/组?
- 11. 无法更改群组所有者(群组所有者意图)
- 12. Linux - QNAP - 无法更改文件夹的所有者
- 13. Install4j不会更改文件的所有者?
- 14. Docker卷,更改文件权限/所有者
- 15. 更改目录中所有内容的所有者
- 16. 在Linux中更改现有进程的所有者
- 17. sed初学者:改变文件夹中的所有事件
- 18. Linux用户更改所有者/组
- 19. 已更改数据库所有者
- 20. 更改表所有者w CockroachDB
- 21. 使用Ant在Windows上获取文件所有者
- 22. 列出Windows中打开文件句柄的所有者进程?
- 23. 提取PTC项目中所有文件的所有更改包
- 24. 获取文件所有者/作者
- 25. 文件所有者+第一响应者
- 26. 文件所有者:组不在位置/ proc/<pid>/setuid()后更改?
- 27. 在mercurial central repository中推送更改会更改所有者和组
- 28. 你如何在sql中更改数据库的所有者?
- 29. 如何在ms sql server中更改架构所有者?
- 30. 更改Jenkins的文件所有权
Windows安全模型并不是真正以UNIX文件系统的文件所有权为基础的,所以这不是一个非常经常需要的工具。 – 2010-02-08 09:00:52