ATL Development   «Prev  Next»
Lesson 13 Adding methods
Objective Add GetPhoneRec to IReadPhBook; add AddPhoneRec and DeletePhoneRec to IManagePhBook.

Adding COM Interface Methods

Let's add our interface methods. We will start by adding GetPhoneRec to interface IReadPhBook. Once that's done, we'll add AddPhoneRec and DeletePhoneRec to interface IManagePhoneRec. These will enable us to get, add, and delete phone records from our project.

Steps to add Interface Methods

Here are the steps requires to add interface methods:
  1. The first step is to add GetPhoneRec to interface IReadPhBook. Right-click on IReadPhBook in the ClassView pane of the Workspace window.
  2. Click Add Method ... to bring up the Add Method to Interface dialog.
  3. In the Add Method to Interface dialog, type GetPhoneRec for the method name. Type [in] PhRec *pPhRec, [out] BOOL *pOK as parameters. Normally, the implementation is displayed in the Implementation window as you type. When you are done, click OK.
  4. We have expanded IReadPhBook in Class View to show GetPhoneRec as a member of IReadPhBook. The next step is to add AddPhoneRec to IManagePhoneRec. Right-click on IManagePhBook.
  5. Click Add Method ... to bring up the Add Method to Interface dialog.
  6. In the Add Method to Interface dialog, type AddPhoneRec for the method name. Type [in] PhRec *pPhRec, [out] BOOL *pOK as parameters. Normally, the implementation is displayed in the Implementation window as you type. When you are done, click OK.
  7. We have expanded IManagePhBook in Class View to show that AddPhoneRec has been added. The last step is to add DeletePhoneRec to IManagePhBook. Right-click on IManagePhBook.
  8. Click Add Method ... to bring up the Add Method to Interface dialog.
  9. In the Add Method to Interface dialog, type DeletePhoneRec for the method name. Type [out] BOOL *pOK as parameters. Normally, the implementation is displayed in the Implementation window as you type. When you are done, click OK.
  10. Class View displays DeletePhoneRec under IManagePhBook. This is the end of the simulation.

Adding methods
  1. GetPhoneRec,
  2. AddPhoneRec, and
  3. DeletePhoneRec
via "Add Method to Interface" adds method declarations to CPhBookObj and adds code stubs to PhBookObj.cpp.

Adding methods ChBookObj

Property and method declarations added to class CPhBookObj:
class CPhBookObj : 
  public IReadPhBook,
  public IManagePhBook,
  public CComObjectRoot,
  public CComCoClass<CPhBookObj,
    &CLSID_PhBookObj>
{
public:
  CPhBookObj() {}
BEGIN_COM_MAP(CPhBookObj)
  COM_INTERFACE_ENTRY(IReadPhBook)
  COM_INTERFACE_ENTRY(IManagePhBook)
END_COM_MAP()

DECLARE_REGISTRY_RESOURCEID(IDR_PhBookObj)

public:

  STDMETHOD(DeletePhoneRec)(/*[out]*/ BOOL *pOK);
  STDMETHOD(AddPhoneRec)(/*[in]*/ PhRec
    *pPhRec, /*[out]*/ BOOL *pOK);
  STDMETHOD(GetPhoneRec)(/*[in]*/ PhRec
    *pPhRec, /*[out]*/ BOOL *pOK);

  STDMETHOD(get_NumRecs)(/*[out, retval]*/
 long *pVal);
  STDMETHOD(get_MaxRecs)(/*[out, retval]*/
    long *pVal);
  STDMETHOD(get_CurRec)(/*[out, retval]*/
    long *pVal);
  STDMETHOD(put_CurRec)(/*[in]*/
    long newVal);
};

Add Code Stubs - PhBookObj.cpp

Code stubs added to PhBookObj.cpp:
STDMETHODIMP CPhBookObj::get_CurRec(long *pVal)
{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::put_CurRec(long newVal)
{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::get_MaxRecs(long *pVal)
{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::get_NumRecs(long *pVal)
{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::GetPhoneRec
  (PhRec *pPhRec, BOOL *pOK)

{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::AddPhoneRec
  (PhRec *pPhRec, BOOL *pOK)
{
  // TODO: Add your implementation code here
  return S_OK;
}

STDMETHODIMP CPhBookObj::DeletePhoneRec(BOOL *pOK)

{
  // TODO: Add your implementation code here
  return S_OK;
}

Declarations for each method were also added into PhBook.idl.

Final State of Interface Definitions - PhBook.idl

The final state of the interface definitions in PhBook.idl:
...
//Partial listing
[
  object,
  uuid
    (EF7C3D7D-653E-11D2-85DB-08001700C57F),
  helpstring("IReadPhBook Interface"),
  pointer_default(unique)
]
interface IReadPhBook : IUnknown
{
  [propget, helpstring("property CurRec")]
    HRESULT CurRec([out, retval] long *pVal);
  [propput, helpstring("property CurRec")]
    HRESULT CurRec([in] long newVal);
  [propget, helpstring("property MaxRecs")]
    HRESULT MaxRecs([out, retval] long *pVal);
  [propget, helpstring("property NumRecs")]
    HRESULT NumRecs([out, retval] long *pVal);

  [helpstring("method GetPhoneRec")] 
    HRESULT GetPhoneRec([in] PhRec *pPhRec,
   [out] BOOL *pOK);

};

[
  object,
  uuid(EF7C3D7E-653E-11D2-85DB-08001700C57F),
  helpstring("IManagePhBook Interface"),
  pointer_default(unique)
]
interface IManagePhBook : IUnknown
{

  [helpstring("method AddPhoneRec")] 
  HRESULT AddPhoneRec([in] PhRec *pPhRec,
    [out] BOOL *pOK);

  [helpstring("method DeletePhoneRec")] 
  HRESULT DeletePhoneRec([out] BOOL *pOK);

};
...

Add Methods - Exercise

Click the Exercise link below to apply what you have learned about adding methods to interfaces.
Add Methods - Exercise