Solved

Get an IStream given a file on disk

Posted on 2002-04-09
10
953 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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