void VssCopy::CopyFileFromSnapshot() { wchar_t volumePathName[MAX_PATH]; if (! ::GetVolumePathName(sourcePath_, volumePathName, MAX_PATH)) { AtlThrowLastWin32(); } CString source(sourcePath_); CString volumeString(volumePathName); CString subPath = source.Mid(volumeString.GetLength()); CString snapshotSourcePath(snapshotDeviceObject_); snapshotSourcePath.Append(L"\\"); snapshotSourcePath.Append(subPath); if (! ::CopyFile(snapshotSourcePath, destinationPath_, FALSE)) { AtlThrowLastWin32(); } return; }
void VssCopy::Init() { CHECK_HRESULT( ::CoInitialize(NULL) ); CHECK_HRESULT( ::CreateVssBackupComponents(&pBackupComponents_) ); CHECK_HRESULT( pBackupComponents_->InitializeForBackup() ); CComPtr<IVssAsync> pWriterMetadataStatus; CHECK_HRESULT( pBackupComponents_->GatherWriterMetadata(&pWriterMetadataStatus) ); WaitAndQueryStatus(pWriterMetadataStatus); GUID snapshotSetId = GUID_NULL; CHECK_HRESULT( pBackupComponents_->StartSnapshotSet(&snapshotSetId) ); wchar_t volumePathName[MAX_PATH]; if (! ::GetVolumePathName(sourcePath_, volumePathName, MAX_PATH)) { AtlThrowLastWin32(); } GUID snapshotId; CHECK_HRESULT( pBackupComponents_->AddToSnapshotSet(volumePathName, GUID_NULL, &snapshotId) ); CHECK_HRESULT( pBackupComponents_->SetBackupState(TRUE, FALSE, VSS_BT_FULL) ); CComPtr<IVssAsync> pPrepareForBackupResults; CHECK_HRESULT( pBackupComponents_->PrepareForBackup(&pPrepareForBackupResults) ); WaitAndQueryStatus(pPrepareForBackupResults); backupState_ = TRUE; CComPtr<IVssAsync> pDoSnapshotSetResults; CHECK_HRESULT( pBackupComponents_->DoSnapshotSet(&pDoSnapshotSetResults) ); WaitAndQueryStatus(pDoSnapshotSetResults); snapshotProperties_ = new VSS_SNAPSHOT_PROP; CHECK_HRESULT( pBackupComponents_->GetSnapshotProperties(snapshotId, snapshotProperties_) ); snapshotDeviceObject_ = snapshotProperties_->m_pwszSnapshotDeviceObject; return; }
//---------------------------------------------------------------------------- // NAME: ConvertUTF16ToUTF8 // DESC: Converts Unicode UTF-16 (Windows default) text to Unicode UTF-8 //---------------------------------------------------------------------------- std::string CStringToStdString( IN const wchar_t * utf16 ) { // // Check input pointer // ATLASSERT( utf16 != NULL ); if ( utf16 == NULL ) AtlThrow( E_POINTER ); // // Handle special case of empty string // if ( *utf16 == L'\0' ) { return ""; } // // Consider wchar_t's count corresponding to total string length, // including end-of-string (L'\0') character. // const int utf16Length = wcslen( utf16 ) + 1; // // Get size of destination UTF-8 buffer, in chars (= bytes) // int utf8Size = ::WideCharToMultiByte( CP_UTF8, // convert to UTF-8 0, // default flags utf16, // source UTF-16 string utf16Length, // total source string length, in wchar_t's, // including end-of-string \0 NULL, // unused - no conversion required in this step 0, // request buffer size NULL, NULL // unused ); ATLASSERT( utf8Size != 0 ); if ( utf8Size == 0 ) { AtlThrowLastWin32(); } // // Allocate destination buffer for UTF-8 string // std::vector< char > utf8Buffer( utf8Size ); // // Do the conversion from UTF-16 to UTF-8 // int result = ::WideCharToMultiByte( CP_UTF8, // convert to UTF-8 0, // default flags utf16, // source UTF-16 string utf16Length, // total source string length, in wchar_t's, // including end-of-string \0 &utf8Buffer[0], // destination buffer utf8Size, // destination buffer size, in bytes NULL, NULL // unused ); ATLASSERT( result != 0 ); if ( result == 0 ) { AtlThrowLastWin32(); } // // Build UTF-8 string from conversion buffer // return std::string( &utf8Buffer[0] ); }