home *** CD-ROM | disk | FTP | other *** search
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Copyright (c) 1998 Microsoft Corporation
-
- Module Name:
-
- names.cpp
-
- Abstract:
-
- What's in a Name Sample application. Implementation of sink and main app
- class.
-
- Environment:
-
- AutoPC
-
- -------------------------------------------------------------------*/
-
- #include <windows.h>
- #include <olectl.h>
- #include <asfc.h> // Apollo Forms Manager
- #include <ascmnctl.h> // Apollo Common Controls
- #include <keypad.h> // VK definitions for faceplate keys
- #include <abapi.h> // Address Book api functions
-
- #define APCDBG_INIT "What's in a name?" // Text used in debug messages
- #include <apcdebug.h> // Standard APC debug header
-
- #include "names.h" // App and Event sink class definitions
- #include "resource.h" // resource IDs
-
- #include "namemeanings.h" // String array of names and their meanings
-
- //--------------------------------------------------------------------------------------------
- // Globals
- //--------------------------------------------------------------------------------------------
- CNamesApp* g_pApp; // Main application class
-
- WCHAR* SZEXIT = L"Exit";
- WCHAR* SZSELECTNAME = L"Select Contact";
-
- HINSTANCE g_hInst = NULL; // application instance handle
- DWORD g_idThread = 0; // the ID of the main application thread
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Class:
- CAppEventSink
-
- Description:
- Implements IASEventSink interface (to receive the exit WM_COMMAND)
- and IUnknown (because its a COM object)
- -------------------------------------------------------------------*/
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Functions:
- QueryInterface, AddRef, Release
-
- Description:
- COM requires the next three functions to be implemented.
-
- Parameters:
- Standard COM object params
- -------------------------------------------------------------------*/
- // Request a specific interface
- STDMETHODIMP
- CAppEventSink::QueryInterface(REFIID riid, PVOID *ppv)
- {
- if (!ppv) return E_INVALIDARG;
-
- // Return a pointer to the requested interface
- if (riid == IID_ASEVENTSINK) *ppv = (PVOID) (IASEventSink *) this;
- else if (riid == IID_IUnknown) *ppv = (PVOID) (IUnknown *) this;
- else if (riid == IID_IDispatch) *ppv = (PVOID) (IDispatch *) this;
- else return E_NOINTERFACE;
-
- AddRef();
- return NOERROR;
- }
-
- // Increment this objects reference count
- STDMETHODIMP_(ULONG)
- CAppEventSink::AddRef()
- {
- InterlockedIncrement(&m_cRef);
- return m_cRef;
- }
-
- // Decrement the objects reference count. Delete if no longer needed
- STDMETHODIMP_(ULONG)
- CAppEventSink::Release()
- {
- if (InterlockedDecrement(&m_cRef) > 0) return m_cRef;
-
- delete this;
- return 0;
- }
-
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- ReceiveMsg
-
- Description:
- Applications event sink.
-
- Parameters:
- Standard Win32 Message params.
- -------------------------------------------------------------------*/
- STDMETHODIMP
- CAppEventSink::ReceiveMsg(long uMsg, long wParam, long lParam)
- {
- if (WM_COMMAND != uMsg)
- return S_FALSE;
-
- // WM_COMMAND message parameters for a menu/PLB:
- // wParamLO - ID of control message sent to
- // wParamHI - Action taken on control
- // LBN_DBLCLK - option selected
- // PLBN_SPINCHANGE - A spinner change
- // PLBN_BEGINEDIT - started to edit an edit item
- // PLBN_ENDEDIT - finished editing an edit item
- // lParamLO - Index of item (selected or spun to)
- // lParamHI - Item ID acted on
- //
- // Since there is only one menu control in this application, we won't
- // check wParamLO
- if(HIWORD(wParam) == LBN_DBLCLK)
- {
- switch (HIWORD(lParam))
- {
- case ID_EXIT:
- PostThreadMessage(g_idThread, WM_QUIT, 0, 0);
- break;
-
- case ID_ADDRESS:
- if(SUCCEEDED(g_pApp->PickName()))
- g_pApp->ShowMeaning();
- break;
- }
- }
- return S_FALSE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Class:
- CNamesApp
-
- Description:
- Main application specific class. Doesn't implement COM or any
- other interfaces.
- -------------------------------------------------------------------*/
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CNamesApp
-
- Description:
- Application object constructor.
- -------------------------------------------------------------------*/
- CNamesApp::CNamesApp()
- {
- m_pManage = NULL;
- m_pForm = NULL;
- m_pSink = NULL;
- m_pPLBMenu = NULL;
- m_oh = NULL;
- m_bstrAppName = NULL;
- m_bstrExit = NULL;
- m_bstrSelectNameText= NULL;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- ~CNamesApp
-
- Description:
- Destructor. Does nothing, all cleanup handled in CleanUp function
- -------------------------------------------------------------------*/
- CNamesApp::~CNamesApp()
- {
-
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- PickName
-
- Description:
- Opens address book, allows user to select a contact, then saves
- the First and Last name from the contacts database.
-
- Parameters:
- Returns HRESULT code
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::PickName()
- {
- HRESULT hr;
- WCHAR *wszName = NULL;
-
- hr = abOpen(m_pManage);
-
- if(SUCCEEDED(hr))
- {
- // Fill out ABDialogInfo structure. Controls what features and views
- // are enabled/disabled on address book popup dialog.
- // Parameter description found in abapi.h
- ABDIALOGINFO abDI = {
- 0, // contact ID - read/write
- ABE_EXITMAIN, // feature enable mask - combination of ABE_ constants - read
- 0, // depends on wInitView - should be zero if unused
- ABV_MAINVIEW, // initial/final view - one of ABV_ constants - read/write
- 1, // field ordinal - read/write
- ABV_CARDHOME }; // initial card
-
- // Bring up the Address Book dialog box
- hr = abDialog(&abDI);
-
- if (hr == S_OK)
- {
- CEPROPID rgidProps[] = { HHPR_GIVEN_NAME, HHPR_SURNAME }; // List of properties we want
- PCEPROPVAL prgProps = 0;
- WORD cProps = sizeof(rgidProps)/sizeof(CEPROPID); // Number of properties
-
- // Try to get properties of the last record the user was looking at
- // Make sure neither the first (given) or last name are NULL
- if(SUCCEEDED(abRead(abDI.ceoid, &cProps, rgidProps, &prgProps)))
- {
- if(prgProps->val.lpwstr)
- wcscpy(m_bstrName, prgProps->val.lpwstr); // Save first name field
-
- if((prgProps+1)->val.lpwstr)
- wcscpy(m_bstrLastName, prgProps->val.lpwstr); // Save last name field
-
- LocalFree(prgProps);
- }
- else hr = E_FAIL;
- }
- else hr = E_FAIL;
- }
-
- abClose();
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- Init
-
- Description:
- Application initialization. Allocates storage for all strings,
- Creates event sink, forms manager, main form, and adds controls
- to it.
-
- Parameters:
- Returns HRESULT code.
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::Init()
- {
- // Load app string name from resource script
- WCHAR wszAppName[40];
- LoadString(g_hInst, STR_APP_SHELL_NAME, wszAppName, 39);
- m_bstrAppName = SysAllocString(wszAppName);
- if(!m_bstrAppName) return E_FAIL;
-
- m_bstrSelectNameText = SysAllocString(SZSELECTNAME);
- if(!m_bstrSelectNameText) return E_FAIL;
-
- m_bstrExit = SysAllocString(SZEXIT);
- if(!m_bstrExit) return E_FAIL;
-
- m_bstrName = SysAllocStringLen(NULL, 100);
- if(!m_bstrName) return E_FAIL;
-
- m_bstrLastName = SysAllocStringLen(NULL, 100);
- if(!m_bstrLastName) return E_FAIL;
-
- if(FAILED(CreateEventSink())) return E_FAIL;
- if(FAILED(GetFormsManager())) return E_FAIL;
- if(FAILED(CreateMainForm())) return E_FAIL;
- if(FAILED(AddControls())) return E_FAIL;
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CleanUp
-
- Description:
- Frees all strings, Releases all pointers.
- -------------------------------------------------------------------*/
- void
- CNamesApp::CleanUp()
- {
- m_pManage->DeregisterStartedApplication(m_oh, m_bstrAppName);
-
- if(m_bstrSelectNameText) SysFreeString(m_bstrSelectNameText);
- if(m_bstrExit) SysFreeString(m_bstrExit);
- if(m_bstrName) SysFreeString(m_bstrName);
- if(m_bstrLastName) SysFreeString(m_bstrLastName);
-
- if(m_pPLBMenu) {
- m_pPLBMenu->RemoveAll();
- m_pPLBMenu->Release();
- }
-
- // Must close the form before we release
- if(m_pForm) {
- m_pForm->Close();
- m_pForm->Release();
- }
-
- if(m_pSink) m_pSink->Release();
- if(m_pManage) m_pManage->Release();
- if(m_bstrAppName) SysFreeString(m_bstrAppName);
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateMainForm
-
- Description:
- Creates the main form and attatches the event sink to it.
-
- Parameters:
- Returns HRESULT return code.
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::CreateMainForm()
- {
- HRESULT hr;
-
- // Create the form
- hr = CoCreateInstance(
- CLSID_ASFORM, // Class ID of form object
- NULL, // Not part of COM aggregation
- CLSCTX_INPROC_SERVER, // Create object in our process space
- IID_ASFORM, // Request the IASForm interface
- (PVOID *) &m_pForm); // Target to receive interface
- if (FAILED(hr)) return hr;
-
- // Have to initialize and start our form
- hr = m_pForm->Init(ID_MAINFORM, NULL, m_pSink);
- if (FAILED(hr)) return hr;
-
- // Must make form visible
- m_pForm->put_Visible(TRUE);
-
- hr = m_pManage->Start(m_pForm);
- if (FAILED(hr)) return hr;
-
- m_pForm->put_Caption(m_bstrAppName); // Add the caption to title bar
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateEventSink
-
- Description:
- Creates the Application's event sink.
-
- Parameters:
- Returns HRESULT return code
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::CreateEventSink()
- {
- HRESULT hr;
- CAppEventSink * pSink;
- pSink = new CAppEventSink;
-
- // get an interface for us to keep
- hr = pSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pSink);
-
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- GetFormsManager
-
- Description:
- Creates forms manager interface, registers the application, and
- Moves the app to foreground (in case it wasn't started form shell)
-
- Parameters:
- Returns HRESULT return code
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::GetFormsManager()
- {
- HRESULT hr;
-
- hr = CoCreateInstance(
- CLSID_FMMANAGE, // Class ID of form's manager object
- NULL, // No COM aggregation
- CLSCTX_INPROC_SERVER, // Create object in our process space
- IID_FMMANAGE, // Request IfmManage interface
- (PVOID *) &m_pManage); // Target to receive interface
- if(FAILED(hr)) return hr;
-
- // Get the default faceplate handle (form context handle)
- hr = m_pManage->GetFormsContextHandle(&m_oh);
- if(FAILED(hr)) return hr;
-
- // Register our application on the default faceplate with the
- // form's manager
- hr = m_pManage->RegisterStartedApplication(m_oh, m_bstrAppName, 0, 0);
- if(FAILED(hr)) return hr;
-
- // Bring our application to the foreground, in case app wasn't started
- // from the AutoPc shell (ppsh or visual C ide)
- hr = m_pManage->MoveAppToForeground(GetCurrentProcessId(), 0, 0);
- if(FAILED(hr)) return hr;
-
- hr = m_pManage->put_EventSink(m_pSink);
- if(FAILED(hr)) return E_FAIL;
-
- return S_OK;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- AddControls
-
- Description:
- Adds controls to the main form.
-
- Parameters:
- Returns HRESULT return code
- -------------------------------------------------------------------*/
- HRESULT
- CNamesApp::AddControls()
- {
- HRESULT hr;
-
- // Create a Power List Box
- hr = CoCreateInstance(
- CLSID_ASPOWERLISTBOX, // Class ID of the power list box
- NULL, // No COM aggregation
- CLSCTX_INPROC_SERVER, // Create object in our process space
- IID_ASPOWERLISTBOX, // Request the IASPowerListBox interface
- (PVOID *) &m_pPLBMenu); // Target for requested interface
- if (FAILED(hr)) return hr;
-
- // Set the dimensions of our power list box
- m_pPLBMenu->SetBounds(0, 18, 220, 46);
-
- // Add the power list box control to our form
- hr = m_pForm->Add(m_pPLBMenu, ID_PLBMENU);
- if (FAILED(hr)) return hr;
-
- IASPowerListBoxItem* pItem;
-
- hr = m_pPLBMenu->CreateItem(&pItem,ID_ADDRESS, ASFC_PWRLB_TYPE_COMMAND);
- if (FAILED(hr)) return hr;
- pItem->put_Caption(m_bstrSelectNameText);
- pItem->Release();
-
- hr = m_pPLBMenu->CreateItem(&pItem,ID_EXIT, ASFC_PWRLB_TYPE_COMMAND);
- if (FAILED(hr)) return hr;
- pItem->put_Caption(m_bstrExit);
- pItem->Release();
-
- return S_OK;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- ShowMeaning
-
- Description:
- Shows the meaning of whatever name is in the bstrName and bstrLastName
- variables. Pops up a message box to display it.
- -------------------------------------------------------------------*/
- void CNamesApp::ShowMeaning()
- {
- BSTR bstrMsg = SysAllocStringLen(NULL, 100);
- BSTR bstrTitle = SysAllocStringLen(NULL, 100);
-
- int i=0;
-
- wsprintf(bstrMsg, L"There is no meaning in the database.");
-
- // Go through all the names in the "database" (big array) and
- // try and find a match
- while(wszaNames[i] != NULL)
- {
- if(!wcscmp(wszaNames[i], m_bstrName))
- {
- wsprintf(bstrMsg, L"%s", wszaMeanings[i]);
- break;
- }
- i++;
- }
-
- // Format a message
- wsprintf(bstrTitle, L"%s %s's Name Means:", m_bstrName, m_bstrLastName);
-
- long lRet = 0;
-
- // bring up a message box
- HRESULT hr = m_pManage->fmMessageBox(
- bstrMsg, // Message to be displayed
- bstrTitle, // Title of message box
- MB_OK, // Only have OK button on MB
- FMMB_FLG_TTS, // TTS out the message
- &lRet); // Return
-
- SysFreeString(bstrMsg);
- SysFreeString(bstrTitle);
- }
-
-
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- WinMain
-
- Description:
- Main Win32 Application entry point
-
- Parameters:
- Standard Win32 CE
- -------------------------------------------------------------------*/
- int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR wszCmd, int nCmdShow)
- {
- MSG msg;
-
- g_hInst = hInst; //save hInstance
- g_idThread = GetCurrentThreadId(); //save the id of the main thread
- g_pApp = new CNamesApp; //Allocate our new App
-
- PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
-
- if(!g_pApp)
- return FALSE;
-
- CoInitializeEx( NULL, COINIT_MULTITHREADED );
-
- HRESULT hr = InitASFormsManager(); // Initialize the forms manager
- if (FAILED(hr))
- {
- CoUninitialize();
- return FALSE;
- }
-
- if(SUCCEEDED(g_pApp->Init()))
- {
- while (GetMessage(&msg, NULL, 0, 0)) // loop until the application quits
- {
- DispatchMessage(&msg);
- }
- }
-
- g_pApp->CleanUp();
- delete g_pApp;
-
- UnInitASFormsManager(); // uninitialize the forms manager
- CoUninitialize(); // uninitialize COM
-
- return TRUE;
- }
-
-
-