COM Component Design and Application (I) origin and composite File

Source: Internet
Author: User

I. Preface

On a dark night in 1995 AD, one of my teachers said to me, "Xiao Yang, I will write programs in the future, just like building blocks. Hurry up and learn some OLE technologies... ". At that time, I thought," Are you kidding me? How do I build blocks to write programs? In May 100 ...... ", but as a good student, I started to" Touch "(Note 1) OLE-related books in the bookstore (note 2 ). I finally bought my first COM book OLE2 advanced programming technology, which cost 800 of my monthly salary for more than 1/5 pages ...... so I started day and night farming .....
I insist on reading all the books. What is this book talking about?
I have read the story again. My feeling is: Cough ~~~, No!
Hard work pays off, I will, I will, and I will read again... feeling: Oh ~~~, I have read a little bit, hahaha.
............
I finally understand it.
The 800-page book is actually useful to me as many as 10 pages. At this time, I realized what the reason is "the book reads thinner and thinner. Later, I had more books to buy, and it was more convenient and cheaper to access the Internet ......

In order to let my friends on VCKBASE no longer experience the hardships I 've ever experienced, I don't want to repeat the hardships I 've been exploring like a "no-head fly", I want to make VCKBASE flourish, and take off for the Chinese software business (bad, too high )...... I plan to save some time to earn points on BBS and write a series of papers. This is called "COM component design and application. Today is the first part -- origin.

Ii. File Storage

It is said that Newton was beheaded by an apple before 350, so he found the universal gravitation. But now in the 21st century, the invention and development of any technology no longer rely on the flash of the sage. Technological advances have been promoted by social needs, commercial interests, competitive pressures, and industry penetration. Microsoft's component technology on Windows is no exception. Its Invention has its own inevitable factors. What is this? The answer is: file storage.
Open the Notepad program, enter an article, and save it. -- Such a file is called an unstructured file ";
Open the workbook program, enter the name and score of the student in the class, and save. -- Such a file is called a standard structured file ";
In the program we write, specific data needs to be written to the file in a certain structure and order for storage. -- Such a file is called a "Custom structured file" (for example, *. bmp file)
For more information about the above three types of files, see. Does NAs meet all application requirements based on the above methods? ~~~, It has been enough for at least 50 years since the invention of the computer. Hey, let's take a look at the promotion effect of commercial interests and what changes have taken place in the form of file storage. A friend over 30 years old, I guess I have used the following famous software before: WordStar (the English editing software under Duba DOS) and WPS (the Chinese editing software written by Chu bojun, it is said that the market share of the year was as high as 90%, and various required courses for computer training courses), LOTUS-123 (spreadsheet software produced by LOTUS )......
After Microsoft successfully launched Windows 3.1, it began to covet the desktop office automation software field. Microsoft's OFFICE Development Department, each group independently developed software such as WORD and EXCEL, and used a "Custom structure" to store files. In the fierce market competition, Microsoft naturally came up with the idea that to defeat its competitors, if I could embed an EXCEL file in a WORD program, so when the user buys my WORD software, there is no need to buy another LOTUS-123 ?! After the "vicious" (Chinese Microsoft comrades saw this word, don't be excited. I added quotation marks) program, they started implementation, this is the origin of OLE, the predecessor of COM (note 3 ). However, we immediately encountered a serious technical problem: we need to save the DOC file generated by WORD and the XLS file generated by EXCEL together.

Solution

Advantages

Disadvantages

Create a sub-directory and store DOC and XLS in the same sub-directory. Data isolation is good, WORD does not need to understand the storage structure of EXCEL; it is easy to expand. The structure is too loose and may cause data damage or loss.
Not easy to carry.
Modify the file storage structure and extend the XLS-Tolerant Structure Based on the DOC structure. Tightly structured, easy to carry, and unified management. Developers of WORD need to be familiar with the EXCEL storage format. If there is no extensibility, can they simply extend the structure without adding a new type ?!

Both of the above solutions have serious defects. How can this problem be solved? If you have a new solution, you can combine the advantages of the first two solutions to eliminate the disadvantages. How good is it ...... microsoft started out as a disk operating system, and naturally they proposed a perfect design solution, that is, migrating the disk file management method to the file ------ composite file, commonly known as "File System in a file ". Even Microsoft did not think of it that year. With such a simple idea, the COM component programming method was finally developed. Composite files are the cornerstone of COM. It is an analogy between the disk file organization mode and the composite file organization mode:

Figure 1. The left side shows the file organization mode under a disk, and the right side shows the internal data organization mode of a composite file.

Iii. Features of composite files

  1. The composite file is internally managed by a tree constructed using pointers. When writing a program, you should note that the one-way pointer is used, so the backward positioning is faster than forward positioning when performing a positioning operation;
  2. The "stream object" in the composite file is the space for truly saving data. The storage unit is 512 bytes. That is to say, even if you store only one byte of data in the stream, it occupies 512 bytes of file space. Ah ~~~, Is it too wasteful? No waste! Because the files are stored on the disk, even one byte will occupy a "cluster" space;
  3. Different processes, or different threads of the same process, can simultaneously access different parts of a composite file without interfering with each other;
  4. Everyone has this experience. To insert a byte into a file, you need to operate the entire file, which is cumbersome and inefficient. Composite files provide very convenient "incremental access" capabilities;
  5. When files are frequently deleted and copied, the disk space becomes fragmented and needs to be reintegrated using the disk sorting tool. Similar to disk management, composite files also cause this problem and need to be sorted out when appropriate. However, it is relatively simple to call a function.

4. Browse composite files

VC6.0 comes with a tool, software, and a compound file browser. The file name is "vcvc commontoolsdfview.exe ". To facilitate the use of this program, you can add it to the tools menu. The method is to add a new project to the ToolsCustomize... Tools card. Run DFView.exe to open a composite file (note 4 ). However, in Microsoft Visual Studio. NET 2003, I couldn't find the tool! However, this just gives you a chance to practice. After you have read this article and mastered the programming method, you can write a "composite File Browser Editor" program and practice it again, there are also practical values.

V., Composite file functions

The function of the composite file is similar to that of the disk directory file. All these functions are divided into three types: win api global functions, store IStorage interface functions, and stream IStream interface functions. What is an interface? What is an interface function? Later articles will be introduced one after another. Here we only need to regard the "interface" as a set of functions for completing a set of related operation functions.
 

Win api functions

Function Description

StgCreateDocfile () Create a composite file to obtain the root storage object
StgOpenStorage () Open a composite file to obtain the root storage object.
StgIsStorageFile () Determine whether a file is a composite File

 

IStorage Function

Function Description

CreateStorage () Create a new storage in the current storage to obtain the sub-storage object
CreateStream () Create a new stream in the current storage to obtain the stream object.
OpenStorage () Open the sub-storage to obtain the sub-storage object.
OpenStream () Open the stream to get the stream object.
CopyTo () Copy all objects in the storage to the target storage. This function can be used to "Sort files and release fragments ".
MoveElementTo () Move objects to target Storage
DestoryElement () Delete object
RenameElement () Rename an object
EnumElements () Enumerate all objects in the current storage
SetElementTimes () Time when the object was modified
SetClass () In

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.