ATL talks (2)
Since the previous written is too light, this is talking about the actual thing.
ATL and STL are a collection of small, efficient and flexible classes. However, accompanying responsibilities are. Like STL, only experienced C programmers (the best STL experience) can use it effectively.
What does ATL provide us? Those who want to establish a COM object without knowing COM, ATL is not suitable for them. In fact, using ATL means that it is very familiar with COM and ATL itself in C .
What is ATL to provide me?
To assemble an excellent component to provide users, you must know what you have excellent equipment yourself.
1. Provide packaging classes for maintaining high-cost data types (for example, interface pointers, variant, bstr, and hwnd) (CComptr, CCOMQIPTR, CCOMDISPATCHDRIVER smart pointer), Comvariant, CCOMBSTR, (CWindow, CWindowImpl) , Cdialogimpl, ccontainedwindow, and caxwindow).
2. Provide some classes that achieve these basic COM interfaces such as IunkNown, IclassFactory, Idispath, IPersistxxx, IconnectionPointContainer, and IEnumxxx (because there are too many things involved too much to explain ATL corresponding classes).
3. Manage the class of COM services (CCOMMODULE class), they are used for exposed objects, self-registration, and server lifecycle management.
4. Save the wizard of manual entry (Wizard).
Here, talk about ATL maintenance data type and smart pointer
There are some other data types outside the CM language. The two most important three are text strings (BSTR), VARIANT data types, and interface pointers. ATL provides useful package classes for these data types.
Processing text data types in C program is quite painful. Mainly because it is not just a text data type, different operating systems and programming languages introduce other semantics on their own text type (EG: ending with NUL characters or adding a length prefix). What kind of character consists of what type of text is needed when we operate and use text. The second is what character set when we use the program. Because the character set used by the source code does not have to be the same as the operating system of the running program. Of course, it will be more convenient. The third is to determine the length of the text string. Some languages (C and C ) and some operating systems (Windows 9x / NT and UNIX) use end character NUL. However, both VB, Java, and Pascal are used by the prefixed string length.
Because COM is cross-platform cross-language, it is clear that it is necessary to develop some standard text data types, and all COM objects in different environments can understand these types.
COM is € with OLECHAR character data. The COM string is an array of OLECHAR characters that are ends as NUL characters, and pointers pointing to such characters are a LPoleChar. As a principle, the text string parameter type passing to the COM interface method must be a LPoleChar type. If a method does not need to change a string, then use the lpColeChar type, that is, point to the constant pointer to the OLECHAR array. Some people may feel confused Ä, don't you say BSTR? Haha! Yes, look at the definition of BSTR, you know (Typedef Olechar Far * BSTR;). BSTR (wide double byte, but in Apple's PowerMac is single byte) is actually an OLECHAR array containing a length prefix. We will often touch the BSTR type and the character types used in our write code. This is to be transformed. However, ATL provides us with some character conversion macros for us (for example : A2ole). We also often change the compilation option (for example: Window NT Unicode Build), which may result in a string code that you don't need to convert it now needs to be converted, or the opposite. WINDOW-based COM components are generally mixed with four types. The following is the abbreviation of the ATL conversion macro:
T Represents a pointer to the Win32 TChar character type
W Represents a pointer to the Unicode Wchar_t character type
A represents a pointer to the MBCS / ANSI CHAR character type
OLE represents pointers pointing to COM OLECHAR characters
All the names of all macros are used "
EG:
#include
// Macro header file
// Create a cstring
CString mycstring (_t ("my string"));
// must be added with this macro to use ATL to change the macro
Uses_Conversion;
BSTR y = sysallocstring (t2cole (mycstring))
......
Sysfreestring (y); // The last must be released
Here should be told the CCOMBSTR class, which is ATL to a packaging class for BSTR, and the file atlbase.h contains its definition. The only state of this class maintenance is a common member variable M_STR of a BSTR type.
// CCOMBSTR
Class CCOMBSTR
{
PUBLIC:
BSTR M_STR;
......
}
The CCOMBSTR object has 8 constructor. The default constructor is simple to initialize the variable m_str to NULL. The destructor calls SysfreString release any BSTR contained in the m_str variable.
CCOMBSTR () {m_str = null;}
~ Ccombstr () {sysfreeestring (m_str);
Using the most constructor is:
CCOMBSTR (LPColestr psrc) {m_str = :: sysallocstring (psrc);
// Take a look at the call of the code below
CCOMBSTR STR1 ("This Is A String of Olechars");
The usage of other constructions is not only one by one, and some people may not be tired. If you won't use or have questions, you can ask me, let's discuss it. I usually use the BSTR_T class, and CCOMBST, the BSTR is packaged, and a classic example is given about the usage of various string types in COM. If you have the process of the string, you will see it. The following code is estimated that it will not be a headache for it! Haha! Ok, let's take a look!
Void cxxxx :: onstrings () {
// Create a BSTR and assign a value to a Variant
// Create a bstr and assign it to a variant
BSTR x = sysallocstring (l "hello");
Variant myvariant;
Myvariant.vt = vt_bs; // Set the data type of Variant
Myvariant.bstrval = x; // assignment to Myvariant
Sysfreestring (x); // Record
// Create a CString and convert into a variant type
// Create a cstring and change it to a variant;
CString mycstring (_t ("my string"));
CString mysecondstring;
/ / Must call this macro before using ATL macro
// this is required to use the t2cole macro.
Uses_Conversion;
BSTR y = sysallocstring (t2cole (mycstring));
Myvariant.bstrval = y;
Mysecondstring = y;
Sysfreeestring (y);
_BSTR_T str2 = "hjljljl";
Char * str1 = (char *) str2; // char * is an overloaded by _BSTR_T class
Wchar_t * str4 = (wchar_t *) str2; // wchar_t * is an overload of the _bstr_t class
CSTRING STR3 = STR1;
// Create two BSTR let them add
// Create Two bstrs and add.
BSTR a = sysallocstring (l "one two";
BSTR B = SYSALLOCSTRING (L "Three Four.");
_BSTR_T MY_BSTR_T (A, TRUE);
MY_BSTR_T = B;
MyVariant.bstrval = my_bstr_t;
// OR
Myvariant.bstrval = _bstr_t (a, false) b;
/ / Convert BSTR to CString
// Change a bstr to a cstring.
CSTRING ANEWSTRING (B);
// or if cstring already exists.
Mycstring = B;
Some usages of // ccombstr
// use of ccombstr
CCOMBSTR myccombstr (l "hello");
Myccombstr.Append (l ", how are you?");
Variant Varfromccom;
Varfromccom.vt = vt_bstr;
Varfromccom.bstrval = myccombstr;
}
All right! The next article will continue to say the smart pointer, because it is busy a few days ago, so there is no time to write! Some people may say something very much! But it is still useful for some people! My MSN has been left behind.