Need to give read-write access to the .log file which is created by other windows user through VC++ application

0

I have application in VC++. Which is creating test.log file when it launch and modify log file after session end of application.

My issue is Lets take two normal windows user test1 and test2 both do not have administrator rights.

1] when i login windfows using test1 user and launch my application. when application session end after 10 min if user do not make any operation. When application ends i am creating datalog.log file in "C:\ProgramData\MyApplication\datalog.log" location.

2] I logout windows test1 user

3]I login test2 user on same windows and run my application, This time when session ends, my application get crashed with error message as follows:

Problem signature: Problem Event Name: BEX Application Name: MyApplication.exe Application Version: 1.0.0.0 Application Timestamp: 5d60cf28 Fault Module Name: ucrtbase.DLL Fault Module Version: 10.0.10240.16390 Fault Module Timestamp: 55a5bf73
Exception Offset: 0007c3b4 Exception Code: c0000417 Exception Data: 00000000 OS Version: 6.1.7601.2.1.0.256.48

To solve this i implemented following function

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);
}

which is called from following

SetFilePermission(FileName); 
    stream = fopen( FileName, "a" );
    //LOG
    //fprintf( stream, "%s\n", s );
    fprintf( stream, "%s %s\n", buff, s );
    //LOG
    fclose( stream );

while debugging i application crashed without any message at fprintf( stream, "%s %s\n", buff, s ); this line.

When i try to modify file manually it gives access rights permission message.

My need is to give read/writer access rights to the datalog.log file regardless of users login on windows and user who created the file.

Operating system is windows 7

windows
visual-studio-2015
visual-c++-2015
asked on Stack Overflow Aug 26, 2019 by stackoverflow • edited Aug 26, 2019 by stackoverflow

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0