Software & Finance





VC++ ATL/COM - Step 4 Adding Methods to the Interface





 

4 Adding Methods to the Interface

You will have the freedom to touch the .idl file or you can use VS2005 IDE to do it. You can select the class view and select the interface, then click on Add Method or Add Property.

[

      object,

      uuid(B129C4F7-81EA-47DA-AA22-4120C487C3FB),

      dual,

      nonextensible,

      helpstring("IMathControl Interface"),

      pointer_default(unique)

]

interface IMathControl : IDispatch{

   

    HRESULT AddTwoNumbers(

                [in]  DWORD Number1,

                [in]  DWORD Number2,

                [out] DWORD *pResult

            );

};

 

With above method added only in IDL, if you compile, you will get the following code in .H file by MIDL.

 

MIDL_INTERFACE("B129C4F7-81EA-47DA-AA22-4120C487C3FB")

    IMathControl : public IDispatch

    {

    public:

        virtual HRESULT STDMETHODCALLTYPE AddTwoNumbers(

            /* [in] */ DWORD Number1,

            /* [in] */ DWORD Number2,

            /* [out] */ DWORD *pResult) = 0;

       

    };

    }

Now you need to implement the pure virtual function defined in the interface IMathControl. The following is the code:

class ATL_NO_VTABLE CMathControl :

      public CComObjectRootEx<CComSingleThreadModel>,

      public CComCoClass<CMathControl, &CLSID_MathControl>,

      public IDispatchImpl<IMathControl, &IID_IMathControl, &LIBID_SFTComServerLib, /*wMajor =*/ 1, /*wMinor =*/ 0>

{

public:

      virtual HRESULT STDMETHODCALLTYPE AddTwoNumbers(

            /* [in] */ DWORD Number1,

            /* [in] */ DWORD Number2,

            /* [out] */ DWORD *pResult)

    {

        *pResult = Number1 + Number2;

        return S_OK ;

    }

 

};

 

Click here to download the complete source code of COM DLL Server, C++ Client and CSharp Client