About composite documents in COM

zhaozj2021-02-16  46

About composite documents in COM

Structured storage

Under the permanent storage mechanism, ordinary files are organized with bytes. Each file is incorporated in one byte, and the entire file is stored in the form, and each block is discrete . When you want to read a file, the file system manages its pointer and returns the byte stream to be read.

COM uses another more reasonable way to store files and data. This method is called structured storage. Structured storage adopted method is to store files in advance in a document structure. Here you need to use two COM Object. Stocks and streams.Storage objects are similar to the directory in the file system, which can include other Storage objects and Stream objects. You can understand the Stream object into files in the file system. Like the file, the Stream object contains data., STREAM objects Stored is like a continuous byte block. Each composite document contains two objects. COM accesss the above two objects IStorage and iStream through two excuses.

Why do we need structured storage?

Yes, you have to know some other data in your composite document. Ok, now you will probably want to put them together. Previous practice is when you want to save the file, the file The system will be overwritten to add your new information. It takes time and energy. Yes, the structured process uses another way. New data is stored behind the old data. Yes. The difference is that it is different from the new folder as the file system is that it uses the Storage object and the Stream object. Let's take a look at what benefits it brings us 1. Structured storage allows you to fully control each. Separate objects, each read / write you don't have to read and write the entire file, and you can read / write the paragraph you want. 2. And a user can concurrently read / write the same file.

A composite document: It is to store different types of data in a file. Such as a Word file can include an Excel chart, an image form, or other data.

Let us look at its two interfaces

The iStorage interface is like what you think is like a directory in the file system.

ISTREAM interface It is used to read / write data to the Stream object.

You can find the appropriate documentation on the MSDN.

Finally, let us give an example to see how they work.

The steps are as follows: 1. First, you want to call the STGCReatedocFile function to create a storage root object. Our composite file To store each storage object and stream object. ThestgcreatedocFile function returns an iStorage interface pointer 2. Call the iStorage CreateStream to create a stream object. This function returns a pointer of iStream 3. Finally call the ISTREAM Wirte function to write.

The source code is as follows:

#include

INT Main (int Argc, char * argv []) {coinitialize (null);

IStorage * PSTR;

HRESULT HR = STGCREATEDOCFILE (l "test.mcb", str_direct | str_create | strg_readwrite | strg_share_exclusive, null, & pstr);

ISTREAM * PSTREAM; HR = PSTR-> CreateStream (l "mcbstream", str_direct | str_create | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, & PSTREAM;

Ulong name; char data [] = "mahesh testing";

PStream-> Write (Data, Strlen (Data), & name);

Couninitialize ();

Return 0;}

I hope that everyone is a bit helpless, I hope everyone can advise. (End)

转载请注明原文地址:https://www.9cbs.com/read-27454.html

New Post(0)