redbaron082997
asked on
MFC & Classes created from typelib
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(pDispat ch) {}
IMainObject(const IMainObject& dispatchSrc) : COleDispatchDriver(dispatc hSrc) {}
// Attributes
public:
// Operations
public:
void Display();
LPDISPATCH CreateSeparateObject();
LPDISPATCH CreateAndInitializeSeparat eObject();
};
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::CreateSeparat eObject. 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(pDispat ch) {}
ISeparateObject(const ISeparateObject& dispatchSrc) : COleDispatchDriver(dispatc hSrc) {}
// 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->CreateSeparateObjec t();
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
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(pDispat
IMainObject(const IMainObject& dispatchSrc) : COleDispatchDriver(dispatc
// Attributes
public:
// Operations
public:
void Display();
LPDISPATCH CreateSeparateObject();
LPDISPATCH CreateAndInitializeSeparat
};
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::CreateSeparat
class ISeparateObject : public COleDispatchDriver
{
public:
ISeparateObject() {} // Calls COleDispatchDriver default constructor
ISeparateObject(LPDISPATCH
ISeparateObject(const ISeparateObject& dispatchSrc) : COleDispatchDriver(dispatc
// 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->CreateSeparateObjec
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
ASKER
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("Ret urningAnOb ject.MainO bject");
pMain->Display();
pMain->CreateSeparateObjec t();
ISeparateObject pSep(pMain->CreateAndIniti alizeSepar ateObject( ));
pSep.Display();
pMain->DetachDispatch();
pSep.DetachDispatch();
delete pMain;
}
}
void CMFCClientDlg::OnButton1()
{
IMainObject* pMain = NULL;
pMain = new IMainObject;
if ( NULL != pMain ) {
pMain->CreateDispatch("Ret
pMain->Display();
pMain->CreateSeparateObjec
ISeparateObject pSep(pMain->CreateAndIniti
pSep.Display();
pMain->DetachDispatch();
pSep.DetachDispatch();
delete pMain;
}
}
That's absolutely correct. The constructor of ISeparateObject uses the base class constructor which calls COleDispatchDriver::Attach Dispatch() 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...)
(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...)
ASKER
Here is the problem. Then function CreateAndInitializeSeparat eObject() 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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER