网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VC编程 >> 文章正文
  初学者入门:写一个简单的COM/ATL DLL            【字体:
初学者入门:写一个简单的COM/ATL DLL
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    
A Beginner tutorial for writing simple COM/ATL DLL
作者 ThatsAlok.
关于事件、方法和属性等简单事物的文章。
正在装载数据……

  • 下载源文件 - 19.7 Kb
  • 下载演示程序 - 67.1 Kb

简介

在论坛中,我身边有很多人希望写COM/ATL DLL但是不知道如何创建属性、方法或者从组件中创建事件,也有人苦于缺少这方面的基础知识。本文中,我将写个小例子,使用VC++ 6.0一步一步创建一个ATL DLL,演示创建过程,以及属性、方法和事件的使用方法。

文章说明

  • -->‘ 符号表示作者的注释。
  • ‘|‘ 竖线符号表示菜单选项,例如: File|New 表示点击File菜单下的New选项。

现在开始

本文是针对初学者的文章,因此含有不少截图来解释COM DLL的创建过程,我会一步一步地详细解释。

  1. 打开VS 6.0 点击 File|NEW 打开下面对话框。
    Fiqure 1: Visual Studio App Wizard
    Sample screenshot
  2. 现在选择 ATL COM AppWizard 并且给项目命名为 SimpleAtlCom 然后点击OK, 接受项目设置, 接下来,您会看到下面这个对话框。
    Figure 2 ATL/COM App Wizard
    figure 2
  3. 点击Finish 接受项目设置。

    以上两个步骤将为您创建一个空的 COM DLL .

  4. Now from menu Insert|NEW ATL Object... , add an ATL object to project. You will see this figure when you click the above menu item.
    figure 3:ATL Object Wizard
    figure 3
  5. Select the Object|Simple object and click Next, then you will see this Property page. Take a view of the following figure.
    Figure 4: ATL Object Wizard after selection of SimpleAtl Object.
    figure4
  6. Here ( in fig. 4 ) give the Short Name as SimpleObj and you will see other fields in this dialog box get self generated.
    Figure 5 :Showing the Attribute of ATL Object
    figure 5
  7. In fig. 5, let me explain every thing.
    • First you see the Threading Model; here I have selected Compiler default which is apartment as most of other applications using our component is comfortable with this model.
    • Second you see Interface, again I have selected the Wizard Default to Dual (the benefit of using dual interface is that, you can use it in scripting language also).
    • Leave the topics of Aggregation and free thread Marshaler, as they are too advanced to be used in this article.
    • Now look at ISupportErrorInfo, this is provided to send rich textual information back to Client Application which is using our Interface. As we are not going to send any Error Information back to Client, I have left this Check Box unchecked.
    • Last but not the least, for invoking event from component, you need support of ConnectionPoint Interface. So check the box for supporting the connection point, click OK to Add the Object.
  8. Now in your project, a new IDL file is added (simpleAtlCom.idl). Here, take a look at IDL file, how’s it look like:
    Collapse
    // SimpleAtlCom.idl : IDL source for SimpleAtlCom.dll

    //

    // This file will be processed by the MIDL tool to

    // produce the type library (SimpleAtlCom.tlb) and marshalling code.

    import "oaidl.idl";

    import "ocidl.idl";

    // Above two File are define the IDispatch Inteface etc.

    [

    object,

    uuid(10CDF249-A336-406F-B472-20F08660D609),

    // Unique Id OF Object

    dual,

    // State our Interface is Dually Suported

    helpstring("ISimpleObj Interface"),

    pointer_default(unique)

    ]

    // Our Empty Interface

    interface ISimpleObj : IDispatch

    {

    };

    [

    uuid(8B1C3F79-07BA-44F8-8C47-AE2685488DFA),

    version(1.0),

    // our Library Name

    helpstring("SimpleAtlCom 1.0 Type Library")

    ]

    library SIMPLEATLCOMLib

    {

    importlib("stdole32.tlb");

    importlib("stdole2.tlb");

    [

    uuid(9B5BC0F8-7421-4C46-AA5F-539ECCAFCB82),

    helpstring("_ISimpleObjEvents Interface")

    ]

    // Disinterface Provides support for raising events,
    //as I already told you about that above

    dispinterface _ISimpleObjEvents

    {

    properties:

    methods:

    };

    [

    uuid(27BF0027-BECC-4847-AF91-99652BCE9791),

    helpstring("SimpleObj Class")

    ]

    // Our object base class where actual coding of our Property
    //and Event resides

    coclass SimpleObj

    {

    [default] interface ISimpleObj;

    [default, source] dispinterface _ISimpleObjEvents;

    };

    };
  9. Now add property and method to our Interface. Let's make a simple application based on class application. Any way, do you know what is a property and a method. If not, here is brief description about them. Method is the name given to a function in interface and property to variable. But remember one thing, every thing in COM/ATL is based on function, one major difference between property and method is that, you can make a property read only (means you can only get data from property but you can’t put them). Now let's get back to our application.
  10. Now put three methods, both Get and Put and one method in our interface. Now you are going to ask how and where to put them. Right click on your interface. You will get the option for putting both the method and property. For clarity, let's take a look on these pictures.
    Figure 6: Showing Popup menu Figure 7: Showing Property Wizard
    figure 6 figure7
  11. Figure 6 shows you from where you can add the properties and methods to your interface. Now as in figure 7, you can add three properties, Name, ATLMarks and ComMarks and one method Calculate and for raising, I will tell you at the end. After adding that, your interface looks like this:
    interface ISimpleObj : IDispatch
    // Our base interface
    {

    [propget, id(1), helpstring("property Name")]
    HRESULT Name([out, retval] BSTR *pVal);

    [propput, id(1), helpstring("property Name")]
    HRESULT Name([in] BSTR newVal);

    [propget, id(2), helpstring("property ATLMarks")]
    HRESULT ATLMarks([out, retval] short *pVal);

    [propput, id(2), helpstring("property ATLMarks")]
    HRESULT ATLMarks([in] short newVal);

    [propget, id(3), helpstring("property COMMarks")]
    HRESULT COMMarks([out, retval] short *pVal);

    [propput, id(3), helpstring("property COMMarks")]
    HRESULT COMMarks([in] short newVal);

    [id(4), helpstring("method Calculate")]
    HRESULT Calculate();

    };
  12. Now you will see function for every properties and methods in you class CSimpleObj. Before going for actual coding, I think you want to know propget, propput and method in above Interface.
    • Propget – stands for property for getting the value from Component.
    • PropPut–stands for property for putting property to Component. This can be optional and if you remove it, this can make your property readonly.
    • Method—simple function to perform some calculation.
    • [in] --- means data is going in or you are putting some value to Component.
    • [out,retval] --- notation states that argument using this will return with data.
    • HRESULT --- Standard Error reporting variable.
  13. Now add some useful variables in the class that will take care of above properties. Add char Name[100], short AtlMarks and short COMMarks in your CSimpleObj class and I have coded the rest of the simple class for you. Let's see and explain each function, I have included. One more property viz total, which will return the total. Still we don’t implement Events.

    Here is our SimpleObj class code.

    Collapse
    STDMETHODIMP CSimpleObj::get_Name(BSTR *pVal)
    {
    // return Name of Student
    CComBSTR bstStr(this->Name);
    *pVal=bstStr.Detach();
    return S_OK;
    }

    STDMETHODIMP CSimpleObj::put_Name(BSTR newVal)
    {
    // put Name of Student

    ::wcstombs(this->Name,newVal,99);
    return S_OK;
    }

    STDMETHODIMP CSimpleObj::get_ATLMarks(short *pVal)
    {
    //return ATL marks

    *pVal=this->ATLMarks;

    return S_OK;
    }

    STDMETHODIMP CSimpleObj::put_ATLMarks(short newVal)
    {
    // return Put of marks of atl

    this->ATLMarks=newVal;

    return S_OK;
    }

    STDMETHODIMP CSimpleObj::get_COMMarks(short *pVal)
    {
    // get the marks for COM

    *pVal=this->COMMarks;
    return S_OK;
    }

    STDMETHODIMP CSimpleObj::put_COMMarks(short newVal)
    {
    //put marks for COM

    this->COMMarks=newVal;
    return S_OK;
    }

    STDMETHODIMP CSimpleObj::get_Total(short *pVal)
    {
    //return total number of marks
    *pVal=this->m_iTotalMarks;
    return S_OK;
    }

    STDMETHODIMP CSimpleObj::Calculate()
    {
    // Calculate total number of marks and store it total number variable
    this->m_iTotalMarks=this->ATLMarks+this->COMMarks;
    return S_OK;
    }
  14. Now compile and build the SimpleAtlCom.dll using BUILD|BUILD SimpleATLCom.dll, and I think, you successfully get yourself a SimpleATLCom.dll.
  15. Now develop a simple Visual Basic Project for it. I have created a sample UI for the above component, let's take a look on it.
    Figure 8: Visual Basic Interface for above Com DLL
    figure8
  16. Now let's go for coding side, first add reference of our com DLLs to project. You can find option for it in PROJECT|REFRENCES of Visual Basic IDE. After clicking that you will get a dialog box like this, just find our SimpleAtlCom library in it and check the box against it. This will refer that DLL in your project (it is same as including a header file in C++ project) and divss OK. This is how the dialog application looks like.
    Figure 9 :Reference Dialog Box of Visual Basic IDE
    figure9
  17. Now let's look at the backend coding of VB application.
    Collapse
    'Our Component Object
    Private Obj As SIMPLEATLCOMLib.SimpleObj

    Private Sub cmdPutValue_Click()

    'give memory to Com object
    Set Obj = New SIMPLEATLCOMLib.SimpleObj

    'put atl marks in component
    Obj.ATLMarks = txtPutATL

    'put com marks
    Obj.COMMarks = Me.txtPutCom

    'put Name
    Obj.Name = Me.txtPutName

    End Sub

    Private Sub cmdGetValue_Click()
    'calculate the marks
    Obj.Calculate

    'put atl marks in component
    Me.txtGetAtl = Obj.ATLMarks

    'put com marks
    Me.txtGetCom = Obj.COMMarks

    'put Name
    Me.txtGetName = Obj.Name

    'get total marks and display it on the Component
    Me.txtTotalMarks = Obj.Total

    End Sub
  18. Now before going for events, let's see test running of our application. Here it is:
    Figure 10:First Run of Our Com DLL
    figure 10
  19. I think, till now nobody have faced any problem. I am getting above. Now take a look at events and how to create them and raise them. You can see an Interface name _ISimpleObjEvents. This is disinterface created by APP Wizard which provides support for events. You can see a divfix '_' (underscore). This underscore notifies MIDL (Microsoft IDL compiler) that this interface is disinterface and don’t make it part of TLB files. Now right click on this interface and add a method say, Void TOTAL([in]short marks) as shown in this fiqure:
    Figure 11: Adding Method to DisInterface
    figure 11
  20. Now right Click on your CSimpleObj class and click on Implement Connection Point Option. You will see this figure, check on _ISimpleObjEvents check box and Click OK. Voila! The CProxy_ISimpleObjEvents class is get added to your project. This is the proxy class that will fire event for you. You can see that, it contains the function VOID Fire_TotalMarks(SHORT TotalMarks) which is a proxy function and raises events. Oh! I forgot, take a look at the picture for Connection Point implementation.
    Fiqure 12: Adding Connection Point
    figure 12
  21. Now event is added to your class. Let's modify the CSimpleObj::Calculate().

    Now new CSimpleObj::Calculate() looks like this:

    STDMETHODIMP CSimpleObj::Calculate()
    {

    //add Marks
    this->m_iTotalMarks=this->ATLMarks + this->COMMarks;

    // when you use this pointer ,Fire_Totalmarks support
    //is added to your class
    // after you implement the Connection points

    // This event will fire the Total though event to Client

    this->Fire_TotalMarks(this->m_iTotalMarks);

    return S_OK;
    }
  22. Now let's modify our Visual Basic application to handle events.

    Change

    Private Obj As SIMPLEATLCOMLib.SimpleObj

    to

    Private withevents Obj As SIMPLEATLCOMLib.SimpleObj

    This helps us to implement events, look this figure for more clarity.

    Figure 14:Visual Basic, Adding Event to Project
    figure 14
  23. Now look at code for implementation of events, as TotalMarks comes from event, just add a line to show total marks. So our event code looks like:
    Private Sub Obj_TotalMarks(ByVal TotalMarks As Integer)
    'Display the MessageBox displaying Total Marks
    MsgBox "total marks " & TotalMarks

    End Sub
  24. Now test and run the application. Here it is the message box showing the event.
    Figure 15: Our Fully Functionable DLL and Application
    figure 15

Download Codes Includes

Source Code Includes

  1. SimpleAtlCom.dll (with source code).
  2. Test Project in Visual Basic.

Test Project Includes

  1. Compiled Test Application.
  2. Compiled SimpleAtlCom.dll.

Using of Demo Application

If you like, use the Com DLL and Test application first. Don’t forget to register Com DLL i.e., SimpleAtlCom.dll, to your computer. You can use this command line to register the Component.

Drive:> %sys%regsvr32 SimpleAtlCom.dll

Author Comment

I tried my level best to tell each and every simple aspect of com DLL. If it is missing something, feel free to contact me.

Special Thanks

  • To My Mother and Father.
  • To CodeProject.com providing platform for Programmer Interaction.
 


本文来源:http://blog.csdn.net/mhoudg/archive/2007/08/25/1758607.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     directx 图形接口指南(…
     win2k下的api函数的拦截
     用crypto  api  实现公钥…
     根据别人的md5源码封装的…
     vc中使用gdi+合并jpg图片
     document/view的交互 --…
     windows下的函数hook技术
     windows api函数大全一
     用vc 6.0实现串行通信的…
     vc++技术内幕(第四版)…
  • Ant入门-配置和使用     选…

  • JSP标准模板库(JSTL)入门教…

  • 关于JSP的隐式对象的使用(供…

  • 初学者入门:一个COM/ATL简单…

  • 3D游戏编程入门经典==第一章

  • 初学者,你应当如何学习C++以…

  • 全新java初学者实践教程9(ja…

  • Struts入门实例

  • Java正则表达式初学者指南

  • Ajax的JSP示例以及相关知识介…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David