The application in this article is vc6.0. For other versions, the wizard may be different from the one described in this article.
Address: http://www.codeproject.com/atl/com_atl.asp
Beginner's Tutorial: COM/ATL simple project
Author: C. Lung.
Introduction
The purpose of this tutorial is to give readers a preliminary impression on How to Use ATL to create a COM Service and learn to call this service when using VC ++ or vbprogramming. This tutorial is only intended for beginners of VC ++ programmers. Therefore, I do not intend to go into the details of COM, nor to add you to the burden of learning IDL. The author hopes to demonstrate how to use ATL to create a "simple" com "Object method, which is easy to use and can be used for beginners to learn more.
Step 1: run the atl com wizard
The first step after VC ++ is started is to create a new project. Select "atl com Appwizard ". Name this project "simple_atl ". Set the Save path for the project and click OK. Here are several options. The first option is "server type ". We plan to create a Server DLL (Server DLL) Here, so select "dynamic link library (Dynamic Link Library)" as the service type )". There are three other options, but they are irrelevant to our project. Click the end button to save the project. The screen displays a "new project information" window, which describes the content of the project just created. Click OK to accept the request.
Step 2: Create a new ATL Object
Make sure to open the workspace view in VC ++ IDE. If not, click the View menu and select "workspace ". Three tabs are displayed in workspace. Click the "classview" tab and you will see "simple_atl classes ". Right-click this item and select "new ATL Object (new ATL Object )". You will see the following window:
The default (Simple Object) is what we need. Click "Next" and you will see the "ATL object wizard properties (" ATL object wizard properties) "window. Enter "first_atl" in "short name". You will see that the Wizard will automatically enter other input columns for you. Click the "attributes (attributes)" tab on the top to display the following options. The first is the threading model (thread mode). We should keep the default apartment model. For interface, select dual ". Finally, we don't care about "aggregation (SET)". Click "no. We do not need to care about the three options below. Click OK.
Step 3: Add a method
If you click the "Class View" tab in the workspace, you will see that the wizard has added a bunch of things here. The first thing we need to do is add a method. You can right-click "ifirst_atl" and select "add method.
After you click "add method", the "add method to interface" window is displayed. Under the return value type, you can see that the default return type of the method is "hresult ". In most cases, you do not need to ignore this option. The next text box allows us to enter the method name. Here we enter "addnumbers ". The last text box requires us to enter practical parameters. If we want to add two numbers and return a result, we need three parameters. The last parameter is a pointer. Now we will not go to the three hundred-plus-page IDL tutorials and directly enter the following content in the parameter box.
[In] Long num1, [in] Long num2, [out] Long * returnval
I
Here, we have two long parameters. These two parameters are [in], that is, the input value, and the last value is the returned [out] output value. (It may seem a little funny for the first time, no
If you have read a book about com, you can accept it.) Click "OK. Click the "classview" tab and open all the plus signs in the view. After the top interface ("ifirst_atl") is opened, you can see our "addnumbers" method and the parameters we have given it. Double-click this item to enter the Encoding Area. The code is modified as follows:
Stdmethodimp cfirst_atl: addnumbers (long num1,
Long num2, long * returnval)
{
// Todo: add your implementation code here
* Returnval = num1 + num2;
Return s_ OK;
} Step 4: Compile the DLL
Do you believe that now we have used ATL to build a executable COM Service! Of course, compile it. Press F7 in the VC ++ environment. The compiler will be "squeaking" for a while-the computer is registering your DLL so that other programs can use it. Next, let's give it a try.
Step 5: use VB to test the COM Service
First, we use VB to test the COM Service. (If you do not have VB, you can skip this chapter and test it with VC.) Start VB and select "Standard EXE (Standard exe)" as your project. Put a command button in the dialog box. Now we need to configure the COM Service. Click the "project" menu and select "configuration ". Find "simple ATL 1.0 Type Library" in the drop-down list ".
Click OK. Double-click the command button to enter the VB code editing window. Enter 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 programmer, it is very easy to understand this code. We declare an object that calls "addnumbers" from the COM Service and then displays the result. Press F5 to run the VB project. click the button and you will see the result.
Not too hard. Lets try this again, cannot with VC ++.
Step 6: Use VC to test the COM Service
Save and close the previous simple_atl project and create a new project. Select "Win32 console application" and name it "test_atl". Click "OK" and accept the default value "An Empty Project (empty project)" in the next window. Use Ctrl + n to create a new file. Select "C ++ source file (C ++ source code file)" and name it "test_atl.cpp ". Press OK. At this time, an empty file will be opened. We need to enter some code here to test our com service. Enter the following code into the new CPP file:
// You need to point this header file to the directory
// You placed the simple_atl Project
# Include ".. \ simple_atl \ simple_atl.h"
# Include <iostream. h>
// Copy the following from the simple_atl_ I .c File
// From the simple_atl project directory
// Note: You can actually skip copying these if you want
// And just include the simple_atl_ I .c file, I simply added
// It For clarity to show where these const variables are
// Coming 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 and hresult and a pointer
// The simple_atl Interface
Hresult hr;
Ifirst_atl * ifirstatl = NULL;
// Now we will intilize com
HR = coinitialize (0 );
// Use the succeeded macro and see if
// We can get a pointer
// To the interface
If (succeeded (HR ))
{
HR = cocreateinstance (clsid_first_atl, null,
Clsctx_inproc_server,
Iid_ifirst_atl, (void **) & ifirstatl );
// If we succeeded then call the addnumbers
// Method, if it failed
// Then display an appropriate message 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;
}
}
// Uninitialize com
Couninitialize ();
} Step 7: Compile and run the program
Press F5 to compile the program, and then press Ctrl + F5 to run the program. This is a DOS window that shows the result you need.
C. Lung
|
Click here to view C. Lung's online profile. |
Source: http://blog.csdn.net/mhoudg/archive/2007/08/25/1758491.aspx