Solved

Get an IStream given a file on disk

Posted on 2002-04-09
10
954 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 161
wordpress limitations 4 124
Where to obtain Oracle Data Provider for .NET 3.5 7 53
What is MicroStrategy.NET? 2 70
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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