sehterm.c

来自「一本已经绝版的好书」· C语言 代码 · 共 241 行

C
241
字号
/************************************************************
Module name: SEHTerm.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <stdio.h>
#include "Resource.H"


/////////////////////////////////////////////////////////////


// The SIMULATION define should always be defined.
// It exists so that you can easily separate the simulation
// aspects of this program from the actual code that you would
// use to perform the various operations.
#define SIMULATION


/////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus, 
   LPARAM lParam) {

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_SEHTERM, IDI_SEHTERM);

   Button_SetCheck(GetDlgItem(hwnd, IDC_OPENSUCCEEDS), TRUE);
   Button_SetCheck(GetDlgItem(hwnd, IDC_SIZESUCCEEDS), TRUE);
   Button_SetCheck(GetDlgItem(hwnd, IDC_MEMSUCCEEDS),  TRUE);
   Button_SetCheck(GetDlgItem(hwnd, IDC_READSUCCEEDS), TRUE);
   return(TRUE);
}


/////////////////////////////////////////////////////////////


LONG Dlg_CountWordsInFile (HWND hwndLog,
   BOOL fOpenSucceeds, BOOL fFileSizeSucceeds,
   BOOL fMemSucceeds, BOOL fReadSucceeds) {

   HANDLE hFile = INVALID_HANDLE_VALUE;
   DWORD  dwFileSize = 0;
   LPVOID lpvFileData = NULL;
   BOOL   fFileReadOk = FALSE;
   LONG   lNumWords = -1;
   DWORD  dwLastError;

   __try {
      // Clear the Execution Log list box.
      ListBox_ResetContent(hwndLog);
      ListBox_AddString(hwndLog,
         __TEXT("Starting execution"));

      // Open the file.
#ifdef SIMULATION
      hFile = (fOpenSucceeds ?
         (HANDLE) !INVALID_HANDLE_VALUE : 
         INVALID_HANDLE_VALUE);
#else
      hFile = CreateFile(...);
#endif
      if (hFile == INVALID_HANDLE_VALUE) {
         // The file could not be opened.
         ListBox_AddString(hwndLog, 
            __TEXT("   File open: Fail"));
         __leave;
      } else {
         ListBox_AddString(hwndLog, 
            __TEXT("   File open: OK"));
      }

      // Determine the size of the file.
#ifdef SIMULATION
      dwLastError = fFileSizeSucceeds ? NO_ERROR : !NO_ERROR;
#else
      dwFileSize = GetFileSize(hFile);
      dwLastError = GetLastError();
#endif

      if (dwLastError != NO_ERROR) {
         // The file size could not be obtained.
         ListBox_AddString(hwndLog, 
            __TEXT("   File size: Fail"));
         __leave;
      } else {
         ListBox_AddString(hwndLog, 
            __TEXT("   File size: OK"));
      }

      // Allocate a block of memory to store the entire file.
#ifdef SIMULATION
      lpvFileData = fMemSucceeds ? !NULL : NULL;
#else
      lpvFileData = HeapAlloc(GetProcessHeap(), 0, dwFileSize);
#endif
      if (lpvFileData == NULL) {
         // Allocation failed.
         ListBox_AddString(hwndLog, 
            __TEXT("   Memory allocation: Fail"));
         __leave;
      } else {
         ListBox_AddString(hwndLog, 
            __TEXT("   Memory allocation: OK"));
      }


      // Read the file into the buffer.
#ifdef SIMULATION
      fReadSucceeds = fReadSucceeds;
#else
      fReadSucceeds =ReadFile(hFile, lpvFileData, dwFileSize,
         NULL, NULL);
#endif
      if (!fReadSucceeds) {
         // The file's data could not be loaded into memory.
         ListBox_AddString(hwndLog, 
            __TEXT("   File read: Fail"));
         __leave;
      } else {
         ListBox_AddString(hwndLog, 
            __TEXT("   File read: OK"));
         fFileReadOk = TRUE;
      }

      // Calculate the number of words in the file.
      // The algorithm to calculate the number of words in the
      // file would go here. For simulation purposes, I'll 
      // just set lNumWords to 37.
      ListBox_AddString(hwndLog,
         __TEXT("   Calculating the number of words"));
      lNumWords = 37;

   }  // try

   __finally {
      // Display a notification that we are cleaning up.
      ListBox_AddString(hwndLog, __TEXT("   Cleaning up"));

      // Guarantee that the memory is freed.
      if (lpvFileData != NULL) {
         ListBox_AddString(hwndLog, 
            __TEXT("   Freeing memory"));
#ifndef SIMULATION
         HeapFree(GetProcessHeap(), 0, lpvFileData);
#endif
      }

      // Guarantee that the file is closed.
      if (hFile != INVALID_HANDLE_VALUE) {
         ListBox_AddString(hwndLog, 
            __TEXT("   Closing file"));
#ifndef SIMULATION
         CloseHandle(hFile);
#endif
      }

   }  // finally

   return(lNumWords);
}


/////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl, 
   UINT codeNotify) {
   
   TCHAR szBuf[100];
   LONG  lNumWords;

   switch (id) {
      case IDOK:
         lNumWords = Dlg_CountWordsInFile(
            GetDlgItem(hwnd, IDC_LOG),
            Button_GetCheck(GetDlgItem(hwnd,
               IDC_OPENSUCCEEDS)),
            Button_GetCheck(GetDlgItem(hwnd,
               IDC_SIZESUCCEEDS)),
            Button_GetCheck(GetDlgItem(hwnd, IDC_MEMSUCCEEDS)),
            Button_GetCheck(GetDlgItem(hwnd, IDC_READSUCCEEDS))
            );

         if (lNumWords == -1) {
            ListBox_AddString(GetDlgItem(hwnd, IDC_LOG),
               __TEXT("Error occurred in function."));
         } else {
               _stprintf(szBuf,
                  __TEXT("Result: Words in file = %d"),
                  lNumWords);
            ListBox_AddString(GetDlgItem(hwnd,
               IDC_LOG), szBuf);
         }

         break;

      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


/////////////////////////////////////////////////////////////


BOOL CALLBACK DlgProc (HWND hwnd, UINT uMsg, 
   WPARAM wParam, LPARAM lParam) {
   
   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
   }
   return(FALSE);
}


/////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_SEHTERM), 
      NULL, DlgProc);

   return(0);
}


//////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?