Adding Properties

To allow your stock tool to be specialized, you define properties whose values are persisted by the Tool Palette framework. The values of these properties distinguish various instances of a single stock tool. Because your project communicates with the framework through COM interfaces, you start by defining Automation properties using ATL. To handle the properties at runtime, you define get and put functions in your coclass.

For this sample project, you define a single property, CmdName, of type BSTR.

To add property support

  1. Using Visual Studio's ATL wizard, add a new BSTR property named CmdName that supports both get and put functions.
  2. In SimpleTool.h, add a public data member to the CSimpleTool coclass to represent the new property:
    TCHAR m_tcCmdName[256];
    
  3. If the ATL wizard has not done so already, add get and put public method declarations to the coclass:
    STDMETHOD(get_CmdName)(/*out, retval*/ BSTR* pVal);
    STDMETHOD(put_CmdName)(/*in*/ BSTR newVal);
    
  4. In SimpleTool.cpp, implement get and put methods for your property, as shown below:
    STDMETHODIMP CSimpleTool:get_CmdName(BSTR* pVal)
    {
        CComBSTR bStr(m_tcCmdName);
        bStr.CopyTo(pVal);
        return S_OK;
    }
    STDMETHODIMP CSimpleTool:put_CmdName(BSTR newVal)
    {
        CComBSTR bStr(newVal);
        COLE2T szName(bStr);
        _tcscpy(m_tcCmdName, szName);
        return S_OK;
    }
    

This example uses a CComBSTR class object and the COLE2T macro provided by ATL 7.0 to handle string type conversions. Consult the Microsoft documentation for caveats and guidelines to consider if you use these ATL features.