Baewolfe
asked on
From Disk File to IStream through OleSaveToStream
I am trying to convert a disk file to an IStream through the OleSaveToStream function. But I seem to need an IPersistStream object, and I can't figure out how to get one.
Here is my function:
void FileToOleData(HWND hWndParent, LPCTSTR szFileName)
{
CComPtr<IOleObject> pOleObject = NULL;
CComPtr<IStream> pStream = NULL;
CComPtr<IOleClientSite> pClientSite = NULL;
HRESULT hr;
WCHAR szWideFileName[255];
#ifdef _UNICODE
_tcsncpy(szWideFileName, szFileName, 254);
#else
USES_CONVERSION;
wcsncpy(szWideFileName, T2W(szFileName), 254);
#endif
// Open a compound storage document
IStorage *pStorage = NULL;
hr = StgOpenStorageEx(
szWideFileName,
STGM_READ | STGM_SHARE_DENY_NONE | STGM_TRANSACTED,
STGFMT_ANY,
0,
0, NULL,
IID_IStorage,
reinterpret_cast<void**>(&
if (FAILED(hr))
{
// error handler here
}
hr = OleCreateFromFile(CLSID_NU
szWideFileName, IID_IOleObject, OLERENDER_ASIS, NULL,
pClientSite, pStorage, (LPVOID*)&pOleObject);
if (FAILED(hr))
{
// error handler here
}
// How do I get pOleObject to an IStream?
}
By the end of the function, I have an IOleObject created from the file. I want to convert it to a stream using the OleSaveToStream function, but this requires an IPersistStream interface. So how do I get an IPersistStream interface from IOleObject?
The ultimate goal is to go from a disk file to the OleSaveToStream functions as would be done to insert an OLE Object in a document. Is there some better way to do this?
ASKER
Yes, that would seem to be the obvious solution, but hr returns with E_NOINTERFACE. The file is a PowerPoint file. Why would a PowerPoint file not support the IPersistStream Interface?
Hm, another Idea would be to
COleStreamFile osf;
osf.CreateStream(pStorage) ;
instead...
COleStreamFile osf;
osf.CreateStream(pStorage)
instead...
ASKER
That creates a stream, but it doesn't take me through the OleSaveToStream, which is a requirement.
You may also want to check out my related question "Problems extracting an OLE Object from a Rich-Text (RTF) File by parsing the file".
You may also want to check out my related question "Problems extracting an OLE Object from a Rich-Text (RTF) File by parsing the file".
have a look at the following article
may be of any help
http://support.microsoft.com/default.aspx?scid=kb;EN-US;242249
may be of any help
http://support.microsoft.com/default.aspx?scid=kb;EN-US;242249
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In response to amrit_82...
Borrowing from the MSDN code you referenced, I implement the following function and call it after OleCreateFromFile in my code from the original question. pPersist is NULL so the function returns E_NOINTERFACE.
My main problem from the original question is that my pOleObject is not supporting the IPersistStream interface. I have tested it with both Excel and PowerPoint files.
HRESULT SaveObjectHelper(IOleObjec t* pObject, IStream* * ppStream)
{
HRESULT hr=S_OK;
try
{
*ppStream=NULL;
// QI and return IPersistStream
IPersistStreamPtr pIPersist(pObject);
// pPersist is NULL here
if (pIPersist)
{
//Create a standard stream in memory
if (FAILED(hr=CreateStreamOnH Global(0, TRUE, (IStream **)ppStream)))
return hr;
// Persist the pRS
if (FAILED(hr=OleSaveToStream (pIPersist , *ppStream)))
return hr;
}
else
return E_NOINTERFACE;
}
catch (_com_error & e)
{
return e.Error();
}
return S_OK;
}
Borrowing from the MSDN code you referenced, I implement the following function and call it after OleCreateFromFile in my code from the original question. pPersist is NULL so the function returns E_NOINTERFACE.
My main problem from the original question is that my pOleObject is not supporting the IPersistStream interface. I have tested it with both Excel and PowerPoint files.
HRESULT SaveObjectHelper(IOleObjec
{
HRESULT hr=S_OK;
try
{
*ppStream=NULL;
// QI and return IPersistStream
IPersistStreamPtr pIPersist(pObject);
// pPersist is NULL here
if (pIPersist)
{
//Create a standard stream in memory
if (FAILED(hr=CreateStreamOnH
return hr;
// Persist the pRS
if (FAILED(hr=OleSaveToStream
return hr;
}
else
return E_NOINTERFACE;
}
catch (_com_error & e)
{
return e.Error();
}
return S_OK;
}
ASKER
In response to YR, pOleObject->GetMoniker returns E_FAIL.
IPersistStream* pps = NULL;
hr = pOleObject ->QueryInterface(IID_IPers
if (FAILED(hr))
{
// error handler here
}