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

  1. // dllhusk.cpp : Defines the class behaviors for the application.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "dllhusk.h"
  15.  
  16. #include "mainfrm.h"
  17. #include "testdll1.h"   // classes exported from TESTDLL1
  18. #include "testdll2.h"   // classes exported from TESTDLL2
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CHuskApp
  27.  
  28. BEGIN_MESSAGE_MAP(CHuskApp, CWinApp)
  29.     //{{AFX_MSG_MAP(CHuskApp)
  30.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  31.     ON_COMMAND(ID_DUMP_CLASSES, OnDumpClasses)
  32.     ON_COMMAND(ID_DUMP_DOCTEMPLATES, OnDumpDocTemplates)
  33.     ON_COMMAND(ID_DUMP_OBJECTS, OnDumpObjects)
  34.     ON_COMMAND(ID_DUMP_DLLS, OnDumpDLLs)
  35.     //}}AFX_MSG_MAP
  36.     // Standard file based document commands
  37.     ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  38.     ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  39.     ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CHuskApp construction
  44. // Place all significant initialization in InitInstance
  45.  
  46. CHuskApp::CHuskApp()
  47. {
  48.     m_pListOut = NULL;
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // The one and only CHuskApp object
  53.  
  54. CHuskApp NEAR theApp;
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CHuskApp initialization
  58.  
  59. BOOL CHuskApp::InitInstance()
  60. {
  61.     // Standard initialization
  62.  
  63.     Enable3dControls(); // use 3d controls in dialogs
  64.     LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  65.  
  66.     // create main MDI Frame window
  67.     CMainFrame* pMainFrame = new CMainFrame;
  68.     if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  69.         return FALSE;
  70.  
  71.     pMainFrame->ShowWindow(m_nCmdShow);
  72.     pMainFrame->UpdateWindow();
  73.     m_pMainWnd = pMainFrame;
  74.  
  75.     // Initialize DLLs - will add doc templates and make other class
  76.     //   implementations available to this application
  77.     InitTestDLL1();
  78.     InitTestDLL2();
  79.  
  80.     CreateListOutput();
  81.  
  82.     if (m_lpCmdLine[0] == '\0')
  83.     {
  84.         if (m_pListOut != NULL)
  85.             m_pListOut->AddString(_T("Attempting to create a new document"));
  86.         // create a new (empty) document
  87.         OnFileNew();
  88.     }
  89.     else
  90.     {
  91.         if (m_pListOut != NULL)
  92.             m_pListOut->AddString(_T("Attempting to open an existing document"));
  93.         // open an existing document
  94.         OpenDocumentFile(m_lpCmdLine);
  95.     }
  96.  
  97. #ifdef _DEBUG
  98.     if (m_pListOut != NULL)
  99.     {
  100.         m_pListOut->AddString(_T(""));
  101.         m_pListOut->AddString(_T("Click the Right-Mouse-Button over"));
  102.         m_pListOut->AddString(_T(" the main TitleBar for diagnostic menu"));
  103.         m_pListOut->AddString(_T(""));
  104.     }
  105. #endif
  106.     return TRUE;
  107. }
  108.  
  109. // Example of calling a class in a DLL
  110. BOOL CHuskApp::CreateListOutput()
  111. {
  112.     m_pListOut = new CListOutputFrame;
  113.             // all objects are allocated on the application's heap,
  114.             //  even CListOutputFrame which is defined in another DLL
  115.  
  116.     CRect rectOriginal(250, 0, 600, 300);
  117.             // hard-coded initial position
  118.  
  119.     if (!m_pListOut->Create(_T("List Output"),
  120.             WS_OVERLAPPEDWINDOW | WS_CHILD | WS_VISIBLE,
  121.             rectOriginal, (CMDIFrameWnd*)m_pMainWnd))
  122.     {
  123.         AfxMessageBox(_T("Failed to create ListOutput Window"));
  124.         m_pListOut = NULL;  // just in case (Create will delete the C++ object)
  125.         return FALSE;
  126.     }
  127.  
  128.     m_pListOut->SetBackpointer(&m_pListOut);
  129.     return TRUE;
  130. }
  131.  
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CAboutDlg dialog used for App About
  134.  
  135. void CHuskApp::OnAppAbout()
  136. {
  137.     if (m_pListOut != NULL)
  138.         m_pListOut->AddString(_T("CHuskApp::OnAppAbout called"));
  139.     CDialog(IDD_ABOUTBOX).DoModal();
  140. }
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CHuskApp debug menu dump commands
  144.  
  145. #ifdef _DEBUG
  146. static void DumpObjectProc(CObject* pObject, void* pContext)
  147. {
  148.     CListOutputFrame* pListOut = (CListOutputFrame*)pContext;
  149.     TCHAR szT[128];
  150.     wsprintf(szT, _T("    a %s at $%08lX"),
  151.         pObject->GetRuntimeClass()->m_lpszClassName, (long)(void*)pObject);
  152.     pListOut->AddString(szT);
  153. }
  154.  
  155. static void DumpClassProc(const CRuntimeClass* pClass, void* pContext)
  156. {
  157.     CListOutputFrame* pListOut = (CListOutputFrame*)pContext;
  158.     TCHAR szT[128];
  159.     wsprintf(szT, _T("    %s"), pClass->m_lpszClassName);
  160.     pListOut->AddString(szT);
  161. }
  162. #endif
  163.  
  164. void CHuskApp::OnDumpClasses()
  165. {
  166. #ifdef _DEBUG
  167.     if (m_pListOut == NULL && !CreateListOutput())
  168.         return; // no output window
  169.     m_pListOut->AddString(_T("Dump of all classes:"));
  170.     AfxDoForAllClasses(DumpClassProc, m_pListOut);
  171.     m_pListOut->AddString(_T(""));
  172. #endif
  173. }
  174.  
  175. void CHuskApp::OnDumpDocTemplates()
  176. {
  177. #ifdef _DEBUG
  178.     if (m_pListOut == NULL && !CreateListOutput())
  179.         return; // no output window
  180.     m_pListOut->AddString(_T("Dump of all Doc Templates:"));
  181.     POSITION pos = GetFirstDocTemplatePosition();
  182.     while (pos != NULL)
  183.     {
  184.         CDocTemplate* pTemplate = GetNextDocTemplate(pos);
  185.         CString str;
  186.         TCHAR szT[128];
  187.         if (pTemplate->GetDocString(str, CDocTemplate::fileNewName))
  188.             wsprintf(szT, _T("    Template for %s documents"), (LPCTSTR)str);
  189.         else
  190.             wsprintf(szT, _T("    Unknown DocTemplate at $%08lX"),
  191.                 (long)(void*)pTemplate);
  192.         m_pListOut->AddString(szT);
  193.     }
  194.     m_pListOut->AddString(_T(""));
  195. #endif
  196. }
  197.  
  198. void CHuskApp::OnDumpObjects()
  199. {
  200. #ifdef _DEBUG
  201.     if (m_pListOut == NULL && !CreateListOutput())
  202.         return; // no output window
  203.     m_pListOut->AddString(_T("Dump of all heap Objects"));
  204.     AfxDoForAllObjects(DumpObjectProc, m_pListOut);
  205.     m_pListOut->AddString(_T(""));
  206. #endif
  207. }
  208.  
  209. void CHuskApp::OnDumpDLLs()
  210. {
  211. #ifdef _DEBUG
  212.     if (m_pListOut == NULL && !CreateListOutput())
  213.         return; // no output window
  214.     m_pListOut->AddString(_T("Dump of DLLs in resource search order"));
  215.     AFX_MODULE_STATE* pState = AfxGetModuleState();
  216.     for (CDynLinkLibrary* pDLL = pState->m_libraryList; pDLL != NULL;
  217.         pDLL = pDLL->m_pNextDLL)
  218.     {
  219.         // get module name
  220.         TCHAR szName[64];
  221.         GetModuleFileName(pDLL->m_hModule, szName, sizeof(szName));
  222.         TCHAR szT[256];
  223.  
  224.         // count classes
  225.         int nClasses = 0;
  226.         for (CRuntimeClass* pClass = pDLL->m_classList;
  227.             pClass != NULL; pClass = pClass->m_pNextClass)
  228.             nClasses++;
  229.  
  230.         // count factories
  231.         int nFactories = 0;
  232.     #ifndef _AFX_NO_OLE_SUPPORT
  233.         for (COleObjectFactory* pFactory = pDLL->m_factoryList;
  234.             pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  235.             nFactories++;
  236.     #endif
  237.  
  238.         wsprintf(szT, _T("    Module %s has %d classes and %d factories"),
  239.             szName, nClasses, nFactories);
  240.         m_pListOut->AddString(szT);
  241.     }
  242.     m_pListOut->AddString(_T(""));
  243. #endif
  244. }
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247.