Design and application of "reprint" COM component (i)--origin and compound file

Source: Internet
Author: User
Tags ole

Original: http://vckbase.com/index.php/wv/1201.html

First, preface

1995 AD a night, one of my teachers told me: "Xiao Yang Ah, after writing the program and build the same building." You quickly learn some of the OLE technology ... "and then I thought," What are you kidding? How to write a program by building blocks? In another 100 years ... "but as a good student, I started to" prowl "(note 1) About OLE books (note 2). Kung Fu, finally bought my first COM book, "OLE2 Advanced Programming Technology", this 800-page large cloth head spent my 1/5 month salary ah ... So began day and night plowing .....

Kung Fu is not a conscientious, I insist on reading all the works, the impression is: This book, what is said?

Kung Fu is not a conscientious, I read again the big cloth head, the impression is: Cough ~ ~ ~ ~, did not understand!

Kung Fu is not a conscientious, I again, I again, I read again ... The impression is: Oh ~ ~ ~, read a little bit, ha haha.

...... ......

Kung Fu is not a conscientious, I finally, I finally understand.

800 pages of the book for me now, in fact, it is 10 pages useful. At this time only to realize what is called "book Read More Thin" reason. Later, can buy more books, the Internet is more convenient and cheaper ...

In order to let Vckbase friends, no longer experience my pain, no longer repeat my "headless flies" like the hardships of exploration, in order to vckbase the vigorous development of China's software industry to take off (bad, blowing too high) ... I plan to save some time on BBS, write a series of papers, called "COM Component design and application" bar. Today is the first part-origin.

Second, the storage of files

Legend has it that 350 years ago Newton was hit by an apple and found gravity. But by the 21st century, the invention and development of any technology is no longer dependent on the flash of the sage. Technological progress has been driven by the needs of society, the interests of the business, the pressure of competition, and the infiltration of the industry. Microsoft's component technology on the Windows platform is no exception, its invention, has its inevitable factors. What is this factor then? The answer is--the storage of the file.

Open Notepad program, enter an article and save it. -such documents are called "unstructured documents";

Open the spreadsheet program, enter the student's name and test results in a class, and save. -such documents are called "standard structured documents";

In the program we write, we need to write the specific data in a certain structure and order to save the file. -Such files are called "Custom structured files"; (e.g. *.bmp files)

The above three types of documents, we all see more. Does file storage rely on the above-mentioned approach to meet all application requirements? Well, at least more than 50 years after the invention of the computer, it has been enough. Hey, look at the driving effect of commercial interests, what changes have been made to the file storage format. Friends over the age of 30, I guess I have used the following several famous software: WordStar (English editing software under the dominance of Dos), WPS (Jiu Bojun written in Chinese editing software, said that year the market share of up to 90%, a variety of computer training courses required), LOTUS-123 (the electronic form software produced by Lotus Company) ...
Microsoft started to covet the field of desktop office automation software after successfully launching Windows 3.1. Microsoft's OFFICE development team independently developed software such as WORD and EXCEL and stored the files in a "custom structure" manner. In the fierce market competition, in order to defeat the competitor, Microsoft naturally produced a thought------if I can embed EXCEL in the Word program, then the user buys my word software in the case, there is no need to buy LOTUS-123 again?! "Vicious" (Chinese Microsoft comrades see this word, not excited, I was quoted) after the plan was produced, they began to implement the work, which is the origin of COM's predecessor OLE (note 3). But immediately a serious technical problem was encountered: The DOC file generated by WORD and the XLS file generated by EXCEL should be saved together.

Scheme

Advantages

Disadvantages

Create a subdirectory where DOC and XLS are stored in the same subdirectory. Data isolation is good, WORD does not understand the storage structure of EXCEL, easy to expand. The structure is too loose to cause data corruption or loss.
Not easy to carry.
Modifies the file storage structure, extending the structure of the inclusive XLS on the basis of the DOC structure. Compact structure, easy to carry and unified management. WORD developers need to be familiar with the storage format of EXCEL, the lack of extensibility, always can not add a new type to extend the structure of it?!

The above two programs, all have serious defects, how to solve that? It would be nice to have a new solution that would combine the merits of the first two scenarios and eliminate the shortcomings. Microsoft is a disk operating system started, so it is natural that they put forward a very perfect design, that is, the management of disk files into a file------compound files, commonly known as "file system in Files." Even Microsoft did not think of the year, just such a simple idea, incredibly finally evolved the COM component programming method. It can be said that compound files are the cornerstone of COM. is an analogy between how disk files are organized and how the composite files are organized:

Figure one, the left side represents a disk under the organization of the file, the right side represents a compound file inside the data organization.

Three, the characteristics of compound documents

1. The interior of a compound file is managed using a tree constructed with pointers. When writing a program, be aware that since the use of a one-way pointer, so as a positioning operation, the backward positioning is faster than the forward positioning;

2. The "Stream object" in the compound file is the space to actually save the data. It has a storage unit of 512 bytes. That is, even if you save only one byte of data in the stream, it takes up 512 bytes of file space. Ah ~ ~ ~, this is too wasteful? No waste! Because the file is saved on disk, even if a byte also occupies a "cluster" of space that;

3. Different processes, or different threads of the same process, can simultaneously access different parts of a compound file without interfering with each other;

4. Everyone has this experience, when it is necessary to insert a byte into a file, the entire file needs to be manipulated, very cumbersome and inefficient. The compound file provides a very convenient "incremental access" capability;

5. When files are deleted frequently and files are copied, disk space becomes fragmented and needs to be re-consolidated using the Defrag tool. and disk Management very similar, compound files will also produce this problem, in the appropriate time also need to tidy up, but relatively simple, just call a function can be completed.

Iv. Browsing Compound Files

VC6.0 comes with a tool software "compound file Browser", the file name is "VC directory \common\tools\dfview.exe". To facilitate the use of the program, you can add it to the Tools menu. To do this, add a new item to the Tools\customize...\tools card. Running DFView.exe, you can open a composite file for observation (note 4). But oddly enough, in Microsoft Visual Studio. NET 2003, I couldn't find the utility, Khan! But this gives you a chance to practice, after you have read this article and mastered the programming method, you write a "Compound File Browsing Editor" program, but also practiced hand, and practical value.

Five , compound file functions

The function of a compound file is very similar to the operation of a disk directory file. All of these functions are divided into 3 types: The WIN API global function, the storage IStorage interface function, the stream IStream interface function. What is an interface? What is an interface function? In the future, we will introduce the "interface" as a set of functions to complete a set of related operation functions.

WIN API function

Function description

Stgcreatedocfile () Create a compound file to get the root storage object
StgOpenStorage () Open a compound file to get the root storage object
Stgisstoragefile () Determine if a file is a compound file

 

IStorage function

Function description

Createstorage () Create a new store in the current store to get the child storage object
CreateStream () Create a new stream in the current store to get the stream object
Openstorage () Open child storage, get child storage object
OpenStream () Open stream, Get stream object
CopyTo () Copy all the objects under storage into the Target store, which can implement the function of "organizing files, freeing up the debris space"
Moveelementto () Moving objects to the destination store
Destoryelement () Delete Object
Renameelement () Renaming objects
Enumelements () Enumerates all objects in the current store
Setelementtimes () Time to modify an object
SetClass () Create a special stream object in the current store to hold the CLSID (Note 5)
Stat () Get system information from the current store
Release () Close Storage Objects
 

IStream function

Function description

Read () Reading data from the stream
Write () Writing data to the stream
Seek () Locating read-Write locations
SetSize () Sets the flow size. If you know the size beforehand, then calling this function first can improve performance
CopyTo () Copy stream data to another stream object
Stat () Get system information in the current stream
Clone () Clones a stream object to facilitate different modules in the program to manipulate the same stream object
Release () Close Stream Object
 
WIN API Supplemental function Function description
WRITECLASSSTG () Write CLSID to storage, same as Istorage::setclass ()
READCLASSSTG () Reads the CLSID written by WRITECLASSSTG (), equivalent to simplifying the call to Istorage::stat ()
Writeclassstm () Write CLSID to the beginning of the stream
Readclassstm () read out the CLSID written by Writeclassstm ()
WRITEFMTUSERTYPESTG () Writes the user-specified Clipboard format and name to the store
READFMTUSERTYPESTG () Read the information written by WRITEFMTUSERTYPESTG (). Make it easy for the application to quickly determine if it is the format data it needs.
CreateStreamOnHGlobal () Memory handle Hglobal converted to stream object
Gethglobalfromstream () Gets the memory handle used in the CreateStreamOnHGlobal () call

In order for everyone to quickly navigate and master the basic method, the above list of functions is not all, I omitted the "transaction" function and the part of the non-implemented function. For a more comprehensive introduction, please read MSDN.

The following program fragment demonstrates some basic function functions and invocation methods.

Example one: Create a compound file and create a sub-store under it, and then create a stream in that sub-store and write the data.

void Samplecreatedoc () {:: CoInitialize (NULL);//COM initialization//If it is an MFC program, you can use the AfxOleInit () alternative HRESULT hr;//function to perform the return value IStorage * PSTG = null;//Root Storage interface pointer IStorage *psub = null;//Sub-storage interface pointer istream *pstm = null;//Stream interface pointer hr =:: Stgcreatedocfile (//build compound file L "c:\\ A.stg ",//file name Stgm_create | Stgm_write | stgm_share_exclusive,//open mode 0,//reserved parameter &PSTG);//Get root Storage interface pointer assert (SUCCEEDED (HR));//In order to focus and simplify the structure of the program, assertions are used. In the actual program to use conditional judgment and exception handling hr = Pstg->createstorage (//Establish sub-store L "SUBSTG",//Sub-store name Stgm_create | Stgm_write | STGM_SHARE_EXCLUSIVE,0,0,&PSUB);//Get sub-storage interface pointer assert (SUCCEEDED (HR)); hr = Psub->createstream (//Build stream L "STM"// Stream name Stgm_create | Stgm_write | STGM_SHARE_EXCLUSIVE,0,0,&PSTM);//Get Stream interface pointer assert (SUCCEEDED (HR)); hr = Pstm->write (//write data to stream "Hello",//Data address 5, Byte length (note that no end of string is written) NULL);//do not need to get the actual write byte length assert (SUCCEEDED (HR)); if (PSTM) pstm->release ();//release flow pointer if (PSUB) Psub->release ();//release child store Pointer if (PSTG) pstg->release ();//release Root store pointer:: CoUninitialize ()//COM release//If using AfxOleInit (), The function is not called}

  

Figure II, after running the sample program, use DFView.exe to open the view compound file

Example two: Open a compound file and enumerate all objects under its root store.

#include < atlconv.h >//ANSI, MBCS, UNICODE convert void SampleEnum () {//Assume that you have already done COM initialization lpctstr lpFileName = _t ("C:\\a.s TG "); HRESULT hr;istorage *PSTG = NULL;  uses_conversion;//(note 6) lpcolestr lpwfilename = T2cole (lpfilename);//convert t type to wide character hr =:: Stgisstoragefile (Lpwfilename);// Is it a compound file? if (FAILED (hr)) Return;hr =:: StgOpenStorage (//Open compound file lpwfilename,//file name Null,stgm_read | STGM_SHARE_DENY_WRITE,0,0,&PSTG);//Get root Storage interface pointer Ienumstatstg *penum=null;//Enumerator hr = pstg->enumelements (0, NULL, 0 , &penum); ASSERT (SUCCEEDED (HR)); STATSTG statstg;while (NoError = = Penum->next (1, &STATSTG, NULL)) {//STATSTG.type Save object type Stgty_stream or Stgty_st orage//Statstg.pwcsname Save the object name//... There is a lot of information about time, length and so on. Please check the msdn::cotaskmemfree (statstg.pwcsname);//Release the memory used for the name (note 6)}if (penum) penum->release (); if (PSTG) pstg-> Release ();}

  

Vi. Summary

Composite files, structured storage, is the origin of Microsoft's component ideas, on this basis continue to develop a continuous, naming, ActiveX, object embedding, field activation ... A series of new technologies and concepts. Therefore, it is very important to understand and master compound files, even if you do not fully use the component technology in your program, the compound file technology can be applied separately. I wish you all a happy learning, for the socialist software cause and struggle:-)

Leave homework ...

Job 1: Write a small application that extracts additional information from the MSWord doc file (author, company ...). )。

Job 2: Write a full-featured "compound file Browse Editor".

Note 1: Prowl (xuemo), verb, Northern dialect, searching for the meaning of search.

NOTE 2: Q: Why not surf the internet for information study?

Answer: What International jokes! In that distant 1995 years, my 500 pay, not eat not to drink just enough to 100 hours of Internet network.

Note 3:ole, the object's connection and embedding.

Note 4: You can use DFView.exe to open the MSWord DOC file for compound file browsing. However, the program is not internationalized, cannot open the compound file of Chinese file name, so it needs to be renamed before browsing.

Note 5:clsid, which is described in the following article.

Note 6: The issue of memory usage in COM is described in a subsequent article.

Design and application of "reprint" COM component (i)--origin and compound file

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.