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

📄 client3.cpp

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


// The main MFC applictaion class
class CFortuneTellerApp : public CWinApp
{
public:

   // The single, overriden virtual method
   BOOL InitInstance();
};

// The one and only CFortuneTellerApp object
CFortuneTellerApp FortuneTeller;


//
// Everything happens inside here
//

BOOL CFortuneTellerApp::InitInstance()
{
   HRESULT          hResult;
   IUnknown         *pIUnknown;

   // Prepare COM for use
   VerboseMsg("Calling COM API CoInitialize.\n");
   hResult = CoInitialize(NULL);
   if (FAILED(hResult)) {
      ReportError("Could not initialize COM.", hResult);
      return FALSE;
   }

   // Create a new FortuneTeller object and return an IUnknown
   // interface pointer
   VerboseMsg("Creating new FortuneTeller3 object instance.\n");
   hResult = CoCreateInstance(CLSID_FortuneTeller3,
                              NULL,
                              CLSCTX_SERVER,
                              IID_IUnknown,
                              (PPVOID) &pIUnknown);
   if (FAILED(hResult)) {
      ReportError("Could not create a new FortuneTeller3 object.", hResult);
      return FALSE;
   }

   // Fortune or Quotation?
   if (rand() % 2) {

      //
      // It's a Fortune
      //

      IFortuneTeller   *pIFortuneTeller;
      BSTR             bstrFortune;

      // Get an IFortune interface pointer
      VerboseMsg("Getting IFortuneTeller interface pointer.\n");
      hResult = pIUnknown->QueryInterface(IID_IFortuneTeller,
                                          (PPVOID) &pIFortuneTeller);
      if (FAILED(hResult)) {
         ReportError("Could not obtain FortuneTeller interface.", hResult);
         pIUnknown->Release();
         return FALSE;
      }

      // Get our fortune, in BSTR form
      VerboseMsg("Getting fortune from FortuneTeller object.\n");
      pIFortuneTeller->GetFortune(&bstrFortune);

      // Show the user the fortune
      CString  strFortune((LPCWSTR) bstrFortune);
      ::AfxMessageBox("\"" + strFortune + "\"",
                      MB_ICONINFORMATION | MB_OK);

      // Release the memory for the BSTR
      VerboseMsg("Releasing fortune BSTR.\n");
      SysFreeString(bstrFortune);

      // We're done with the IFortuneTeller interface
      VerboseMsg("Releasing IFortuneTeller interface.\n");
      pIFortuneTeller->Release();
   }

   else {

      //
      // It's a Quotation
      //

      IQuotation       *pIQuotation;
      BSTR             bstrQuotation;
      BSTR             bstrAuthor;

      // Get an IQuotation interface pointer
      VerboseMsg("Getting IQuotation interface pointer.\n");
      hResult = pIUnknown->QueryInterface(IID_IQuotation,
                                          (PPVOID) &pIQuotation);
      if (FAILED(hResult)) {
         ReportError("Could not obtain Quotation interface.", hResult);
         pIUnknown->Release();
         return FALSE;
      }

      // Get our quotation and author, in BSTR form
      VerboseMsg("Getting quotation and author from FortuneTeller object.\n");
      pIQuotation->GetQuotation(&bstrQuotation, &bstrAuthor);

      // Show the user the quotation
      CString  strQuotation((LPCWSTR) bstrQuotation);
      CString  strAuthor((LPCWSTR) bstrAuthor);
      ::AfxMessageBox("\"" + strQuotation + "\"\n\n" +
                      "\t-- " + strAuthor,
                      MB_ICONINFORMATION | MB_OK);

      // Release the memory for the BSTRs
      VerboseMsg("Releasing quotation and author BSTRs.\n");
      SysFreeString(bstrQuotation);
      SysFreeString(bstrAuthor);

      // We're done with the IQuotation interface
      VerboseMsg("Releasing IQuotation interface.\n");
      pIQuotation->Release();
   }

   // We're done with the FortuneTeller object
   VerboseMsg("Releasing FortuneTeller object.\n");
   pIUnknown->Release();
   pIUnknown = NULL;

   // We're all done with COM
   CoUninitialize();

   return CWinApp::InitInstance();
}

⌨️ 快捷键说明

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