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

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

By , 20 Jul 2006
 

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

About the Author

William.Wang
Web Developer
China China
Member
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 | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThis is very cool. Really really coolmemberhujunxi20 Oct '09 - 20:14 
GeneralNow, the frame window works as a child window.memberWilliam.Wang14 Jan '07 - 23:24 
Questioncan not runmembergotopt23 Jul '06 - 17:05 
AnswerRe: can not runmemberWilliam.Wang24 Jul '06 - 0:47 
hi, thanks for question.
please confirm, if IEmbOleObjControl::Open(REFCLSID clsidServer,BSTR bstrPath), the parameters r dilivered correctly, assume that ur first parameter is not right, for that case, Ole object couldn't be created sucessfully, make sure your OLE SERVER does exist in your local machine, and it's clsid is copyed correctly. furthermore my code is not so strong or that will give u friendly information instead of asserting crash!
I will update it later.
 
life is like a box of chocolate,you never know what you r going to get.

AnswerRe: can not runmemberWilliam.Wang24 Jul '06 - 1:00 
GeneralRe: can not runmembergotopt9 Aug '06 - 20:50 
GeneralRe: can not runmembergotopt9 Aug '06 - 22:59 
GeneralRe: can not runmemberWilliam.Wang20 Aug '06 - 16:46 
GeneralQuestionmembercharfeddine_ahmed10 Jul '06 - 21:30 
GeneralRe: QuestionmemberWilliam.Wang12 Jul '06 - 23:01 
GeneralRe: Questionmembercharfeddine_ahmed12 Jul '06 - 23:53 
GeneralRe: QuestionmemberWilliam.Wang13 Jul '06 - 15:40 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Jul 2006
Article Copyright 2005 by William.Wang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid