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