Basic tutorial of ATL Programming

Source: Internet
Author: User

Http://www.vckbase.com/document/viewdoc? Id = 211

 

Introduction
The purpose of this tutorial is to show you how to use ATL to create a COM server and use visual c ++ and Visual Basic programs to call this server separately. I don't want to go deep into the details of COM, and I don't want you to get stuck in IDL. This tutorial was designed only for novice programmers of VC ++. It tells them how simple it is to create a COM Object using ATL and gives them more interest in ATL.

Step 2: Start atl com wizard

The first thing you need to do is to start visual c ++ and create a new project. Select "atl com wizard" and the project name is "simple_atl ". After setting the project path, click OK. You will see several options on the screen. The first option is "server type ". We will create a Server DLL, so please confirm that the server type is "Dynamic Link Library ". We do not need to care about the other three check boxes below, so we can ignore them. Press the finish button, so that the Wizard will generate an appropriate file for you. After that, a new project information window will appear. You can learn from the above what files will be created by the wizard, and press OK to accept all this.

Step 2: Create a new ATL Object

Make sure that you can see the workspace view in the VC ++ ide. If not, click the "View" menu and select "workspace ". In this view, you will see three tabs. Click the "classview" column and you will see "simple_atl classes ". Right-click the mouse and select "new ATL Object" in the pop-up menu. You will see the following window:

The default option "Simple Object" is what we need. Click the next button and you will be in the "ATL object wizard properties" window. In the "short name" text box, enter "first_atl ". Note that the Wizard will automatically fill in other text boxes. Then, click the "attributes" tab on the top. here you need to make some choices. For the first threading model, we select the default unit model. For interfaces, we select dual ). Finally, because our program has nothing to do with aggregation, we select the "no" radio button. You don't have to worry about the three check boxes at the bottom, just click OK, then the Wizard will create a new simple ATL object for us.

Step 2: Add a method

If you click the "classview" label in the workspace, you will notice that the wizard has added a series of items. The first thing we want to add is a method. You can right-click "ifirst_atl" and select "add method ".

Once you click "add method", you will see the "add method to interface" window. In return type, you will see that this method will return hresult by default, and you do not need to change it in most cases. The next text box allows us to enter the method name. We can enter "addnumbers ". The last text box is for us to input parameters. Because we want to add two numbers and get a returned result, we need three parameters and the last parameter is a pointer. Now, you don't have to read the 300 Page tutorial on IDL. You can enter the following in the parameter text box:
[In] Long num1, [in] Long num2, [out] Long * returnval
To put it simply, we declare two long parameters. These two values are passed in ([in]), and there is a final output return value ([out]). (It may be a bit strange to see such a thing for the first time, but if you read one or two books about com, you will feel more cordial .) Now you can click OK. Click the "classview" label and expand all the "+" labels to completely expand the Tree View. You will see our "addnumbers" method and the parameters we have given it at the top of the interface (ifirst_atl. Double-click the mouse button in this method and insert the following code:
Stdmethodimp cfirst_atl: addnumbers (long num1,
Long num2, long * returnval)
{
// Todo: add your implementation code here
* Returnval = num1 + num2;

Return s_ OK;
}

Step 2: Compile the DLL

Believe it or not, you already have a COM server written in ATL! Of course, we also need to compile it. Press the F7 key so that VC ++ can compile. The compiler registers your new DLL in the registry after a while, so that other programs can use it. Let's test it.

Step 2: Test the COM server in Visual Basic

So, let's test the COM server with VB first. (If you do not have VB, you can skip this section and test it directly in VC ++ .) Start VB, select "Standard EXE (Standard exe)" to create a project, and place a command button in the dialog box. Now, we need to add a reference to the COM server. Click the project menu and select references, find simple ATL 1.0 Type Library, and select it.

After clicking OK, you can double-click the previously placed command button. VB switches to the code window of this button. Add the following code:
Private sub commandementclick ()
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 programmer, the code is intuitive to you: We declare an object, call the "addnumbers" method from the COM server, and then display the result. Now press F5 to run the VB project. Click the command button and you will see the expected results:

Isn't it difficult? So let's try again. This time we use VC ++.

Step 2: Test the COM server in Visual C ++

If your simple_atl project is still on, close it and create a new project. Select "Win32 console application" and name it "test_atl". In the next window, click OK to accept all default values, and then click Finish. Now you should have an empty project. Press Ctrl + n to add a new file to the project, select "C ++ source file" and name it "test_atl.cpp", and click OK to accept the file. Now you should have an open blank file. We need to add some code in it to test the COM server. The Code is as follows:
// You need to specify the simple_atl project path to reference this header file

# Include "../simple_atl/simple_atl.h"
# Include <iostream. h>

// Copy the following content from the simple_atl_ I .c file in the simple_atl project directory.
// Note: You can also directly include the simple_atl_ I .c file. Here I just want to clearly indicate where these const constants come from and what they look like.

Const IID iid_ifirst_atl =
{0xc8f6e230, 0x2672, 0x11d3,
{0xa8, 0xa8, 0x00,0x10, 0x5a, 0xa9, 0x43, 0xdf }};

Const CLSID clsid_first_atl =
{0x970599e0, 0x2673, 0x11d3,
{0xa8, 0xa8, 0x00,0x10, 0x5a, 0xa9, 0x43, 0xdf }};

Void main (void)
{
// Declare an hresult variable and a pointer to the simple_atl Interface
Hresult hr;
Ifirst_atl * ifirstatl = NULL;

// Initialize com now
HR = coinitialize (0 );

// Use the succeeded macro to check whether we can obtain the interface pointer.
If (succeeded (HR ))
{
HR = cocreateinstance (clsid_first_atl, null,
Clsctx_inproc_server,
Iid_ifirst_atl, (void **) & ifirstatl );

// If the operation succeeds, call the addnumbers method.
// Otherwise, an appropriate message is displayed to the user.
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;
}
}
// Uninstall com
Couninitialize ();
}

Step 2: Compile and run the program

Now you can press F5 to compile the program and press Ctrl + F5 to run it. You should be able to see a DOS window with the expected results.

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.