⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.cpp

📁 VC++串口通信设。本书详细说明讲解了在VC++环境下编写串口通信得过程。值得一看
💻 CPP
字号:
// Get needed include files
#define INITGUID
#include "Client.h"
#include "InitDlg.h"
#include "SizeDlg.h"
#include "TopDlg.h"
#include "FinalDlg.h"
#include "Resource.h"
#include "Utility.h"


//
// Useful constants
//
const ULONG   g_ulMaxBufferLen = 256;
const CString g_strKnownComponentsRegKey = 
       "Software\\Que\\PizzaOrderTaker Client\\1.0\\Known Components";


//
// Main application/client class
//

class CPizzaWizard : public CWinApp
{
public:

   // Public method inherited from CWinApp
   virtual BOOL InitInstance();

private:

   // Private helper method
   BOOL LoadComponentListFromRegistry(CComponentAssocList& AssociationList);
};

// Our one-and-only application object
CPizzaWizard TheApp;


//
// The entire program is run out of this method
//

BOOL CPizzaWizard::InitInstance()
{
   // Standard initialization
   Enable3dControls();

   // Initialize OLE libraries
   if (!AfxOleInit()) {
	   AfxMessageBox("Could not initialize OLE subsystem.");
	   return FALSE;
   }

   // This list contains a list of component structures describing all
   // of the components implementing IPizzaOrderTaker that the Wizard
   // knows about
   CComponentAssocList  AssociationList;

   // Load up info about known components from the registry
   if (!LoadComponentListFromRegistry(AssociationList))
      return FALSE;

   // This structure contains data that needs to be accessible 
   // from all Wizard pages
   CSharedData SharedData;

   // Create our property sheet object...
   CPropertySheet  PropSheets("COM Pizza Order-Taking Wizard");

   // And create the four pages
   CInitialDialog  InitialPage(SharedData, AssociationList);
   CSizeDialog     SizePage(SharedData);
   CToppingDialog  ToppingsPage(SharedData);
   CFinalDialog    FinalPage(SharedData);

   // Add the pages to the property sheet
   PropSheets.AddPage(&InitialPage);
   PropSheets.AddPage(&SizePage);
   PropSheets.AddPage(&ToppingsPage);
   PropSheets.AddPage(&FinalPage);

   // Put ourselves into Wizard mode and fire up the dialog box
   PropSheets.SetWizardMode();
   PropSheets.DoModal();

   // Clear out our list
   POSITION posCurrent = AssociationList.GetHeadPosition();
   while (posCurrent)
      delete AssociationList.GetNext(posCurrent);
   AssociationList.RemoveAll();

   return FALSE;
}


BOOL CPizzaWizard::LoadComponentListFromRegistry(CComponentAssocList& AssociationList)
{
   // Initialize data
   HRESULT              hResult = S_OK;
   ULONG                ulComponentIndex = 0;
   HKEY                 hBaseKey = 0;
   HKEY                 hSubKey = 0;
   LPOLESTR             pszCLSIDStr = NULL;
   CStringToCLSIDAssoc* pNewAssoc = NULL;

   // Open the base application registry key
   hResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                          g_strKnownComponentsRegKey, 
                          0, 
                          KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE , 
                          &hBaseKey); 

   if (hResult) {
      ReportError("Could not open the Wizard's base configuration registry "
                  "key. The PizzaOrderTaker client is probably not correctly "
                  "installed.", hResult);
      return FALSE;
   }

   // Enumerate the subkeys
   while (hResult != ERROR_NO_MORE_ITEMS)
   {

      // Enumerate over the Known Components key
      CHAR  achSubkey[g_ulMaxBufferLen];
      ULONG ulBufferLen = g_ulMaxBufferLen;
      hResult = RegEnumKeyEx(hBaseKey,
                             ulComponentIndex,
                             achSubkey,
                             &ulBufferLen,
                             NULL, NULL, NULL, NULL);

      if (hResult && hResult != ERROR_NO_MORE_ITEMS) {
         ReportError("Could not enumerate over the \"Known Components\" "
                     "registry key.", hResult);
         RegCloseKey(hBaseKey);
         return FALSE;
      }

      // Are we at the end of the list of subkeys?
      if (hResult != ERROR_NO_MORE_ITEMS) {

         // Create a new entry for our component list
         pNewAssoc = new CStringToCLSIDAssoc;

         // The name of the subkey is the CLSID of the component
         // Convert the string CLSID to the real thing
         AnsiToUnicode(achSubkey, &pszCLSIDStr);
         CLSIDFromString(pszCLSIDStr, &(pNewAssoc->m_ComponentCLSID));
         CoTaskMemFree(pszCLSIDStr);

         // Create the fully qualified key name
         CString strFullSubkey(g_strKnownComponentsRegKey);
         strFullSubkey += "\\";
         strFullSubkey += achSubkey;

         // Open the subkey
         hResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                strFullSubkey, 
                                0, 
                                KEY_QUERY_VALUE , 
                                &hSubKey); 
         if (hResult) {
            ReportError("Could not open a \"Known Components\" subkey.", hResult);
            RegCloseKey(hBaseKey);
            return FALSE;
         }

         // Now retrieve the component description
         BYTE  achDescription[g_ulMaxBufferLen];
         ULONG ulDescriptionLen = g_ulMaxBufferLen;
         ULONG ulValueType;
         hResult = RegQueryValueEx(hSubKey,
                                   "",      // The "Default" value
                                   NULL,
                                   &ulValueType,
                                   achDescription,
                                   &ulDescriptionLen);
        if (hResult) {
            ReportError("Could not retrieve a default value.", hResult);
            RegCloseKey(hSubKey);
            RegCloseKey(hBaseKey);
            return FALSE;
        }

        // Set the association description
        pNewAssoc->m_strComponentDesc = achDescription;

        // Add the association to the main list
        AssociationList.AddTail(pNewAssoc);

        // Close the subkey
        RegCloseKey(hSubKey);

        // On to the next subkey
        ulComponentIndex++;
      }
   }

   // Close the base key
   RegCloseKey(hBaseKey);

   // Did we successfully find at least one component?
   if (!ulComponentIndex) {
      ReportError("Could not find a correctly configured PizzaOrderTaker "
                  "component.", hResult);
      return FALSE;
   }

   return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -