home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / COMSRVR.PAK / USECOM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.2 KB  |  279 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents - (c) Copyright 1996, 1996 by Borland International
  3. //
  4. // $Revision:   2.0  $
  5. //----------------------------------------------------------------------------
  6. #if !defined(STRICT)
  7. #define STRICT
  8. #endif
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <stdlib.h>
  12. #include <ole2.h>
  13. #include <initguid.h>
  14. #include "usecom.rh"
  15.  
  16. #if defined(BI_COMP_BORLANDC)
  17. # pragma hdrstop
  18. #endif
  19.  
  20. #include "comintf.h"
  21.  
  22. // Global variables
  23. //
  24. LPCLASSFACTORY  pIClassFactory = 0;     // Pointer to class factory
  25. LPCALENDAR      pICalendar     = 0;     // Pointer to calendar interface
  26. LPSHAPE         pIShape        = 0;     // Pointer to shape interface
  27.  
  28.  
  29. // Frame window's WNDPROC
  30. //
  31. LRESULT CALLBACK _export FrameWndProc(HWND, UINT, WPARAM, LPARAM);
  32.  
  33. // Frame window's class name
  34. //
  35. const char FrameClassName[] = "__fenΩtrePrincipale";
  36. const char FrameTitle[] = "COM Object User";
  37.  
  38.  
  39. // App's entry point
  40. //
  41. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  42.                    LPSTR /*lpCmdLine*/,  int nCmdShow)
  43. {
  44.   // Register main window class
  45.   //
  46.   if (!hPrevInstance) {
  47.     WNDCLASS wc;
  48.     wc.style            = CS_HREDRAW|CS_VREDRAW;
  49.     wc.lpfnWndProc      = FrameWndProc;
  50.     wc.cbClsExtra       = 0;
  51.     wc.cbWndExtra       = 0;
  52.     wc.hInstance        = hInstance;
  53.     wc.hIcon            = LoadIcon(0, IDI_APPLICATION);
  54.     wc.hCursor          = LoadCursor(0, IDC_ARROW);
  55.     wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  56.     wc.lpszMenuName     = 0;
  57.     wc.lpszClassName    = FrameClassName;
  58.     if (!RegisterClass(&wc))
  59.         return -1;
  60.   }
  61.  
  62.   // Create main/frame window
  63.   //
  64.   HWND hwnd;
  65.   hwnd = CreateWindow(FrameClassName,
  66.                       FrameTitle,
  67.                       WS_OVERLAPPEDWINDOW,
  68.                       CW_USEDEFAULT,
  69.                       CW_USEDEFAULT,
  70.                       CW_USEDEFAULT,
  71.                       CW_USEDEFAULT,
  72.                       0,
  73.                       LoadMenu(hInstance, MAKEINTRESOURCE(ID_MAINMENU)),
  74.                       hInstance,
  75.                       0);
  76.  
  77.   ShowWindow(hwnd, nCmdShow);
  78.   UpdateWindow(hwnd);
  79.  
  80.   // Init OLE
  81.   //
  82.   CoInitialize(0);
  83.  
  84.   // Spin message loop
  85.   //
  86.   MSG msg;
  87.   while (GetMessage(&msg, NULL, NULL, NULL)) {
  88.     TranslateMessage(&msg);
  89.     DispatchMessage(&msg);
  90.   }
  91.  
  92.   // Unitialize OLE
  93.   //
  94.   CoUninitialize();
  95.  
  96.   return(msg.wParam);
  97. }
  98.  
  99. // Handles WM_CREATE sent to frame window
  100. //
  101. BOOL
  102. FrameOnCreate(HWND /*hwnd*/, CREATESTRUCT FAR* /*lpCreateStruct*/)
  103. {
  104.   return TRUE;
  105. }
  106.  
  107. // Handles WM_DESTROY sent to frame window
  108. //
  109. void
  110. FrameOnDestroy(HWND /*hwnd*/)
  111. {
  112.   // Release any interface pointers we have have laying around
  113.   //
  114.   if (pICalendar)
  115.     pICalendar->Release();
  116.  
  117.   if (pIShape)
  118.     pIShape->Release();
  119.  
  120.   // Post WM_QUIT to terminate message loop
  121.   //
  122.   PostQuitMessage(0);
  123. }
  124.  
  125.  
  126. // Retrieves class factory interface and asks factory
  127. // to 'manufacture' requested interface..
  128. //
  129. void
  130. CreateFromScatch(GUID id, LPVOID far* pInterface)
  131. {
  132.   // Retrieve class factory interface pointer to our custom COM object
  133.   //
  134.   HRESULT hr = CoGetClassObject(CLSID_UtilObject,
  135.                                 CLSCTX_INPROC_SERVER,
  136.                                 0,
  137.                                 IID_IClassFactory,
  138.                                 (LPVOID far*)&pIClassFactory);
  139.  
  140.   // Have class factory make the object for us - then release factory
  141.   //
  142.   if (SUCCEEDED(hr)) {
  143.     pIClassFactory->CreateInstance(0, id, pInterface);
  144.     pIClassFactory->Release();
  145.     pIClassFactory = 0;
  146.   }
  147. }
  148.  
  149. // Handles WM_COMMAND sent to frame window
  150. //
  151. void
  152. FrameOnCommand(HWND hwnd, int id, HWND /*hwndCtl*/, UINT /*codeNotify*/)
  153. {
  154.   switch(id) {
  155.     //
  156.     // Handle request to terminate
  157.     //
  158.     case CM_FILEEXIT:
  159.       PostMessage(hwnd, WM_CLOSE, 0, 0);
  160.       break;
  161.  
  162.     // Retrieve Shape interface
  163.     //
  164.     case CM_GETSHAPE:
  165.       if (!pIShape) {
  166.         if (pICalendar) {
  167.           pICalendar->QueryInterface(IID_IShape, (LPVOID far*)&pIShape);
  168.         } else {
  169.           CreateFromScatch(IID_IShape, (LPVOID far*)&pIShape);
  170.         }
  171.         InvalidateRect(hwnd, 0, TRUE);
  172.       }
  173.       break;
  174.  
  175.     // Retrieve Calendar interface
  176.     //
  177.     case CM_GETCALENDAR:
  178.       if (!pICalendar) {
  179.         if (pIShape) {
  180.           pIShape->QueryInterface(IID_ICalendar, (LPVOID far*)&pICalendar);
  181.         } else {
  182.           CreateFromScatch(IID_ICalendar, (LPVOID far*)&pICalendar);
  183.         }
  184.         InvalidateRect(hwnd, 0, TRUE);
  185.       }
  186.       break;
  187.  
  188.     // Release any interfaces we've acquired - Freeing unused libraries
  189.     // after this should free the DLL (if no one else is using it).
  190.     //
  191.     case CM_RELEASEINTERFACES:
  192.       if (pICalendar) {
  193.         pICalendar->Release();
  194.         pICalendar = 0;
  195.       }
  196.       if (pIShape) {
  197.         pIShape->Release();
  198.         pIShape = 0;
  199.       }
  200.       InvalidateRect(hwnd, 0, TRUE);
  201.       break;
  202.  
  203.     // Handle request to free unused libraries. This is a good debugging
  204.     // option as it tests to make sure that our custom COM DLL stays in
  205.     // memory for as long as it is serving an object.
  206.     //
  207.     // NOTE: Retrieving a class factory interface does *NOT* lock down
  208.     //       an inproc server - The LockServer method must be called if the
  209.     //       factory interface is kept but no object is created.
  210.     //
  211.     case CM_FREEUNUSEDLIBRARIES:
  212.       CoFreeUnusedLibraries();
  213.       break;
  214.   }
  215. }
  216.  
  217. // Handles WM_PAINT sent to frame window
  218. //
  219. void
  220. FrameOnPaint(HWND hwnd)
  221. {
  222.   PAINTSTRUCT ps;
  223.   HDC dc = BeginPaint(hwnd, &ps);
  224.  
  225.   RECT rect;
  226.   GetClientRect(hwnd, &rect);
  227.  
  228.   // Use IShape interface pointer to draw a few shapes
  229.   //
  230.   if (pIShape) {
  231.     int width  = rect.right - rect.left;
  232.     int height = rect.bottom- rect.top;
  233.  
  234.     for (int i=0; i<10; i++) {
  235.       pIShape->Rectangle(dc, random(width/2), random(height/2),
  236.                              random(width/2)+(width/2),
  237.                              random(height/2)+(height/2),
  238.                              RGB(random(0xff), random(0xff), random(0x0ff)));
  239.     }
  240.   }
  241.  
  242.   EndPaint(hwnd, &ps);
  243. }
  244.  
  245. // Handles WM_INITMENU sent to frame window
  246. //
  247. void
  248. FrameOnInitMenu(HWND /*hwnd*/, HMENU hMenu)
  249. {
  250.   EnableMenuItem(hMenu, CM_GETSHAPE,
  251.                  MF_BYCOMMAND|((pIShape) ? MF_GRAYED : MF_ENABLED));
  252.   EnableMenuItem(hMenu, CM_GETCALENDAR,
  253.                  MF_BYCOMMAND|((pICalendar) ? MF_GRAYED : MF_ENABLED));
  254.   EnableMenuItem(hMenu, CM_RELEASEINTERFACES,
  255.                  MF_BYCOMMAND|
  256.                  ((pICalendar || pIShape) ? MF_ENABLED : MF_GRAYED));
  257. }
  258.  
  259. // Frame Window's Callback
  260. //
  261. LRESULT CALLBACK _export FrameWndProc(HWND hwnd, UINT msg,
  262.                                       WPARAM wParam, LPARAM lParam)
  263. {
  264.   switch (msg) {
  265.     HANDLE_MSG(hwnd, WM_CREATE,   FrameOnCreate);
  266.     HANDLE_MSG(hwnd, WM_COMMAND,  FrameOnCommand);
  267.     HANDLE_MSG(hwnd, WM_PAINT,    FrameOnPaint);
  268.     HANDLE_MSG(hwnd, WM_DESTROY,  FrameOnDestroy);
  269.     HANDLE_MSG(hwnd, WM_INITMENU, FrameOnInitMenu);
  270.     default:
  271.       break;
  272.   }
  273.   return DefWindowProc(hwnd, msg, wParam, lParam);
  274. }
  275.  
  276.  
  277.  
  278.  
  279.