home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / dlltrace / trace.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  4.1 KB  |  178 lines

  1. // trace.cpp : Contains TRACE.DLL implementation and initialization
  2. //              code.
  3. //
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include <afxwin.h>
  15. #include "traceapi.h"
  16. #include "traceres.h"       // trace resources
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Dialog class
  20.  
  21. class CPromptDlg : public CDialog
  22. {
  23. public:
  24.     CPromptDlg(BOOL bEnabled, UINT nFlags, CWnd* pParent);
  25.  
  26.     //{{AFX_DATA(CPromptDlg)
  27.     enum { IDD = IDD_PROMPT };
  28.     BOOL    m_bEnabled;
  29.     BOOL    m_b0;
  30.     BOOL    m_b1;
  31.     BOOL    m_b2;
  32.     BOOL    m_b3;
  33.     BOOL    m_b4;
  34.     //}}AFX_DATA
  35.  
  36.     UINT CombineFlags();
  37.     virtual void DoDataExchange(CDataExchange* pDX);
  38.     //{{AFX_MSG(CPromptDlg)
  39.     //}}AFX_MSG
  40.     DECLARE_MESSAGE_MAP()
  41. };
  42.  
  43. BEGIN_MESSAGE_MAP(CPromptDlg, CDialog)
  44.     //{{AFX_MSG_MAP(CPromptDlg)
  45.     //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47.  
  48. CPromptDlg::CPromptDlg(BOOL bEnabled, UINT nFlags, CWnd* pParent)
  49.     : CDialog(CPromptDlg::IDD, pParent)
  50. {
  51.     //{{AFX_DATA_INIT(CPromptDlg)
  52.     m_bEnabled = bEnabled;
  53.     m_b0 = (nFlags & 1) != 0;
  54.     m_b1 = (nFlags & 2) != 0;
  55.     m_b2 = (nFlags & 4) != 0;
  56.     m_b3 = (nFlags & 8) != 0;
  57.     m_b4 = (nFlags & 0x10) != 0;
  58.     //}}AFX_DATA_INIT
  59. }
  60.  
  61. void CPromptDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     //{{AFX_DATA_MAP(CPromptDlg)
  64.     DDX_Check(pDX, IDC_ENABLEALL, m_bEnabled);
  65.     DDX_Check(pDX, IDC_BIT0, m_b0);
  66.     DDX_Check(pDX, IDC_BIT1, m_b1);
  67.     DDX_Check(pDX, IDC_BIT2, m_b2);
  68.     DDX_Check(pDX, IDC_BIT3, m_b3);
  69.     DDX_Check(pDX, IDC_BIT4, m_b4);
  70.     //}}AFX_DATA_MAP
  71. }
  72.  
  73. UINT CPromptDlg::CombineFlags()
  74. {
  75.     UINT nFlags = 0;
  76.     if (m_b0)
  77.         nFlags |= 1;
  78.     if (m_b1)
  79.         nFlags |= 2;
  80.     if (m_b2)
  81.         nFlags |= 4;
  82.     if (m_b3)
  83.         nFlags |= 8;
  84.     if (m_b4)
  85.         nFlags |= 0x10;
  86.     return nFlags;
  87. }
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // Public C interface
  91.  
  92. extern "C"
  93. BOOL FAR PASCAL EXPORT PromptTraceFlags(HWND hWndParent, TracerData FAR* lpData)
  94. {
  95.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  96.     TRACE0("Inside Trace DLL\n");
  97.  
  98.     // All DLL entry points should have a top-level TRY/CATCH block.
  99.     // Otherwise, it would be possible to throw an uncaught exception from
  100.     //  an the DLL.  This would most likely cause a crash.
  101.  
  102.     TRY
  103.     {
  104.         CPromptDlg dlg(lpData->bEnabled, lpData->flags,
  105.             CWnd::FromHandle(hWndParent));
  106.  
  107.         if (dlg.DoModal() != IDOK)
  108.             return FALSE;
  109.  
  110.         // update the data
  111.         lpData->bEnabled = dlg.m_bEnabled;
  112.         lpData->flags = dlg.CombineFlags();
  113.     }
  114.     CATCH_ALL(e)
  115.     {
  116.         // a failure caused an exception.
  117.         return FALSE;
  118.     }
  119.     END_CATCH_ALL
  120.  
  121.     return TRUE;
  122. }
  123.  
  124. /////////////////////////////////////////////////////////////////////////////
  125. // Library init
  126.  
  127. class CTracerDLL : public CWinApp
  128. {
  129. public:
  130.     virtual BOOL InitInstance(); // Initialization
  131.     virtual int ExitInstance();  // Termination (WEP-like code)
  132.  
  133.     // nothing special for the constructor
  134.     CTracerDLL(LPCTSTR pszAppName) : CWinApp(pszAppName) { }
  135. };
  136.  
  137. BOOL CTracerDLL::InitInstance()
  138. {
  139.     // any DLL initialization goes here
  140.     TRACE0("TRACE.DLL initializing\n");
  141.  
  142.     return TRUE;
  143. }
  144.  
  145. int CTracerDLL::ExitInstance()
  146. {
  147.     // any DLL termination goes here (WEP-like code)
  148.     return CWinApp::ExitInstance();
  149. }
  150.  
  151. extern "C" BOOL FAR PASCAL EXPORT FilterDllMsg(LPMSG lpMsg)
  152. {
  153.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  154.     TRY
  155.     {
  156.         return AfxGetThread()->PreTranslateMessage(lpMsg);
  157.     }
  158.     END_TRY
  159.     return FALSE;
  160. }
  161.  
  162. extern "C" void FAR PASCAL EXPORT ProcessDllIdle()
  163. {
  164.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  165.     TRY
  166.     {
  167.         // flush it all at once
  168.         long lCount = 0;
  169.         while (AfxGetThread()->OnIdle(lCount))
  170.             lCount++;
  171.     }
  172.     END_TRY
  173. }
  174.  
  175. CTracerDLL  NEAR tracerDLL(_T("trace.dll"));
  176.  
  177. /////////////////////////////////////////////////////////////////////////////
  178.