Solved

MFC & Classes created from typelib

Posted on 1999-01-24
5
555 Views
Last Modified: 2013-11-25
I have created an ATL library, that has a method to display a messagebox. I am having a problem with the proper way to create an instance, and call it's display method within MFC. My test app is a MFC APPWIZARD Dialog based. I have added a button, and on it onclick is where I want to create the object, and call it's display method.

I have used the classwizard to add a class from a type library for the object. Here is the class header:

class IMainObject : public COleDispatchDriver
{
public:
      IMainObject() {}            // Calls COleDispatchDriver default constructor
      IMainObject(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
      IMainObject(const IMainObject& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}

// Attributes
public:

// Operations
public:
      void Display();
      LPDISPATCH CreateSeparateObject();
      LPDISPATCH CreateAndInitializeSeparateObject();
};

What would I place in this function to call IMainObjects Display method?

void CMFCClientDlg::OnButton1()
{
}

Second: How would I properly address the function of IMainObject::CreateSeparateObject. This is supposed to return an initialized object. Here is it's header

class ISeparateObject : public COleDispatchDriver
{
public:
      ISeparateObject() {}            // Calls COleDispatchDriver default constructor
      ISeparateObject(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
      ISeparateObject(const ISeparateObject& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}

// Attributes
public:

// Operations
public:
      void Display();
};

What is confusing is that it is returning a type of LPDISPATCH, and I can not properly cast it to a type ISeparateObject.

I thought I could do this:

ISeparateObject *pSep;
pSep = pMain->CreateSeparateObject();

This works when I am not using MFC, but rather straight ATL.

These are examples taken from WROX Press's Beginning ATL COM Programming.

Thanks
0
Comment
Question by:redbaron082997
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:redbaron082997
ID: 1328241
Edited text of question
0
 
LVL 1

Author Comment

by:redbaron082997
ID: 1328242
I have it working with this code, Is this correct? What could be different and/or improved?
void CMFCClientDlg::OnButton1()
{
      IMainObject* pMain = NULL;

      pMain = new IMainObject;
      if ( NULL != pMain ) {
            pMain->CreateDispatch("ReturningAnObject.MainObject");
            pMain->Display();
            pMain->CreateSeparateObject();
            ISeparateObject pSep(pMain->CreateAndInitializeSeparateObject());
            pSep.Display();
            pMain->DetachDispatch();
            pSep.DetachDispatch();
            delete pMain;
      }
}

0
 
LVL 86

Expert Comment

by:jkr
ID: 1328243
That's absolutely correct. The constructor of ISeparateObject uses the base class constructor which calls COleDispatchDriver::AttachDispatch() internally...
(I submit this as a comment, as you solved your problem. If you want me to answe and/or need more information, feel free to ask...)
0
 
LVL 1

Author Comment

by:redbaron082997
ID: 1328244
Here is the problem. Then function CreateAndInitializeSeparateObject()  creates the memory space, and needs to be deleted. How is it deleted?  In ATL, I used something like this
ISeparateObject *pSep;
pSep = pMain->CreateAndIn...();
pSep->Display();
pSep->Release();

Second (You can submit this as the answer)
Why would one use this approach vs #import "file.dll" or #import "File.tlb"?

Thanks
Ronnie


0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1328245
It simply depends on your implementation of the object. If the last call to 'Release()' also frees the object (e.g. 'if ( !m_cRef) delete this;', there should be no problem. But the MFC implementation of IDispatch usualy takes care of cleaning up, anyway....

And, the only reason to use this approach is simply MFC, so to say, the benefits of being able to use the application framework - i agree to you, it's kinda clumsy... but it fits into the framework ;-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question