Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get an IStream given a file on disk

Posted on 2002-04-09
10
Medium Priority
?
967 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 400 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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