Beginners Guide: ATL / COM simple works
Original author: C. Lung
Original link: http://www.codeproject.com/atl/com_atl.asp
Click here to download this paper supporting source code
Introduction The purpose of this Guide is to tell you how to use ATL create a COM server, and use Visual C + + and Visual Basic procedures were to call the server. I do not want to explore in depth the details of COM not want you in the deep of IDL. This handbook is for the novice VC + + programmers design, use of ATL told them to create a COM object How easy is it, and so that they can produce more of the ATL interest.
Step 1: Start ATL COM Wizard
You need to do first thing is to start Visual C + + and create a new project, select "ATL COM Wizard," works entitled "Simple_ATL." Set the path after good project, click the OK button. You will see on the screen gives you a number of options. The first option as "Server Type." We will want to create a server DLL, so please confirm the type of server selected as the "Dynamic Link Library." We do not need the other three concern Below the box, so we they can be ignored. Finish by pressing the button, this will guide you to the appropriate file. , A "New Project Information" window will appear, you can guide from the above that will create what document, press the "OK" to accept all this.
Step 2: Create a new object ATL
Please make sure that you can in VC + + IDE see Workspace View, and if not, then click the "View" menu, then choose "Workspace." In this view you will see three tab, click "ClassView" column, you should see "Simple_ATL Classes." Please right click this button, and pop-up menu, select "New ATL Object", so you will see the following window: 
The default options "Simple Object" is what we want, and click the next button, you will come to "ATL Object Wizard Properties" window. "Short Name" text box, enter "First_ATL." Please note that this time the wizard will automatically fill in other text box. Then, click on the top of the "Attributes" tab, where you need to do a number of options. The first thread model (Threading Model) choice, we chose the default modules (Apartment) model. The interface (Interface), we chose to double (Dual). Finally, because our procedures and polymerization (Aggregation) has nothing to do, so we choose "No" button. You do not have to concern three at the bottom of the box, and click OK to be direct, and this time we will guide the creation of a new ATL simple object. 
Step 3: add a method
If you are in the work area, click the "ClassView" label, then you will notice in which the wizard added a bunch of things. We would like to add the first things is a way to "IFirst_ATL" right-click the mouse button and select "Add Method." 
Once you click the "Add Method", you will see the "Add Method to Interface" window. In return value type (Return Type), you will see, this method will return HRESULT default, in most cases you do not need to change it. A text box allows us to the name of input method, we can enter "AddNumbers." Finally a text box is to allow our input parameters, because we want to add two numbers and get a return of the results, we need three parameters, and the last parameter is a pointer. Now, we do not have Kanne on the IDL 300 Guide, parameters can be directly in the text box:
[In] long Num1, [in] long Num2 [out] long * ReturnVal
In simple terms, we have long statement of the two types of parameters, these two values are imported ([in]), there is a final result to come. Return value ([out]). (You first see these kinds of things may be somewhat strange, but if you read the 12 books on COM, they would feel more cordial.) Can now click on the OK button. Then, click "ClassView" label, and start all of the "+" signs, making tree view fully launched. You will interface (IFirst_ATL) that the top of our "AddNumbers" approach and we give it the parameters. In this way, double-click the mouse button, and insert the following code:
STDMETHODIMP CFirst_ATL:: AddNumbers (long Num1,
Long Num2, ReturnVal long *)
(
/ / TODO: Add your implementation code here
* ReturnVal Num1 + = Num2;
Return S_OK;
)
Step 4: Build DLL
Whether you believe it or not, you already have a prepared using ATL COM server! Of course, we also need to compile it. Please press the F7 key to this VC + + can be compiled. Compiler work for a while and will be registered in the registry of your new DLL, this one to the other procedures can use it. Let us test.
Step 5: Test in Visual Basic COM server
Well, let us use this VB COM server to test it. (If you do not have VB, then you can skip this section, directly in VC + + test.) Launched VB, and choose the "standard EXE (Standard EXE)" established works, and in the dialog box to place an order button. Now, we need to add the application of COM server, click "Project (Project)" menu and select "quoted (References)," find "Simple ATL 1.0 Type Library" and select it. 
Click OK (OK) button, you can double-click the button previously placed orders, VB this button will switch to the code window. Add the following code:
Private Sub Command1_Click ()
Dim objTestATL As SIMPLE_ATLLib.First_ATL
Set objTestATL = New First_ATL
Dim lngReturnValue As Long
ObjTestATL.AddNumbers 5, 7, lngReturnValue
MsgBox "The value of 5 + 7 is:" & lngReturnValue
Set objTestATL = Nothing
End Sub
If you are a VB programmers, then the code for you is very intuitive: We declare an object, and calls from the COM server "AddNumbers" approach, then the result will be displayed. Now press F5 to run the VB works, click command buttons, you will see the desired results: 
Is not difficult? Then we again, this time with VC + +.
Step 6: Visual C + + COM server test
If you Simple_ATL remains open, then its customs and create a new project. Select "Win32 Console Application", named "Test_ATL", the next window, click the OK button to accept all the default values, the final click Finish button. Now, you should have a space project. Then, press Ctrl + N for the project to add a new document, select "C + + Source File" and was named "Test_ATL.cpp" Click OK to accept. You should now have a blank document open, we need to add some code in which to test the COM server. Code as follows:
/ / Simple_ATL you need to specify the path of the first document to refer to this
# Include ".. \ Simple_ATL \ Simple_ATL.h"
# Include <iostream.h>
/ / The contents of the following directories from Simple_ATL works copying documents from Simple_ATL_i.c
/ / Note: You can also contain Simple_ATL_i.c document, I would just want to clear that the source of these const constants, as well as their appearance
Const IID IID_IFirst_ATL =
(0xC8F6E230, 0×2672, 0×11D3,
(0xA8, 0xA8, 0×00, 0×10, 0×5A, 0xA9, 0×43, 0xDF));
Const CLSID CLSID_First_ATL =
(0×970599E0, 0×2673, 0×11D3,
(0xA8, 0xA8, 0×00, 0×10, 0×5A, 0xA9, 0×43, 0xDF));
Void main (void)
(
/ / Declare a variable and a Simple_ATL HRESULT Interface Guidelines
HRESULT hr;
IFirst_ATL * IFirstATL = NULL;
/ / Initialization COM
Hr = CoInitialize (0);
/ / Use SUCCEEDED-to see if we can obtain Interface Guidelines
If (SUCCEEDED (hr))
(
Hr = CoCreateInstance (CLSID_First_ATL, NULL,
CLSCTX_INPROC_SERVER,
IID_IFirst_ATL, (void **) & IFirstATL);
/ / If it is successful, then call AddNumbers method
/ / It to the user display an appropriate message
If (SUCCEEDED (hr))
(
Long ReturnValue;
IFirstATL-> AddNumbers (5, 7, & ReturnValue);
Cout << "The answer for 5 + 7 is:"
<<ReturnValue <<endl;
IFirstATL-> Release ();
)
Else
(
Cout << "CoCreateInstance Failed." <<Endl;
)
)
/ / Unloading COM
CoUninitialize ();
)
Step 7: compile and run a program
Now you can press the F5 key to compiler, and then press Ctrl + F5 to run the. You should be able to see a DOS window gives you the desired results.
Tags: com








0 Comments to “Beginners Guide: ATL / COM simple works”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.