C++ 中的卷影复制
我正在开发一个应用程序,需要复制锁定的文件。我打算在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少 AdjustTokenPrivileges() 上所需的第四个参数,即 DWORD BufferLength。
请参阅 http://msdn.microsoft.com/en-us /library/aa375202(VS.85).aspx
另外,您需要始终检查操作系统 API 结果;)
这里是一些示例代码:
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: