Domino Implementation pdf Online Edit control and so on ... Two

Source: Internet
Author: User
Tags function definition stub knowledge base
Second, the development of online editing control with ATL (author: Yang Teacher Source: VC Knowledge Base)
First, the preface

1, if you are using vc5.0 and previous version, please upgrade to vc6.0 or Vc.net 2003;
2, if you are using vc6.0 (ATL 3.0) Please read this back content;
3, if you are using Vc.net (ATL 7.0) Please read the next content; (of course, read this article is also good)
4, this first component, in addition to all COM components must be IUnknown interface, we implement a defined interface Ifun, it has two functions: Add () completes two numeric addition, Cat () completes two strings of the connection.
5, below ... Listen carefully! Here we go,:-).


Ii. establishment of the ATL project

Step 2.1: Create a Workspace (WorkSpace).
Step 2.2: In the workspace, create an ATL project (project). The sample program is called Simple1 and the DLL method is selected, as shown in Figure I.


Figure I, building an ATL DLL project

   Dynamic Link Library (DLL)Represents a component program that establishes a DLL.
   executable (EXE)Represents a component program that establishes an EXE.
   Service (EXE)Represents the establishment of a service program that is loaded and executed after the system is started.
   Allow Merging of proxy/stub codeSelecting this item means merging the proxy/stub code into the component program, otherwise you will need to compile separately to register the agent stub program separately. Agent/stub, what is this concept. Do you remember what we introduced in the last book? When callers invoke out-of-process or remote component functionality, the proxy/stub is responsible for data exchange. About the agent/stub of the specific changes and operations, and then again ...
   Support MFCUnless there are special reasons why we write an ATL program, it is best not to select the item. You might say, if there is no MFC support, that CString how to do AH. Let me tell you a secret, I don't even tell him, I live on this secret for the rest of my life:
1, you can STL? Can be replaced by a string in STL;
2, oneself write a MyString class, hehe;
3, quietly, secretly, do not tell others (especially not to tell Microsoft), MFC in the CString source code to take over;
4, the use of CComBSTR class, at least can simplify our string operation;
5, directly with the API operation string, anyway, we all learn C language, is from here. (Equals did not say, hehe)
   Support MTSSupports transaction processing, that is, whether COM + functionality is supported. COM + may introduce it in the 99th time.


Iii. adding ATL object classes

Step 3.1: Menu Insert\new ATL Object ... (or use the right mouse button to pop-up the menu in the ClassView card) and select the object category to select the Simple object item. See figure II.


Figure II, choose to establish a simple COM object

   Category ObjectCommon components. There are many types of component objects that can be selected, but in essence, let the wizard help us add some interfaces by default. For example, if we choose Simple Object, the wizard adds the IUnknown interface to our component, and we choose Internet Explorer Object, the wizard adds a IUnknown interface to the IOBJECTWI used by IE Thsite interface. Of course, we can simply add any interface manually.
   Category ControlsActiveX control. There are also many ActiveX types that can be selected. We'll talk about it later in our special introduction to ActiveX programming.
   Category MiscellaneousAuxiliary Miscellaneous components.
   Categroy Data AccessDatabase class components (I hate database programming, so I don't).

Step 3.2: Add the Custom Class Cfun (interface Ifun), as shown in Figure three.


Figure three, the name of the input class
In fact, we only need to enter the short name (shorter name), the other items will be automatically filled in. Nothing more to say, just please pay attention to the ProgID item, the default ProgID construction method is "project name. Short name".

Step 3.3: Fill in the interface properties, as shown in Figure four.

Figure IV, Interface properties

   Threading ModelSelect the threading model supported by the component. COM in the thread that I think is the most annoying, most complicated part. The concept of COM threads and apartments is left for subsequent introductions. Now .... Everybody chooses Apartment, it stands for what that. Simply put: When a component function is called in a thread, these calls are queued. Therefore, in this mode, we can not consider the problem of synchronization for the time being. (Note 1)
   InterfaceInterface base type. Dual says support for dual interfaces (note 2), which is very, very important, very, very common, but we do not speak today. Custom represents a customized excuse.remember. Remember. In our first COM program, be sure to select it .... (If you choose the wrong one, delete all the content and start over again.) )
Aggregation The components we write are allowed to be aggregated (note 3) in the future. Only means that must be aggregated to use, a bit like C + + in the pure virtual class, if you are the chief Engineer, only responsible for the design but do not write the code in person, select it.
   Support ISupportErrorInfoWhether to support error-handling interfaces with rich information. I'll tell you later.
   Support Connection PointsWhether the connection point interface (event, callback) is supported. I'll tell you later.
   Free threaded MarshalerI will not tell you, even if I kill you, I will not say. (Note 4)


Iv. Adding interface functions


Figure Five, the menu that adds the interface method


Figure vi, ADD interface function add


Figure VII, ADD interface function Cat

Please add the Add () function strictly as shown in Figure six, because the parameters of the cat () function in Figure Seven are longer, I do not have the appropriate input space, please pay attention when you input. [In] indicates that the parameter direction is input; [out] indicates that the parameter direction is output; [Out,retval] indicates that the parameter direction is output and can be the return value of the result of the function operation. In a function, you can have multiple [in], [out], but [retval] can only have one, and it will be combined with [out] at the last position. (Note 5)

Figure VIII, diagram of interface function definition after completion
We all know that to change the class functions in C + +, you need to modify two places: one is the function declaration of the class in the header (. h), and the other is the implementation of the function body (. cpp) file. And we are now using ATL to write component programs, but also to modify a place, is the interface definition (IDL) file. Don't worry, IDL will discuss it next time.
Because of vc6.0 bugs, you may not be able to figure out the style shown above (figure eight) after adding interfaces and interface functions. Workaround:
 

1 Close the project, and then reopen This method is often effective
2 Close the IDE, and then rerun  
3 Open the IDL file, check the interface function is correct, if not correct, please modify  
4 Open the IDL file, modify it casually (add a space, delete the space), and then save This method is often effective
5 Open the H/cpp file to check whether the function exists or is correct, chance No Jiamian, do not finish this idiom psychological awkward
6 Delete the interface functions in the Idl/h/cpp, and then Just one more time.
7 Re-establish the project, reinstall the VC, reinstall Windows, and smash the computer Hit.


V. Implement interface functions


Mouse double point figure eight Cfun\ifun\add (...) You can start the input function implementation:

STDMETHODIMP Cfun::add (Long N1, long N2, long *pval)
{
*pval = n1 + n2;
return S_OK;
}
This is too simple to waste "tongue". Here we implement the cat () function of string concatenation:
Stdmethodimp Cfun::cat (BSTR s1, BSTR S2, BSTR *pval)
{
int nLen1 =:: Sysstringlen (S1);	S1 character length
int nLen2 =:: Sysstringlen (S2);	S2 character length
*pval =:: SysAllocStringLen (S1, nLen1 + nLen2);	Construct new BSTR and save S1 first
if (nLen2)
{
:: memcpy (*pval + nLen1, s2, nLen2 * sizeof (WCHAR));	Then connect the S2 back into
//		wcscat (*pval, S2);
}
return S_OK;
}
Student: The above function implementation is completely done by invoking the basic API method.
Teacher: Yes, to tell the truth, it is quite cumbersome indeed.
Student: We finished connecting the second string function with memcpy (), so why not use the function wcscat ().
Teacher: Most of the cases, but you need to know: Because BSTR contains string lengths, the actual BSTR string content can store L ' "", and Function Wcscat () is the "L" "as the copy-end flag, so data may be lost." Do you understand.
Student: Yes, I understand. I have read the "COM Component design and application (iii) data type" after the understanding. So, teacher, there's no easy way to do that.
Teacher: Yes, you see ...
Stdmethodimp Cfun::cat (BSTR s1, BSTR S2, BSTR *pval)
{
CComBSTR sresult (S1);
SRESULT.APPENDBSTR (S2);
*pval = Sresult.copy ();	*pval = Sresult.detach ();
return S_OK;
}
Student: Haha, good. Using the CComBSTR, this is much simpler. Ccombstr::copy () and CComBSTR::D Etach () What is the difference.
Teacher: Ccombstr::copy () will make a copy of the BSTR, and Ccombstr::copyto () also has a similar function. The CComBSTR::D Etach () is to split the object with the internal BSTR pointer, which is a little bit faster because there is no copy process. But note that once stripped, you cannot use the object again.
Student: Teacher, you speak of too cattle, I admire you like towering taishan, straight into the sky ...
Teacher: Stop,stop. Leave homework ...
1, oneself first according to today's content writes this component;
2, whether you understand, be sure to observe the IDL file, CPP file;
3, after compiling, look what produced some documents. If it's a text file, just open it and see;
4, download the sample program (vc6.0 version) compiled run, see the effect. Then preview the calling method in the sample program;
Student: Know, quickly class, I have to go to the bathroom, I have to choke ...
Teacher: Class is over. Don't forget to top my posts ...


Vi. Summary

This back describes the steps to build the first ATL component program, and how to use it, please be aware of the COM component design and application (vii).

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.