Search code examples
windowswinapiole

Opening OLE Compound Documents read-only with StgOpenStorage


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)


Solution

  • 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.