Solved

Get an IStream given a file on disk

Posted on 2002-04-09
10
957 Views
Last Modified: 2013-12-03
Hi,
I have a file on disk.
I want a COM IStream interface (which will only be used for sequential reading).
It would be real nice if someone could tell me a more sensible way to get from A to B, without having to:
a) GlobalAlloc
b) Load my entire file to memory; and
c) CreateStreamOnHGlobal.

Fingers crossed

0
Comment
Question by:GGRUNDY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6928396
Why not using 'StgOpenStorage()'?
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6928419
Hmm, couldn't find a sample, but this snippet from MSDN illustrates it:

void CTestflexgridDlg::OnLoadsavedbitmap()
 {
     IStorage* pStg = 0;
 
     HRESULT hr;
     hr = ::StgOpenStorage(L"c:\\picttest",
          NULL,
          STGM_SHARE_EXCLUSIVE |
          STGM_READ,
          NULL, 0, &pStg);
     if(SUCCEEDED(hr))
     {
         IStream* pStream = 0;
     
         hr = pStg->OpenStream(L"PICTURE", NULL,
              STGM_SHARE_EXCLUSIVE |
              STGM_READ,
              0, &pStream);
         if(SUCCEEDED(hr))
         {
             IDispatch* pDispatch;
             hr = ::OleLoadPicture(pStream,
                  0, // read entire stream
                  TRUE,
                  IID_IDispatch,
                  (void**)&pDispatch);
             if(SUCCEEDED(hr))
             {
                 m_flexgrid.SetRefCellPicture(pDispatch);    
                 pDispatch->Release();
             }
             pStream->Release();
         }
         pStg->Release();
     }
 }
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6928460
I thought StgOpenStorage opened compound storage files. The file I want to hook up to my input IStream is just a raw data file. Am I missing something?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 3

Author Comment

by:GGRUNDY
ID: 6928483
Oh!!! Can I use StgOpenStorage on the directory which contains my file?????
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6928627
>>>Oh!!! Can I use StgOpenStorage on the directory which contains my file?????
NO :-(

But I just found the following on MSDN

Streams on Existing Files
Because streams and files are so close in nature, it seems natural that you should be able to create a stream object that accesses a file through IStream instead of through a file handle. OLE doesn't currently provide a simple API function for this exact purpose, although for small files you can easily load the file into memory and create a stream on that memory.

Structured Storage does, however, define a special access mode that allows a client to open an existing file as if it were a root storage, and the contents of the existing file are exposed through a stream named CONTENTS (big surprise). So while you can't simply open a stream on top of a file, you can open a root storage and then open a stream.
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6929883
:-( :-( :-( :-(
Before anyone gets too excited about the availabilty of a stream named contents...
Turns out only to be available if you open the storage STGM_CONVERT which (big surprise) trashes your file.
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6929957
SHCreateStreamOnFileEx is available but only XP.
So I guess I'm going to have to write my own code.
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6938234
Hi JKR, What do you think I should do about finalising this question?
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 10655162
I've decided to accept JKRs answer rather than delete the question. Because I know when I go looking for solutions it often saves me heaps of fruitless searching when I find an "It can't be done" answer.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question