Esempio n. 1
0
////////////////////////////////////////////////////////////////////////////
// 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;
}
Esempio n. 2
0
HRESULT CAGCEventDef::ExpandFmtString(BSTR bstrFmt, IAGCEvent* pEvent,
  BSTR* pbstrOut)
{
  assert(BSTRLen(bstrFmt));
  assert(pbstrOut);

  // Create a growable stream into which we'll write
  IStreamPtr spStm;
  RETURN_FAILED(CreateStreamOnHGlobal(NULL, true, &spStm));

  // Initialize the parsing data
  XParseData data =
    {pEvent, spStm, NULL, NULL, NULL, bstrFmt, NULL, false};

  // Iterate through the characters of the format string
  XStateProc pfnState = ParseState_Base;
  for (UINT cch = SysStringLen(bstrFmt); cch && pfnState; --cch)
  {
    RETURN_FAILED(pfnState(data));
    pfnState = data.m_pfnNextState;
    ++data.m_pszInput;
  }
  data.m_bEndOfString = true;
  while (pfnState)
  {
    RETURN_FAILED(pfnState(data));
    pfnState = data.m_pfnNextState;
  }

  // Get the current seek pointer of the stream (which is it's size)
  LARGE_INTEGER li = {0};
  ULARGE_INTEGER uli;
  RETURN_FAILED(spStm->Seek(li, STREAM_SEEK_CUR, &uli));
  UINT cchStream = (UINT)uli.QuadPart / sizeof(OLECHAR);

  // Get the HGLOBAL underlying the stream
  HGLOBAL hGlobal = NULL;
  RETURN_FAILED(GetHGlobalFromStream(spStm, &hGlobal));
  assert(hGlobal);

  // Lock the HGLOBAL
  LPCOLESTR pszOut = reinterpret_cast<LPCOLESTR>(GlobalLock(hGlobal));
  assert(pszOut);

  // Create a BSTR from the byte stream
  *pbstrOut = SysAllocStringLen(pszOut, cchStream);

  // Unlock the HGLOBAL
  GlobalUnlock(hGlobal);

  // Indicate success or failure
  return *pbstrOut ? S_OK : E_OUTOFMEMORY;
}
Esempio n. 3
0
////////////////////////////////////////////////////////////////////////////
// Function: Return an in-memory encoded version of the XML data which
// we can squirrel away in memory for use later.  This is better 
// and faster than storing the document in a huge BSTR via get_xml.
////////////////////////////////////////////////////////////////////////////
HRESULT CXMLDocument::PersistToMemory(PBYTE* ppBuffer, ULONG* pLen)
{
	// Create stream in global memory and save the xml document to it
	IStreamPtr pStream = NULL;
	HRESULT hr = ::CreateStreamOnHGlobal(NULL, true/*fDeleteOnRelease*/, &pStream);
	hr = CheckHR(hr, "in persist to memory");

	IPersistStreamInitPtr pPSI = m_pDoc;
	if (pPSI == NULL)
		return E_FAIL;

	hr = pPSI->Save(pStream, true);
	hr = CheckHR(hr, "in persist to memory save");

	// now to get the real size of the stream, we have to use the
	// seek function.  You can't depend on the GlobalSize because
	// the hGlobal might have been rounded up to some paragraph boundary.
	LARGE_INTEGER li = {0, 0};
	ULARGE_INTEGER uli;
	hr = pStream->Seek(li, STREAM_SEEK_CUR, &uli);
	hr = CheckHR(hr, "in persist to memory seek");

	ULONG size = (int)uli.QuadPart;
	*pLen = size;

	BYTE* pbData = new BYTE[size];
	if (pbData == NULL)
		hr = E_OUTOFMEMORY;
	else
	{
		*ppBuffer = pbData;

		HGLOBAL hGlobal;
		hr = ::GetHGlobalFromStream(pStream, &hGlobal);
		hr = CheckHR(hr, "in persist to memory");

		::memcpy(pbData, (PBYTE)GlobalLock(hGlobal), size);
		GlobalUnlock(hGlobal);
	}

	return hr;
}
Esempio n. 4
0
ULONG CV8File::Deflate(unsigned char *DataIn, ULONG DataInSize, unsigned char **DataOut)
{
	// ������� ����� ����� �� string
    HRESULT hr;
    //USES_CONVERSION;

	/*
	IzlibEnginePtr iLibEngine;
	CoInitialize(NULL);
	hr = iLibEngine.CreateInstance(L"V75.zlibEngine");

	if (FAILED(hr))
	{
		AfxMessageBox("err");
		return 0;
	}
	*/
	
    IStreamPtr pStreamNew;
    IStream *pIStrNew;
    if (S_OK == CreateStreamOnHGlobal(NULL, TRUE, &pIStrNew))
	{
		pStreamNew.Attach(pIStrNew, false);
		
		
		ULONG pcbWritten = 0;
		if (pIStrNew->Write(DataIn, DataInSize, &pcbWritten) != S_OK)
		{
			AfxMessageBox("err");
			//pBkEndUI->DoMessageLine("pStreamOut->Read(&lpStr[0], cbStreamSizeOut, &pcbRead) != S_OK", mmBlackErr);	                            
			return 0;
		}              
		
	}
	
    LARGE_INTEGER __zero;
	__zero.QuadPart = __int64(0);
    if (pStreamNew->Seek(__zero, 0, NULL) == S_OK)
    {
	}
	
	
	ULONG cbStreamSizeOut;
    IStreamPtr pStreamOut;
    IStream *pIStrOut;
    if (S_OK == CreateStreamOnHGlobal(NULL, TRUE, &pIStrOut))
    {     
		pStreamOut.Attach(pIStrOut, false);
		try 
		{
			//CoInitialize(NULL);
			//IzlibEnginePtr iLibEngine;
			//hr = iLibEngine.CreateInstance(L"V75.zlibEngine");
			
			//if (FAILED(hr))
			//{
			//	//pBkEndUI->DoMessageLine("error iLibEngine->pkDecompress", mmBlackErr);          
			//	AfxMessageBox("err");
			//	return 0;
			//}
			
			hr = iLibEngine->pkCompress(pStreamNew, pStreamOut);            
			
			if (FAILED(hr))
			{
				//pBkEndUI->DoMessageLine("error iLibEngine->pkDecompress", mmBlackErr);          
				AfxMessageBox("err");
				return 0;
			}          
			else
			{          
				STATSTG St;
				pStreamOut->Stat(&St, STATFLAG_NONAME);
				cbStreamSizeOut = St.cbSize.LowPart;

				LARGE_INTEGER __zero;
				__zero.QuadPart = __int64(0);
				if (pStreamOut->Seek(__zero, 0, NULL) == S_OK)
				{
					
					*DataOut = (unsigned char*)realloc(*DataOut, cbStreamSizeOut);
					ULONG pcbRead = 0;
					if (pStreamOut->Read(*DataOut, cbStreamSizeOut, &pcbRead) != S_OK)
					{
						AfxMessageBox("err");
						//pBkEndUI->DoMessageLine("pStreamOut->Read(&lpStr[0], cbStreamSizeOut, &pcbRead) != S_OK", mmBlackErr);	                            
						return 0;
					}
				}
				else
				{
					AfxMessageBox("err");
					//pBkEndUI->DoMessageLine("pStreamOut->Seek(__zero, 0, NULL) != S_OK", mmBlackErr);	                            
					return 0;
				}
			}
			
		} 
		catch (_com_error& err) 
		{
			//CString str;
			//str.Format("V75.zlibEngine Error:%s", err.ErrorMessage());
			AfxMessageBox(err.ErrorMessage());
			//pBkEndUI->DoMessageLine(str, mmBlackErr);
			return 0;
		}                  
    }
    else
    {
		//pBkEndUI->DoMessageLine("error in CreateStreamOnHGlobal", mmBlackErr);    
		AfxMessageBox("err");
		return 0; 
    }        
    return cbStreamSizeOut;    

}
STDMETHODIMP CAdminSessionClass::CreateSession(IAdminSessionHost* pHost,
	IAdminSession** ppSession)
{
	// pHost must be valid
	if (!pHost)
		return E_INVALIDARG;

	// Create a decoy cookie based on the current FILETIME
	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);

	// Get the IUnknown from the admin session host
	IUnknownPtr spUnk;
	HRESULT hr = pHost->GetIdentity(ft.dwLowDateTime, &spUnk);
	if (hr != (ft.dwLowDateTime & 0x7FFFFFFF))
		return E_INVALIDARG;

	// IUnknown must support IStream
	IStreamPtr spStm(spUnk);
	if (NULL == spStm)
		return E_INVALIDARG;

	// Create a decoded copy of the specified stream
	IStreamPtr spStmOut;
	RETURN_FAILED(CAdminSessionSecure<
		&CLSID_AdminInterfaces>::CreateDecryptedStream(spStm, &spStmOut));

	// Rewind the decoded stream
	LARGE_INTEGER li = { 0 };
	RETURN_FAILED(spStmOut->Seek(li, STREAM_SEEK_SET, NULL));

	// Create a version info object for the host
	IAGCVersionInfoPtr spVersionHost;
	RETURN_FAILED(spVersionHost.CreateInstance(CLSID_AGCVersionInfo));

	// Initialize the version info object from the decoded stream
	IPersistStreamPtr spPersist(spVersionHost);
	if (NULL == spPersist)
		return E_UNEXPECTED;
	RETURN_FAILED(spPersist->Load(spStmOut));

	// Create a version info object for this module
	IAGCVersionInfoPtr spVersion;
	RETURN_FAILED(spVersion.CreateInstance(CLSID_AGCVersionInfo));
	RETURN_FAILED(spVersion->put_FileName(NULL));

	// Compare exactly for several fields of the version info object

	// CompanyName
	{
		CComBSTR bstr, bstrHost;
		RETURN_FAILED(spVersion->get_CompanyName(&bstr));
		RETURN_FAILED(spVersionHost->get_CompanyName(&bstrHost));
		if (!IsEqualBSTR(bstr, bstrHost))
			return E_INVALIDARG;
	}
	// LegalCopyright
	{
		CComBSTR bstr, bstrHost;
		RETURN_FAILED(spVersion->get_LegalCopyright(&bstr));
		RETURN_FAILED(spVersionHost->get_LegalCopyright(&bstrHost));
		if (!IsEqualBSTR(bstr, bstrHost))
			return E_INVALIDARG;
	}

	// Now, we can FINALLY create the sesion by calling the base class
	return CComClassFactory::CreateInstance(NULL, IID_IAdminSession,
		(void**)ppSession);
}