C++ 中的卷影复制

发布于 2024-09-28 08:35:15 字数 887 浏览 6 评论 0原文

我正在开发一个应用程序,需要复制锁定的文件。我打算在 Windows XP+ 中使用卷影复制服务,但在实施过程中遇到了问题。

我目前在尝试调用 CreateVssBackupComponents() 时收到 E_ACCESSDENIED,我认为这是因为没有备份权限,因此我正在调整进程权限令牌以包含 SE_BACKUP_NAME,虽然成功了,但我仍然收到错误。

到目前为止我的代码(为简洁起见删除了错误检查):

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>

任何人都可以帮助我或为我指出正确的方向吗?经过数小时的谷歌搜索,几乎没有找到有关卷影复制服务的信息。

谢谢, J

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-10-05 08:35:15

您缺少 AdjustTokenPrivileges() 上所需的第四个参数,即 DWORD BufferLength。
请参阅 http://msdn.microsoft.com/en-us /library/aa375202(VS.85).aspx

另外,您需要始终检查操作系统 API 结果;)

这里是一些示例代码:

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

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()
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文