//////////////////////////////////////////////////////////////////////////// // Function: Load the document from memory encoded bytes. //////////////////////////////////////////////////////////////////////////// HRESULT CXMLDocument::LoadFromMemory(PBYTE pData, ULONG ulLen) { // Create a stream and write the bytes to it IStreamPtr pStream = NULL; HRESULT hr = ::CreateStreamOnHGlobal(NULL, true/*fDeleteOnRelease*/, &pStream); hr = CheckHR(hr, "load from memory"); ULONG ulWritten; pStream->Write(pData, ulLen, &ulWritten); // Reset the stream back to the beginning LARGE_INTEGER li = {0, 0}; hr = pStream->Seek(li, STREAM_SEEK_SET, NULL); hr = CheckHR(hr, "in load from memory seek"); // Now, load the document from the stream IPersistStreamInitPtr pPSI = m_pDoc; if (pPSI == NULL) return E_FAIL; hr = pPSI->Load(pStream); hr = CheckHR(hr, "load from memory load"); hr = CheckLoad(); return hr; }
STDMETHODIMP CPigMissionParams::GetData(IStream** ppstm) { // Initialize the [out] parameter CLEAROUT(ppstm, (IStream*)NULL); // Create a stream on global IStreamPtr spstm; RETURN_FAILED(CreateStreamOnHGlobal(NULL, true, &spstm)); // Write the size of the data structure to the stream UINT cb = sizeof(m_mp); RETURN_FAILED(spstm->Write(&cb, sizeof(cb), NULL)); // Write the data structure to the stream RETURN_FAILED(spstm->Write(&m_mp, cb, NULL)); // Detach the interface to the [out] parameter *ppstm = spstm.Detach(); // Indicate success return S_OK; }
/*---------------------------------------------------------------------------------------------- Save the text to a file. ----------------------------------------------------------------------------------------------*/ bool WpDa::SaveToFile(StrAnsi staFileName) { IStreamPtr qstrm; FileStream::Create(staFileName.Chars(), kfstgmWrite | kfstgmCreate, &qstrm); // FileStream::Create("c:\\output.txt", kfstgmWrite | kfstgmCreate, &qstrm); ULONG cbWritten; // Write byte-order mark. byte b = 0xFF; CheckHr(qstrm->Write(&b, 1, &cbWritten)); b = 0xFE; CheckHr(qstrm->Write(&b, 1, &cbWritten)); int ctss; CheckHr(get_VecSize(1, kflidStText_Paragraphs, &ctss)); for (int itss = 0; itss < ctss; itss++) { ITsStringPtr qtss; CheckHr(get_StringProp(itss + 2, kflidStTxtPara_Contents, &qtss)); BSTR bstr = NULL; int cchw; CheckHr(qtss->get_Length(&cchw)); qtss->GetChars(0, cchw, &bstr); // Write the string to the file. if (cchw > 0) { Assert(bstr); CheckHr(qstrm->Write(bstr, cchw * isizeof(wchar), &cbWritten)); } if (itss < ctss - 1) { // CRLF--little-endian b = 13; CheckHr(qstrm->Write(&b, 1, &cbWritten)); b = 0; CheckHr(qstrm->Write(&b, 1, &cbWritten)); b = 10; CheckHr(qstrm->Write(&b, 1, &cbWritten)); b = 0; CheckHr(qstrm->Write(&b, 1, &cbWritten)); } ReleaseBstr(bstr); } return true; }