I haven't tested it yet but I'm just about sure I've found the source of the problem. First let me state I tried your code and it works Jose. However, only on your file created with your code. If I use your code to read my file, it still doesn't work. That of course suggested to me that the effect I'm seeing had something to do with the way I created the IStream. Here are the flags I used...
grfMode=STGM_SIMPLE | STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
hr=StgCreateDocfile(szFile,grfMode,0,&pStorage);
I believe the STGM_SIMPLE flag is the culpret.
STGM_SIMPLE STGM_SIMPLE is a mode that provides a much faster implementation of a compound file in a limited, but frequently used case. It is described in detail in the following Remarks section.
This mode is useful for applications that perform complete save operations. It has the following constraints:
There is no support for substorages. Access to streams follows a linear pattern. Once a stream is released, that stream cannot be opened for read/write
operations again. The IStorage::OpenStream method is not supported in this implementation. The storage and stream objects cannot be marshaled.
Each stream is at least 4096 bytes in length. If fewer than 4096 bytes are written into a stream by the time the stream is released, the stream will be extended to contain 4096 bytes. In this compound file implementation, only a subset of the methods of IStorage and IStream are available.
Specifically, in simple mode, supported IStorage methods are QueryInterface, AddRef, Release, CreateStream, Commit, and SetClass. In addition, SetElementTimes is supported with a NULL name, allowing applications to set times on a root storage in simple mode.
Supported IStream methods are QueryInterface, AddRef, Release, Seek, and SetSize. Also, Read and Write methods on ISequentialStream are supported.
I don't think the StgCreateDocfile() verses StgCreateDocfileEx() is the problem. I'll test it now but I'm about sure that's it.