Continue with the summary.
Instance
Take the processing of common HTML documents as an example to describe the use of COM in depth. The use of other types is similar (such as office programming objects)
Project includes
# Include <mshtml. h>
SpecificCodeAs follows:
// Traverse HTML documents as DOM objects
Int algorithm tree (ccomptr <ihtmldomnode> domnode)
{
Assert (domnode! = NULL );
Long type;
Ccombstr tag;
If (domnode-> get_nodetype (& type) = s_ OK)
{
Switch (type)
{
Case 1: // MSXML: node_element:
{
// Get the element name and set the tag name...
If (succeeded (domnode-> get_nodename (& tag )))
{
/// Find the element <a... If (wcscmp (tag. m_str, l "A") = 0)
{
Ccomptr <idispatch> Pdisp;
Domnode-> get_attributes (& Pdisp );
Ccomqiptr <ihtmlattributecollection, & iid_ihtmlattributecollection> pattrs;
Pattrs = Pdisp;
Pdisp = NULL;
Long L;
Colevariant index;
Index. Vt = vt_i4;
Pattrs-> get_length (& L );
For (long I = 0; I <L; I ++)
{
Pdisp = NULL;
Index. lval = I;
Pattrs-> item (index, & Pdisp );
/// Ccomqiptr is also available for the query interface
Ccomptr <ihtmldomattribute> phref;
If (succeeded (Pdisp-> QueryInterface (iid_ihtmldomattribute, (void **) & phref )))
{
Variant_bool vbspecified;
Phref-> get_specified (& vbspecified );
If (variant_true = vbspecified)
{
Ccombstr text;
If (succeeded (phref-> get_nodename (& text )))
{
/// You can do...
}
Colevariant V;
If (succeeded (phref-> get_nodevalue (& V )))
{
/// You can do...
}
}
}
}
}
}
Ccomptr <ihtmldomnode> pchild;
If (domnode-> get_firstchild (& pchild) = s_ OK)
{
// If we have children, loop through and handle each one...
If (pchild)
{
// Recurse for all the children of this tag...
Ccomptr <ihtmldomnode> domchild;
While (pchild)
{
Spanning Tree (pchild );
Domchild = NULL;
Domchild = pchild;
Pchild = NULL;
Domchild-> get_nextsibling (& pchild );
}
}
}
Break;
}
Case 3: // MSXML: node_text:
{
Colevariant val;
If (domnode-> get_nodevalue (& Val) = s_ OK)
{
/// You can do...
}
}
Break;
}
}
Return 0;
}
Void cbrowserdomdlg: onbnclickedok ()
{
Ccomptr <idispatch> Pdisp;
Pdisp = m_web.get_document ();
Ccomqiptr <ihtmldocument2, & iid_ihtmldocument2> phtml;
Phtml = Pdisp;
/// View the title of HTML
Ccombstr bstrtitle;
If (phtml! = NULL & phtml-> get_title (& bstrtitle) = s_ OK)
{
// Title is
}
Ccomptr <ihtmlelementcollection> pelems;
Hresult hr;
Phtml-> get_all (& pelems );
If (pelems = NULL) return;
Long L;
If (! Succeeded (pelems-> get_length (& L )))
Return;
// Traverse the element to find the body Node
Ccomptr <ihtmlelement> htmlbody;
Colevariant index;
For (INT I = 0; I <L; I ++)
{
Ccomptr <idispatch> pchild;
Index. Vt = vt_i4;
Index. lval = I;
HR = pelems-> item (index, index, & pchild );
Htmlbody = NULL;
If (succeeded (HR) & succeeded (pchild. QueryInterface (& htmlbody) & htmlbody! = NULL)
{
Ccombstr name;
HR = htmlbody-> get_tagname (& name );
If (wcscmp (name. m_str, l "body") = 0)
Break;
}
}
If (htmlbody = NULL) return;
Ccomptr <ihtmldomnode> pdom;
Htmlbody-> QueryInterface (& pdom );
If (pdom = NULL) return;
Partition tree (pdom );
}
There are also commonly used ADO data access methods in applications:
# Import "C: \ Program Files \ common files \ System \ ADO \ msado15.dll" no_namespace Rename ("EOF", "endoffile") Rename ("Bof", "firstoffile ")
_ Recordsetptr m_prs;
The specific operation is similar to the above example.
Deployment
Generally, COM components are developed in C ++ [because currently. NET provides a better implementation form, and the time to simply use Com is much less.] during deployment, you only need to pay attention to the settings of the corresponding MFC Library and ATL library.
There are two deployment modes:[Set compilation options in the project]:
Static Link |
Link MFC and ATL to components statically [This reduces deployment troubles, and the size of components is larger than that of dynamic connections] For example, if a project is a COM component, this form is the best. |
Dynamic connection |
There are two deployment methods: 1. Install the corresponding versions of the MFC and ATL release libraries [vs is included] 2. Put the required directories and files of Microsoft. VC **. CRT Microsoft. VC **. MFC Microsoft. VC **. ATL in the same place as the component library. |
For all the instance code, see:
Http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.Public/ComTutorial.zip