Shadow copy of volume in C ++

I am developing an application that will need to copy files that are locked. I intend to use Volume Shadow Copy Service on Windows XP +, but I have an implementation problem.

I am currently getting E_ACCESSDENIED when I try to call CreateVssBackupComponents() , which in my opinion does not have backup privileges, so I adjust the process privilege token to include SE_BACKUP_NAME, which succeeds, but I still get the error.

My code is still (validation bug 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 someone help me or point me in the right direction? Hours of work with search engines showed very little in the volume shadow copy service.

Thanks J

+4
source share
1 answer

You are missing the 4th argument for the AdjustTokenPrivileges () parameter, which is DWORD BufferLength. See http://msdn.microsoft.com/en-us/library/aa375202(VS.85).aspx

In addition, you need to always check the results of the Windows operating system;)

here is a sample 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() } 
+3
source

All Articles