STDMETHODIMP VLCPersistStorage::Save(LPSTORAGE pStg, BOOL fSameAsLoad) { if( NULL == pStg ) return E_INVALIDARG; if( fSameAsLoad && (S_FALSE == IsDirty()) ) return S_OK; LPSTREAM pStm = NULL; HRESULT result = pStg->CreateStream(L"VideoLAN ActiveX Plugin Data", STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &pStm); if( FAILED(result) ) return result; LPPERSISTSTREAMINIT pPersistStreamInit; if( SUCCEEDED(QueryInterface(IID_IPersistStreamInit, (void **)&pPersistStreamInit)) ) { result = pPersistStreamInit->Save(pStm, fSameAsLoad); pPersistStreamInit->Release(); } pStm->Release(); return result; };
BOOL CSummInfo::WriteToStorage(LPSTORAGE lpRootStg) { if (lpRootStg != NULL) { LPSTREAM lpStream = NULL; if (FAILED(lpRootStg->CreateStream(szSummInfo, STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, 0, &lpStream))) { TRACE(_T("CreateStream failed\n")); return FALSE; } else { if(!m_propSet.WriteToStream(lpStream)) { TRACE(_T("WriteToStream failed\n")); return FALSE; } lpRootStg->Commit(STGC_DEFAULT); lpStream->Release(); return TRUE; } } return FALSE; }
void ReadDirectory(const char* szPath, LPSTORAGE pStg) { // recursive function USES_CONVERSION; WIN32_FIND_DATA fData; HANDLE h; char szNewPath[MAX_PATH]; char szStorageName[100]; char szStreamName[100]; char szData[81]; char* pch = NULL; LPSTORAGE pSubStg = NULL; LPSTREAM pStream = NULL; g_nIndent++; strcpy(szNewPath, szPath); strcat(szNewPath, "*.*"); h = ::FindFirstFile(szNewPath, &fData); if (h == (HANDLE) 0xFFFFFFFF) return; // can't find directory do { if (!strcmp(fData.cFileName, "..") || !strcmp(fData.cFileName, ".") ) continue; while((pch = strchr(fData.cFileName, '!')) != NULL) { *pch = '|'; } if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // It's a directory, so make a storage strcpy(szNewPath, szPath); strcat(szNewPath,fData.cFileName); strcat(szNewPath, "\\"); strcpy(szStorageName, fData.cFileName); szStorageName[31] = '\0'; // limit imposed by OLE TRACE("%0.*sStorage = %s\n", (g_nIndent - 1) * 4, g_szBlanks, szStorageName); VERIFY(pStg->CreateStorage(T2COLE(szStorageName), STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pSubStg) == S_OK); ASSERT(pSubStg != NULL); ReadDirectory(szNewPath, pSubStg); pSubStg->Release(); } else { if ((pch = strrchr(fData.cFileName, '.')) != NULL) { if (!stricmp(pch, ".TXT")) { // It's a text file, so make a stream strcpy(szStreamName, fData.cFileName); strcpy(szNewPath, szPath); strcat(szNewPath, szStreamName); szStreamName[32] = '\0'; // OLE max length TRACE("%0.*sStream = %s\n", (g_nIndent - 1) * 4, g_szBlanks, szNewPath); CStdioFile file(szNewPath, CFile::modeRead); // Ignore zero-length files if(file.ReadString(szData, 80)) { TRACE("%s\n", szData); VERIFY(pStg->CreateStream(T2COLE(szStreamName), STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pStream) == S_OK); ASSERT(pStream != NULL); // Include the null terminator in the stream pStream->Write(szData, strlen(szData) + 1, NULL); pStream->Release(); } } } } } while (::FindNextFile(h, &fData)); g_nIndent--; }
BOOL COleControl::GetPropsetData(LPFORMATETC lpFormatEtc, LPSTGMEDIUM lpStgMedium, REFCLSID fmtid) { ASSERT_VALID(this); ASSERT(AfxIsValidAddress(lpFormatEtc, sizeof(FORMATETC), FALSE)); ASSERT(AfxIsValidAddress(lpStgMedium, sizeof(STGMEDIUM))); BOOL bGetDataHere = (lpStgMedium->tymed != TYMED_NULL); // Allow IStream or IStorage as the storage medium. if (!(lpFormatEtc->tymed & (TYMED_ISTREAM|TYMED_ISTORAGE))) { TRACE0("Propset only supported for stream or storage.\n"); return FALSE; } LPSTORAGE lpStorage = NULL; LPSTREAM lpStream = NULL; if (lpFormatEtc->tymed & TYMED_ISTORAGE) { // Caller wants propset data in a storage object. if (bGetDataHere) { // Use the caller-supplied storage object. lpStorage = lpStgMedium->pstg; } else { // Create a storage object on a memory ILockBytes implementation. LPLOCKBYTES lpLockBytes = NULL; if (FAILED(CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes))) { TRACE0("CreateILockBytesOnHGlobal failed.\n"); return FALSE; } ASSERT_POINTER(lpLockBytes, ILockBytes); if (FAILED(StgCreateDocfileOnILockBytes(lpLockBytes, STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &lpStorage))) { TRACE0("StgCreateDocfileOnILockBytes failed.\n"); lpLockBytes->Release(); return FALSE; } // Docfile now has reference to ILockBytes, so release ours. lpLockBytes->Release(); } ASSERT_POINTER(lpStorage, IStorage); // Create a stream within the storage. if (FAILED(lpStorage->CreateStream(OLESTR("Contents"), STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, 0, &lpStream))) { TRACE0("IStorage::CreateStream failed.\n"); if (!bGetDataHere) lpStorage->Release(); return FALSE; } } else { // Caller wants propset data in a stream object. if (bGetDataHere) { // Use the caller-supplied stream object lpStream = lpStgMedium->pstm; } else { lpStream = _AfxCreateMemoryStream(); if (lpStream == NULL) return FALSE; } } ASSERT_POINTER(lpStream, IStream); // Create the property set. CLSID clsid; GetClassID(&clsid); CPropertySet pset(clsid); pset.SetOSVersion(MAKELONG(LOWORD(GetVersion()), OSTYPE)); CPropertySection* ppsec = pset.AddSection(fmtid); if (ppsec == NULL) { TRACE0("CPropertySet::AddSection failed.\n"); lpStream->Release(); lpStorage->Release(); return FALSE; } // Set the name, based on the ambient display name (from the container). ppsec->SetSectionName(AmbientDisplayName()); CPropsetPropExchange propx(*ppsec, lpStorage, FALSE); BOOL bPropExchange = FALSE; TRY { DoPropExchange(&propx); bPropExchange = TRUE; } END_TRY if (!bPropExchange) { TRACE0("DoPropExchange failed.\n"); lpStream->Release(); lpStorage->Release(); return FALSE; } // Store the property set in the stream. if (FAILED(pset.WriteToStream(lpStream))) { TRACE0("CPropertySet::WriteToStream failed.\n"); lpStream->Release(); lpStorage->Release(); return FALSE; } // Return the property set in the requested medium. if (lpFormatEtc->tymed & TYMED_ISTORAGE) { // Return as a storage object. ASSERT_POINTER(lpStorage, IStorage); lpStream->Release(); lpStgMedium->pstg = lpStorage; lpStgMedium->tymed = TYMED_ISTORAGE; lpStgMedium->pUnkForRelease = NULL; } else { // Return as a stream. ASSERT_POINTER(lpStream, IStream); lpStgMedium->pstm = lpStream; lpStgMedium->tymed = TYMED_ISTREAM; lpStgMedium->pUnkForRelease = NULL; } return TRUE; }