Windows NT Systems Programming: Spring 2000

[ Home | Syllabus | Course Notes | Assignments | Search]


VB Automation (chapter 25 ex25b)


Dialog Box


ODL

// ex25b.odl : type library source for ex25b.dll

// This file will be processed by the MIDL compiler to produce the
// type library (ex25b.tlb).

[ uuid(A9515ACA-5B85-11D0-848F-00400526305B), version(1.0) ]
library Ex25b
{
	// library name for Excel's object borrower
	importlib("stdole32.tlb");
	
	// primary dispatch interface for CEx25bAuto
	
	[ uuid(A9515AD7-5B85-11D0-848F-00400526305B) ]
	
	// GUID from component's interface map--matches Registry Interface
	//  entry

	dispinterface IEx25bAuto
	{
		// name used in VBA Dim statement and Object list
		properties:
			[id(1)] long LongData;
			[id(2)] VARIANT TextData;
						
		methods:
			[id(3)] boolean DisplayDialog();
	};

	//  component's clsid

	[ uuid(A9515AD8-5B85-11D0-848F-00400526305B) ]
	coclass Ex25bAuto
	{
		[default] dispinterface IEx25bAuto;
	};

};

COM implementation

IMPLEMENT_DYNCREATE(CEx25bAuto, CCmdTarget)

CEx25bAuto::CEx25bAuto()
{
	EnableAutomation();
    ::VariantInit(&m_vaTextData); // necessary initialization
    m_lData = 0;
}

BEGIN_DISPATCH_MAP(CEx25bAuto, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CEx25bAuto)
	DISP_PROPERTY_NOTIFY(CEx25bAuto, "LongData", m_lData, OnLongDataChanged, VT_I4)
	DISP_PROPERTY_NOTIFY(CEx25bAuto, "TextData", m_vaTextData, OnTextDataChanged, VT_VARIANT)
	DISP_FUNCTION(CEx25bAuto, "DisplayDialog", DisplayDialog, VT_BOOL, VTS_NONE)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// CEx25bAuto message handlers

void CEx25bAuto::OnLongDataChanged() 
{
    TRACE("CEx25bAuto::OnLongDataChanged\n");
}

void CEx25bAuto::OnTextDataChanged() 
{
    TRACE("CEx25bAuto::OnTextDataChanged\n");
}

BOOL CEx25bAuto::DisplayDialog() 
{
    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){
        dlg.m_strData = m_vaTextData.bstrVal; // converts double-byte(UNICODE)
                                              //  character to
		                                      //  single-byte 
		                                      //  character                                                                                                                      
      }
      dlg.m_lData = m_lData; // data exchange will move into dialog CEdit controls
      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;
}

Registering DLL

As before, can load DLL and call its DllRegisterServer function.


Visual Basic Programs

Dim Dllcomp As Object
Private Declare Sub CoFreeUnusedLibraries Lib "OLE32" ()

Sub LoadDllComp()
    Set Dllcomp = CreateObject("Ex25b.Auto")
    Range("C3").Select
    Dllcomp.LongData = Selection.Value
    Range("D3").Select
    Dllcomp.TextData = Selection.Value
End Sub

Sub RefreshDllComp() 'Gather Data button
    Range("C3").Select
    Dllcomp.LongData = Selection.Value
    Range("D3").Select
    Dllcomp.TextData = Selection.Value
    Dllcomp.DisplayDialog
    Range("C3").Select
    Selection.Value = Dllcomp.LongData
    Range("D3").Select
    Selection.Value = Dllcomp.TextData
End Sub
    
Sub UnloadDllComp()
    Set Dllcomp = Nothing
    Call CoFreeUnusedLibraries
End Sub



Copyright chris wild 1999/2000.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: March 23, 2000.