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

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents - (c) Copyright 1994, 1996 by Borland International
  3. //
  4. // $Revision:   2.1  $
  5. //----------------------------------------------------------------------------
  6. #include <ocf/ocfpch.h>
  7. #if !defined(OCF_OCREG_H)
  8. # include <ocf/ocreg.h>
  9. #endif
  10. #if !defined(OCF_OLEUTIL_H)
  11. # include <ocf/oleutil.h>
  12. #endif
  13. #include <windowsx.h>
  14. #include <initguid.h>
  15.  
  16. #if defined(BI_COMP_BORLANDC)
  17. # pragma hdrstop
  18. #endif
  19.  
  20. #include "comintf.h"
  21.  
  22. // Registrar pointer to handle registration chores
  23. //
  24. TPointer<TRegistrar>    Registrar;
  25.  
  26. // String to hold command line
  27. //
  28. TPointer<string>        CmdLine;
  29.  
  30. // Class Object registration information
  31. //
  32. BEGIN_REGISTRATION(ObjectReg)
  33.   REGDATA(clsid,       "{6034C8E0-C35B-101B-AFFC-00608C19FAA0}" )
  34.   REGDATA(description, "Sample OCF Object with COM Interfaces")
  35.   REGDATA(progid,      "CUSTOM.OBJECT.1")
  36. END_REGISTRATION
  37.  
  38. // Declare new class derived from interface with TUnknown mixin
  39. //
  40. DECLARE_COMBASES2(TUtilObjectCom, IShape, ICalendar);
  41.  
  42. // Declare class derived from mixin which implements interface members
  43. //
  44. class _ICLASS TUtilObject : public TUtilObjectCom {
  45.  
  46.   public:
  47.     TUtilObject();
  48.    ~TUtilObject();
  49.  
  50.     // { C++ methods implementing interfaces }
  51.  
  52.     // IShape methods
  53.     //
  54.     BOOL _IFUNC Rectangle(HDC, int left, int top,
  55.                           int right, int bottom,
  56.                           COLORREF fill);
  57.     BOOL _IFUNC  Ellipse(HDC, int left, int top, int right, int bottom,
  58.                          COLORREF fill);
  59.  
  60.     // ICalendar methods
  61.     //
  62.     int _IFUNC   GetDayOf(int year, int month, int date);
  63. };
  64.  
  65. // Declare ClsName##QueryInterface inline
  66. //
  67. DEFINE_QI_BASE(IShape,    IID_IShape.Data1);
  68. DEFINE_QI_BASE(ICalendar, IID_ICalendar.Data1);
  69.  
  70.  
  71. // Define class which inherits from our interfaces and
  72. // TUnknown as a mixin class.
  73. //
  74. DEFINE_COMBASES2(TUtilObjectCom, IShape, ICalendar);
  75.  
  76. // C++ Object constructor
  77. //
  78. TUtilObject::TUtilObject()
  79. {
  80.   // Increment ref. count of DLL since we're serving an Object
  81.   //
  82.   Registrar->GetAppDescriptor().LockServer(TRUE);
  83. }
  84.  
  85. // C++ Object destructor
  86. //
  87. TUtilObject::~TUtilObject()
  88. {
  89.   // Decrement ref. count of DLL
  90.   //
  91.   Registrar->GetAppDescriptor().LockServer(FALSE);
  92. }
  93.  
  94. //
  95. //
  96. //
  97. BOOL _IFUNC
  98. TUtilObject::Rectangle(HDC dc, int left, int top, int right, int bottom,
  99.                        COLORREF fill)
  100. {
  101.   HBRUSH oldBrush = SelectBrush(dc, CreateSolidBrush(fill));
  102.   BOOL result = ::Rectangle(dc, left, top, right, bottom);
  103.   DeleteBrush(SelectBrush(dc, oldBrush));
  104.   return result;
  105. }
  106.  
  107. //
  108. //
  109. //
  110. BOOL _IFUNC
  111. TUtilObject::Ellipse(HDC dc, int left, int top, int right, int bottom,
  112.                      COLORREF fill)
  113. {
  114.   HBRUSH oldBrush = SelectBrush(dc, CreateSolidBrush(fill));
  115.   BOOL result = ::Ellipse(dc, left, top, right, bottom);
  116.   DeleteBrush(SelectBrush(dc, oldBrush));
  117.  
  118.   return result;
  119. }
  120.  
  121. //
  122. // ICalendar methods implemented
  123. //
  124.  
  125. //
  126. // The following function determines the day of a specified date.
  127. // The parameters passed are the month, date and year (in 4 digits -
  128. // eg. 1989 instead of 89).  The return value indicates the day
  129. // the of the week with:
  130. //     Sunday=0, Monday=1 etc. etc.
  131. //
  132. int _IFUNC
  133. TUtilObject::GetDayOf(int year, int month, int date)
  134. {
  135.   int century, day;
  136.  
  137.   century = year/100;
  138.   year    = year%100;
  139.   if (month < 3) {
  140.     month += 12;
  141.     year > 0 ? year-- : (year = 99, century--);
  142.   }
  143.   day = date;
  144.   day = day + (((month+1) * 26) / 10);
  145.   day = day + year;
  146.   day = day + (year / 4);
  147.   day = day + (century / 4);
  148.   day = day - century - century;
  149.   day = day % 7;
  150.   if (day == 0)
  151.       day = 7;
  152.   return --day;
  153. }
  154.  
  155. #if defined(BI_PLAT_WIN16)
  156. //
  157. // DLL's entry point
  158. //
  159. int FAR PASCAL
  160. LibMain(HINSTANCE, WORD, WORD wHeapSize, LPSTR lpszCmdLine)
  161. {
  162.     if (wHeapSize != 0)
  163.         UnlockData(0);
  164.  
  165. #elif defined(BI_PLAT_WIN32)
  166.  
  167. int WINAPI
  168. DllEntryPoint(HINSTANCE hInstance, uint32 reason, LPVOID)
  169. {
  170.   if (reason != DLL_PROCESS_ATTACH)
  171.     return 1;
  172.  
  173.   LPSTR lpszCmdLine = 0;
  174. #endif
  175.  
  176.   try {
  177.     CmdLine   = new string(lpszCmdLine);
  178.     Registrar = new TRegistrar(::ObjectReg, TOcComFactory<TUtilObject>(),
  179.                               *::CmdLine);
  180.   }
  181.   catch(xmsg &msg) {
  182.     MessageBox(0, msg.why().c_str(), "EXCEPTION",
  183.                MB_OK|MB_TASKMODAL);
  184.     return 0;
  185.   }
  186.   return 1;
  187. }
  188.  
  189. #if defined(BI_PLAT_WIN16)
  190.  
  191. //
  192. // Exit routine of DLL
  193. //
  194. int FAR PASCAL WEP(int bSystemExit)
  195. {
  196.   return 1;
  197. }
  198.  
  199. #endif
  200.  
  201.  
  202.