Solved

Get an IStream given a file on disk

Posted on 2002-04-09
10
944 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
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6928483
Oh!!! Can I use StgOpenStorage on the directory which contains my file?????
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now