正确的VSS备份流 - VSS_E_BAD_STATE错误

时间:2011-04-14 11:42:32

标签: c++ c windows winapi

我在使用VSS和C ++进行备份工作时遇到问题。它现在正处于工作和备份文件的状态,但是当我使用结果BackupComplete()调用VSS_E_BAD_STATE时它失败了所以我希望任何熟悉VSS的人都知道我的流程是否正确。我目前正在做以下事情:

if( !CHECK_HRESULT(::CreateVssBackupComponents(&m_pBackupComponents))  )
{
    throw;
}

if( !CHECK_HRESULT((hr = m_pBackupComponents->InitializeForBackup())) )
{
    throw;
}

WCHAR wszVolumePathName[MAX_PATH];
GUID snapshotId;
BOOL supported = TRUE;
HRESULT hr;
SnapshotMap::iterator it;
BOOL bWorked = ::GetVolumePathName(path.c_str(), wszVolumePathName, MAX_PATH); 

if( !bWorked )
{
    throw;
}

if( !CHECK_HRESULT((hr = m_pBackupComponents->IsVolumeSupported(GUID_NULL, wszVolumePathName, &supported))) || !supported )
{
    throw;
}

GUID snapshotSetId;
if( !CHECK_HRESULT((hr = m_pBackupComponents->StartSnapshotSet(&snapshotSetId))) )
{
    throw;
}

m_SnapshotIdList.push_back(snapshotId);

if( !CHECK_HRESULT((hr = m_pBackupComponents->AddToSnapshotSet(wszVolumePathName, GUID_NULL, &snapshotId))) )
{
    throw;
}

if( !CHECK_HRESULT((hr = m_pBackupComponents->SetBackupState(FALSE, FALSE, VSS_BT_COPY, FALSE))) )
{
    throw;
}

CComPtr<IVssAsync> pPrepareForBackupResults; 
if( !CHECK_HRESULT((hr = m_pBackupComponents->PrepareForBackup(&pPrepareForBackupResults))) )
{
    throw;
}

if( !CHECK_HRESULT((hr = pPrepareForBackupResults->Wait())) )
{
    pPrepareForBackupResults.Release();
    throw;
}

HRESULT hrPrepareForBackupResults; 
if( !CHECK_HRESULT((hr = pPrepareForBackupResults->QueryStatus(&hrPrepareForBackupResults, NULL))) )
{
    pPrepareForBackupResults.Release();
    throw;
}

pPrepareForBackupResults.Release();
if( hrPrepareForBackupResults != VSS_S_ASYNC_FINISHED )
{
    throw;
}

CComPtr<IVssAsync> pDoSnapshotSetResults;
if( !CHECK_HRESULT((hr = m_pBackupComponents->DoSnapshotSet(&pDoSnapshotSetResults))) )
{
    throw;
}

m_VssSyncList.push_back(pDoSnapshotSetResults);

VSS_SNAPSHOT_PROP snapshotProperties;
if( !CHECK_HRESULT((hr = m_pBackupComponents->GetSnapshotProperties(snapshotId, &snapshotProperties))) )
{
    throw;
}

TSTRING newPath(snapshotProperties.m_pwszSnapshotDeviceObject);
m_SnapshotMap.insert(SnapshotMap_Entry(TSTRING(wszVolumePathName), newPath));
newPath.append(path.substr(2));

<Backup files here>

::VssFreeSnapshotProperties(&snapshotProperties);

for( SnapshotIdList::iterator it = m_SnapshotIdList.begin(); it != m_SnapshotIdList.end(); it++ )
{
    LONG cDeletedSnapshots; 
    GUID nonDeletedSnapshotId;
    m_pBackupComponents->DeleteSnapshots(*it, VSS_OBJECT_SNAPSHOT_SET, TRUE, &cDeletedSnapshots, &nonDeletedSnapshotId); 
}

m_SnapshotIdList.clear();

for( VssSyncList::iterator it = m_VssSyncList.begin(); it != m_VssSyncList.end(); it++ )
{
    (*it).Release();
}

m_VssSyncList.clear();

CComPtr<IVssAsync> pBackupCompleteResults;
if( !CHECK_HRESULT((hr = m_pBackupComponents->BackupComplete(&pBackupCompleteResults))) )
{
    throw;
}
else
{
    if( !CHECK_HRESULT((hr = pBackupCompleteResults->Wait())) )
    {
        throw;
    }

    HRESULT hrBackupCompleteResults; 
    if( CHECK_HRESULT(pBackupCompleteResults->QueryStatus(&hrBackupCompleteResults, NULL)) )
    {
        if( hrBackupCompleteResults != VSS_S_ASYNC_FINISHED )
        {
            throw;
        }
    }

    pBackupCompleteResults.Release();
}

m_SnapshotMap.clear();

似乎我正在调用的是错误的顺序但是查看文档和各种来源我无法弄清楚错误的顺序。

我错过了一些令人眼花缭乱的事吗?

2 个答案:

答案 0 :(得分:0)

您是在处理完每个组件之后调用BackupComplete之前调用SetBackupSucceeded吗?

答案 1 :(得分:0)

我在学习VSS时遇到了这些错误,主要是错误是由于使用CoInitialize()调用VSS序列。请检查VSS序列之前和之后是否有CoInitialize和CoUninitialize。