024 SaveGraphToFile

Post date: 2015/4/14 上午 10:11:46

/*------------------------------------------------------------------------- Routine: DV_SaveGraph Purpose: Save the filter graph into a *.grf file Arguments: FileName Returns: HRESULT as appropriate Notes: ------------------------------------------------------------------------*/ HRESULT SaveGraphToFile(TCHAR* sGraphFile,IGraphBuilder *m_pGraph) { IStorage * pStorage = NULL; IStream * pStream = NULL; IPersistStream * pPersistStream = NULL; HRESULT hr = S_OK; if(sGraphFile == NULL) return E_FAIL; USES_CONVERSION; // Either Open or Create the *.GRF file hr = StgOpenStorage( T2W (sGraphFile), NULL, STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_DENY_WRITE, NULL, NULL, &pStorage ); if ( STG_E_FILENOTFOUND == hr ) hr = StgCreateDocfile( T2W (sGraphFile), STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE , NULL , &pStorage); CHECK_ERROR( TEXT(" StgCreateDocfile failed."), hr); hr = pStorage->CreateStream( L"ActiveMovieGraph", STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, NULL, NULL, &pStream ); CHECK_ERROR( TEXT(" CreateStream failed."), hr); // Persist the stream, save & commit to disk hr = m_pGraph->QueryInterface( IID_IPersistStream, (void **) &pPersistStream ); CHECK_ERROR( TEXT(" QI IPersistStream failed."), hr); hr = pPersistStream->Save(pStream, TRUE); CHECK_ERROR( TEXT(" pPersistStream->Save() failed."), hr); hr = pStorage->Commit( STGC_DEFAULT ); CHECK_ERROR( TEXT(" Save GRF file failed."), hr); SAFE_RELEASE(pStorage); SAFE_RELEASE(pStream); SAFE_RELEASE(pPersistStream); return hr; }