在Windows中更改文件所有者
发布时间:2020-12-14 01:51:29 所属栏目:Windows 来源:网络整理
导读:Windows中的API是否类似于 Linux的 chown? 解决方法 取自这里: 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; PSECURIT
Windows中的API是否类似于
Linux的
chown?
解决方法
取自这里:
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","SeBackupPrivilege","SeRestorePrivilege",1); } else retValue = 0; // Create the security descriptor if (retValue) { GetFileSecurity(filename,OWNER_SECURITY_INFORMATION,security,&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,&sidUse)) retValue = 0; } } // Set the sid to be the new owner if (retValue && !SetSecurityDescriptorOwner(security,0)) retValue = 0; // Save the security descriptor if (retValue) retValue = SetFileSecurity(filename,security); if (security) free(security); if (sid) free(sid); return retValue; ` (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读