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

📄 fortune2.cpp

📁 VC++串口通信设。本书详细说明讲解了在VC++环境下编写串口通信得过程。值得一看
💻 CPP
字号:
// Get needed compiler include files
#include <string>

// Get needed system and custom include files
#include "Utility.h"
#include "Counts.h"
#include "Fortune2.h"
#include "Factory2.h"

using namespace std;

// Our list of fortunes
static string g_astrFortunes[] = 
   { 
      "You are strong in body and spirit.",
      "You are strong in perspiration and odor.",
      "Good fortune smiles upon the distributed.",
      "VB Programmer for Hire -- Cheap!!",
      "I never met an object I didn't like.",
      "Edit, Compile, Link, Crash, Sigh.",
      "Never judge a stack dump by its arguments.",
      "Card carrying member of the Bug Police.",
      "You make Dilbert look charming and sophisticated.",
      "BMW, The Ultimate Driving Machine."
   };

// Constant that tracks the number of fortunes we know about
static const ULONG g_ulNumFortunes = 
   sizeof(g_astrFortunes) / sizeof(string);


// Constructor
ComFortuneTeller::ComFortuneTeller()
        : m_cRef(0)
{
   // Be annoying and verbose
   VerboseMsg("Constructing new ComFortuneTeller object.\n");

   // Seed the random number generator
   SeedRandomGenerator();
}


//
// IUnknown methods
//

STDMETHODIMP ComFortuneTeller::QueryInterface(REFIID rIID,
                                              PPVOID ppInterface)
{
   VerboseMsg("In ComFortuneTeller::QueryInterface\n");

   // Set the interface pointer
   if (rIID == IID_IUnknown) {
      VerboseMsg("   Requesting IUnknown interface.\n");
      *ppInterface = this;
   }

   else if (rIID == IID_IFortuneTeller) {
      VerboseMsg("   Requesting IFortuneTeller interface.\n");
      *ppInterface = this;
   }

   // We don't support this interface
   else {
      VerboseMsg("   Requesting unsupported interface.\n");
      *ppInterface = NULL;
      return E_NOINTERFACE;
   }

   // Bump up the reference count
   ((LPUNKNOWN) *ppInterface)->AddRef();

   return NOERROR;
}

STDMETHODIMP_(ULONG) ComFortuneTeller::AddRef()
{
   // Increment the reference count
   m_cRef++;

   // Convert to string and display
   VerboseMsg("In ComFortuneTeller::AddRef. Reference count = "
              + ULongToStr(m_cRef) + "\n");

   return m_cRef;
}

STDMETHODIMP_(ULONG) ComFortuneTeller::Release()
{
   // Decrement the reference count
   m_cRef--;

   // Convert to string and display
   VerboseMsg("In ComFortuneTeller::Release. Reference count = "
              + ULongToStr(m_cRef) + "\n");

   // Is this the last reference to the object?
   if (m_cRef)
      return m_cRef;

   // Decrement the server object count
   Counters::DecObjectCount();

   // Self destruct
   VerboseMsg("   ComFortuneTeller reference count is 0. Deleting self.\n");
   delete this;
   return 0;
}


//
// IFortuneTeller methods
//

STDMETHODIMP ComFortuneTeller::GetFortune(PBSTR pbstrFortune)
{
  VerboseMsg("In ComFortuneTeller::GetFortune\n");

  // Initialize the output string
  *pbstrFortune = NULL;

  // Convert the ANSI string to Unicode
  LPOLESTR pszFortuneW;
  AnsiToUnicode(g_astrFortunes[rand() % g_ulNumFortunes].c_str(), 
                &pszFortuneW);

  // Create a BSTR out of the Unicode string
  *pbstrFortune = SysAllocString(pszFortuneW);

  // Release the Unicode string memory
  CoTaskMemFree(pszFortuneW);

  return NOERROR;
}

⌨️ 快捷键说明

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