views:

473

answers:

1

Hi everyone!
I'm using the StgOpenStorage API under XP to read an OLE Compound Document, but I can't find the right mix of flags to tell Windows that I need just to read the file without blocking access to it to any other process...
Right now I'm using STGM_READ | STGM_EXCLUSIVE but obviously if at the same time I try to open the file from another application, it complains about denied access.. So I tried with STGM_READ | STGM_SHARE_DENY_NONE but it complains about invalid flags..

Is there a way to do this? Am I going the wrong way?

Thanks in advance!

(I'm calling the API from Python via pythoncom, if it's relevant)

+2  A: 

This is a guess, but from the StgOpenStorage API documentation:

When the STGM_DIRECT flag is specified, only one of the following combination
of flags may be specified from the access and sharing groups.
  STGM_READ | STGM_SHARE_DENY_WRITE
  STGM_READWRITE | STGM_SHARE_EXCLUSIVE
  STGM_READ | STGM_PRIORITY
Be aware that direct mode is implied by the absence of STGM_TRANSACTED.
That is, if neither STGM_DIRECT nor STGM_TRANSACTED is specified, STGM_DIRECT
is assumed.

This suggests that changing the flags to

STGM_READ | STGM_SHARE_DENY_NONE | STGM_TRANSACTED

should do what you want. This makes sense: in 'direct' mode, if anyone could start writing to the storage, then they would overwrite what we were reading.

DavidK
Using those flags the call to StgOpenStorage works, many thanks! :) The next step is calling OpenStream on the returned instance, but I see that I can't use the same flags this time, since it complains about STG_E_INVALIDFUNCTION.. So I tried with STG_READ | STGM_SHARE_EXCLUSIVE, and it works.. Now I can open the file with its official editor (a CAD) and with my application at the same time, with no errors.. Does this make sense? ^^;
Joril
I guess it makes some sense: it seems to be just the way things are that you've got to open the stream with exclusive access, but once you've opened the storage object in 'transacted' mode you've got something like your own private copy of the storage object, so having an exclusive lock on that doesn't block access to the underlying file. (If this sounds a bit like me waffling, it's because I am!)
DavidK
Ok :D Anyway I just passed the new version of my application to beta testing, so we'll see :) Thanks again!
Joril
Also, you might want to accept my answer ... :-)
DavidK
Yes of course, I have a habit of waiting a few days before accepting, just in case someone else would come and join the discussion.. :) But I guess there's no need this time.
Joril