CrdFileDriverDoc.cpp
上传用户:lsj_816
上传日期:2007-01-01
资源大小:37k
文件大小:4k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // CrdFileDriverDoc.cpp : implementation of the CCrdFileDriverDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "CrdFileDriver.h"
  5. #include "CrdFileDriverDoc.h"
  6. #include "CardFile/CardFile.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CCrdFileDriverDoc
  14. IMPLEMENT_DYNCREATE(CCrdFileDriverDoc, CDocument)
  15. BEGIN_MESSAGE_MAP(CCrdFileDriverDoc, CDocument)
  16. //{{AFX_MSG_MAP(CCrdFileDriverDoc)
  17. // NOTE - the ClassWizard will add and remove mapping macros here.
  18. //    DO NOT EDIT what you see in these blocks of generated code!
  19. //}}AFX_MSG_MAP
  20. END_MESSAGE_MAP()
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CCrdFileDriverDoc construction/destruction
  23. CCrdFileDriverDoc::CCrdFileDriverDoc()
  24. {
  25. // TODO: add one-time construction code here
  26. }
  27. CCrdFileDriverDoc::~CCrdFileDriverDoc()
  28. {
  29. }
  30. BOOL CCrdFileDriverDoc::OnNewDocument()
  31. {
  32. if (!CDocument::OnNewDocument())
  33. return FALSE;
  34. ((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);
  35. // TODO: add reinitialization code here
  36. // (SDI documents will reuse this document)
  37. return TRUE;
  38. }
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CCrdFileDriverDoc serialization
  41. void CCrdFileDriverDoc::Serialize(CArchive& ar)
  42. {
  43. // CEditView contains an edit control which handles all serialization
  44. ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CCrdFileDriverDoc diagnostics
  48. #ifdef _DEBUG
  49. void CCrdFileDriverDoc::AssertValid() const
  50. {
  51. CDocument::AssertValid();
  52. }
  53. void CCrdFileDriverDoc::Dump(CDumpContext& dc) const
  54. {
  55. CDocument::Dump(dc);
  56. }
  57. #endif //_DEBUG
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CCrdFileDriverDoc commands
  60. BOOL CCrdFileDriverDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  61. {
  62. // It is not necessary to call the base clas implementation of OnOpenDocument()
  63. // if (!CDocument::OnOpenDocument(lpszPathName))
  64. // return FALSE;
  65. HINSTANCE hInst;
  66. LPCSTR lpszDllPath = "CardFile.dll";
  67. IMPORTCARDFILE ImportCardFile;
  68. FREECARDFILEPTR FreeCardFilePtr;
  69. LPCARDFILECARD cfc;
  70. DWORD dwNumCards, dwResult;
  71. LPCSTR lpszDivider = "========================================";
  72. CString strFormat;
  73. // Load the carfile dll, and obtain a pointer to the import function
  74. hInst = LoadLibrary(lpszDllPath);
  75. if (!hInst)
  76. {
  77. AfxMessageBox("Cannot load the cardfile DLL");
  78. return FALSE;
  79. }
  80. ImportCardFile = (IMPORTCARDFILE)GetProcAddress(hInst, "ImportCardFile");
  81. if (!ImportCardFile)
  82. {
  83. AfxMessageBox("Cannot find the "ImportCardFile" process");
  84. return FALSE;
  85. }
  86. // Empty the string that will be shown in the edit control of the child window
  87. m_strText.Empty();
  88. // call the function that imports the cardfile. Each car will be loaded into a separate structure
  89. dwResult = ImportCardFile(lpszPathName, &cfc, &dwNumCards);
  90. // The cards was imported successfully
  91. if (dwResult == CF_SUCCESS)
  92. {
  93. // For each of the cards create string and add it to the main string m_strText
  94. // The main string will eventually be shown in the edit window
  95. for (DWORD i=0 ; i<dwNumCards ; i++)
  96. {
  97. strFormat.Format("%srn%srn%srn", 
  98. lpszDivider, cfc[i].szHeading, cfc[i].szBodyText);
  99. m_strText += strFormat;
  100. }
  101. }
  102. else
  103. {
  104. // Error - show a message
  105. switch (dwResult)
  106. {
  107. case CF_FILETOSMALL:
  108. AfxMessageBox("The file was to small.");
  109. break;
  110. case CF_NOTACARDFILE:
  111. AfxMessageBox("This is not a valid cardfile database.");
  112. break;
  113. default:
  114. AfxMessageBox("Cardfile import failed.");
  115. }
  116. return FALSE;
  117. }
  118. // Load the process that will perform clean up of the imported cards
  119. FreeCardFilePtr = (FREECARDFILEPTR)GetProcAddress(hInst, "FreeCardFilePtr");
  120. if (!FreeCardFilePtr)
  121. {
  122. AfxMessageBox("Cannot find the "FreeCardFilePtr" process");
  123. return FALSE;
  124. }
  125. FreeCardFilePtr(&cfc, dwNumCards);
  126. FreeLibrary(hInst);
  127. // Make sure that the contents of the string with the cardfiles are shown
  128. UpdateAllViews(NULL);
  129. return TRUE;
  130. }