译注:本文应用是VC6.0,对于其他版本向导可能与本文介绍的不同。
原文地址:http://www.codeproject.com/atl/com_atl.asp
作者: C. Lung.
简介
第一步:运行 ATL COM 向导
启动VC++之后的第一步是创建一个新项目。选择“New Project Information新项目信息”的窗口,这个窗口描述了刚才创建的项目内容。点击OK表示接受。
第二步:创建一个新的ATL对象
确保打开VC++ IDE中的Workspace(工作区)视图。如果没打开,您可以点击视图菜单,然后选择“Workspace
默认选择
第三步:添加一个方法
如果您现在在工作区点击“类视图”选项卡,您将看到向导已经在这里添加了一堆东西。我们要做的第一件事就是添加一个方法。我们可以通过右键点击“IFirst_ATL”然后选择”Add Method“轻松添加。
您点击“Add Method”之后,将看到“添加方法至界面”的窗口。在返回值类型下面,您可以看到默认的方法返回类型为“HRESULT”。大多数情况下,您不需要理会这个选项。下一个文字框允许我们输入方法名。我们在这里输入“教程,直接把下面的内容输入到参数框中。
I 在这里,我们生命了两个long型的参数,这两个参数是[in]也就是输入值,而最后一个值是返回的[out]输出值。(第一次看到可能觉得有点搞笑,不 过如果您读过关于COM的书,就能够接受这种东西了)点击确认按钮。点击“ClassView”选项卡并且打开视图中的所有加号。最上面的接口”方法,以及我们给他的参数。双击这个东东,将带我们进入编码区,我们修改代码如下:
long Num2, long *ReturnVal)
{
// TODO: Add your implementation code here
*ReturnVal = Num1 + Num2;
return S_OK;
}
第四步:编译DLL
你相信么,现在已经我们已经用ATL建立好一个可以执行的COM服务了!当然,还要编译一下。在VC++的环境中按F7就可以了。编译器会“吱嘎吱嘎”一会儿——计算机在注册你的DLL,以便让其他程序可以使用。接下来,我们试一下。第五步:用VB测试COM服务
首先我们用VB来试验一下COM服务。(如果你没有VB,也可以跳过这章,直接用VC测试)启动VB并选择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
Not too hard. Lets try this again, except with VC++.
第六步:用VC测试COM服务
保存并关闭以前的// 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();
}
第七步:编译并运行程序
按“F5”可以编译程序,然后按CTRL+F5可以运行程序。这是您可以看到一个DOS窗口,显示这您需要的结果。C. Lung
本文来源:http://blog.csdn.net/mhoudg/archive/2007/08/25/1758491.aspx