IStorage
IStream
两个接口

解决方案 »

  1.   

    复合文档中,包含了"存储"和"流",相当于文件系统中的"目录"和"文件"
    "存储"用IStorage进行操作
    "流"用IStream进行操作建立文档用API:HRESULT StgCreateDocfile(
      const WCHAR *pwcsName,   //Pointer to path of compound file 
                               // to create
      DWORD grfMode,           //Specifies the access mode for 
                               // opening the storage object
      DWORD reserved,          //Reserved; must be zero
      IStorage **ppstgOpen     //Pointer to location for 
                               // returning the new storage object
    );最后一个参数将给出"根目录"的IStorage接口
    然后利用此接口,可建立其下的"流"和"目录"
    HRESULT CreateStream(
      const WCHAR *pwcsName,   //Pointer to the name of the new stream
      DWORD grfMode,           //Access mode for the new stream
      DWORD reserved1,         //Reserved; must be zero
      DWORD reserved2,         //Reserved; must be zero
      IStream **ppstm          //Pointer to new stream object
    );HRESULT CreateStorage(
      const WCHAR *pwcsName,   //Pointer to the name of the new storage object
      DWORD grfMode,           //Access mode for the new storage object
      DWORD reserved1,         //Reserved; must be zero
      DWORD reserved2,         //Reserved; must be zero
      IStorage **ppstg         //Pointer to new storage object
    );建立后,就能得到新建项的接口(ppstm和ppstg),进行下一级的操作
    示例:
    USES_CONVERSION;

    //建立根"目录"
    IStorage * p_root;
    if( SUCCEEDED(StgCreateDocfile(T2COLE("testfilename"),
    STGM_READWRITE| STGM_CREATE|STGM_SHARE_EXCLUSIVE
    ,0,&p_root)))
    {
    //建立根下的一个流
    IStream *p_stream;
    if(SUCCEEDED(p_root->CreateStream(T2COLE("rootstream"), 
    STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE ,
    0,0,&p_stream)))
    {
    char *p="test";
    DWORD writed;
    p_stream->Write(p,4,&writed);
    p_stream->Release();
    }
    p_root->Release(); }