Click here to Skip to main content
Click here to Skip to main content

A COM class (DLL) for embedding an embeddable OLE object

, 20 Jul 2006
Rate this:
Please Sign up or sign in to vote.
A COM class (DLL) that embeds an embeddable OLE object.

Introduction

Using MFC, we can easily develop an ActiveX container application, but unfortunately, it only works with the doc/view architecture as an executing file. If you want to embed an OLE embeddable object but your program is not an executable program and it has to be a DLL (for example, for many kinds of plug-ins, they work as DLL files), then this article will give you help.

Using the code

It's very simple to use. This COM object has only three methods, Create, Open, and OnSize.

  1. You must declare the interface, and add the include files where it works (note: this COM object will work as a child window).

    In the tester, I added it to CTesterDlg as an element.

    #include "..\emboleobjctrl\emboleobjctrl_i.h"
    
    IEmbOleObjControl * m_pCtrl;
  2. Initialize the object's pointer to NULL:
    CtesterDlg::CtesterDlg(CWnd* pParent /*=NULL*/)
        :CDialog(CtesterDlg::IDD, pParent)
    
    {
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
        m_pCtrl = NULL;
    }
  3. Create the instance, and create the window:
    void CtesterDlg::ObBnClickedCreate()
    {
      if(m_pCtrl)   //if the object exists then quit
         return;
      HRESULT hr = ::CoCreateInstance(CLSID_EmbOleObjControl, 
                   NULL,CLSCTX_ALL,IID_IEmbOleObjControl, 
                   (void **)&m_pCtrl);
      if(SUCCEEDED(hr))
      {
          HWND hWnd;
          GetDlgItem(IDC_STATICOWNER,&hWnd);
          m_pCtrl->Create(hWnd);
          //create needs a parameter as the control's container window,
      }
    }
  4. Open the specific file and the specific OLE server:
    void CtesterDlg::OnBnClickedBtnopen()
    {
        if(!m_pCtrl)
        {
            AfxMessageBox("please create the control first");
            return;
        }
        CString strInfo;
        CWnd *pWnd = GetDlgItem(IDC_EDTPATH);
        pWnd->GetWindowText(strInfo);
        _bstr_t bstrInfo(strInfo.GetBuffer());
        static CLSID const clsid_App ={ 0x7b93e267, 0x6bbc, 0x11d4, 
               { 0xa5, 0x4d, 0x0, 0x50, 0xba, 0xdb, 0x14, 0xa3 } };
        m_pCtrl->Open(clsid_App,bstrInfo);
           //clsid_app : the ole server's clsid
           //bstrInfo: the file's path
    }
  5. When the container window's size changes, call IFoxitPDFControl::OnSize() (for the current case, the container is not sizeable).
  6. Before the container window is destroyed, destroy (release) it.
    void CtesterDlg::OnCancel()
    {
         if(m_pCtrl)
            m_pCtrl->Release();
         CDialog::OnCancel();
    }

Points of interest

Wrapping the doc/view architecture into a DLL module is not easy since the mainframe window in that architecture works as an overlapped window, it doesn't work as a child window; but here it must work as a child window (strictly, it works as a popup window here). In order to adjust the window's position, there is an additional method exported (OnMove). Even now, there is a problem annoying me, that this COM object can't work with MS-Word correctly. Hope somebody here can correct it to work with any OLE server without problems.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

William.Wang
Web Developer
China China
My name is Wangwanxin, living in beijing,China.since spe,2002 till now,working as a windows software developer,be faimliar with c++/mfc,atl. devotional,faithful,... I'll devote more time here, I'd like to exchange my limited ideas here,open my heart, and then have the whole world. Smile | :)

Comments and Discussions

 
GeneralThis is very cool. Really really cool Pinmemberhujunxi20-Oct-09 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 20 Jul 2006
Article Copyright 2005 by William.Wang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid