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

📄 pizzamakeimp.cpp

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


// Initialize our static pizza ID counter
ULONG ComPizzaMaker::m_ulPizzaIDCounter = 0x00;


//
// Constructor and destructor
//

ComPizzaMaker::ComPizzaMaker()
      : m_pIMoniker(NULL),
        m_dwROTCookie(0)
{
   VerboseMsg("In PizzaMaker constructor.\n");
}

ComPizzaMaker::~ComPizzaMaker()
{
   VerboseMsg("In PizzaMaker destructor.\n");
}


//
// Overidden public methods
//

HRESULT ComPizzaMaker::FinalConstruct()
{
   USES_CONVERSION;
   VerboseMsg("In PizzaMaker FinalConstruct method.\n");

   // Create the file moniker
   HRESULT hResult = CreateFileMoniker(A2OLE("PizzaMaker"),
                                       &m_pIMoniker);
   if (FAILED(hResult)) {
      // We'll let the client report the error
      // ReportError("Could not create file moniker.", hResult);
      return hResult;
   }

   // Get an interface pointer for the ROT
   IRunningObjectTable* pIROT = NULL;
   hResult = GetRunningObjectTable(0, &pIROT);
   if (FAILED(hResult)) {
      // We'll let the client report the error
      // ReportError("Could not obtain ROT interface pointer.", hResult);
      m_pIMoniker->Release();
      m_pIMoniker = NULL;
      return hResult;
   }

   // Now place the moniker into the ROT
   hResult = pIROT->Register(0, GetUnknown(), m_pIMoniker, &m_dwROTCookie);
   if (FAILED(hResult)) {
      // We'll let the client report the error
      // ReportError("Could not register Moniker in the ROT.", hResult);
      m_pIMoniker->Release();
      m_pIMoniker = NULL;
      pIROT->Release();
      return hResult;
   }

   // Release our IROT pointer
   pIROT->Release();

   return NOERROR;  
}

void ComPizzaMaker::FinalRelease()
{
   // Remove our moniker from the ROT
   if (m_dwROTCookie) {

      // Get an interface pointer for the ROT
      IRunningObjectTable* pIROT = NULL;
      HRESULT hResult = GetRunningObjectTable(0, &pIROT);

      // Now remove the moniker
      if (SUCCEEDED(hResult)) {
         hResult = pIROT->Revoke(m_dwROTCookie);
         pIROT->Release();
      }
      m_dwROTCookie = 0;
   }

   // And get rid of our Moniker
   if (m_pIMoniker) {
      m_pIMoniker->Release();
      m_pIMoniker = NULL;
   }
}


//
// IPizzaMaker interface members
//

STDMETHODIMP
ComPizzaMaker::MakePizza(PizzaSize      ePizzaSize,
                         ULONG          ulNumToppings,
                         ToppingInfo    aToppingInfo[],
                         DeliveryInfo*  pDeliveryInfo,
                         PULONG         pulPizzaID)
{
   ULONG       ulNewPizzaID = ++m_ulPizzaIDCounter;
   VerboseMsg("In MakePizza.\n");

   // Start by notifying everyone who's interested that we
   // have a new pizza to make
   SendIncomingOrderEvents(ePizzaSize,
                           ulNumToppings,
                           aToppingInfo,
                           pDeliveryInfo,
                           ulNewPizzaID);

   // Now its time to make the pizza.
   // In our fantasy world, it takes 10 seconds to make a pizza
   // (I wish! Wouldn't that be great!!)
   Sleep(10000);

   // Finally send out our notifications that the pizza is done
   SendCompletedOrderEvents(ulNewPizzaID);

   // Set the pizza id return parameter
   *pulPizzaID = ulNewPizzaID;

   return NOERROR;
}


//
// Misc private methods
//

void ComPizzaMaker::SendIncomingOrderEvents(PizzaSize      ePizzaSize,
                                            ULONG          ulNumToppings,
                                            ToppingInfo    aToppingInfo[],
                                            DeliveryInfo*  pDeliveryInfo,
                                            ULONG          ulNewPizzaID)
{
   CONNECTDATA cd;

   // Retrieve a connection point container interface pointer
	CComQIPtr<IConnectionPointContainer, &IID_IConnectionPointContainer> pCPC(this);
   if (pCPC) {

      // Now lookup our pizza notify sink connection point
      CComPtr<IConnectionPoint> pCP;
      pCPC->FindConnectionPoint(IID_IPizzaNotifySink, &pCP);
      if (pCP) {

         // Retrieve an enumerator for this connection point
         CComPtr<IEnumConnections> pEnum;
         if (SUCCEEDED(pCP->EnumConnections(&pEnum))) {

            // Iterate over our connections, one at a time
            while (pEnum->Next(1, &cd, NULL) == S_OK) {

               // Do we have a valid object pointer
               if (cd.pUnk) {

                  // Retrieve an IPizzaNotifySink interface pointer
                  CComQIPtr<IPizzaNotifySink, &IID_IPizzaNotifySink> pSink(cd.pUnk);

                  // Send out the event notification
                  if (pSink)
                     pSink->OnNewPizzaRequest(ulNewPizzaID,
                                              ePizzaSize,
                                              ulNumToppings,
                                              aToppingInfo,
                                              pDeliveryInfo);

                  // Release our reference count
                  cd.pUnk->Release();
               }
            }
         }
      }
   }
}

void ComPizzaMaker::SendCompletedOrderEvents(ULONG  ulNewPizzaID)
{
   CONNECTDATA cd;

   // Retrieve a connection point container interface pointer
   CComQIPtr<IConnectionPointContainer, &IID_IConnectionPointContainer> pCPC(this);
   if (pCPC) {

      // Now lookup our pizza notify sink connection point
      CComPtr<IConnectionPoint> pCP;
      pCPC->FindConnectionPoint(IID_IPizzaNotifySink, &pCP);
      if (pCP) {

         // Retrieve an enumerator for this connection point
         CComPtr<IEnumConnections> pEnum;
         if (SUCCEEDED(pCP->EnumConnections(&pEnum))) {

            // Iterate over our connections, one at a time
            while (pEnum->Next(1, &cd, NULL) == NOERROR) {

               // Do we have a valid object pointer
               if (cd.pUnk) {

                  // Retrieve an IPizzaNotifySink interface pointer
                  CComQIPtr<IPizzaNotifySink, &IID_IPizzaNotifySink> pSink(cd.pUnk);

                  // Send out the event notification
                  if (pSink)
                     pSink->OnPizzaComplete(ulNewPizzaID);

                  // Release our reference count
                  cd.pUnk->Release();
               }
            }
         }
      }
   }
}

⌨️ 快捷键说明

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