home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n10 / gmmfsrc.exe / GMMFTEST.C < prev    next >
C/C++ Source or Header  |  1995-10-01  |  8KB  |  238 lines

  1. /*****************************************************************************
  2. Module name: GMMFTest.C
  3. Written by: Jeffrey Richter
  4. Purpose: Demonstrates how to use a growable memory-mapped file.
  5. *****************************************************************************/
  6.  
  7.  
  8. #define STRICT
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #pragma warning(disable: 4001)    /* Single line comment */
  12.  
  13. #include <tchar.h>
  14.  
  15. #include "resource.h"
  16. #include "GMMF.h"                // Growable Memory-Mapped File Functions
  17.  
  18.  
  19. /////////////////////////////// ARRAY_SIZE Macro /////////////////////////////
  20.  
  21.  
  22. // This macro evaluates to the number of elements in an array. 
  23. #define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
  24.  
  25.  
  26. ///////////////////////////// HANDLE_DLGMSG Macro ////////////////////////////
  27.  
  28.  
  29. // The normal HANDLE_MSG macro in WINDOWSX.H does not work properly for dialog
  30. // boxes because DlgProc's return a BOOL instead of an LRESULT (like
  31. // WndProcs). This HANDLE_DLGMSG macro corrects the problem:
  32. #define HANDLE_DLGMSG(hwnd, message, fn)                    \
  33.    case (message): return (SetDlgMsgResult(hwnd, uMsg,      \
  34.       HANDLE_##message((hwnd), (wParam), (lParam), (fn))))
  35.  
  36.  
  37. //////////////////////////////////////////////////////////////////////////////
  38.  
  39.  
  40. BOOL GMMFTest_OnInitDialog (HWND hwnd, HWND hwndFocus, LPARAM lParam) {
  41.  
  42.    // Initialize the GMMF creation parameters
  43.    SetDlgItemText(hwnd, IDC_PATHNAME,    __TEXT("C:\\GMMFTest.Test"));
  44.    SetDlgItemInt(hwnd,  IDC_FILESIZEMAX, 1024 * 1024, FALSE);
  45.    SetDlgItemInt(hwnd,  IDC_FILEGROWINC,    8 * 1024, FALSE);
  46.    SetDlgItemInt(hwnd,  IDC_OVERRUNBUF,    10 * 1024, FALSE);
  47.  
  48.    // Initialize the What to write to the GMMF parameters
  49.    SetDlgItemText(hwnd, IDC_STRING,      __TEXT("A test string."));
  50.    SetDlgItemInt(hwnd,  IDC_OFFSET,        12 * 1024, FALSE);
  51.    SetDlgItemInt(hwnd,  IDC_COUNT,               100, FALSE);
  52.  
  53.    return(TRUE);                  // Accept default focus window.
  54. }
  55.  
  56.  
  57. //////////////////////////////////////////////////////////////////////////////
  58.  
  59.  
  60. LONG WINAPI ExcFilter (PEXCEPTION_POINTERS pexc, PGMMF pgmmf) {
  61.  
  62.    // We know how to handle exceptions raised by the GMMF functions.
  63.    switch (pexc->ExceptionRecord->ExceptionCode) {
  64.       case EXCEPTION_GMMF_DISKFULL:
  65.       case EXCEPTION_GMMF_WRITEPASTMAX:
  66.       case EXCEPTION_GMMF_CORRUPTEDRGN:
  67.          return(EXCEPTION_EXECUTE_HANDLER);
  68.    }
  69.  
  70.    // If the exception is not from the GMMF functions, allow GMMF_ExcFIlter
  71.    // an opportunity to processes the exception possible growing the GMMF.
  72.    return(GMMF_ExcFilter(pexc, pgmmf));
  73. }
  74.  
  75.  
  76. //////////////////////////////////////////////////////////////////////////////
  77.  
  78.  
  79. void GMMFTest_Write2GMMF (LPCTSTR szFileName, LPCTSTR szString, 
  80.    DWORD cbFileSizeMax, DWORD cbFileSizeInc, DWORD cbOverrunBuf,
  81.    DWORD dwFileOffset, DWORD dwTimesToWrite) {
  82.  
  83.    HANDLE hFile = INVALID_HANDLE_VALUE;
  84.    GMMF gmmf;
  85.    PBYTE pbFile;
  86.  
  87.    // Create the new disk file.
  88.    hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, 
  89.       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 
  90.       NULL);
  91.    if (hFile == INVALID_HANDLE_VALUE) {
  92.       MessageBox(GetFocus(), __TEXT("Couldn't create file."), NULL, MB_OK);
  93.       return;
  94.    }
  95.  
  96.    // Create the GMMF.
  97.    // NOTE: It's possible that GMMF_Create will raise an 
  98.    // EXCEPTION_GMMF_CORRUPTEDRGN exception.  This is very unlikely and
  99.    // this sample application does not attempt to recover from this.
  100.    pbFile = (PBYTE) GMMF_Create(&gmmf, hFile, 
  101.       cbFileSizeMax, cbFileSizeInc, cbOverrunBuf);
  102.    if (pbFile == NULL) {
  103.       CloseHandle(hFile);
  104.       MessageBox(GetFocus(), __TEXT("Couldn't create GMMF."), NULL, MB_OK);
  105.       return;
  106.    }
  107.  
  108.  
  109.    // All accesses to the GMMF must be in a SEH block.
  110.    __try {
  111.       // Write data to the GMMF.
  112.       while (dwTimesToWrite--) {
  113.          // NOTE: We cannot use lstrcpy here because it contains its own SEH
  114.          // code on Windows 95.
  115.          _tcscpy(&pbFile[dwFileOffset], szString);
  116.          dwFileOffset += lstrlen(szString) + 1;
  117.       }
  118.    }
  119.    __except (ExcFilter(GetExceptionInformation(), &gmmf)) {
  120.  
  121.       // If a GMMF exception is raised, we display a message 
  122.       // indicating the error to the user.
  123.       LPCTSTR p = __TEXT("Unknown exception");
  124.       switch (GetExceptionCode()) {
  125.          case EXCEPTION_GMMF_DISKFULL:
  126.             p = __TEXT("Can't grow the GMMF - Out of disk space?"); 
  127.             break;
  128.  
  129.          case EXCEPTION_GMMF_WRITEPASTMAX:
  130.             p = __TEXT("Attempt to write past the GMMF"); 
  131.             break;
  132.  
  133.          case EXCEPTION_GMMF_CORRUPTEDRGN:
  134.             p = __TEXT("Corrupted GMMF region"); 
  135.             break;
  136.       }
  137.       MessageBox(GetFocus(), p, NULL, MB_OK);
  138.    }
  139.  
  140.    // When we are done writing, close the GMMF and shrink it to
  141.    // its required size.
  142.    GMMF_Close(&gmmf, dwFileOffset);
  143.  
  144.    // Close the file.
  145.    CloseHandle(hFile);
  146. }
  147.  
  148.  
  149. //////////////////////////////////////////////////////////////////////////////
  150.  
  151.  
  152. void GMMFTest_OnCommand (HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
  153.  
  154.    TCHAR szPathname[_MAX_PATH], szString[100];
  155.    DWORD cbFileSizeMax, cbFileGrowInc, cbOverrunBuf, cbFinalFizeSize;
  156.    DWORD dwFileOffset, dwTimesToWrite;
  157.    BOOL fTranslated;
  158.    SYSTEM_INFO sinf;
  159.  
  160.    // Obtain the user's settings from the dialog box controls.
  161.    GetDlgItemText(hwnd, IDC_PATHNAME, szPathname, ARRAY_SIZE(szPathname));
  162.    cbFileSizeMax  = GetDlgItemInt(hwnd, IDC_FILESIZEMAX, &fTranslated, FALSE);
  163.    cbFileGrowInc  = GetDlgItemInt(hwnd, IDC_FILEGROWINC, &fTranslated, FALSE);
  164.    cbOverrunBuf   = GetDlgItemInt(hwnd, IDC_OVERRUNBUF,  &fTranslated, FALSE);
  165.  
  166.    GetDlgItemText(hwnd, IDC_STRING, szString, ARRAY_SIZE(szString));
  167.    dwFileOffset   = GetDlgItemInt(hwnd, IDC_OFFSET, &fTranslated, FALSE);
  168.    dwTimesToWrite = GetDlgItemInt(hwnd, IDC_COUNT, &fTranslated, FALSE);
  169.  
  170.    switch (id) {
  171.       case IDCANCEL:              // Allows dialog box to close
  172.          EndDialog(hwnd, id);
  173.          break;
  174.  
  175.       case IDC_FILESIZEMAX:
  176.       case IDC_FILEGROWINC:
  177.       case IDC_STRING:
  178.       case IDC_OFFSET:
  179.       case IDC_COUNT:
  180.          if (codeNotify == EN_CHANGE) {
  181.  
  182.             // Update the statistics fields
  183.             GetSystemInfo(&sinf);
  184.             SetDlgItemInt(hwnd, IDC_MAXDISKSIZE, 
  185.                RoundUp(cbFileSizeMax, sinf.dwAllocationGranularity), FALSE);
  186.             SetDlgItemInt(hwnd, IDC_ACTUALGROWINC, 
  187.                RoundUp(cbFileGrowInc, sinf.dwAllocationGranularity), FALSE);
  188.             cbFinalFizeSize = dwFileOffset + 
  189.                (lstrlen(szString) + 1) * dwTimesToWrite;
  190.             SetDlgItemInt(hwnd, IDC_FINALDISKSIZE, cbFinalFizeSize, FALSE);
  191.             SetDlgItemText(hwnd, IDC_WILLOVERRUNOCCUR, 
  192.                (cbFinalFizeSize > 
  193.                   RoundUp(cbFileSizeMax, sinf.dwAllocationGranularity)) ? 
  194.                   __TEXT("Yes") : __TEXT("No"));
  195.          }
  196.          break;
  197.  
  198.       case IDC_TESTGMMF:
  199.          // Perform a test run of the GMMF code.
  200.          SetCursor(LoadCursor(NULL, IDC_WAIT));
  201.          GMMFTest_Write2GMMF(szPathname, szString, cbFileSizeMax, 
  202.             cbFileGrowInc, cbOverrunBuf, dwFileOffset, dwTimesToWrite);
  203.          SetCursor(LoadCursor(NULL, IDC_ARROW));
  204.          break;
  205.    }   
  206. }
  207.  
  208.  
  209. //////////////////////////////////////////////////////////////////////////////
  210.  
  211.         
  212. BOOL WINAPI GMMFTest_DlgProc (HWND hwnd, UINT uMsg, 
  213.    WPARAM wParam, LPARAM lParam) {
  214.  
  215.    switch (uMsg) {
  216.  
  217.       // Standard Window's messages
  218.       HANDLE_DLGMSG(hwnd, WM_INITDIALOG, GMMFTest_OnInitDialog);
  219.       HANDLE_DLGMSG(hwnd, WM_COMMAND,    GMMFTest_OnCommand);
  220.    }
  221.    return(FALSE);                 // We didn't process the message.
  222. }
  223.  
  224.  
  225. //////////////////////////////////////////////////////////////////////////////
  226.  
  227.  
  228. int WINAPI WinMain (HINSTANCE hinstExe, HINSTANCE hinstPrev, 
  229.    LPSTR lpszCmdLine, int nCmdShow) {
  230.  
  231.    DialogBox(hinstExe, MAKEINTRESOURCE(IDD_GMMFTEST), NULL, GMMFTest_DlgProc);
  232.  
  233.    return(0);
  234. }
  235.  
  236.  
  237. //////////////////////////////// End of File /////////////////////////////////
  238.