// Mymfc29BAuto.cpp : implementation file
//

#include "stdafx.h"
#include "mymfc29B.h"
#include "Mymfc29BAuto.h"
#include "PromptDl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMymfc29BAuto

IMPLEMENT_DYNCREATE(CMymfc29BAuto, CCmdTarget)

CMymfc29BAuto::CMymfc29BAuto()
{
	EnableAutomation();
	
	// To keep the application running as long as an OLE automation 
	//	object is active, the constructor calls AfxOleLockApp.
	::VariantInit(&m_vaTextData); // necessary initialization
    m_lData = 0;

	AfxOleLockApp();
}

CMymfc29BAuto::~CMymfc29BAuto()
{
	// To terminate the application when all objects created with
	// 	with OLE automation, the destructor calls AfxOleUnlockApp.
	
	AfxOleUnlockApp();
}


void CMymfc29BAuto::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(CMymfc29BAuto, CCmdTarget)
	//{{AFX_MSG_MAP(CMymfc29BAuto)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CMymfc29BAuto, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CMymfc29BAuto)
	DISP_PROPERTY_NOTIFY(CMymfc29BAuto, "LongData", m_lData, OnLongDataChanged, VT_I4)
	DISP_PROPERTY_NOTIFY(CMymfc29BAuto, "TextData", m_vaTextData, OnTextDataChanged, VT_VARIANT)
	DISP_FUNCTION(CMymfc29BAuto, "DisplayDialog", DisplayDialog, VT_BOOL, VTS_NONE)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IMymfc29BAuto to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {7A97BA38-BF4A-4586-93C6-72B5EE7E0DC2}
static const IID IID_IMymfc29BAuto =
{ 0x7a97ba38, 0xbf4a, 0x4586, { 0x93, 0xc6, 0x72, 0xb5, 0xee, 0x7e, 0xd, 0xc2 } };

BEGIN_INTERFACE_MAP(CMymfc29BAuto, CCmdTarget)
	INTERFACE_PART(CMymfc29BAuto, IID_IMymfc29BAuto, Dispatch)
END_INTERFACE_MAP()

// {39D9E31F-25CB-4511-B5A8-5406E29BA565}
IMPLEMENT_OLECREATE(CMymfc29BAuto, "mymfc29B.Auto", 0x39d9e31f, 0x25cb, 0x4511, 0xb5, 0xa8, 0x54, 0x6, 0xe2, 0x9b, 0xa5, 0x65)

/////////////////////////////////////////////////////////////////////////////
// CMymfc29BAuto message handlers

void CMymfc29BAuto::OnLongDataChanged() 
{
	// TODO: Add notification handler code
	TRACE("CMymfc29BAuto::OnLongDataChanged\n");
}

void CMymfc29BAuto::OnTextDataChanged() 
{
	// TODO: Add notification handler code
	TRACE("CMymfc29BAuto::OnTextDataChanged\n");
}

BOOL CMymfc29BAuto::DisplayDialog() 
{
	// TODO: Add your dispatch handler code here
	TRACE("Entering CMymfc29BAuto::DisplayDialog %p\n", this);
    BOOL bRet = TRUE;
    AfxLockTempMaps();  // See MFC Tech Note #3
    CWnd* pTopWnd = CWnd::FromHandle(::GetTopWindow(NULL));
    try
	{
        CPromptDlg dlg /*(pTopWnd)*/;
        if (m_vaTextData.vt == VT_BSTR)
		{
			// converts double-byte character to single-byte
            //  character
            dlg.m_strData = m_vaTextData.bstrVal; 
        }
        dlg.m_lData = m_lData;

        if (dlg.DoModal() == IDOK)
		{
            
			m_vaTextData = COleVariant(dlg.m_strData).Detach();
            m_lData = dlg.m_lData;
            bRet = TRUE;
        }
        else
		{
            bRet =  FALSE;
        }
    }
    catch (CException* pe)
	{
        TRACE("Exception: failure to display dialog\n");
        bRet =  FALSE;
        pe->Delete();
    }
    AfxUnlockTempMaps();
    return bRet;
}

