VC ATL COM 新手入门教程[转]

                                

介绍
本教程的目的是告诉你如何使用ATL创建一个COM服务器,并使用Visual C++和Visual Basic程序来分别调用这个服务器。我并不想深入探讨COM的细节,也不想让你深陷于IDL之中。这一教程只是为VC++的新手程序员设计的,告诉他们利用ATL来创建一个COM对象有多么简单,并让他们能对ATL产生更多的兴趣。

第1步:启动ATL COM Wizard

你所需要做的第一件事情就是启动Visual C++并创建一个新的工程,选择“ATL COM Wizard”,工程名为“Simple_ATL”。设置好工程的路径之后,单击OK按钮。你会看到,屏幕上给了你若干选项。第一个选项为“Server Type”。我们将要创建一个服务器DLL,所以请确认服务器的类型选为“Dynamic Link Library”。我们并不需要关心下面的其它三个复选框,所以我们可以将它们忽略掉。按下Finish按钮,这样向导就会为你产生适当的文件了。之后,一个“New Project Information”窗口就会出现,你可以从上面得知向导都会创建什么文件,按下“OK”接受这一切。

第2步:创建一个新的ATL对象

请确认你能在VC++的IDE中看到Workspace View,如果不能的话则请单击“View”菜单,然后选择“Workspace”。在这个视图中你会看到三个选项卡,请单击“ClassView”栏,你应该会看到“Simple_ATL Classes”。请在此右击鼠标键,并在弹出菜单中选择“New ATL Object”,你将会看到下面这样的窗口:



默认的选择项“Simple Object”就是我们所要的了,请单击next按钮,你会来到“ATL Object Wizard Properties”窗口中。在“Short Name”文本框中输入“First_ATL”。请注意,这时候向导就会自动地填写其它的文本框。然后,单击顶部的“Attributes”标签,在这里你需要做一些选择。第一项线程模型(Threading Model)的选择,我们选择默认的单元(Apartment)模型。对于接口(Interface),我们选择双重(Dual)。最后,因为我们的程序与聚合(Aggregation)无关,所以我们选择“No”的单选按钮。你不必关心底部的三个复选框,直接单击OK就可以了,这时候向导就会为我们创建一个新的ATL简单对象。



第3步:添加一个方法

如果你现在在工作区中单击了“ClassView”标签,那么你会注意到向导在其中添加了一串东西。我们想添加的第一个东西是一个方法,可以在“IFirst_ATL”上右击鼠标键,并选择“Add Method”。



一旦你单击了“Add Method”之后,你就会看到“Add Method to Interface”窗口。在返回值类型(Return Type)处你会看到,这个方法会默认返回HRESULT,在大多数情况下你不需要改变它。下一个文本框允许我们输入方法的名称,我们可以输入“AddNumbers”。最后一个文本框是让我们输入参数的,由于我们想做两个数的相加并获得一个返回的结果,所以我们需要三个参数,并且最后一个参数是一个指针。现在,我们不必看那关于IDL的300页教程了,可以直接在参数文本框中输入:

[in] long Num1, [in] long Num2, [out] long *ReturnVal

简单地说来,我们声明了两个long类型的参数,这两个值是传入的([in]),还有一个最后传出的返回值结果([out])。(你第一次看到这样的东西可能会有些奇怪,但是如果你读了一两本关于COM的书的话,就会觉得亲切多了。)现在就可以单击OK按钮了。然后,单击“ClassView”标签,并展开所有的“+”标志,使得树型视图完全展开。你会在接口(IFirst_ATL)的顶部看到我们的“AddNumbers”方法以及我们给予它的参数。在这个方法上双击鼠标键,并插入以下的代码:

STDMETHODIMP CFirst_ATL::AddNumbers(long Num1, 
                long Num2, long *ReturnVal)
{
    // TODO: Add your implementation code here
    *ReturnVal = Num1 + Num2;

    return S_OK;
}

第4步:编译DLL

无论你相信与否,你已经拥有一个用ATL编写的COM服务器了!当然,我们还需要编译它。请按下F7键,这样VC++就可以编译了。编译器工作片刻后就会在注册表中注册你的新DLL了,这样一来其它的程序就可以使用它了。让我们来测试一下。

第5步:在Visual Basic中测试COM服务器

那么,先让我们用VB来测试这个COM服务器吧。(如果你没有VB的话,你可以跳过这一节,直接在VC++中测试。)启动VB,并选择“标准EXE(Standard EXE)”建立工程,并在对话框上放置一个命令按钮。现在,我们需要添加COM服务器的引用,请单击“工程(Project)”菜单并选择“引用(References)”,找到“Simple ATL 1.0 Type Library”并选择它。



单击确定(OK)按钮之后,你可以双击先前放置的命令按钮,VB会切换到这个按钮的代码窗口。添加以下的代码:

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

如果你是个VB的程序员,那么这些代码对于你是很直观的:我们声明了一个对象,并从COM服务器调用“AddNumbers”的方法,然后显示结果。现在按下F5来运行这个VB工程,单击命令按钮,你就会看到期望的结果了:



并不是很难吧?那么我们再来一次,这一次用VC++。

第6步:在Visual C++中测试COM服务器

如果你的Simple_ATL工程仍然开着,那么就关了它并创建一个新工程。选择“Win32 Console Application”,起名为“Test_ATL”,在下一个窗口中单击OK按钮接受所有默认值,最后单击Finish按钮。现在,你应该有了一个空工程。那么,按下Ctrl+N为工程加入一个新文件,选择“C++ Source File”并命名为“Test_ATL.cpp”,单击OK接受。你现在应该有了一个打开的空白文件,我们需要在其中添加一些代码来测试COM服务器。代码如下:

// 你需要指明Simple_ATL工程的路径来引用这个头文件

#include "../Simple_ATL/Simple_ATL.h"
#include <iostream.h>

// 把以下的内容从Simple_ATL工程目录的Simple_ATL_i.c文件中复制过来
// 注意:你也可以直接包含Simple_ATL_i.c文件,我在此只想清楚地表明这些const常量来自何处以及它们的样子

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)
{
    // 声明一个HRESULT变量以及一个Simple_ATL接口的指针
    HRESULT         hr;
    IFirst_ATL      *IFirstATL = NULL;

    // 现在初始化COM
    hr = CoInitialize(0);

    // 使用SUCCEEDED宏来看看我们是否能够获得接口的指针
    if(SUCCEEDED(hr))
    {
        hr = CoCreateInstance( CLSID_First_ATL, NULL, 
            CLSCTX_INPROC_SERVER,
            IID_IFirst_ATL, (void**) &IFirstATL);

        // 如果成功了,那么调用AddNumbers方法
        // 否则给用户显示一条适当的信息
        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;
        }
    }
    // 卸载COM
    CoUninitialize();
}

第7步:编译并运行程序

现在你可以按下F5键来编译程序,然后按下Ctrl+F5来运行之。你应该可以看到一个DOS窗口,给出了你所期望的结果。

来源:http://www.codeproject.com/atl/com_atl.asp

原文:

Introduction

The purpose of this tutorial is to give you an idea on how to create a COM Server using ATL, and then being able to call the server from both a Visual C++ program, and a Visual Basic program. I am not going to go into the depths of COM details or burden you down with IDL, this tutorial is designed to show a new VC++ programmer, how easy "Simple" COM objects are to create using ATL, and to whet their appetite for wanting to learn more.

Step 1: Running the ATL COM Wizard

The first thing you need to do is to fire up Visual C++ and create a new project. Choose the "ATL COM AppWizard". In the project name call it "Simple_ATL". Set the location where you want this project to be saved in, then hit the Ok button. You will see a screen that gives you several choices. The first choice is "Server Type". We are going to build a Server DLL, so make sure that the Server Type is set to "Dynamic Link Library". The other three checkboxes below do not concern us for this particular project, so we can ignore them. Press the finish button to have the Wizard generate to appropriate files for you. A "New Project Information" window will appear to tell you what files are going to be created. Press the Ok button to accept this.

Step 2: Creating a new ATL object

Make sure you can see the "Workspace View" inside the VC++ IDE. You can do this by clicking the "View" menu, then choosing "Workspace". There will be three tabs, click on the "ClassView" tab. You should see "Simple_ATL Classes". Right click on this and choose "New ATL Object" from the popup menu. You will see a window like the following:

The ATL Object Wizard

The default choice (Simple Object) is what we want. Click the next button and you will be in the "ATL Object Wizard Properties" window. In the "Short Name" textbox, enter "First_ATL". Notice how the Wizard automatically fills in the rest of the textboxes for you. Click on the "Attributes" tab at the top. Here you have several choices to make. For the first choice, Threading Model, we will stick with the default Apartment Model. For the "Interface", click on "Dual". Finally, as we are not going to be concerned with "Aggregation", click on the "No" radio button. We need not worry about any of the three checkboxes at the bottom. Click on the Ok button and let the Wizard create our new ATL Simple Object.

ATL Object Wizard Properties

Step 3: Adding a method

If you click on the "ClassView" tab now in your workspace, you will notice that the Wizard added a bunch of things. The first thing we want to do is add a method. We can do this easily by right clicking on "IFirst_ATL" and choosing "Add Method".

Adding a method

Once you have clicked on "Add Method" you will see the "Add Method to Interface" window. Under the Return Type you can see that by default the method will return "HRESULT". In most cases you should leave this as is. The next textbox allows us to type in the Method Name. Lets type in "AddNumbers". The last textbox asks us the Parameters we wish to use. As we want to add two numbers together, and get a result back, we will use three parameters. The last parameter will be a pointer. Now without going into a 300 page tutorial on IDL, we need to type in the following in the parameter textbox:

[in] long Num1, [in] long Num2, [out] long *ReturnVal

In a nutshell, we are declaring two parameters as long, the values are going in [in], and a final value to return [out] the answer. (It might looking kind of weird the first time you see it, but once you read a book or two on COM, this will make more sense) Click on the Ok button. Click on the "ClassView" tab and expand all the "+" symbols so the tree is fully open to view. Under the top interface ("IFirst_ATL") you will see our "AddNumbers" method, and the parameters we gave it. Double click on this, and it will place you into the code. Add 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 4: Compiling the DLL

Believe it or not, but you have a working COM Server built with ATL! Of course we need to compile it. To do this, press the "F7" button and let VC++ do its work. The compiler will grind away for a few seconds. The compiler will register your new DLL in the registry so that other programs can make use of it. Lets try it out.

Step 5: Testing the COM Server with Visual Basic

To start with, we will use Visual Basic to test out the COM Server. (If you do not have a copy of VB, you can skip ahead to the section on testing the COM Server in VC++) Fire up VB and choose "Standard EXE" as your project. Place a Command button on the dialog. Now we need to add a reference to the COM Server. Click on the "Project" menu and choose "References". Scroll down until you see "Simple ATL 1.0 Type Library" and click on it.

Adding VB Reference

Click the Ok button. Now, double click on the command button you placed earlier and VB will drop you into the code window for the command button. 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 your a VB programmer this should be pretty straight forward. We declare and object, call the "AddNumbers" from the COM Server, then display the results. Press the "F5" key to run to the VB project, click on the command button, and you should see the expected results:

Testing it in VB

Not too hard. Lets try this again, except with VC++.

Step 6: Testing the COM Server with Visual C

Save and close the Simple_ATL project if it is still open and create a new project. Choose a "Win32 Console Application" and name it "Test_ATL". Click the Ok button and accept the default (An empty project) for the next window. Click on the finish button, then hit the Ok button again. You should now have an empty project. Press the "Control" and "N" keys to add a file to this project. From the window, choose "C++ Source File" and name it "Test_ATL.cpp". Press the Ok button to accept. You should have a blank file open. We need to add some code now to test out the COM Server. Start adding the following code to 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 to 
    // 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

Compile the program by pressing the "F5" key, then run it by pressing the "Control" and "F5" keys. You should see a DOS window open up and giving you the expected results.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值