views:

53

answers:

1

Hi,

I'm developing an application which will need to copy files that are locked. I intend on using the Volume Shadow Copy service in Windows XP+ but I'm running into a problem with the implementation.

I am currently getting E_ACCESSDENIED when attempting calling CreateVssBackupComponents() which I believe is down to not having backup privileges so I am adjusting the process privilege token to include SE_BACKUP_NAME which succeeds but I still get the error.

My code so far (error checking removed for brevity):

CoInitialize(NULL);

OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &luid);
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &NewState, 0, NULL, NULL);

IVssBackupComponents *pBackup = NULL;
HRESULT result = CreateVssBackupComponents(&pBackup);

// result == E_ACCESSDENIED at this point

pBackup->InitializeForBackup();
<snip>

Can anyone help me out or point me in the right direction? Hours of googling have turned up very little on Volume Shadow Copy service.

Thanks, J

+1  A: 

You're missing the required 4th arg on AdjustTokenPrivileges() which is DWORD BufferLength. See http://msdn.microsoft.com/en-us/library/aa375202(VS.85).aspx

Plus you need to always check your OS API results ;)

here is some example code:

            TOKEN_PRIVILEGES tp;
        TOKEN_PRIVILEGES oldtp;
        DWORD dwSize = sizeof (TOKEN_PRIVILEGES);

        ZeroMemory (&tp, sizeof (tp));
        tp.PrivilegeCount = 1;
        tp.Privileges[0].Luid = luid;
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

        if (AdjustTokenPrivileges(hToken, FALSE, &tp, 
            sizeof(TOKEN_PRIVILEGES), &oldtp, &dwSize))

        {
            DWORD lastError = GetLastError();
            switch (lastError)
            {
            case ERROR_SUCCESS:
                // success
                break;
            case ERROR_NOT_ALL_ASSIGNED:
                // fail
                break;
            default:
                // unexpected value!!
            }
        }
        else
        {
            // failed! check GetLastError()
        }
Rudi Bierach
Thanks for the input, it seems I wasn't checking the error code from AdjustTokenPrivileges() and was wrongfully assuming that a non-zero response was OK. GetLastError() is returning ERROR_NOT_ALL_ASSIGNED for SE_BACKUP_NAME which would appear to be the problem. Is there a was to enable this privilege in Windows 7 without running as an administrator?