如何在VC ++中使用sprintf创建对所有Windows用户(所有人)具有全部权限(读,写)的文本文件(日志文件)

时间:2019-07-17 08:21:50

标签: visual-c++ visual-studio-2015

我正在使用以下代码创建Smart.log文件:

void S_SetLogFileName()
    {
        char HomeDir[MAX_PATH]; 

        if (strlen(LogFileName) == 0)
        {

            TCHAR AppDataFolderPath[MAX_PATH];
            if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, AppDataFolderPath)))
            {
                sprintf(AppDataFolderPath, "%s\\Netcom\\Logs", AppDataFolderPath);          
                if (CreateDirectory(AppDataFolderPath, NULL) || ERROR_ALREADY_EXISTS == GetLastError())
                    sprintf(LogFileName,"%s\\Smart.log",AppDataFolderPath);
                else
                    goto DEFAULTVALUE;               
            }
            else        
            {
    DEFAULTVALUE:

                if (S_GetHomeDir(HomeDir,sizeof(HomeDir)))
                    sprintf(LogFileName,"%s\\Bin\\Smart.log",HomeDir);                  
                else
                    strcpy(LogFileName,"Smart.log");
            }
        }
    }

并按如下所示对其进行打开和修改:

void LogMe(char *FileName,char *s, BOOL PrintTimeStamp)
{
    FILE *stream;


    char buff[2048] = "";
    char date[256];
    char time[256];
    SYSTEMTIME SystemTime;

    if(PrintTimeStamp)
    {
        GetLocalTime(&SystemTime);
        GetDateFormat(LOCALE_USER_DEFAULT,0,&SystemTime,"MM':'dd':'yyyy",date,sizeof(date));
        GetTimeFormat(LOCALE_USER_DEFAULT,0,&SystemTime,"HH':'mm':'ss",time,sizeof(time));
        sprintf(buff,"[%d - %s %s]", GetCurrentThreadId(),date,time);
    }


    stream = fopen( FileName, "a" ); 

    fprintf( stream, "%s %s\n", buff, s );

    fclose( stream );
}

问题出在这里

UserA首先运行程序,它使用S_SetLogFileName()创建\ ProgramData \ Netcom \ Smart.log

UserB接下来运行该程序,它尝试对Smart.log进行追加/修改并拒绝访问。

我应该更改我的代码以允许所有用户访问Smart.log文件吗?

1 个答案:

答案 0 :(得分:0)

这是我正在寻找的解决方案,希望对某些人有用。

refer from

  void SetFilePermission(LPCTSTR FileName)
    {
        PSID pEveryoneSID = NULL;
        PACL pACL = NULL;
        EXPLICIT_ACCESS ea[1];
        SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;

        // Create a well-known SID for the Everyone group.
        AllocateAndInitializeSid(&SIDAuthWorld, 1,
            SECURITY_WORLD_RID,
            0, 0, 0, 0, 0, 0, 0,
            &pEveryoneSID);

        // Initialize an EXPLICIT_ACCESS structure for an ACE.
        ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
        ea[0].grfAccessPermissions = 0xFFFFFFFF;
        ea[0].grfAccessMode = GRANT_ACCESS;
        ea[0].grfInheritance = NO_INHERITANCE;
        ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
        ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
        ea[0].Trustee.ptstrName = (LPTSTR)pEveryoneSID;

        // Create a new ACL that contains the new ACEs.
        SetEntriesInAcl(1, ea, NULL, &pACL);

        // Initialize a security descriptor.  
        PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
            SECURITY_DESCRIPTOR_MIN_LENGTH);

        InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);

        // Add the ACL to the security descriptor. 
        SetSecurityDescriptorDacl(pSD,
            TRUE,     // bDaclPresent flag   
            pACL,
            FALSE);   // not a default DACL 


                      //Change the security attributes
        SetFileSecurity(FileName, DACL_SECURITY_INFORMATION, pSD);

        if (pEveryoneSID)
            FreeSid(pEveryoneSID);
        if (pACL)
            LocalFree(pACL);
        if (pSD)
            LocalFree(pSD);
    }