home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / freethrd / server / apputil.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  33.8 KB  |  1,062 lines

  1. /*+==========================================================================
  2.   File:      APPUTIL.H
  3.  
  4.   Summary:   Include file for the general application utility classes
  5.              and functions offered by the APPUTIL library.  APPUTIL is
  6.              meant to be statically linked to applications that want
  7.              to exploit it.
  8.  
  9.              For a comprehensive tutorial code tour of APPUTIL's
  10.              contents and offerings see the accompanying APPUTIL.TXT file.
  11.              For more specific details on the internal workings see the
  12.              comments dispersed throughout the APPUTIL source code.
  13.  
  14.   Classes:   CVirWindow, CVirDialog, CAboutBox, CMsgBox, CMsgLog,
  15.              CThreaded.
  16.  
  17.   Functions: WindowProc, DialogProc, FileExist, MakeFamilyPath, CmdExec,
  18.              ReadMe, ReadMeFile, ReadSource, OutputDebugFmt, lRandom,
  19.              UcToAnsi, A_StringFromGUID2, A_WriteFmtUserTypeStg,
  20.              A_StgIsStorageFile, A_StgCreateDocfile, A_StgOpenStorage,
  21.              CreateColorScalePalette, PaintWindow.
  22.  
  23.   Origin:    7-27-95: atrent - Created based on WINHLPRS by stevebl.
  24.  
  25. ----------------------------------------------------------------------------
  26.   This file is part of the Microsoft OLE Tutorial Code Samples.
  27.  
  28.   Copyright (C) Microsoft Corporation, 1996.  All rights reserved.
  29.  
  30.   This source code is intended only as a supplement to Microsoft
  31.   Development Tools and/or on-line documentation.  See these other
  32.   materials for detailed information regarding Microsoft code samples.
  33.  
  34.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  35.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  36.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  37.   PARTICULAR PURPOSE.
  38. ==========================================================================+*/
  39.  
  40. // Don't allow recursive includes of this file.
  41. #ifndef APPUTIL_H
  42. #define APPUTIL_H
  43.  
  44.  
  45. // This pragma disables the C4355 warning which reads:
  46. //
  47. // "'this': used in base member initializer list."
  48. //
  49. // This warning is triggered by the nested interface implementation
  50. // classes--a coding technique used throughout the sample series.
  51. // The warning says: "The 'this' pointer is only valid within nonstatic
  52. // member functions, but was used in the initializer list for a base class.
  53. // This is a level 1 warning when Microsoft extensions are enabled (/Ze)
  54. // but a level 4 warning otherwise."  The initializer technique is
  55. // necessary for the coding of nested class interface implementations.
  56. // See COMOBJ.TXT for details on the technique.
  57. #pragma warning( disable : 4355 )
  58.  
  59.  
  60. // Types that should be in OLE2.H.
  61. #ifndef PPVOID
  62. typedef LPVOID* PPVOID;
  63. #endif
  64.  
  65. // Convenient macros.
  66.  
  67. #define DELETE_POINTER(p)\
  68. {\
  69.   if (NULL != p)\
  70.   {\
  71.     delete p;\
  72.     p = NULL;\
  73.   }\
  74. }
  75.  
  76. #define RELEASE_INTERFACE(p)\
  77. {\
  78.   IUnknown* pTmp = (IUnknown*)p;\
  79.   p = NULL;\
  80.   if (NULL != pTmp)\
  81.     pTmp->Release();\
  82. }
  83.  
  84. #define GETINSTANCE(hWnd)   (HANDLE)GetWindowLong(hWnd,GWL_HINSTANCE)
  85. #define GETCLASSBRUSH(hWnd) (HBRUSH)GetClassLong(hWnd,GCL_HBRBACKGROUND)
  86.  
  87.  
  88. // Some General defines for Message Boxes and Dialogs
  89. #define GUID_SIZE 128
  90. #define MAX_STRING_LENGTH 256
  91. #define MAXLOGST 128
  92. #define MAX_LOG_LINES 500
  93.  
  94. #define COLOR_SCALE_RED     1
  95. #define COLOR_SCALE_GREEN   2
  96. #define COLOR_SCALE_BLUE    3
  97. #define COLOR_SCALE_GRAY    4
  98.  
  99. #define EDITOR_FILE_STR "notepad.exe "
  100. #define OFN_SOURCEFILES_STR "Source Files (*.CPP;*.H;*.RC;*.C;*.MAK;*.TXT;*.)\0*.CPP;*.H;*.RC;*.C;*.MAK;*.TXT;makefile\0"
  101. #define OFN_SOURCETITLE_STR "Open Source File"
  102. #define NOTICE_TITLE_STR "-Notice-"
  103. #define ERROR_TITLE_STR "-Error-"
  104. #define NOEDITOR_ERROR_STR "Can't run editor."
  105. #define NOREADME_ERROR_STR "Can't find .TXT file."
  106. #define ASSERTFAIL_ERROR_STR "Assertion Failed."
  107. #define HELP_FILE_EXT    ".hlp"
  108. #define README_FILE_EXT  ".txt"
  109. #define LICENSE_FILE_EXT ".lic"
  110.  
  111.  
  112. #ifdef __cplusplus
  113. // If compiling under C++ Ensure that Windows callbacks are
  114. // undecorated extern C calls.
  115. extern "C" {
  116. #endif
  117.  
  118.  
  119. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  120.   Function: WindowProc
  121.  
  122.   Summary:  Standard WindowProc callback function that forwards Windows
  123.             messages on to the CVirWindow::WindowProc method.  This
  124.             Window procedure expects that it will receive a "this"
  125.             pointer as the lpCreateParams member passed as part of the
  126.             WM_NCCREATE message.  It saves the "this" pointer in the
  127.             GWL_USERDATA field of the window structure.
  128.  
  129.   Args:     HWND hWnd,
  130.               Window handle
  131.             UINT uMsg,
  132.               Windows message
  133.             WPARAM wParam,
  134.               First message parameter (word sized)
  135.             LPARAM lParam);
  136.               Second message parameter (long sized)
  137.  
  138.   Returns:  LRESULT.  Windows window procedure callback return value.
  139. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  140. LRESULT CALLBACK WindowProc(
  141.                    HWND hWnd,
  142.                    UINT uMsg,
  143.                    WPARAM wParam,
  144.                    LPARAM lParam);
  145.  
  146.  
  147. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  148.   Function: DialogProc
  149.  
  150.   Summary:  The general dialog procedure callback function.  Used by all
  151.             CVirDialog class objects.  This procedure is the DialogProc
  152.             registered for all dialogs created with the CVirDialog class.
  153.             It uses the parameter passed with the WM_INITDIALOG message
  154.             to identify the dialog classes' "this" pointer which it then
  155.             stores in the window structure's GWL_USERDATA field.
  156.             All subsequent messages can then be forwarded on to the
  157.             correct dialog class's DialogProc method by using the pointer
  158.             stored in the GWL_USERDATA field.
  159.  
  160.   Args:     HWND hWndDlg,
  161.               Handle of dialog box.
  162.             UINT uMsg,
  163.               Message
  164.             WPARAM wParam,
  165.               First message parameter (word sized).
  166.             LPARAM lParam);
  167.               Second message parameter (long sized).
  168.  
  169.   Returns:  BOOL.  Return of the CVirDialog::DialogProc method.
  170. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  171. BOOL CALLBACK DialogProc(
  172.                 HWND hWndDlg,
  173.                 UINT uMsg,
  174.                 WPARAM wParam,
  175.                 LPARAM lParam);
  176.  
  177.  
  178. #ifdef __cplusplus
  179. }
  180.  
  181.  
  182. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  183.   Class:    CVirWindow
  184.  
  185.   Summary:  Abstract base class for wrapping a window.
  186.  
  187.             This class allows a window to be cleanly wrapped in a
  188.             c++ class.  Specifically, it provides a way for a c++ class
  189.             to use one of its methods as a WindowProc, giving it a "this"
  190.             pointer and allowing it to have direct access to all of its
  191.             private members.
  192.  
  193.   Methods:  Create:
  194.               Maps to Windows' CreateWindow function.
  195.             WindowProc:
  196.               Pure virtual WindowProc for the window.
  197.             Gethwnd:
  198.               Get the handle to the window.
  199.             CVirWindow:
  200.               Constructor.
  201.             ~CVirWindow:
  202.               Destructor.
  203. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  204. class CVirWindow
  205. {
  206. public:
  207.   // Constructor
  208.   CVirWindow()
  209.   {
  210.     m_hWnd = NULL;
  211.     m_hInst = NULL;
  212.   };
  213.  
  214.   // Destructor
  215.   virtual ~CVirWindow(){};
  216.  
  217.   // Envelopes the Windows' CreateWindow function call.
  218.   HWND Create(
  219.          LPTSTR lpszClassName,   // Address of registered class name
  220.          LPTSTR lpszWindowName,  // Address of window name
  221.          DWORD dwStyle,          // Window style
  222.          int x,                  // Horizontal position of window
  223.          int y,                  // Vertical position of window
  224.          int nWidth,             // Window width
  225.          int nHeight,            // Window height
  226.          HWND hWndParent,        // Handle of parent or owner window
  227.          HMENU hmenu,            // Handle of menu, or child window identifier
  228.          HINSTANCE hinst);       // Handle of application instance
  229.  
  230.   // Get the protected handle of this window.
  231.   HWND GetHwnd(void)
  232.   {
  233.     return(m_hWnd);
  234.   }
  235.  
  236.   // WindowProc is a pure virtual member function and MUST be over-ridden
  237.   // in any derived classes.
  238.   virtual LRESULT WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;
  239.  
  240. protected:
  241.   // Instance handle for this window.
  242.   HINSTANCE m_hInst;
  243.   // Handle of this window.
  244.   HWND m_hWnd;
  245.  
  246.   // Tell the compiler that the outside WindowProc callback is a friend
  247.   // of this class and can get at its protected data members.
  248.   friend LRESULT CALLBACK ::WindowProc(
  249.                               HWND hWnd,
  250.                               UINT uMsg,
  251.                               WPARAM wParam,
  252.                               LPARAM lParam);
  253. };
  254.  
  255.  
  256. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  257.   Class:    CVirDialog
  258.  
  259.   Summary:  Abstract base class for wrapping a Windows dialog box window.
  260.  
  261.             This class allows a dialog box to be cleanly wrapped in
  262.             a c++ class.  Specifically, it provides a way for a c++ class
  263.             to use one of its methods as a DialogProc, giving it a "this"
  264.             pointer and allowing it to have direct access to all of its
  265.             private members.
  266.  
  267.   Methods:  ShowDialog:
  268.               Maps to Windows' DialogBox function.
  269.             GetHwnd:
  270.               Get the handle to the dialog window.
  271.             DialogProc:
  272.               Pure virtual DialogProc for the dialog box
  273.             ~CVirDialog:
  274.               Destructor
  275. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  276. class CVirDialog
  277. {
  278. public:
  279.   // Destructor
  280.   virtual ~CVirDialog(){};
  281.  
  282.   // ShowDialog creates the Dialog (using the DialogBoxParam API).
  283.   virtual int ShowDialog(
  284.                 HINSTANCE hInst,
  285.                 LPTSTR lpszTemplate,
  286.                 HWND hWndOwner);
  287.  
  288.   // DialogProc is a pure virtual member function and MUST be over-ridden
  289.   // in any derived classes.
  290.   virtual BOOL DialogProc(
  291.                  HWND hWndDlg,
  292.                  UINT uMsg,
  293.                  WPARAM wParam,
  294.                  LPARAM lParam) = 0;
  295.  
  296. protected:
  297.   HINSTANCE m_hInst;
  298.   HWND m_hWnd;
  299.  
  300.   // Tell the compiler that the outside DialogProc callback is a friend
  301.   // of this class and can get at its protected data members.
  302.   friend BOOL CALLBACK ::DialogProc(
  303.                            HWND hWndDlg,
  304.                            UINT uMsg,
  305.                            WPARAM wParam,
  306.                            LPARAM lParam);
  307. };
  308.  
  309.  
  310. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  311.   Class:    CAboutBox
  312.  
  313.   Summary:  A class that implements a common About Dialog Box
  314.             functionality for the APPUTIL library.
  315.  
  316.   Methods:  DialogProc
  317.               Dialog procedure
  318. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  319. class CAboutBox: public CVirDialog
  320. {
  321. public:
  322.   // This DialogProc method definition overrides the same-named pure
  323.   // virtual function in abstract base class CVirDialog thus giving
  324.   // AboutBox-unique message dispatching behavior to this derivation
  325.   // of CVirDialog.  The remaining behavior of CAboutBox is inherited
  326.   // from CVirDialog and is common to all CVirDialogs.
  327.   BOOL DialogProc(
  328.          HWND hWndDlg,
  329.          UINT uMsg,
  330.          WPARAM wParam,
  331.          LPARAM lParam);
  332. };
  333.  
  334.  
  335. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  336.   Class:    CMsgBox
  337.  
  338.   Summary:  Class for showing messages in a Message Box dialog.  Uses the
  339.             standard Windows MessageBox function but retrieves the box
  340.             title and message strings from the application's resources
  341.             using specified resource IDs.  These message boxes have only
  342.             an OK button.
  343.  
  344.   Methods:  Init
  345.               Initializes the owner window and the module instance.
  346.             Error
  347.               Shows a message string in an Error MessageBox Dialog.
  348.             ErrorID
  349.               Shows a resource message string in an Error MessageBox Dialog.
  350.             Note
  351.               Shows a message string in a Notice MessageBox Dialog.
  352.             NoteID
  353.               Shows a resource message string in a Notice MessageBox Dialog.
  354.             NoteFmt
  355.               Shows a printf-style formatted message string in a Notice
  356.               MessageBox Dialog.
  357.             NoteFmtID
  358.               Shows a printf-style formatted resource message string in a
  359.               Notice MessageBox Dialog.
  360. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  361. class CMsgBox
  362. {
  363. public:
  364.   // Constructor.
  365.   CMsgBox()
  366.   {
  367.     m_hInst = 0;
  368.     m_hWndOwner = 0;
  369.   };
  370.  
  371.   BOOL Init(HINSTANCE hInst, HWND hWndOwner);
  372.   int  Error(LPTSTR szMsg);
  373.   int  ErrorID(UINT uMsgID);
  374.   int  Note(LPTSTR szMsg);
  375.   int  NoteID(UINT uMsgID);
  376.   int  NoteFmt(LPTSTR szFmtMsg, ...);
  377.   int  NoteFmtID(UINT uMsgID, ...);
  378.  
  379. private:
  380.   // Remember the App Instance Handle.
  381.   HINSTANCE m_hInst;
  382.   // Remember the Owner Window Handle.
  383.   HWND m_hWndOwner;
  384. };
  385.  
  386.  
  387. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  388.   Class:    CMsgLog
  389.  
  390.   Summary:  Class for logging messages to a Listbox.  This is for tutorial
  391.             code samples that employ debug messages to announce internal
  392.             activity in the code being studied.  When used, this Message
  393.             Log listbox occupies the entire client area of the parent
  394.             window.  The various message output functions use string
  395.             resource IDs to retrieve the message strings from the app's
  396.             resources.
  397.  
  398.   Methods:  Create
  399.               Creates the Trace Message Log ListBox window.
  400.             Logging
  401.               Toggles logging on or off.
  402.             Msg
  403.               Logs a message as a separate line.
  404.             MsgFmt
  405.               Logs a printf-style formated message as a separate line.
  406.             Resize
  407.               Resizes the listbox to a new width and height.
  408.             Clear
  409.               Clears all logged messages from the message log.
  410.             CMsgLog
  411.               Constructor.
  412.             ~CMsgLog
  413.               Destructor.
  414. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  415. class CMsgLog
  416. {
  417. public:
  418.   // Constructor.
  419.   CMsgLog()
  420.   {
  421.     m_hInstLog = 0;
  422.     m_hWndLog = 0;
  423.     m_bChild = FALSE;
  424.     m_bLogging = FALSE;
  425.     m_hLogData = 0;
  426.   };
  427.  
  428.   BOOL Create(HINSTANCE hInst, HWND hWndParent, BOOL bChild);
  429.   BOOL Logging(BOOL bLogging);
  430.   BOOL Msg(LPTSTR szMsg);
  431.   BOOL MsgFmt(LPTSTR szFmtMsg, ...);
  432.   BOOL MsgID(int iMsgID);
  433.   BOOL MsgFmtID(int iFmtID, ...);
  434.   BOOL Resize(int nWidth, int nHeight);
  435.   BOOL Clear(void);
  436.   BOOL Copy(void);
  437.  
  438. private:
  439.   // Remember the App Instance Handle.
  440.   HINSTANCE m_hInstLog;
  441.   // Remember the handle of the listbox window.
  442.   HWND m_hWndLog;
  443.   // Remember if CMsgLog was created as child window.
  444.   BOOL m_bChild;
  445.   // Remember whether logging or not.
  446.   BOOL m_bLogging;
  447.   // Remember the global handle to clipboard data.
  448.   HGLOBAL m_hLogData;
  449. };
  450.  
  451.  
  452. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  453.   Class:    CSendLog
  454.  
  455.   Summary:  Class for logging messages to a CMsgLog facility in another
  456.             application process. This is for tutorial code samples that
  457.             employ debug messages to announce internal activity in the
  458.             code being studied.  Uses the WM_COPYDATA message of the
  459.             Win32 SendMessage API.
  460.  
  461.   Methods:  CreateServerLog
  462.               Initializes the SendLog facility and creates the Trace
  463.               Message Log ListBox window shown in the local server app.
  464.             SetClient
  465.               Sets destination client window handle.
  466.             LogToServer
  467.               Set whether logging is to local server or to client.
  468.             Msg
  469.               Logs a message as a separate line.
  470.             MsgFmt
  471.               Logs a printf-style formated message as a separate line.
  472.             Resize
  473.               Resizes the listbox to a new width and height.
  474.             Clear
  475.               Clears all logged messages from the message log.
  476.             CSendLog
  477.               Constructor.
  478.             ~CSendLog
  479.               Destructor.
  480. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  481. class CSendLog
  482. {
  483. public:
  484.   // Constructor.
  485.   CSendLog()
  486.   {
  487.     m_hInstSender = 0;
  488.     m_hWndSender = 0;
  489.     m_hWndReceiver = 0;
  490.     m_hWndListBox = 0;
  491.     m_bLogToServer = FALSE;
  492.     m_bChild = FALSE;
  493.     m_hLogData = 0;
  494.   };
  495.  
  496.   BOOL CreateServerLog(HINSTANCE hInst, HWND hWndParent, BOOL bChild);
  497.   BOOL SetClient(HINSTANCE hInstSender, HWND hWndSender, HWND hWndReceiver);
  498.   BOOL LogToServer(BOOL bLogToServer);
  499.   BOOL Msg(LPTSTR szMsg);
  500.   BOOL MsgFmt(LPTSTR szFmtMsg, ...);
  501.   BOOL MsgID(int iMsgID);
  502.   BOOL MsgFmtID(int iFmtID, ...);
  503.   BOOL Resize(int nWidth, int nHeight);
  504.   BOOL Clear(void);
  505.   BOOL Copy(void);
  506.  
  507. private:
  508.   // Remember the Sender App Instance Handle.
  509.   HINSTANCE m_hInstSender;
  510.   // Remember the handle of the sending application main window.
  511.   HWND m_hWndSender;
  512.   // Remember the handle of the destination receiving window.
  513.   HWND m_hWndReceiver;
  514.   // Remember the handle of the server's listbox window.
  515.   HWND m_hWndListBox;
  516.   // Boolean for logging to client or logging to server.
  517.   BOOL m_bLogToServer;
  518.   // Remember if CMsgLog was created as child window.
  519.   BOOL m_bChild;
  520.   // Remember the global handle to clipboard data.
  521.   HGLOBAL m_hLogData;
  522. };
  523.  
  524.  
  525. /*C+C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C+++C
  526.   Class:    CThreaded
  527.  
  528.   Summary:  Derive a class from this class to provide multi-threaded
  529.             access to objects of the derived class. This is a utility
  530.             base class providing useful methods for achieving mutually
  531.             exclusive access to data in objects of the derived class.
  532.  
  533.   Methods:  OwnThis
  534.               Blocks executing thread until access to this object is
  535.               permitted at which point the thread "owns" this object.
  536.             UnOwnThis
  537.               Executed by owning thread to relinquish ownership of
  538.               this object.
  539.             CThreaded
  540.               Constructor. Creates mutex to govern mutually exclusive
  541.               access to data in this object by multiple threads.
  542.             ~CThreaded
  543.               Destructor. Destroys mutex.
  544. C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C---C-C*/
  545. class CThreaded
  546. {
  547.   protected:
  548.     // Variables for providing mutually exclusive access by multiple
  549.     // threads to objects of classes derived from this class.
  550.     HANDLE m_hOwnerMutex;
  551.     BOOL   m_bOwned;
  552.  
  553.   public:
  554.     // Methods.
  555.     CThreaded(void) :
  556.       m_hOwnerMutex(CreateMutex(0,FALSE,0)),
  557.       m_bOwned(FALSE)
  558.     {
  559.     };
  560.  
  561.     ~CThreaded(void)
  562.     {
  563.       // Close down the mutex.
  564.       CloseHandle(m_hOwnerMutex);
  565.     };
  566.  
  567.     // The following virtual functions have overriding definitions in
  568.     // the derived class to provide convenient trace logging. Rely on
  569.     // the below default definitions for non-tutorial applications.
  570.  
  571.     virtual BOOL OwnThis(void)
  572.     {
  573.       BOOL bOwned = FALSE;
  574.  
  575.       if (WAIT_OBJECT_0 == WaitForSingleObject(m_hOwnerMutex, INFINITE))
  576.         m_bOwned = bOwned = TRUE;
  577.  
  578.       return bOwned;
  579.     };
  580.  
  581.     virtual void UnOwnThis(void)
  582.     {
  583.       if (m_bOwned)
  584.       {
  585.         m_bOwned = FALSE;
  586.         ReleaseMutex(m_hOwnerMutex);
  587.       }
  588.  
  589.       return;
  590.     };
  591. };
  592.  
  593.  
  594. #endif //__cplusplus
  595.  
  596.  
  597. #if !defined(UNICODE)
  598.  
  599. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  600.   Function: UcToAnsi
  601.  
  602.   Summary:  Convert an UNICODE input string to an output ANSI string for
  603.             specified number of characters.
  604.  
  605.   Args:     LPWSTR pszUnicodeString
  606.               Pointer to an input wide UNICODE string.
  607.             LPSTR pszAnsiString
  608.               Pointer to output ANSI string.
  609.             int cch
  610.               Character count. If cch == 0 then all input characters are
  611.               converted.
  612.  
  613.   Returns:  HRESULT
  614. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  615. HRESULT UcToAnsi(
  616.           LPWSTR pszUc,
  617.           LPSTR pszAnsi,
  618.           int cch);
  619.  
  620.  
  621. /*---------------------------------------------------------------------------
  622.   Here are some surrogate API macro prototypes--all names begin with A_.
  623.   These are for use when the code sample is being compiled for ANSI
  624.   strings (the default) and the calls they stand in place of only support
  625.   UNICODE (thus forcing some string conversions from ANSI to UNICODE
  626.   before the real call is made inside the surrogate). For example, if
  627.   UNICODE is NOT defined during the compile then any calls in the samples
  628.   to the OLE StringFromGUID2 function are actually changed (by the below
  629.   macros) into calls to an A_StringFromGUID2 function implemented in
  630.   APPUTIL. This function accepts the ANSI input string pointer, calls the
  631.   actual OLE StringFromGUID2 call, and does the necessary Unicode to ANSI
  632.   conversion. Other A_ calls will convert input ANSI strings to Unicode
  633.   strings prior to calling the actual OLE function.
  634. ---------------------------------------------------------------------------*/
  635.  
  636. STDAPI A_StringFromGUID2(REFGUID, LPSTR, int);
  637.  
  638. STDAPI A_WriteFmtUserTypeStg(IStorage*, CLIPFORMAT, LPSTR);
  639.  
  640. STDAPI A_StgIsStorageFile(LPSTR);
  641. STDAPI A_StgCreateDocfile(LPSTR, DWORD, DWORD, IStorage**);
  642. STDAPI A_StgOpenStorage(LPSTR, IStorage*, DWORD, SNB, DWORD, IStorage**);
  643.  
  644. #if !defined(_NOANSIMACROS_)
  645.  
  646. #undef StringFromGUID2
  647. #define StringFromGUID2(a, b, c) A_StringFromGUID2(a, b, c)
  648.  
  649. #undef WriteFmtUserTypeStg
  650. #define WriteFmtUserTypeStg(a, b, c) A_WriteFmtUserTypeStg(a, b, c)
  651.  
  652. #undef StgIsStorageFile
  653. #define StgIsStorageFile(a) A_StgIsStorageFile(a)
  654.  
  655. #undef StgCreateDocfile
  656. #define StgCreateDocfile(a, b, c, d) A_StgCreateDocfile(a, b, c, d)
  657.  
  658. #undef StgOpenStorage
  659. #define StgOpenStorage(a, b, c, d, e, f) A_StgOpenStorage(a, b, c, d, e, f)
  660.  
  661. #endif
  662.  
  663. #endif
  664.  
  665.  
  666. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  667.   Function: lRandom
  668.  
  669.   Summary:  Simple random number generator. Returns a random DWORD.
  670.  
  671.   Args:     void
  672.  
  673.   Returns:  DWORD
  674.               Random number.
  675. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  676. DWORD lRandom(void);
  677.  
  678.  
  679. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  680.   Function: CreateColorScalePalette
  681.  
  682.   Summary:  This routine creates a palette representing the scale values
  683.             of a particular RGB color.  A gray-scale palette can also be
  684.             created.
  685.  
  686.   Args:     HDC hDC,
  687.               Handle to device context.
  688.             int nColor
  689.               New color.
  690.  
  691.   Returns:  HPALETTE
  692.               Handle to new pallete.
  693. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  694. HPALETTE CreateColorScalePalette(HWND hDC, int nColor);
  695.  
  696.  
  697. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  698.   Function: PaintWindow
  699.  
  700.   Summary:  This routine is used to wash the background of a window.
  701.  
  702.   Args:     HWND hWnd,
  703.               Window handle.
  704.             int nColor
  705.               New window color.
  706.  
  707.   Returns:  void.
  708. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  709. VOID PaintWindow(HWND hWnd, int nColor);
  710.  
  711.  
  712. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  713.   Function: FileExist
  714.  
  715.   Summary:  Function to test for the existance of a file.
  716.  
  717.   Args:     TCHAR* szFileName
  718.               String pointer to file's path/name.
  719.  
  720.   Returns:  BOOL.  TRUE if file exists; FALSE if not.
  721. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  722. BOOL FileExist(TCHAR *szFileName);
  723.  
  724.  
  725. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  726.   Function: MakeFamilyPath
  727.  
  728.   Summary:  Function to make a family file/path/name string using the
  729.             detetected pathname of the current executable module.
  730.  
  731.   Args:     HINSTANCE hInst
  732.               Handle to the module intstance.
  733.             TCHAR* pszNewPath
  734.               String pointer to the new path/name.
  735.             TCHAR* pszFileExt
  736.               String pointer to the filename extension for the new path.
  737.  
  738.   Returns:  BOOL.  TRUE if success; FALSE if not.
  739. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  740. BOOL MakeFamilyPath(
  741.        HINSTANCE hInst,
  742.        TCHAR* pszNewPath,
  743.        TCHAR* pszFileExt);
  744.  
  745.  
  746. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  747.   Function: CmdExec
  748.  
  749.   Summary:  Execute an EXE Win32 program by creating a process and
  750.             running the specified EXE in it.  A Unicode version of
  751.             WinExec that always has SW_SHOW.
  752.  
  753.   Args:     LPTSTR szCmd,
  754.               Entire command line (eg, "notepad.exe mytext.txt")
  755.  
  756.   Returns:  BOOL
  757.               TRUE if succeed; FALSE if fail.
  758. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  759. BOOL CmdExec(
  760.        LPTSTR szCmd);
  761.  
  762.  
  763. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  764.   Function: ReadMe
  765.  
  766.   Summary:  For use by code sample applications that study themselves as
  767.             it were.  This function assumes a "family" named <sample>.TXT
  768.             "readme" text file is located next to the main .EXE file for
  769.             the code sample.  This function launches a reader/editor on
  770.             that text file.  The default editor\reader is NOTEPAD.EXE.
  771.             You can change that editor by changing EDITOR_FILE_STR in
  772.             APPUTIL.H above.
  773.  
  774.   Args:     HINSTANCE hInst,
  775.               Handle of the executable module instance.
  776.             HWND hWndOwner,
  777.               Handle to owner parent window.
  778.  
  779.   Returns:  void
  780. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  781. void ReadMe(
  782.        HINSTANCE hInst,
  783.        HWND hWndOwner);
  784.  
  785.  
  786. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  787.   Function: ReadMeFile
  788.  
  789.   Summary:  For use by code sample applications that study themselves
  790.             as it were.  This function allows you to pass a filename for
  791.             the <sample>.TXT "readme" file associated with the code sample
  792.             and to launch a reader/editor on that file.  The default
  793.             editor\reader is NOTEPAD.EXE.  You can change that editor by
  794.             changing EDITOR_FILE_STR in APPUTIL.H above.
  795.  
  796.   Args:     HWND hWndOwner,
  797.               Handle to owner parent window.
  798.             LPTSTR szFileName,
  799.               Filename string.
  800.  
  801.   Returns:  void
  802. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  803. void ReadMeFile(
  804.        HWND hWndOwner,
  805.        LPTSTR szFileName);
  806.  
  807.  
  808. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  809.   Function: ReadSource
  810.  
  811.   Summary:  For use by code sample applications that study themselves
  812.             as it were.  This function allows you to select one of the
  813.             source files of this code sample and launch a reader to
  814.             edit/read it.  NOTEPAD.EXE is the default editor/reader.
  815.             You can change this by changing EDITOR_FILE_STR in
  816.             APPUTIL.H above.
  817.  
  818.   Args:     HWND hWndOwner
  819.               Handle of parent window.
  820.             OPENFILENAME* pofn,
  821.               Pointer to the Open File Name Common Dialog structure.
  822.  
  823.   Returns:  void
  824. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  825. void ReadSource(
  826.        HWND hWndOwner,
  827.        OPENFILENAME* pOfn);
  828.  
  829.  
  830. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  831.   Function: OutputDebugFmt
  832.  
  833.   Summary:  Wraps the Win32 OutputDebugString API call to provide
  834.             printf-style formatted (and variable argument) output.
  835.  
  836.   Args:     LPTSTR szFmt,
  837.               Format string.
  838.             [...]
  839.               Arguments to match those specified in the format string.
  840.  
  841.   Returns:  void
  842. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  843. void OutputDebugFmt(
  844.        LPTSTR szFmt,
  845.        ...);
  846.  
  847.  
  848. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  849.   Function: GetErrorMsg
  850.  
  851.   Summary:  Accepts a Win32 error code and retrieves a human readable
  852.             system message for it.
  853.  
  854.   Args:     HRESULT hr
  855.               SCODE error code.
  856.             LPTSTR pszMsg
  857.               Pointer string where message will be placed.
  858.             UINT uiSize
  859.               Max size of the msg string.
  860.  
  861.   Returns:  BOOL
  862.               TRUE if hr was error; FALSE if not.
  863. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  864. BOOL GetErrorMsg(
  865.        HRESULT hr,
  866.        LPTSTR pszMsg,
  867.        UINT uiSize);
  868.  
  869.  
  870. #define TID GetCurrentThreadId()
  871.  
  872. #if defined(_DEBUG)
  873.  
  874. //Basic debug and logging macros
  875.  
  876. // Output debug string; literal or variable.
  877. #define ODS(sMsg) \
  878. {\
  879.   TCHAR szDebug[MAXLOGST];\
  880.   OutputDebugFmt(TEXT("%s"),TEXT(sMsg));\
  881.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  882.   OutputDebugString(szDebug);\
  883. }
  884.  
  885. // Output debug string formatted; one argument.
  886. #define ODF1(sFmt,x) \
  887. {\
  888.   TCHAR szDebug[MAXLOGST];\
  889.   OutputDebugFmt(TEXT(sFmt),x);\
  890.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  891.   OutputDebugString(szDebug);\
  892. }
  893.  
  894. // Output debug string formatted; two argument.
  895. #define ODF2(sFmt,x,y) \
  896. {\
  897.   TCHAR szDebug[MAXLOGST];\
  898.   OutputDebugFmt(TEXT(sFmt),x,y);\
  899.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  900.   OutputDebugString(szDebug);\
  901. }
  902.  
  903. // Output debug string formatted; three argument.
  904. #define ODF3(sFmt,x,y,z) \
  905. {\
  906.   TCHAR szDebug[MAXLOGST];\
  907.   OutputDebugFmt(TEXT(sFmt),x,y,z);\
  908.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  909.   OutputDebugString(szDebug);\
  910. }
  911.  
  912. // Check condition and Output assertion failed message to debugger.
  913. #define ASSERT(condition) \
  914. {\
  915.   if (!(condition))\
  916.     ODS(TEXT("Assertion Failed"));\
  917. }
  918.  
  919. // Log debug string (output both to Message Log and to Debugger)
  920. #define LOG(sMsg) \
  921. {\
  922.   TCHAR szDebug[MAXLOGST];\
  923.   if (NULL!=g_pMsgLog)\
  924.     g_pMsgLog->Msg(TEXT(sMsg));\
  925.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  926.   OutputDebugString(szDebug);\
  927. }
  928.  
  929. // Log debug string (output both to Message Log and to Debugger).
  930. // One argument.
  931. #define LOGF1(sFmt,x) \
  932. {\
  933.   TCHAR szDebug[MAXLOGST];\
  934.   if (NULL!=g_pMsgLog)\
  935.     g_pMsgLog->MsgFmt(TEXT(sFmt),x);\
  936.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  937.   OutputDebugString(szDebug);\
  938. }
  939.  
  940. // Log debug string (output both to Message Log and to Debugger).
  941. // Two argument.
  942. #define LOGF2(sFmt,x,y) \
  943. {\
  944.   TCHAR szDebug[MAXLOGST];\
  945.   if (NULL!=g_pMsgLog)\
  946.     g_pMsgLog->MsgFmt(TEXT(sFmt),x,y);\
  947.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  948.   OutputDebugString(szDebug);\
  949. }
  950.  
  951. // Log debug string (output both to Message Log and to Debugger).
  952. // Three argument.
  953. #define LOGF3(sFmt,x,y,z) \
  954. {\
  955.   TCHAR szDebug[MAXLOGST];\
  956.   if (NULL!=g_pMsgLog)\
  957.     g_pMsgLog->MsgFmt(TEXT(sFmt),x,y,z);\
  958.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  959.   OutputDebugString(szDebug);\
  960. }
  961.  
  962. // Log error message string (output both to Message Log and to Debugger).
  963. // One argument. Pfx = Prefix msg string; Ec = error code.
  964. #define LOGERROR(Pfx,Ec) \
  965. {\
  966.   TCHAR szMsg[MAX_PATH];\
  967.   if (NULL!=g_pMsgLog && GetErrorMsg(Ec,szMsg,MAX_PATH))\
  968.   {\
  969.     g_pMsgLog->MsgFmt(TEXT("%s Error=0x%X: %s"),TEXT(Pfx),Ec,szMsg);\
  970.     wsprintf(szMsg, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  971.     OutputDebugString(szMsg);\
  972.   }\
  973. }
  974.  
  975. // Log debug string (output both to Message Log and to Debugger).
  976. // Use a string resource ID to retrieve the display string.
  977. #define LOGID(id) \
  978. {\
  979.   TCHAR szDebug[MAXLOGST];\
  980.   if (NULL!=g_pMsgLog)\
  981.     g_pMsgLog->MsgID(id);\
  982.   wsprintf(szDebug, TEXT(" [%s, %u]\r\n"), (LPTSTR)__FILE__, __LINE__);\
  983.   OutputDebugString(szDebug);\
  984. }
  985.  
  986. // Log assertion failure.
  987. #define LOGASSERT(condition) \
  988. {\
  989.   if (!(condition))\
  990.     LOGID(IDS_ASSERT_FAIL);\
  991. }
  992.  
  993. #else  // !defined(_DEBUG)
  994.  
  995. #define ODS(x)
  996.  
  997. #define ODF1(sFmt,x)
  998.  
  999. #define ODF2(sFmt,x,y)
  1000.  
  1001. #define ODF3(sFmt,x,y,z)
  1002.  
  1003. #define ASSERT(condition)
  1004.  
  1005. #define LOG(sMsg) \
  1006. {\
  1007.   if (NULL!=g_pMsgLog)\
  1008.     g_pMsgLog->Msg(TEXT(sMsg));\
  1009. }
  1010.  
  1011. // Log debug string (output both to Message Log and not to Debugger).
  1012. // One argument.
  1013. #define LOGF1(sFmt,x) \
  1014. {\
  1015.   if (NULL!=g_pMsgLog)\
  1016.     g_pMsgLog->MsgFmt(TEXT(sFmt),x);\
  1017. }
  1018.  
  1019. // Log debug string (output both to Message Log and not to Debugger).
  1020. // Two argument.
  1021. #define LOGF2(sFmt,x,y) \
  1022. {\
  1023.   if (NULL!=g_pMsgLog)\
  1024.     g_pMsgLog->MsgFmt(TEXT(sFmt),x,y);\
  1025. }
  1026.  
  1027. // Log debug string (output both to Message Log and not to Debugger).
  1028. // Three argument.
  1029. #define LOGF3(sFmt,x,y,z) \
  1030. {\
  1031.   if (NULL!=g_pMsgLog)\
  1032.     g_pMsgLog->MsgFmt(TEXT(sFmt),x,y,z);\
  1033. }
  1034.  
  1035. // Log error message string (output both to Message Log and not to Debugger).
  1036. // One argument. Pfx = Prefix msg string; Ec = error code.
  1037. #define LOGERROR(Pfx,Ec) \
  1038. {\
  1039.   TCHAR szMsg[MAX_PATH];\
  1040.   if (NULL!=g_pMsgLog && GetErrorMsg(Ec,szMsg,MAX_PATH))\
  1041.     g_pMsgLog->MsgFmt(TEXT("%s Error=0x%X: %s"),TEXT(Pfx),Ec,szMsg);\
  1042. }
  1043.  
  1044. // Log debug string (output both to Message Log and not to Debugger).
  1045. // Use a string resource ID to retrieve the display string.
  1046. #define LOGID(id) \
  1047. {\
  1048.   if (NULL!=g_pMsgLog)\
  1049.     g_pMsgLog->MsgID(id);\
  1050. }
  1051.  
  1052. // Log assertion failure.
  1053. #define LOGASSERT(condition)\
  1054. {\
  1055.   if (!(condition))\
  1056.     LOGID(IDS_ASSERT_FAIL);\
  1057. }
  1058.  
  1059. #endif // _DEBUG
  1060.  
  1061. #endif //APPUTIL_H
  1062.