home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap06 / objuser3 / objuser3.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  12KB  |  442 lines

  1. /*
  2.  * OBJUSER3.CPP
  3.  * Koala Client #3 Chapter 6
  4.  *
  5.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  6.  *
  7.  * Kraig Brockschmidt, Microsoft
  8.  * Internet  :  kraigb@microsoft.com
  9.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  10.  */
  11.  
  12.  
  13. #define INITGUIDS
  14. #include "objuser3.h"
  15.  
  16.  
  17. /*
  18.  * WinMain
  19.  *
  20.  * Purpose:
  21.  *  Main entry point of application.
  22.  */
  23.  
  24. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  25.     , LPSTR pszCmdLine, int nCmdShow)
  26.     {
  27.     MSG         msg;
  28.     PAPP        pApp;
  29.  
  30.     SETMESSAGEQUEUE;
  31.  
  32.     pApp=new CApp(hInst, hInstPrev, nCmdShow);
  33.  
  34.     if (NULL==pApp)
  35.         return -1;
  36.  
  37.     if (pApp->Init())
  38.         {
  39.         while (GetMessage(&msg, NULL, 0,0 ))
  40.             {
  41.             TranslateMessage(&msg);
  42.             DispatchMessage(&msg);
  43.             }
  44.         }
  45.  
  46.     delete pApp;
  47.     return msg.wParam;
  48.     }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. /*
  55.  * ObjectUserWndProc
  56.  *
  57.  * Purpose:
  58.  *  Window class procedure.  Standard callback.
  59.  */
  60.  
  61. LRESULT APIENTRY ObjectUserWndProc(HWND hWnd, UINT iMsg
  62.     , WPARAM wParam, LPARAM lParam)
  63.     {
  64.     HRESULT         hr;
  65.     PAPP            pApp;
  66.     IUnknown       *pUnk;
  67.     IAnimal        *pIAnimal;
  68.     IKoala         *pIKoala;
  69.     TCHAR           szMsg[256];
  70.  
  71.     pApp=(PAPP)GetWindowLong(hWnd, OBJUSERWL_STRUCTURE);
  72.  
  73.     switch (iMsg)
  74.         {
  75.         case WM_NCCREATE:
  76.             pApp=(PAPP)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  77.             SetWindowLong(hWnd, OBJUSERWL_STRUCTURE, (LONG)pApp);
  78.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  79.  
  80.         case WM_DESTROY:
  81.             PostQuitMessage(0);
  82.             break;
  83.  
  84.         case WM_COMMAND:
  85.             if (NULL!=pApp)
  86.                 pUnk=pApp->m_pIUnknown;
  87.  
  88.             switch (LOWORD(wParam))
  89.                 {
  90.                 case IDM_OBJECTCREATE:
  91.                     if (NULL!=pUnk)
  92.                         {
  93.                         ReleaseInterface(pUnk);
  94.                         CoFreeUnusedLibraries();
  95.                         }
  96.  
  97.                     hr=CoCreateInstance(CLSID_Koala, NULL
  98.                         , CLSCTX_LOCAL_SERVER, IID_IUnknown
  99.                         , (PPVOID)&pApp->m_pIUnknown);
  100.  
  101.                     if (SUCCEEDED(hr))
  102.                         pApp->Message(TEXT("Creation succeeded"));
  103.                     else
  104.                         pApp->Message(TEXT("Creation failed"));
  105.  
  106.                     break;
  107.  
  108.  
  109.                 case IDM_OBJECTRELEASE:
  110.                     if (NULL==pUnk)
  111.                         {
  112.                         pApp->Message(TEXT("There is no object"));
  113.                         break;
  114.                         }
  115.                     ReleaseInterface(pApp->m_pIUnknown);
  116.                     pApp->Message(TEXT("Object released"));
  117.                     break;
  118.  
  119.  
  120.                 case IDM_ANIMALEAT:
  121.                     if (NULL==pUnk)
  122.                         {
  123.                         pApp->Message(TEXT("There is no object"));
  124.                         break;
  125.                         }
  126.  
  127.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IAnimal
  128.                         , (PPVOID)&pIAnimal)))
  129.                         {
  130.                         const short cch=80;
  131.                         TCHAR       szEaten[cch];
  132.  
  133.                         pIAnimal->Eat(TEXT("Your Choice"), szEaten
  134.                             , cch);
  135.                         pIAnimal->Release();
  136.  
  137.                         wsprintf(szMsg, TEXT("Animal ate %s")
  138.                             , szEaten);
  139.                         pApp->Message(szMsg);
  140.                         }
  141.                     else
  142.                         pApp->Message(TEXT("QueryInterface failed"));
  143.  
  144.                     break;
  145.  
  146.  
  147.                case IDM_ANIMALSLEEP:
  148.                     if (NULL==pUnk)
  149.                         {
  150.                         pApp->Message(TEXT("There is no object"));
  151.                         break;
  152.                         }
  153.  
  154.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IAnimal
  155.                         , (PPVOID)&pIAnimal)))
  156.                         {
  157.                         short   n=120;
  158.  
  159.                         pIAnimal->Sleep(&n);
  160.                         wsprintf(szMsg, TEXT("Animal slept %u"), n);
  161.                         pApp->Message(szMsg);
  162.                         pIAnimal->Release();
  163.                         }
  164.                     else
  165.                         pApp->Message(TEXT("QueryInterface failed"));
  166.  
  167.                     break;
  168.  
  169.  
  170.                case IDM_ANIMALPROCREATE:
  171.                     if (NULL==pUnk)
  172.                         {
  173.                         pApp->Message(TEXT("There is no object"));
  174.                         break;
  175.                         }
  176.  
  177.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IAnimal
  178.                         , (PPVOID)&pIAnimal)))
  179.                         {
  180.                         short   n;
  181.  
  182.                         hr=pIAnimal->Procreate(&n);
  183.                         pIAnimal->Release();
  184.  
  185.                         if (NOERROR!=hr)
  186.                             {
  187.                             pApp->Message(TEXT("No offspring this time"));
  188.                             break;
  189.                             }
  190.  
  191.                         wsprintf(szMsg
  192.                             , TEXT("Animal produced %u offspring")
  193.                             , n);
  194.                         pApp->Message(szMsg);
  195.                         }
  196.                     else
  197.                         pApp->Message(TEXT("QueryInterface failed"));
  198.  
  199.                     break;
  200.  
  201.  
  202.                case IDM_ANIMALWHATKIND:
  203.                     if (NULL==pUnk)
  204.                         {
  205.                         pApp->Message(TEXT("There is no object"));
  206.                         break;
  207.                         }
  208.  
  209.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IAnimal
  210.                         , (PPVOID)&pIAnimal)))
  211.                         {
  212.                         IID     iid;
  213.  
  214.                         pIAnimal->WhatKindOfAnimal(&iid);
  215.                         pIAnimal->Release();
  216.  
  217.                         StringFromGUID2(iid, szMsg, 256);
  218.                         pApp->Message(szMsg);
  219.                         }
  220.                     else
  221.                         pApp->Message(TEXT("QueryInterface failed"));
  222.  
  223.                     break;
  224.  
  225.  
  226.                case IDM_KOALACLIMB:
  227.                     if (NULL==pUnk)
  228.                         {
  229.                         pApp->Message(TEXT("There is no object"));
  230.                         break;
  231.                         }
  232.  
  233.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IKoala
  234.                         , (PPVOID)&pIKoala)))
  235.                         {
  236.                         pIKoala->ClimbEucalyptusTree(0);
  237.                         pIKoala->Release();
  238.                         pApp->Message(TEXT("Tree climbed"));
  239.                         }
  240.                     else
  241.                         pApp->Message(TEXT("QueryInterface failed"));
  242.  
  243.                     break;
  244.  
  245.  
  246.                case IDM_KOALAPOUCH:
  247.                     if (NULL==pUnk)
  248.                         {
  249.                         pApp->Message(TEXT("There is no object"));
  250.                         break;
  251.                         }
  252.  
  253.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IKoala
  254.                         , (PPVOID)&pIKoala)))
  255.                         {
  256.                         pIKoala->PouchOpensDown();
  257.                         pIKoala->Release();
  258.                         pApp->Message(TEXT("PouchOpenDown called"));
  259.                         }
  260.                     else
  261.                         pApp->Message(TEXT("QueryInterface failed"));
  262.  
  263.                     break;
  264.  
  265.  
  266.                case IDM_KOALASLEEP:
  267.                     if (NULL==pUnk)
  268.                         {
  269.                         pApp->Message(TEXT("There is no object"));
  270.                         break;
  271.                         }
  272.  
  273.                     if (SUCCEEDED(pUnk->QueryInterface(IID_IKoala
  274.                         , (PPVOID)&pIKoala)))
  275.                         {
  276.                         pIKoala->SleepAfterEating(120);
  277.                         pIKoala->Release();
  278.                         pApp->Message(TEXT("Set extra sleep to 120"));
  279.                         }
  280.                     else
  281.                         pApp->Message(TEXT("QueryInterface failed"));
  282.  
  283.                     break;
  284.  
  285.  
  286.                 case IDM_OBJECTEXIT:
  287.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  288.                     break;
  289.                 }
  290.             break;
  291.  
  292.         default:
  293.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  294.         }
  295.  
  296.     return 0L;
  297.     }
  298.  
  299.  
  300.  
  301.  
  302.  
  303. /*
  304.  * CApp::CApp
  305.  * CApp::~CApp
  306.  *
  307.  * Constructor Parameters: (from WinMain)
  308.  *  hInst           HINSTANCE of the application.
  309.  *  hInstPrev       HINSTANCE of a previous instance.
  310.  *  nCmdShow        UINT specifying how to show the app window.
  311.  *
  312.  */
  313.  
  314. CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
  315.     , UINT nCmdShow)
  316.     {
  317.     m_hInst       =hInst;
  318.     m_hInstPrev   =hInstPrev;
  319.     m_nCmdShow    =nCmdShow;
  320.  
  321.     m_hWnd        =NULL;
  322.  
  323.     m_pIUnknown   =NULL;
  324.     m_fInitialized=FALSE;
  325.     return;
  326.     }
  327.  
  328.  
  329. CApp::~CApp(void)
  330.     {
  331.     ReleaseInterface(m_pIUnknown);
  332.  
  333.     if (IsWindow(m_hWnd))
  334.         DestroyWindow(m_hWnd);
  335.  
  336.     if (m_fInitialized)
  337.         CoUninitialize();
  338.  
  339.     return;
  340.     }
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.  * CApp::Init
  350.  *
  351.  * Purpose:
  352.  *  Initializes an CApp object by registering window classes,
  353.  *  creating the main window, and doing anything else prone to
  354.  *  failure such as calling CoInitialize.  If this function fails
  355.  *  the caller should insure that the destructor is called.
  356.  *
  357.  * Parameters:
  358.  *  None
  359.  *
  360.  * Return Value:
  361.  *  BOOL            TRUE if successful, FALSE otherwise.
  362.  */
  363.  
  364. BOOL CApp::Init(void)
  365.     {
  366.     WNDCLASS    wc;
  367.  
  368.     CHECKVER_COM;
  369.  
  370.     if (FAILED(CoInitialize(NULL)))
  371.         return FALSE;
  372.  
  373.     m_fInitialized=TRUE;
  374.  
  375.     if (!m_hInstPrev)
  376.         {
  377.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  378.         wc.lpfnWndProc    = ObjectUserWndProc;
  379.         wc.cbClsExtra     = 0;
  380.         wc.cbWndExtra     = CBWNDEXTRA;
  381.         wc.hInstance      = m_hInst;
  382.         wc.hIcon          = LoadIcon(m_hInst, TEXT("Icon"));
  383.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  384.         wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  385.         wc.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU);
  386.         wc.lpszClassName  = TEXT("OBJUSER3");
  387.  
  388.         if (!RegisterClass(&wc))
  389.             return FALSE;
  390.         }
  391.  
  392.     m_hWnd=CreateWindow(TEXT("OBJUSER3")
  393.         , TEXT("Koala Client #3"), WS_OVERLAPPEDWINDOW
  394.         , 35, 35, 350, 250, NULL, NULL, m_hInst, this);
  395.  
  396.     if (NULL==m_hWnd)
  397.         return FALSE;
  398.  
  399.     ShowWindow(m_hWnd, m_nCmdShow);
  400.     UpdateWindow(m_hWnd);
  401.  
  402.     return TRUE;
  403.     }
  404.  
  405.  
  406.  
  407. /*
  408.  * CApp::Message
  409.  *
  410.  * Purpose:
  411.  *  Displays a message in the client area of the window.  This is
  412.  *  just to centralize the call to simpify other code.
  413.  *
  414.  * Parameters:
  415.  *  psz             LPTSTR to the string to display.
  416.  *
  417.  * Return Value:
  418.  *  None
  419.  */
  420.  
  421. void CApp::Message(LPTSTR psz)
  422.     {
  423.     HDC     hDC;
  424.     RECT    rc;
  425.  
  426.     hDC=GetDC(m_hWnd);
  427.     GetClientRect(m_hWnd, &rc);
  428.  
  429.     SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  430.     SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  431.  
  432.     /*
  433.      * We'll just be sloppy and clear the whole window as
  434.      * well as write the string with one ExtTextOut call.
  435.      * No word wrapping here...
  436.      */
  437.  
  438.     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, psz, lstrlen(psz), NULL);
  439.     ReleaseDC(m_hWnd, hDC);
  440.     return;
  441.     }
  442.