Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.45/5 (7 votes)
20 Jul 20061 min read 58.8K   1.4K   23   12
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


Written By
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 Pin
hujunxi20-Oct-09 20:14
hujunxi20-Oct-09 20:14 
GeneralNow, the frame window works as a child window. Pin
William.Wang14-Jan-07 23:24
William.Wang14-Jan-07 23:24 
Questioncan not run Pin
gotopt23-Jul-06 17:05
gotopt23-Jul-06 17:05 
AnswerRe: can not run Pin
William.Wang24-Jul-06 0:47
William.Wang24-Jul-06 0:47 
AnswerRe: can not run Pin
William.Wang24-Jul-06 1:00
William.Wang24-Jul-06 1:00 
GeneralRe: can not run Pin
gotopt9-Aug-06 20:50
gotopt9-Aug-06 20:50 
GeneralRe: can not run Pin
gotopt9-Aug-06 22:59
gotopt9-Aug-06 22:59 
GeneralRe: can not run Pin
William.Wang20-Aug-06 16:46
William.Wang20-Aug-06 16:46 
GeneralQuestion Pin
Ahmed Charfeddine10-Jul-06 21:30
Ahmed Charfeddine10-Jul-06 21:30 
I have many resembling ActiveX controls. i need to develop run-time, exportable classes ( for evry control ) such they provide a uniform interface but then relay the application calls to the specific ActiveX operations. ( a sort of encapsulation to allow the independence between those ActiveXs and the application such plugin components and a main one)
So is this a way how do that ?
( i do'nt know much about COM )
Thank you in advance.
charfeddine_ahmed@yahoo.fr
GeneralRe: Question Pin
William.Wang12-Jul-06 23:01
William.Wang12-Jul-06 23:01 
GeneralRe: Question Pin
Ahmed Charfeddine12-Jul-06 23:53
Ahmed Charfeddine12-Jul-06 23:53 
GeneralRe: Question Pin
William.Wang13-Jul-06 15:40
William.Wang13-Jul-06 15:40 

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

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