home *** CD-ROM | disk | FTP | other *** search
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Copyright (c) 1998 Microsoft Corporation
-
- Module Name:
-
- hello.cpp
-
- Abstract:
-
- Hello World sample application. Only source file.
-
- This sample creates one form, an EventSink attached to the form
- (to receive WM_COMMAND messages), and a power list box (a menu-like
- object) control placed on the form.
-
- Environment:
-
- AutoPC
-
- -------------------------------------------------------------------*/
- //Standard includes
- #include <windows.h> // Standard Win CE header file
- #include <olectl.h> // COM functions
- #include <asfc.h> // AutoPC Forms Manager
- #include <ascmnctl.h> // AutoPC Common Controls
-
- //Application specific includes
- #include "resource.h"
-
- // Globals
- WCHAR* SZEXIT = L"Exit";
-
- IfmManage* g_pManage = NULL; // the forms manager object
- IASForm* g_pForm = NULL; // the main application form
- IASEventSink* g_pSink = NULL; // the default event sink
- DWORD g_idThread = 0; // the ID of the main application thread
- BSTR g_bstrAppName=0;
- HINSTANCE g_hInst;
-
- // Prototypes
- HRESULT CreateEventSink();
- HRESULT GetFormsManager();
- HRESULT CreateMainForm();
- HRESULT AddControls();
- HRESULT PutCaption(IASControl *pControl, WCHAR *wsz);
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Class:
- CAppEventSink
-
- Description:
- Implements IASEventSink interface (to receive the exit WM_COMMAND)
- -------------------------------------------------------------------*/
- class CAppEventSink : public IASEventSink
- {
- public:
-
- CAppEventSink() { m_cRef = 0; } //Constructor initializes reference count
- ~CAppEventSink(void) {} //Destructor does nothing
-
- // IUnknown implementation
- // Standard COM QueryInterface. We support both IUnknown, IDispatch and IASEventSink
- STDMETHODIMP QueryInterface(REFIID riid, PVOID *ppv)
- {
- if (!ppv) return E_INVALIDARG; //incoming pointer invalid
-
- //Return an interface pointer if we implement 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(); //Add to our reference count
-
- return NOERROR;
- }
-
- // Increment the object's reference count
- STDMETHODIMP_(ULONG) AddRef(void)
- {
- InterlockedIncrement(&m_cRef);
- return m_cRef;
- }
-
- // Decrease reference count, delete this object if necessary
- STDMETHODIMP_(ULONG) Release(void)
- {
- if (InterlockedDecrement(&m_cRef) > 0)
- return m_cRef;
-
- delete this;
- return 0;
- }
-
- // IASEventSink
- // This method is invoked whenever an event is fired
- STDMETHODIMP ReceiveMsg(long uMsg, long wParam, long lParam)
- {
- if(uMsg != WM_COMMAND) return NOERROR; //We only care about WM_COMMAND msgs
-
- switch(HIWORD(lParam))
- {
- case ID_EXIT:
- PostThreadMessage(g_idThread, WM_QUIT, 0, 0); //Send the quit mesage to this app's thread
- break;
-
- default:
- break;
- }
- return NOERROR;
- }
-
- // IDispatch token implementation. All functions return E_NOTIMPL
- STDMETHODIMP GetTypeInfoCount(UINT *pctInfo)
- { return E_NOTIMPL; }
- STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo **pptinfo)
- { return E_NOTIMPL; }
- STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR **rgszNames, UINT cNames, LCID lcid, DISPID *rgdispids)
- { return E_NOTIMPL; }
- STDMETHODIMP Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdispparams,
- VARIANT *pVarResult, EXCEPINFO *pei, UINT *puArgErr)
- { return E_NOTIMPL; }
-
- protected:
- long m_cRef; // this object's ref count
- };
-
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- WinMain
-
- Description:
- Main Win32 Application entry point
-
- Parameters:
- Standard Win32 CE params.
- -------------------------------------------------------------------*/
- int APIENTRY WinMain(HINSTANCE hInst,
- HINSTANCE hPrevInst,
- LPWSTR wszCmd,
- int nCmdShow)
- {
- MSG msg;
- HRESULT hr;
-
- g_idThread = GetCurrentThreadId(); // save the id of the main thread
- g_hInst = hInst; // Save instance handle for LoadString calls
-
- // Initialize COM and the Forms Manager
- CoInitializeEx(NULL, COINIT_MULTITHREADED);
- hr = InitASFormsManager();
- if (FAILED(hr)) goto LReturn;
-
- hr = CreateEventSink(); // create the event sink
- if (FAILED(hr)) goto LReturn;
-
- hr = GetFormsManager(); // get a pointer to the forms manager
- if (FAILED(hr)) goto LReturn;
-
- hr = CreateMainForm(); // create the main application form
- if (FAILED(hr)) goto LReturn;
-
- // Our main message pump. We just dispatch all messages coming
- // into our application
- while (GetMessage(&msg, NULL, 0, 0)) // loop until the application quits
- {
- DispatchMessage(&msg);
- }
- // If GetMessage returns NULL, that means our application received
- // a WM_QUIT or WM_CLOSE message, so we exit the message loop
- // and clean up.
-
- LReturn:
-
- // We HAVE to deregister with the forms manager
- if (g_pManage && g_bstrAppName){
- g_pManage->DeregisterStartedApplication(FC_MAINFACEPLATE, g_bstrAppName);
- }
-
- if (g_pForm){
- g_pForm->Close();
- g_pForm->Release(); // release the form
- }
-
- if (g_pManage){
- g_pManage->Release();
- }
-
- if (g_pSink){ // release event sink
- g_pSink->Release();
- }
-
- if (g_bstrAppName){
- SysFreeString(g_bstrAppName);
- }
-
- UnInitASFormsManager(); // uninitialize the forms manager
- CoUninitialize(); // uninitialize COM
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateMainForm
-
- Description:
- Creates the one and only form and calls AddControls
- -------------------------------------------------------------------*/
- HRESULT CreateMainForm(void)
- {
- HRESULT hr = NOERROR;
-
- // Create the form
- hr = CoCreateInstance(CLSID_ASFORM, NULL, CLSCTX_INPROC_SERVER, IID_ASFORM,
- (PVOID *) &g_pForm);
- if (FAILED(hr)) return hr;
-
- hr = g_pForm->Init(ID_MAINFORM, NULL, g_pSink); // initialize it
- if (FAILED(hr)) goto LReturn;
-
- hr = g_pManage->Start(g_pForm); // start the form
- if (FAILED(hr)) goto LReturn;
-
- PutCaption(g_pForm, g_bstrAppName); // give it a title
-
- hr = AddControls(); // add controls
- if (FAILED(hr)) goto LReturn;
-
- // We have to make the form visible (or we'll get no display)
- hr = g_pForm->put_Visible(TRUE);
-
- LReturn:
- if (FAILED(hr)) // there was an error. Cleanup
- {
- g_pForm->Close(); // close the form
- g_pForm->Release();
- g_pForm = NULL;
- }
-
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateEventSink
-
- Description:
- Creates application event sink. First we create our object of
- type CAppEventSink, which leaves that objects reference count at
- 0. Then we QueryInterface, get an IASEventSink interface pointer,
- and increment the objects reference count.
-
- At application clean-up time, we will Release() our interface,
- which will decrement the object's reference count to 0, and delete
- the object.
- -------------------------------------------------------------------*/
- HRESULT CreateEventSink(void)
- {
- HRESULT hr;
- CAppEventSink *pSink;
-
- pSink = new CAppEventSink(); // create an event sink object
- if (!pSink) return E_OUTOFMEMORY;
-
- // get an interface for us to keep
- hr = pSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &g_pSink);
- if (FAILED(hr)) delete pSink;
-
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- GetFormsManager
-
- Description:
- Creates the forms manager and registers the application as started
- -------------------------------------------------------------------*/
- HRESULT GetFormsManager()
- {
- HRESULT hr;
-
- hr = CoCreateInstance(CLSID_FMMANAGE, NULL, CLSCTX_INPROC_SERVER, IID_FMMANAGE,
- (PVOID *) &g_pManage);
- if (FAILED(hr)) goto LReturn;
-
- WCHAR wszAppName[ _MAX_PATH + 1 ];
-
- //We have to use the name in the resource script with ID #1 (STR_APP_SHELL_NAME)
- LoadString(g_hInst, STR_APP_SHELL_NAME, wszAppName,_MAX_PATH );
-
- g_bstrAppName = ::SysAllocString(wszAppName);
-
- if (g_bstrAppName){
- // Register or application as started with the forms manager, Also, send it
- // Which faceplate to execute on (for future support of multiple screens)
- g_pManage->RegisterStartedApplication(FC_MAINFACEPLATE, g_bstrAppName, 0, 0);
- }
- else {
- hr = E_OUTOFMEMORY;
- }
-
- hr = g_pManage->MoveAppToForeground(GetCurrentProcessId(), 0, 0);
-
- LReturn:
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- AddControls
-
- Description:
- Adds the Clock, Label, and Power List Box control to the main form
- -------------------------------------------------------------------*/
- HRESULT AddControls(void)
- {
- HRESULT hr;
- IASPowerListBox* pPLB = NULL;
- IASPowerListBoxItem* pPLBCExit = NULL;
- BSTR m_bstrExit = NULL;
-
- //Create the powerlistbox control
- hr = CoCreateInstance(
- CLSID_ASPOWERLISTBOX, // GUID of power list box class
- NULL, // Doesn't use COM aggregation
- CLSCTX_INPROC_SERVER, // Create object in our process
- IID_ASPOWERLISTBOX, // Give us the IASPowerListBox interface
- (PVOID *) &pPLB);
-
- if (FAILED(hr))
- goto LReturn;
-
- //Create a command item in the power list box
- hr = pPLB->CreateItem(&pPLBCExit, ID_EXIT, ASFC_PWRLB_TYPE_COMMAND);
- if (FAILED(hr))
- goto LReturn;
-
- //Give the command item a name
- m_bstrExit = SysAllocString(SZEXIT);
- if (!m_bstrExit)
- {
- hr = E_OUTOFMEMORY;
- goto LReturn;
- }
- pPLBCExit->put_Caption(m_bstrExit);
- pPLBCExit->Release();
- SysFreeString(m_bstrExit);
-
- pPLB->SetBounds(0,18,225,45); //Format
- pPLB->put_Alignment(ASFC_ALIGN_CENTER); //And
- g_pForm->Add(pPLB, IDC_PLB); //Add control to form
-
- LReturn:
- if(pPLB) pPLB->Release();
-
- return hr;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- PutCaption
-
- Description:
- Takes the given WCHAR *, converts it to a BSTR and does a
- put_caption on the given control
-
- Parameters:
- IASControl *pControl - Control to add caption too
- WCHAR *wsz - Caption string
- -------------------------------------------------------------------*/
- HRESULT PutCaption(IASControl *pControl, WCHAR *wsz)
- {
- BSTR bstr;
- HRESULT hr;
-
- bstr = ::SysAllocString(wsz);
- if (!bstr)
- return E_OUTOFMEMORY;
-
- // Set the caption
- hr = pControl->put_Caption(bstr);
-
- ::SysFreeString(bstr);
-
- return hr;
- }
-