home *** CD-ROM | disk | FTP | other *** search
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Copyright (c) 1998 Microsoft Corporation
-
- Module Name:
-
- sysapp.cpp
-
- Abstract:
-
- System Information AutoPC Sample control panel applet.
- This file implements the applet's main application class. It
- contains all the code to create the forms, forms manager,
- and sinks.
-
- Environment:
-
- AutoPC
-
- -------------------------------------------------------------------*/
- // AutoPC includes
- #include <Windows.h>
- #include <asfc.h>
- #include <ascmnctl.h>
- #include <keypad.h>
-
- #include <apcdebug.h>
-
- // Application specific includes
- #include "resource.h"
- #include "SysInfo.h"
-
- // Macro to release an interface pointer and set it to zero
- #define RELEASE_IF(i)\
- if(i){\
- i->Release();\
- i=NULL; }
-
- #define ONE_KB 1024
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Main Application Class
-
- -------------------------------------------------------------------*/
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::CSysInfoApp
-
- Description:
- Constructor. Initializes necessary variables.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- CSysInfoApp::CSysInfoApp(HINSTANCE hInst)
- {
- // Sinks
- m_pAppEventSink = NULL;
- m_pAppMessageSink = NULL;
-
- m_pForm = NULL;
- m_pManage = NULL;
-
- m_bstrAppName = NULL;
-
- m_hInst = hInst;
-
- m_dwThreadId = GetCurrentThreadId();
-
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::~CSysInfoApp
-
- Description:
- Destructor. Releases all the interface pointers and strings
- we use in the application.
-
- Returns:
- Nothing.
- -------------------------------------------------------------------*/
- CSysInfoApp::~CSysInfoApp()
- {
- // Close the main form
- if(m_pForm) m_pForm->Close();
-
- // Release interface pointers (uses macro defined at top of file)
- RELEASE_IF(m_pForm)
- RELEASE_IF(m_pAppEventSink)
- RELEASE_IF(m_pAppMessageSink)
- RELEASE_IF(m_pManage)
-
- // Free strings we use
- if(m_bstrAppName)
- {
- SysFreeString(m_bstrAppName);
- m_bstrAppName = NULL;
- }
-
- UnInitASFormsManager(); // uninitialize the forms manager
- CoUninitialize(); // uninitialize COM
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::Init
-
- Description:
- Called after main application class is allocated. Creates the
- event sinks, forms manager, forms, and menus.
-
- Parameters:
- None.
-
- Returns:
- HRESULT - Result code.
- -------------------------------------------------------------------*/
- BOOL
- CSysInfoApp::Init()
- {
- HRESULT hr;
-
- // Allocate a BSTR with the appName
- if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bstrAppName, m_hInst))
- {
- DEBUGCHK(0);
- return FALSE;
- }
-
- CoInitializeEx( NULL, COINIT_MULTITHREADED ); // Initialize COM
- hr = InitASFormsManager(); // Initialize the forms manager
- if(FAILED(hr))
- {
- CoUninitialize();
- return FALSE;
- }
-
- hr = CreateSink();
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return FALSE;
- }
-
- hr = CreateFormManager();
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return FALSE;
- }
-
- hr = CreateForm();
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return FALSE;
- }
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::CreateSink
-
- Description:
- Creates the main application event sink. Allocates a CSysInfoSink
- object and requests the IASEventSink and IASClassMsgSink interfaces \
- from it.
-
- Parameters:
- None.
-
- Returns:
- HRESULT - Result code.
- -------------------------------------------------------------------*/
- HRESULT CSysInfoApp::CreateSink()
- {
- HRESULT hr;
- CSysInfoSink* pSinks;
-
- pSinks = new CSysInfoSink();
-
- hr = pSinks->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pAppEventSink);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- hr = pSinks->QueryInterface(IID_ASCLASSMSGSINK, (PVOID *) &m_pAppMessageSink);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::CreateFormsManager
-
- Description:
- Creates the forms manager object, brings our forms manager to
- the foreground, and attaches our Event and ClassMsg sink.
-
- Parameters:
- None.
-
- Returns:
- HRESULT - Result code.
- -------------------------------------------------------------------*/
- HRESULT
- CSysInfoApp::CreateFormManager()
- {
- HRESULT hr;
-
- // Create the forms manager
- hr = CoCreateInstance(
- CLSID_FMMANAGE, // Forms manager class
- NULL, // Not part of a COM aggregate
- CLSCTX_INPROC_SERVER, // Create project in process
- IID_FMMANAGE, // Request the IfmManage interface
- (PVOID *) &m_pManage); // Put the interface pointer here
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- // Bring our Forms manager to the foreground
- hr = m_pManage->MoveToForeground(TRUE, NULL);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- // Attach an EventSink
- hr = m_pManage->put_EventSink(m_pAppEventSink);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- // Attach a ClassMsgSink
- hr = m_pManage->put_ClassMsgSink(m_pAppMessageSink);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::CreateForm
-
- Description:
- Creates the main form using the LoadFormResource call. If the
- interface pointer we send it is set to NULL, LoadFormResource
- calls CoCreateInstance and creates the COM object for us. Then
- we get the interface to the only control on the form, the power
- list box, and build the menu of info. Its not so much a menu, since
- none of the items are selectable, but currently there is no way
- to put a read-only text string into a power list box.
-
- Parameters:
- None.
-
- Returns:
- HRESULT - Result code.
- -------------------------------------------------------------------*/
- HRESULT
- CSysInfoApp::CreateForm()
- {
- HRESULT hr;
- IDispatch* pDispForm = NULL;
-
- //LoadFormResource() creates the form for us as long as pDispForm is NULL
- hr = m_pManage->LoadFormResource(
- (OLE_HANDLE)m_hInst, // Application instance handle
- IDF_MAIN, // Resource ID of form resource
- (IDispatch **)&pDispForm, // Target to put interface
- m_pAppEventSink); // EventSink to attached to form
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
-
- hr = pDispForm->QueryInterface(IID_ASFORM, (PVOID *)&m_pForm);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
-
- IDispatch* pDispPLBInfo = NULL;
- // Get Interface Pointers to the menu so we can add items to it with BuildMenu.
- hr = m_pForm->ItemFromID(IDPLB_INFO, &pDispPLBInfo);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
-
- hr = pDispPLBInfo->QueryInterface(IID_ASPOWERLISTBOX, (PVOID *)&m_pPLBInfo);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
- pDispPLBInfo->Release();
-
- // Add all the items to the menu
- hr = BuildMenu();
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
-
-
- // Start the form going
- hr = m_pManage->Start(m_pForm);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
-
- pDispForm->Release();
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSysInfoApp::BuildMenu
-
- Description:
- Fills the menu with some random system information items. The
- actual items aren't important for this sample application.
-
- Parameters:
- None.
-
- Returns:
- HRESULT - Result code.
- -------------------------------------------------------------------*/
- HRESULT
- CSysInfoApp::BuildMenu()
- {
- HRESULT hr;
- STORE_INFORMATION si;
- MEMORYSTATUS ms;
- WCHAR wszBuffer[100];
- BSTR bstrBuffer;
- IASPowerListBoxItem *pInfoItem = NULL;
-
- GlobalMemoryStatus(&ms);
- GetStoreInformation(&si);
-
- hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_MEMORYFREE, ASFC_PWRLB_TYPE_COMMAND);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
- wsprintf(wszBuffer, L"Total Phys Mem: %dkb", ms.dwTotalPhys/ONE_KB);
- bstrBuffer = SysAllocString(wszBuffer);
- if(!bstrBuffer)
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
- pInfoItem->put_Caption(bstrBuffer);
- SysFreeString(bstrBuffer);
- pInfoItem->Release();
-
- pInfoItem = NULL;
- hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_STOREUSED, ASFC_PWRLB_TYPE_COMMAND);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
- wsprintf(wszBuffer, L"Obj Store Size: %dkb", si.dwStoreSize/ONE_KB);
- bstrBuffer = SysAllocString(wszBuffer);
- if(!bstrBuffer)
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
- pInfoItem->put_Caption(bstrBuffer);
- SysFreeString(bstrBuffer);
- pInfoItem->Release();
-
- pInfoItem = NULL;
- hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_PROGRAMUSED, ASFC_PWRLB_TYPE_COMMAND);
- if(FAILED(hr))
- {
- DEBUGCHK(0);
- return hr;
- }
- wsprintf(wszBuffer, L"Obj Store Free: %dkb", si.dwFreeSize/ONE_KB);
- bstrBuffer = SysAllocString(wszBuffer);
- if(!bstrBuffer)
- {
- DEBUGCHK(0);
- return E_FAIL;
- }
- pInfoItem->put_Caption(bstrBuffer);
- SysFreeString(bstrBuffer);
- pInfoItem->Release();
-
- return NOERROR;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- LoadBSTR
-
- Description:
- Loads a resource string into a BSTR.
-
- Parameters:
- UINT uId - Resource id of string to load
- BSTR* pBStr - Where to return the BSTR
- HINSTANCE hInst - Application's instance handle
-
- Returns:
- BOOL - TRUE on succes, FALSE on failure.
- -------------------------------------------------------------------*/
- BOOL LoadBSTR(UINT uID, BSTR* pBStr, HINSTANCE hInst)
- {
- if(!pBStr)
- return FALSE;
-
- WCHAR wszTemp[128];
- int iStringLen = 128;
-
- if((iStringLen = LoadStringW(hInst, uID, wszTemp, iStringLen)) == 0)
- {
- return(FALSE);
- }
-
- *pBStr = SysAllocString(wszTemp);
- // see if the string was allocated
- if (!(*pBStr))
- {
- return(FALSE);
- }
-
- return(TRUE);
- }
-