如何给予“所有人"在Windows 8上通过C ++ MFC写入权限? [英] How to give "Everyone" write permissions via C++ MFC on Windows 8?

查看:112
本文介绍了如何给予“所有人"在Windows 8上通过C ++ MFC写入权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为更改权限而苦苦挣扎. 在Windows 8上,我需要更改文件的权限,以具有所有人"组的写权限. 我该怎么做? 我试图用C ++ MFC编辑一个文件,而该文件已经存在,没有选中写入"(每个人),这给我带来了很多问题.

Im struggling with changing permissions. I need, on Windows 8, to change the permissions of a file to have group "Everyone" write permissions. How to I do that? Im trying to edit a file with C++ MFC which already exists with no "Write" (Everyone) checked, thats causing me many problems.

推荐答案

您的应用程序需要具有更改文件权限的权限.

Your Application need to have the rights to change the permissions for the file.

#pragma comment(lib, "Advapi32.lib") 

#include "Aclapi.h"
#include "Sddl.h"
#include <io.h>
#include <sys/stat.h>

void AllowEveryone(CString path) 
{
    PACL pDacl,pNewDACL;
    EXPLICIT_ACCESS ExplicitAccess;
    PSECURITY_DESCRIPTOR ppSecurityDescriptor;
    PSID psid;

    LPTSTR lpStr;
    CString str = path;
    lpStr = str.GetBuffer();

    GetNamedSecurityInfo(lpStr, SE_FILE_OBJECT,DACL_SECURITY_INFORMATION, NULL, NULL, &pDacl, NULL, &ppSecurityDescriptor);
    ConvertStringSidToSid("S-1-1-0", &psid);

    ExplicitAccess.grfAccessMode = SET_ACCESS;
    ExplicitAccess.grfAccessPermissions = GENERIC_ALL;
    ExplicitAccess.grfInheritance = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;
    ExplicitAccess.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
    ExplicitAccess.Trustee.pMultipleTrustee = NULL;
    ExplicitAccess.Trustee.ptstrName = (LPTSTR) psid;
    ExplicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ExplicitAccess.Trustee.TrusteeType = TRUSTEE_IS_UNKNOWN;

    SetEntriesInAcl(1, &ExplicitAccess, pDacl, &pNewDACL);
    SetNamedSecurityInfo(lpStr,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,pNewDACL,NULL);

    LocalFree(pNewDACL);
    LocalFree(psid);

    str.ReleaseBuffer();
}

这篇关于如何给予“所有人"在Windows 8上通过C ++ MFC写入权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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