home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / AutoPC / apcsdk10.exe / data1.cab / Win32_Samples / win32 / sysinfo / sysapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-13  |  10.1 KB  |  466 lines

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     sysapp.cpp
  8.  
  9. Abstract:
  10.  
  11.     System Information AutoPC Sample control panel applet.
  12.     This file implements the applet's main application class.  It 
  13.     contains all the code to create the forms, forms manager, 
  14.     and sinks.
  15.     
  16. Environment:
  17.  
  18.     AutoPC
  19.  
  20. -------------------------------------------------------------------*/
  21. // AutoPC includes
  22. #include <Windows.h>
  23. #include <asfc.h>           
  24. #include <ascmnctl.h>  
  25. #include <keypad.h>
  26.  
  27. #include <apcdebug.h>
  28.  
  29. // Application specific includes
  30. #include "resource.h"
  31. #include "SysInfo.h"
  32.  
  33. // Macro to release an interface pointer and set it to zero
  34. #define RELEASE_IF(i)\
  35. if(i){\
  36.     i->Release();\
  37.     i=NULL; }
  38.  
  39. #define ONE_KB        1024
  40.  
  41. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  42.  
  43.     Main Application Class
  44.  
  45. -------------------------------------------------------------------*/
  46.  
  47. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  48. Function:
  49.     CSysInfoApp::CSysInfoApp
  50.  
  51. Description:
  52.     Constructor.  Initializes necessary variables.
  53.  
  54. Returns:
  55.     Nothing.
  56. -------------------------------------------------------------------*/
  57. CSysInfoApp::CSysInfoApp(HINSTANCE hInst)
  58. {
  59.     // Sinks
  60.     m_pAppEventSink        = NULL;
  61.     m_pAppMessageSink    = NULL;
  62.  
  63.     m_pForm                = NULL;
  64.     m_pManage            = NULL;
  65.  
  66.     m_bstrAppName        = NULL;
  67.  
  68.     m_hInst                = hInst;
  69.     
  70.     m_dwThreadId        = GetCurrentThreadId();
  71.     
  72. }
  73.  
  74. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  75. Function:
  76.     CSysInfoApp::~CSysInfoApp
  77.  
  78. Description:
  79.     Destructor.  Releases all the interface pointers and strings
  80.     we use in the application.
  81.  
  82. Returns:
  83.     Nothing.
  84. -------------------------------------------------------------------*/
  85. CSysInfoApp::~CSysInfoApp()
  86. {
  87.     // Close the main form
  88.     if(m_pForm) m_pForm->Close();
  89.  
  90.     // Release interface pointers (uses macro defined at top of file)
  91.     RELEASE_IF(m_pForm)
  92.     RELEASE_IF(m_pAppEventSink)
  93.     RELEASE_IF(m_pAppMessageSink)
  94.     RELEASE_IF(m_pManage)
  95.  
  96.     // Free strings we use
  97.     if(m_bstrAppName)
  98.     {
  99.         SysFreeString(m_bstrAppName);
  100.         m_bstrAppName = NULL;
  101.     }
  102.  
  103.     UnInitASFormsManager(); // uninitialize the forms manager
  104.     CoUninitialize();       // uninitialize COM
  105. }
  106.  
  107. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  108. Function:
  109.     CSysInfoApp::Init
  110.  
  111. Description:
  112.     Called after main application class is allocated.  Creates the 
  113.     event sinks, forms manager, forms, and menus.
  114.  
  115. Parameters:
  116.     None.
  117.  
  118. Returns:
  119.     HRESULT - Result code.
  120. -------------------------------------------------------------------*/
  121. BOOL 
  122. CSysInfoApp::Init()
  123. {
  124.     HRESULT    hr;
  125.  
  126.     //    Allocate a BSTR with the appName
  127.     if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bstrAppName, m_hInst))
  128.     {
  129.         DEBUGCHK(0);
  130.         return FALSE;
  131.     }
  132.  
  133.     CoInitializeEx( NULL, COINIT_MULTITHREADED );    // Initialize COM
  134.     hr = InitASFormsManager();   // Initialize the forms manager
  135.     if(FAILED(hr))
  136.     {
  137.         CoUninitialize();
  138.         return FALSE;
  139.     }
  140.  
  141.     hr = CreateSink();
  142.     if(FAILED(hr))
  143.     {
  144.         DEBUGCHK(0);
  145.         return FALSE;
  146.     }
  147.  
  148.     hr = CreateFormManager();
  149.     if(FAILED(hr))
  150.     {
  151.         DEBUGCHK(0); 
  152.         return FALSE;
  153.     }
  154.  
  155.     hr = CreateForm();
  156.     if(FAILED(hr))
  157.     {
  158.         DEBUGCHK(0);
  159.         return FALSE;
  160.     }
  161.  
  162.     return TRUE;
  163. }
  164.  
  165. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  166. Function:
  167.     CSysInfoApp::CreateSink
  168.  
  169. Description:
  170.     Creates the main application event sink.  Allocates a CSysInfoSink
  171.     object and requests the IASEventSink and IASClassMsgSink interfaces \
  172.     from it.
  173.  
  174. Parameters:
  175.     None.
  176.  
  177. Returns:
  178.     HRESULT - Result code.
  179. -------------------------------------------------------------------*/
  180. HRESULT    CSysInfoApp::CreateSink()
  181. {
  182.     HRESULT             hr;
  183.     CSysInfoSink*        pSinks;
  184.  
  185.     pSinks = new CSysInfoSink();
  186.         
  187.     hr = pSinks->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pAppEventSink);
  188.     if(FAILED(hr))
  189.     {
  190.         DEBUGCHK(0);
  191.         return E_FAIL;
  192.     }
  193.  
  194.     hr = pSinks->QueryInterface(IID_ASCLASSMSGSINK, (PVOID *) &m_pAppMessageSink);
  195.     if(FAILED(hr))
  196.     {
  197.         DEBUGCHK(0);
  198.         return E_FAIL;
  199.     }
  200.  
  201.     return NOERROR;
  202. }
  203.  
  204. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  205. Function:
  206.     CSysInfoApp::CreateFormsManager
  207.  
  208. Description:
  209.     Creates the forms manager object, brings our forms manager to 
  210.     the foreground, and attaches our Event and ClassMsg sink.
  211.     
  212. Parameters:
  213.     None.
  214.  
  215. Returns:
  216.     HRESULT - Result code.
  217. -------------------------------------------------------------------*/
  218. HRESULT    
  219. CSysInfoApp::CreateFormManager()
  220. {
  221.        HRESULT hr;
  222.  
  223.     // Create the forms manager
  224.     hr = CoCreateInstance(    
  225.         CLSID_FMMANAGE,            // Forms manager class
  226.            NULL,                    // Not part of a COM aggregate
  227.            CLSCTX_INPROC_SERVER,    // Create project in process
  228.         IID_FMMANAGE,            // Request the IfmManage interface
  229.         (PVOID *) &m_pManage);    // Put the interface pointer here
  230.     if(FAILED(hr))
  231.     {
  232.         DEBUGCHK(0);
  233.         return E_FAIL;
  234.     }
  235.     
  236.     // Bring our Forms manager to the foreground
  237.     hr = m_pManage->MoveToForeground(TRUE, NULL);
  238.     if(FAILED(hr))
  239.     {
  240.         DEBUGCHK(0);
  241.         return E_FAIL;
  242.     }
  243.  
  244.     // Attach an EventSink
  245.     hr = m_pManage->put_EventSink(m_pAppEventSink);
  246.     if(FAILED(hr)) 
  247.     {
  248.         DEBUGCHK(0);
  249.         return E_FAIL;
  250.     }
  251.  
  252.     // Attach a ClassMsgSink
  253.     hr = m_pManage->put_ClassMsgSink(m_pAppMessageSink);
  254.     if(FAILED(hr))
  255.     {
  256.         DEBUGCHK(0);
  257.         return E_FAIL;
  258.     }
  259.  
  260.     return NOERROR;
  261. }
  262.  
  263. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  264. Function:
  265.     CSysInfoApp::CreateForm
  266.  
  267. Description:
  268.     Creates the main form using the LoadFormResource call.  If the 
  269.     interface pointer we send it is set to NULL, LoadFormResource
  270.     calls CoCreateInstance and creates the COM object for us.  Then
  271.     we get the interface to the only control on the form, the power
  272.     list box, and build the menu of info.  Its not so much a menu, since
  273.     none of the items are selectable, but currently there is no way
  274.     to put a read-only text string into a power list box.
  275.  
  276. Parameters:
  277.     None.
  278.  
  279. Returns:
  280.     HRESULT - Result code.
  281. -------------------------------------------------------------------*/
  282. HRESULT
  283. CSysInfoApp::CreateForm()
  284. {
  285.     HRESULT     hr;
  286.     IDispatch*     pDispForm = NULL;
  287.  
  288.     //LoadFormResource() creates the form for us as long as pDispForm is NULL
  289.     hr = m_pManage->LoadFormResource(
  290.         (OLE_HANDLE)m_hInst,            // Application instance handle
  291.         IDF_MAIN,                       // Resource ID of form resource
  292.         (IDispatch **)&pDispForm,        // Target to put interface
  293.         m_pAppEventSink);                // EventSink to attached to form
  294.     if(FAILED(hr)) 
  295.     {
  296.         DEBUGCHK(0);
  297.         return hr;
  298.     }
  299.  
  300.     hr = pDispForm->QueryInterface(IID_ASFORM, (PVOID *)&m_pForm);
  301.     if(FAILED(hr))
  302.     {
  303.         DEBUGCHK(0);
  304.         return hr;
  305.     }
  306.  
  307.     IDispatch* pDispPLBInfo = NULL;
  308.     // Get Interface Pointers to the menu so we can add items to it with BuildMenu.
  309.     hr = m_pForm->ItemFromID(IDPLB_INFO, &pDispPLBInfo);
  310.     if(FAILED(hr))
  311.     {
  312.         DEBUGCHK(0);
  313.         return hr;
  314.     }
  315.  
  316.     hr = pDispPLBInfo->QueryInterface(IID_ASPOWERLISTBOX, (PVOID *)&m_pPLBInfo);
  317.     if(FAILED(hr))
  318.     {
  319.         DEBUGCHK(0);
  320.         return hr;
  321.     }
  322.     pDispPLBInfo->Release();
  323.  
  324.     // Add all the items to the menu
  325.     hr = BuildMenu();
  326.     if(FAILED(hr))
  327.     {
  328.         DEBUGCHK(0);
  329.         return hr;    
  330.     }
  331.  
  332.  
  333.     // Start the form going
  334.     hr = m_pManage->Start(m_pForm);
  335.     if(FAILED(hr))
  336.     {
  337.         DEBUGCHK(0);
  338.         return hr;
  339.     }
  340.  
  341.     pDispForm->Release();
  342.     
  343.     return NOERROR;
  344. }
  345.  
  346. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  347. Function:
  348.     CSysInfoApp::BuildMenu
  349.  
  350. Description:
  351.     Fills the menu with some random system information items.  The 
  352.     actual items aren't important for this sample application.
  353.  
  354. Parameters:
  355.     None.
  356.  
  357. Returns:
  358.     HRESULT - Result code.
  359. -------------------------------------------------------------------*/
  360. HRESULT
  361. CSysInfoApp::BuildMenu()
  362. {
  363.     HRESULT hr;
  364.     STORE_INFORMATION    si;
  365.     MEMORYSTATUS        ms;
  366.     WCHAR                wszBuffer[100];
  367.     BSTR                bstrBuffer;
  368.     IASPowerListBoxItem *pInfoItem = NULL;
  369.  
  370.     GlobalMemoryStatus(&ms);
  371.     GetStoreInformation(&si);
  372.  
  373.     hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_MEMORYFREE, ASFC_PWRLB_TYPE_COMMAND);
  374.     if(FAILED(hr))
  375.     {
  376.         DEBUGCHK(0);
  377.         return hr;
  378.     }
  379.     wsprintf(wszBuffer, L"Total Phys Mem: %dkb", ms.dwTotalPhys/ONE_KB);
  380.     bstrBuffer = SysAllocString(wszBuffer);
  381.     if(!bstrBuffer) 
  382.     {
  383.         DEBUGCHK(0);
  384.         return E_FAIL;
  385.     }
  386.     pInfoItem->put_Caption(bstrBuffer);
  387.     SysFreeString(bstrBuffer);
  388.     pInfoItem->Release();
  389.  
  390.     pInfoItem = NULL;
  391.     hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_STOREUSED, ASFC_PWRLB_TYPE_COMMAND);
  392.     if(FAILED(hr))
  393.     {
  394.         DEBUGCHK(0);
  395.         return hr;
  396.     }
  397.     wsprintf(wszBuffer, L"Obj Store Size: %dkb", si.dwStoreSize/ONE_KB);
  398.     bstrBuffer = SysAllocString(wszBuffer);
  399.     if(!bstrBuffer) 
  400.     {
  401.         DEBUGCHK(0);
  402.         return E_FAIL;
  403.     }
  404.     pInfoItem->put_Caption(bstrBuffer);
  405.     SysFreeString(bstrBuffer);
  406.     pInfoItem->Release();
  407.  
  408.     pInfoItem = NULL;
  409.     hr = m_pPLBInfo->CreateItem(&pInfoItem, IDMI_PROGRAMUSED, ASFC_PWRLB_TYPE_COMMAND);
  410.     if(FAILED(hr))
  411.     {
  412.         DEBUGCHK(0);
  413.         return hr;
  414.     }
  415.     wsprintf(wszBuffer, L"Obj Store Free: %dkb", si.dwFreeSize/ONE_KB);
  416.     bstrBuffer = SysAllocString(wszBuffer);
  417.     if(!bstrBuffer) 
  418.     {
  419.         DEBUGCHK(0);
  420.         return E_FAIL;
  421.     }
  422.     pInfoItem->put_Caption(bstrBuffer);
  423.     SysFreeString(bstrBuffer);
  424.     pInfoItem->Release();
  425.  
  426.     return NOERROR;
  427. }
  428.  
  429. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  430. Function:
  431.     LoadBSTR
  432.  
  433. Description:
  434.     Loads a resource string into a BSTR.
  435.  
  436. Parameters:
  437.     UINT uId        - Resource id of string to load
  438.     BSTR* pBStr        - Where to return the BSTR
  439.     HINSTANCE hInst    - Application's instance handle
  440.  
  441. Returns:
  442.     BOOL - TRUE on succes, FALSE on failure.
  443. -------------------------------------------------------------------*/
  444. BOOL LoadBSTR(UINT uID, BSTR* pBStr, HINSTANCE hInst)
  445. {
  446.     if(!pBStr)
  447.         return    FALSE;
  448.  
  449.     WCHAR wszTemp[128];
  450.     int iStringLen = 128;
  451.  
  452.     if((iStringLen = LoadStringW(hInst, uID, wszTemp, iStringLen)) == 0)
  453.     {
  454.         return(FALSE);
  455.     }        
  456.     
  457.     *pBStr = SysAllocString(wszTemp);
  458.     // see if the string was allocated
  459.     if (!(*pBStr))
  460.     {
  461.         return(FALSE);
  462.     }
  463.  
  464.     return(TRUE);
  465. }
  466.