Create com step by step

目录:
介绍
接口
第一步:创建IDL文件
第二步:生成类型库
第三步:从IAdd继承
第四步:实现IAdd的方法
第五步:实现IUnkown
第六步:类工厂
第七步:实现IClassFactory的方法
第八步:实现DllGetClassObject
第九步:实现DllCanUnloadNow
第十步:DllRegisterServer和UnregisterServer
第十一步:将IDL文件插入到工作空间中
第十二步:在Visual Basic 中使用COM刚创建的对象
第十三步:分析我们刚创建的所有文件
第十四步:在ActiveX DLL中嵌入类型库
第十五步:从Visual C++客户方使用刚创建的COM对象

介绍
对于我来说,理解COM(Component Object Model)至少是不止一次的冒险旅行了。我相信,每一个希望理解COM背后基本理论的程序员,一定有过使用没有MFC/ATL的模板和宏帮助下的C++语言写了至少一个以上的简单的COM对象。在这篇文章中,我会根据COM的基本原理,介绍创建一个简单COM对象的步骤。这个创建的组件可以在VC/VB的客户端使用。
作为练习,我们会试图设计一个COM对象,实现一个假想的超级快速的加法算法。这个组件有两个长整型(long type)的参数,其返回值也是一个长整型数,是我们加法算法的结果。下面我们就开始设计这个接口。
接口
COM对象的接口,我们不会谈到实际的实现,而是要谈论使用这个COM对象并与之通信的方法签名(method signatures)。我们将会命名我们的接口为IAdd。 这个接口的声明使用接口定义语言来完成(Interface Definition Language --IDL),接口定义语言是用来定义函数签名(Function Signatures)的,它使用一种独立于编程语言的格式,这有助于RPC组织从一个计算机到另一个的打包、分发和解包的参数。在我们的接口IAdd中,我们有两个方法SetFirstNumber和SetSecondNumber,这两个方法用来传递加法使用的参数。然后,还有另外一个方法DoTheAddition,他用来计算实际的结果并将结果返回给客户。
第一步:创建IDL文件
创建一个新的win32 DLL工程(命名为AddObj)。接下来的所有文件将被创建到此文件夹。创建一个空文件并键入下面的内容,把它保存为IAdd.idl。接口标识符使用uuidgen.exe生成(uuidgen.exe是一个dos文件,可以在命令行直接运行它)
import "unknwn.idl";

[
object,
uuid(1221db62-f3d8-11d4-825d-00104b3646c0),
helpstring("interface IAdd is used for implementing a super-fast addition Algorithm")
]

interface IAdd : IUnknown
    {
    HRESULT     SetFirstNumber(long nX1);

    HRESULT     SetSecondNumber(long nX2);
   
    HRESULT     DoTheAddition([out,retval] long *pBuffer);
    };

[
uuid(3ff1aab8-f3d8-11d4-825d-00104b3646c0),
helpstring("Interfaces for Code Guru algorithm implementations .")
]
library CodeGuruMathLib
    {
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    interface IAdd;
    }
第二步 生成类型库
使用IDL编译器MIDL.EXE编译接口定义文件IAdd.idl。编译完成后,将会生成以下的文件:
IAdd.h
包含C++格式的接口声明

dlldata.c
包含代理DLL的代码。当在一个不同的处理器/计算机上调用对象时使用

IAdd.tlb
二进制文件。使用一个定义好的格式完全的描述我们的IAdd接口和它所有的方法。这个文件和COM组件分发给所有的客户。

IAdd_p.c
包含代理DLL的列集代码。当在一个不同的处理器/计算机上调用对象时使用

IAdd_i.c
包含接口ID(IID)

(译者注:编译方法为,如过你的机器已经安装了Visual Studio环境的话,那么你可以直接从命令行写入如下的语句,midl.exe ../ AddObj/iadd.idl )
第三步 从IAdd继承
我们将在此步骤中创建一个COM对象。创建一个新文件(AddObj.h),声明一个C++类,命名这个类为CAddObj,从接口IAdd继承(在文件IAdd.h)。记住,IAdd继承自IUnknown,IUnknown是一个抽象基类。因而我们不得不像IUnknown一样为抽象基类IAdd声明所有的方法。

///
//
//AddObj.h
//Contains the C++ class declarations for implementing the IAdd
//interfaces
//
#include    "IAdd.h"
extern long g_nComObjsInUse;
class CAddObj :
        public IAdd
    {
    public:

    //IUnknown interface
    HRESULT __stdcall QueryInterface(
                                REFIID riid ,
                                void **ppObj);
    ULONG   __stdcall AddRef();
    ULONG   __stdcall Release();

    //IAdd interface
    HRESULT __stdcall SetFirstNumber( long nX1);
    HRESULT __stdcall SetSecondNumber( long nX2);
    HRESULT __stdcall DoTheAddition( long *pBuffer);

    private:

    long m_nX1 , m_nX2; //operands for addition
    long m_nRefCount;   //for managing the reference count
    };

///

第四步 实现IAdd的方法

我们将在此步骤中提供接口IAdd所有方法的实现。
创建一个新文件(AddObj.cpp),在此文件中添加如下实现代码。

///
//
//AddObj.cpp
//Contains the method implementations of the IAdd interface
//interfaces
//

#include    <objbase.h>

#include    "AddObj.h"
#include    "IAdd_i.c"

HRESULT __stdcall CAddObj::SetFirstNumber( long nX1)
    {
    m_nX1=nX1;
    if (m_bIsLogEnabled) WriteToLog("Junk");
    return S_OK;
    }

HRESULT __stdcall CAddObj::SetSecondNumber( long nX2)
    {
    m_nX2=nX2;
    return S_OK;
    }

HRESULT __stdcall CAddObj::DoTheAddition( long *pBuffer)
    {
    *pBuffer =m_nX1 + m_nX2;

    return S_OK;
    }
/

第五步 实现IUnknown

IUnknown方法需要被实现。在文件AddObj.cpp中,我们将实现它的三个强制的方法(AddRef,Release和QueryInterface)。私有成员变量m_nRefCount是用来保持对象生命期的,他不被直接的自加/减的,而是我们亲自使用线程安全的方法,使用API函数InterlockedIncrement 和 InterlockedDecrement。

/

HRESULT __stdcall CAddObj::QueryInterface(
                                    REFIID riid ,
                                    void **ppObj)
    {
    if (riid == IID_IUnknown)
        {
        *ppObj = static_cast(this) ;
        AddRef() ;
        return S_OK;
        }

    if (riid == IID_IAdd)
        {
        *ppObj = static_cast(this) ;
        AddRef() ;
        return S_OK;
        }

    //
    //if control reaches here then , let the client know that
    //we do not satisfy the required interface
    //

    *ppObj = NULL ;
    return E_NOINTERFACE ;
    }//QueryInterface method



ULONG   __stdcall CAddObj::AddRef()
    {
    return InterlockedIncrement(&m_nRefCount) ;
    }

   
   
ULONG   __stdcall CAddObj::Release()
    {    
    long nRefCount=0;
    nRefCount=InterlockedDecrement(&m_nRefCount) ;
    if (nRefCount == 0) delete this;
    return nRefCount;
    }

/
第六步 类工厂

我们已经完成了Add COM对象的部分功能。作为每一个COM的准则,每个COM对象必须有一个独立的IClassFactory实现。客户将使用这个接口得到我们IAdd接口实现的实例。IClassFactory接口,像所有其他COM接口一样,继承自IUnknown。因此我们也必须提供一个IUnknown的实现,以及IClassFactory的方法(LockServer 和 CreateInstance)。创建一个新文件(命名为AddObjFactory.h)。声明一个类CAddFactory并使之继承自IClassFactory。

///
//AddObjFactory.h
//Contains the C++ class declarations for the IClassFactory implementations
//

class CAddFactory : public IClassFactory
    {

    public:


    //interface IUnknown methods
    HRESULT __stdcall QueryInterface(
                                REFIID riid ,
                                void **ppObj);
    ULONG   __stdcall AddRef();
    ULONG   __stdcall Release();


    //interface IClassFactory methods
    HRESULT __stdcall CreateInstance(IUnknown* pUnknownOuter,
                                             const IID& iid,
                                             void** ppv) ;
    HRESULT __stdcall LockServer(BOOL bLock) ;

    private:
    long m_nRefCount;
    };
/
第七步 实现IClassFactory的方法

实现类CAddFactory的方法。创建一个新文件(AddObjFactory.cpp)。提供类IUnknown和IClassFactory的方法实现。AddRef,Release和QueryInterface方法实现和前面类CAddObj中这三个函数实现基本一样。在方法CreateInstance中,类CaddObj被实例化并且传回其接口指针。LockServer方法没有给出细节的实现。

HRESULT __stdcall CAddFactory::CreateInstance(IUnknown* pUnknownOuter,
                                           const IID& iid,
                                           void** ppv)
    {
    //
    //This method lets the client manufacture components en masse
    //The class factory provides a mechanism to control the way
    //the component is created. Within the class factory the
    //author of the component may decide to selectivey enable
    //or disable creation as per license agreements
    //
    //

    // Cannot aggregate.
    if (pUnknownOuter != NULL)
        {
        return CLASS_E_NOAGGREGATION ;
        }

    //
    // Create an instance of the component.
    //
    CAddObj* pObject = new CAddObj ;
    if (pObject == NULL)
        {
        return E_OUTOFMEMORY ;
        }

    //
    // Get the requested interface.
    //
    return pObject->QueryInterface(iid, ppv) ;
    }


HRESULT __stdcall CAddFactory::LockServer(BOOL bLock)
    {
    return E_NOTIMPL;
    }
/

第八步 实现DllGetClassObject的方法

一个进程内COM对象只是一个简单的WIN32DLL,他遵循既定的协议。每一个COM DLL必须有一个通过名字DllGetClassObject的出口函数。客户端将调用这个函数以得到类厂的接口(IUnknown or IClassFactory),之后就是调用CreateInstance方法。创建一个新文件(Exports.cpp),在其中实现DllGetClassObject。(代码如下)

STDAPI DllGetClassObject(const CLSID& clsid,
                         const IID& iid,
                         void** ppv)
    {
    //
    //Check if the requested COM object is implemented in this DLL
    //There can be more than 1 COM object implemented in a DLL
    //

    if (clsid == CLSID_AddObject)
        {
        //
        //iid specifies the requested interface for the factory object
        //The client can request for IUnknown, IClassFactory,
        //IClassFactory2
        //
        CAddFactory *pAddFact = new CAddFactory;
        if (pAddFact == NULL)
            return E_OUTOFMEMORY;
        else
            {
            return pAddFact->QueryInterface(iid , ppv);
            }
        }
   

    //
    //if control reaches here then that implies that the object
    //specified by the user is not implemented in this DLL
    //

    return CLASS_E_CLASSNOTAVAILABLE;
    }
/

第九步 实现DllCanUnloadNow

客户需要知道什么时候COM DLL可以被从内存中卸载。更进一步,一个进程内COM对象显示的通过调用API函数LoadLibrary装入内存中的客户程序的进程空间中。这个显示装载的DLL也可以使用FreeLibrary卸载。COM客户必须知道什么时候DLL可以被安全的卸载。一个客户必须确定没有任何来自于特别是DLL中的COM对象的实例仍在生命期中。要使得这个可以被简单的计算,在一个COM DLL中,我们在类CAddObj和CAddFactory的构造函数中自加一个全局变量(g_nComObjsInUse)。类似的,在析构函数中,自减这个全局变量。
我们输出另一个COM规定的函数:DllCanUnloadedNow,实现如下:

STDAPI DllCanUnloadNow()
    {
    //
    //A DLL is no longer in use when it is not managing any existing objects
    // (the reference count on all of its objects is 0).
    //We will examine the value of g_nComObjsInUse
    //

    if (g_nComObjsInUse == 0)
        {
        return S_OK;
        }
    else
        {
        return S_FALSE;        }

}

第十步 DllRegisterServer 和 UnregisterServer

COM对象的位置信息将会进入到注册表中。这个工作可以在外面通过一个.REG文件完成,也可以使用一个输出函数DllRegisterServer。要去除注册表的内容,我们将使用另一个输出函数DllUnregisterServer。这两个函数的实现在文件Registry.cpp中。一个简单的工具regsrv32.exe也可以用来装入一个指定的DLL并执行DllRegisterServer/DllUnregisterServer。
要使得连接器输出这两个函数,我们将创建一个模块定义文件(Exports.def)
;
;contains the list of functions that are being exported from this DLL
;

DESCRIPTION     "Simple COM object"

EXPORTS
                DllGetClassObject   PRIVATE
                DllCanUnloadNow    PRIVATE
                DllRegisterServer   PRIVATE
                DllUnregisterServer PRIVATE

第十一步
我们下面就给出最后完成我们AddObj Win32 DLL工程的绝技。将文件IAdd.idl插入到工程工作空间中。
为此文件设置自定义的编译选项。(如下图)

为了在每一次编译完成后执行regsrv32.exe,在"Post-bulid step"对话框中插入一个命令串如下图示:

编译这个DLL。将IDL文件插入到工程中是为了减少每一次文件被编辑后必须要执行外部编译。每次我们成功的编译我们的工程,这个COM对象就会被注册。

第十二步 在Visual Basic 中使用COM刚创建的对象

要从Visual Basic中使用AddObj COM 对象,创建一个简单的EXE工程,添加如下的代码。确定已经添加了对IAdd.tlb模板库的一个对象的引用。
Dim iAdd As CodeGuruMathLib.iAdd
    Set iAdd = CreateObject("CodeGuru.FastAddition")

    iAdd.SetFirstNumber 100
    iAdd.SetSecondNumber 200

MsgBox "total = " & iAdd.DoTheAddition()

第十三步 分析我们刚创建的所有文件

如下是我们曾使用过的文件

IAdd.idl
接口定义文件的内容

AddObj.h
包含类 CAddObj 声明

AddObjFactory.h
包含类 CAddFactory 声明

AddObj.cpp
包含类CAddObj 实现

AddObj.cpp
包含类CAddFactory 实现

Exports.cpp
包含DllGetClassObject,DllCanUnloadNow & DllMain 的实现

Registry.cpp
包含r DllRegisterServer,DllUnregisterServer 的实现

AddObjGuid.h
包含我们 AddObj COM 对象的GUID值


第十四步 在ActiveX DLL中嵌入类型库

随AddObj.dll一起,类型模板库也可以被发布。对于一个简单的进程,类型模板库IAdd.tlb可以作为二进制资源嵌入到AddObj.dll文件中。以后,仅仅将这个DLL文件AddObj.dll发布给客户就可以了。


第十五步 从Visual C++客户方使用刚创建的COM对象

通过如下的任一种方式,Visual C++客户可以使用这个COM接口

1、#import “IAdd.tlb”
2、IAdd.h头文件。在此情况下,这个DLL提供商必须将IAdd.h随DLL文件一起发布。
3、使用一些工具向导生成c++代码(例如,MFC类向导)

在第一种情况下,编译器创建一些中间文件(.TLH,.TLI),这些文件中含有扩展的接口声明。此外,编译器也可以声明根据接口建立(bulit around the raw interfaces)智能指针类。对于COM程序员来说,通过适当的管理COM对象的生存期,智能指针类使生存期更容易(控制)。
下面的例子中,#import 来输入AddObj.dll文件,而不是IAdd.tlb,因为我们把TLB文件放到了DLL中(译者注,前面提到的将类型库作为二进制资源放到DLL资源中)。此外,#import也应该使用TLB文件。
在Visual C++中,创建一个控制台EXE工程。键入(或拷贝)如下的内容并编译。
//
///Client.cpp
//
//Demo of client using the COM object from AddObj.dll
//

#include    <objbase.h>
#include    <stdio.h>
#import     "AddObj.dll"
//
//Here we do a #import on the DLL ,you can also do a #import on the .TLB
//The #import directive generates two files (.tlh/.tli) in the output folders.
//

void main()
    {

    long n1 =100, n2=200;       
    long nOutPut = 0;

    CoInitialize(NULL);
    CodeGuruMathLib::IAddPtr pFastAddAlgorithm;
    //
    //IAddPtr is not the actual interface IAdd, but a template C++ class (_com_ptr_t)
    //that contains an embedded instance of the raw IAdd pointer
    //While destructing , the destructor makes sure to invoke Release() on the internal
    //raw interface pointer. Further, the operator -> has been overloaded to direct all
    //method invocations to the internal raw interface pointer.
    //
    pFastAddAlgorithm.CreateInstance("CodeGuru.FastAddition");

    pFastAddAlgorithm->SetFirstNumber(n1);//"->" overloading in action
    pFastAddAlgorithm->SetSecondNumber(n2);
    nOutPut = pFastAddAlgorithm->DoTheAddition();

    printf("/nOutput after adding %d & %d is %d/n",n1,n2,nOutPut);
    }

To create a PAL decoder in GNU Radio Companion (GRC), follow these steps: 1. Open GNU Radio Companion (GRC) and create a new flowgraph. 2. Drag and drop a "File Source" block onto the flowgraph workspace. 3. Double click on the "File Source" block and select the PAL audio file that you want to decode. 4. Drag and drop a "Quadrature Demod" block onto the flowgraph workspace. 5. Connect the output of the "File Source" block to the input of the "Quadrature Demod" block. 6. Drag and drop a "Low Pass Filter" block onto the flowgraph workspace. 7. Connect the output of the "Quadrature Demod" block to the input of the "Low Pass Filter" block. 8. Double click on the "Low Pass Filter" block and set the cutoff frequency to 4.5 kHz. 9. Drag and drop a "Throttle" block onto the flowgraph workspace. 10. Connect the output of the "Low Pass Filter" block to the input of the "Throttle" block. 11. Drag and drop a "PAL Decoder" block onto the flowgraph workspace. 12. Connect the output of the "Throttle" block to the input of the "PAL Decoder" block. 13. Double click on the "PAL Decoder" block and set the "Standard" to "PAL-B/G". 14. Drag and drop a "WAV File Sink" block onto the flowgraph workspace. 15. Connect the output of the "PAL Decoder" block to the input of the "WAV File Sink" block. 16. Double click on the "WAV File Sink" block and set the filename and format of the output file. 17. Save and run the flowgraph. This should create a PAL decoder that reads in a PAL audio file, filters and demodulates it, decodes it using the PAL-B/G standard, and saves the output as a WAV file.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值