Does using ReadDirectoryChangesW require administrator rights?

Posted by Alex Jenter on Stack Overflow See other posts from Stack Overflow or by Alex Jenter
Published on 2010-04-14T20:00:44Z Indexed on 2010/04/14 20:03 UTC
Read the original article Hit count: 366

The MSDN says that using ReadDirectoryChangesW implies the calling process having the Backup and Restore priviliges.

Does this mean that only process launched under administrator account will work correctly? I've tried the following code, it fails to enable the required privileges when running as a restricted user.

        void enablePrivileges() 
        {               
            enablePrivilege(SE_BACKUP_NAME);
            enablePrivilege(SE_RESTORE_NAME);
        }

        void enablePrivilege(LPCTSTR name) 
        {               
            HANDLE hToken;    
            DWORD status;
            if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))    
            {        
                TOKEN_PRIVILEGES tp = { 1 };   
                if( ::LookupPrivilegeValue(NULL, name,  &tp.Privileges[0].Luid) )
                {
                    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
                    BOOL result = ::AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
                    verify (result != FALSE);
                    status = ::GetLastError();                      
                }
                ::CloseHandle(hToken);  
            }   
        }

Am I doing something wrong? Is there any workaround for using ReadDirectoryChangesW from a non-administrator user account? It seems that the .NET's FileSystemWatcher can do this. Thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about filesystemwatcher