none
How to write a COM Object that need parameter to be created? RRS feed

  • Question

  • I am learnning ms Word object model and find a question. Most objects in Office namespace have an Application property, whose definition is in Word namespace. How does that property be initialized? I think it may happen when the object is created, but I don't know how.
    Thursday, October 15, 2009 11:29 AM

Answers

  • If you are writing an exe then the Word Application object is created with CoCreateInstance like this:

    CComPtr<Word::_Application> m_pApp;
    HRESULT hr = m_pApp.CoCreateInstance(__uuidof(Word::Application), NULL, CLSCTX_SERVER);

    If  you are writing a COM Addin DLL then the Application object is passed to you in the OnConnection event. Save it.

    STDMETHODIMP CComAddin::OnConnection(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
    {
    // Save Word Application Object
    m_pApp = CComQIPtr <Word::_Application> (Application);

    • Marked as answer by Joseph.Sun Friday, October 16, 2009 8:56 AM
    Thursday, October 15, 2009 1:49 PM