home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n10
/
gmmfsrc.exe
/
GMMFTEST.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-10-01
|
8KB
|
238 lines
/*****************************************************************************
Module name: GMMFTest.C
Written by: Jeffrey Richter
Purpose: Demonstrates how to use a growable memory-mapped file.
*****************************************************************************/
#define STRICT
#include <windows.h>
#include <windowsx.h>
#pragma warning(disable: 4001) /* Single line comment */
#include <tchar.h>
#include "resource.h"
#include "GMMF.h" // Growable Memory-Mapped File Functions
/////////////////////////////// ARRAY_SIZE Macro /////////////////////////////
// This macro evaluates to the number of elements in an array.
#define ARRAY_SIZE(Array) (sizeof(Array) / sizeof((Array)[0]))
///////////////////////////// HANDLE_DLGMSG Macro ////////////////////////////
// The normal HANDLE_MSG macro in WINDOWSX.H does not work properly for dialog
// boxes because DlgProc's return a BOOL instead of an LRESULT (like
// WndProcs). This HANDLE_DLGMSG macro corrects the problem:
#define HANDLE_DLGMSG(hwnd, message, fn) \
case (message): return (SetDlgMsgResult(hwnd, uMsg, \
HANDLE_##message((hwnd), (wParam), (lParam), (fn))))
//////////////////////////////////////////////////////////////////////////////
BOOL GMMFTest_OnInitDialog (HWND hwnd, HWND hwndFocus, LPARAM lParam) {
// Initialize the GMMF creation parameters
SetDlgItemText(hwnd, IDC_PATHNAME, __TEXT("C:\\GMMFTest.Test"));
SetDlgItemInt(hwnd, IDC_FILESIZEMAX, 1024 * 1024, FALSE);
SetDlgItemInt(hwnd, IDC_FILEGROWINC, 8 * 1024, FALSE);
SetDlgItemInt(hwnd, IDC_OVERRUNBUF, 10 * 1024, FALSE);
// Initialize the What to write to the GMMF parameters
SetDlgItemText(hwnd, IDC_STRING, __TEXT("A test string."));
SetDlgItemInt(hwnd, IDC_OFFSET, 12 * 1024, FALSE);
SetDlgItemInt(hwnd, IDC_COUNT, 100, FALSE);
return(TRUE); // Accept default focus window.
}
//////////////////////////////////////////////////////////////////////////////
LONG WINAPI ExcFilter (PEXCEPTION_POINTERS pexc, PGMMF pgmmf) {
// We know how to handle exceptions raised by the GMMF functions.
switch (pexc->ExceptionRecord->ExceptionCode) {
case EXCEPTION_GMMF_DISKFULL:
case EXCEPTION_GMMF_WRITEPASTMAX:
case EXCEPTION_GMMF_CORRUPTEDRGN:
return(EXCEPTION_EXECUTE_HANDLER);
}
// If the exception is not from the GMMF functions, allow GMMF_ExcFIlter
// an opportunity to processes the exception possible growing the GMMF.
return(GMMF_ExcFilter(pexc, pgmmf));
}
//////////////////////////////////////////////////////////////////////////////
void GMMFTest_Write2GMMF (LPCTSTR szFileName, LPCTSTR szString,
DWORD cbFileSizeMax, DWORD cbFileSizeInc, DWORD cbOverrunBuf,
DWORD dwFileOffset, DWORD dwTimesToWrite) {
HANDLE hFile = INVALID_HANDLE_VALUE;
GMMF gmmf;
PBYTE pbFile;
// Create the new disk file.
hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBox(GetFocus(), __TEXT("Couldn't create file."), NULL, MB_OK);
return;
}
// Create the GMMF.
// NOTE: It's possible that GMMF_Create will raise an
// EXCEPTION_GMMF_CORRUPTEDRGN exception. This is very unlikely and
// this sample application does not attempt to recover from this.
pbFile = (PBYTE) GMMF_Create(&gmmf, hFile,
cbFileSizeMax, cbFileSizeInc, cbOverrunBuf);
if (pbFile == NULL) {
CloseHandle(hFile);
MessageBox(GetFocus(), __TEXT("Couldn't create GMMF."), NULL, MB_OK);
return;
}
// All accesses to the GMMF must be in a SEH block.
__try {
// Write data to the GMMF.
while (dwTimesToWrite--) {
// NOTE: We cannot use lstrcpy here because it contains its own SEH
// code on Windows 95.
_tcscpy(&pbFile[dwFileOffset], szString);
dwFileOffset += lstrlen(szString) + 1;
}
}
__except (ExcFilter(GetExceptionInformation(), &gmmf)) {
// If a GMMF exception is raised, we display a message
// indicating the error to the user.
LPCTSTR p = __TEXT("Unknown exception");
switch (GetExceptionCode()) {
case EXCEPTION_GMMF_DISKFULL:
p = __TEXT("Can't grow the GMMF - Out of disk space?");
break;
case EXCEPTION_GMMF_WRITEPASTMAX:
p = __TEXT("Attempt to write past the GMMF");
break;
case EXCEPTION_GMMF_CORRUPTEDRGN:
p = __TEXT("Corrupted GMMF region");
break;
}
MessageBox(GetFocus(), p, NULL, MB_OK);
}
// When we are done writing, close the GMMF and shrink it to
// its required size.
GMMF_Close(&gmmf, dwFileOffset);
// Close the file.
CloseHandle(hFile);
}
//////////////////////////////////////////////////////////////////////////////
void GMMFTest_OnCommand (HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) {
TCHAR szPathname[_MAX_PATH], szString[100];
DWORD cbFileSizeMax, cbFileGrowInc, cbOverrunBuf, cbFinalFizeSize;
DWORD dwFileOffset, dwTimesToWrite;
BOOL fTranslated;
SYSTEM_INFO sinf;
// Obtain the user's settings from the dialog box controls.
GetDlgItemText(hwnd, IDC_PATHNAME, szPathname, ARRAY_SIZE(szPathname));
cbFileSizeMax = GetDlgItemInt(hwnd, IDC_FILESIZEMAX, &fTranslated, FALSE);
cbFileGrowInc = GetDlgItemInt(hwnd, IDC_FILEGROWINC, &fTranslated, FALSE);
cbOverrunBuf = GetDlgItemInt(hwnd, IDC_OVERRUNBUF, &fTranslated, FALSE);
GetDlgItemText(hwnd, IDC_STRING, szString, ARRAY_SIZE(szString));
dwFileOffset = GetDlgItemInt(hwnd, IDC_OFFSET, &fTranslated, FALSE);
dwTimesToWrite = GetDlgItemInt(hwnd, IDC_COUNT, &fTranslated, FALSE);
switch (id) {
case IDCANCEL: // Allows dialog box to close
EndDialog(hwnd, id);
break;
case IDC_FILESIZEMAX:
case IDC_FILEGROWINC:
case IDC_STRING:
case IDC_OFFSET:
case IDC_COUNT:
if (codeNotify == EN_CHANGE) {
// Update the statistics fields
GetSystemInfo(&sinf);
SetDlgItemInt(hwnd, IDC_MAXDISKSIZE,
RoundUp(cbFileSizeMax, sinf.dwAllocationGranularity), FALSE);
SetDlgItemInt(hwnd, IDC_ACTUALGROWINC,
RoundUp(cbFileGrowInc, sinf.dwAllocationGranularity), FALSE);
cbFinalFizeSize = dwFileOffset +
(lstrlen(szString) + 1) * dwTimesToWrite;
SetDlgItemInt(hwnd, IDC_FINALDISKSIZE, cbFinalFizeSize, FALSE);
SetDlgItemText(hwnd, IDC_WILLOVERRUNOCCUR,
(cbFinalFizeSize >
RoundUp(cbFileSizeMax, sinf.dwAllocationGranularity)) ?
__TEXT("Yes") : __TEXT("No"));
}
break;
case IDC_TESTGMMF:
// Perform a test run of the GMMF code.
SetCursor(LoadCursor(NULL, IDC_WAIT));
GMMFTest_Write2GMMF(szPathname, szString, cbFileSizeMax,
cbFileGrowInc, cbOverrunBuf, dwFileOffset, dwTimesToWrite);
SetCursor(LoadCursor(NULL, IDC_ARROW));
break;
}
}
//////////////////////////////////////////////////////////////////////////////
BOOL WINAPI GMMFTest_DlgProc (HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
// Standard Window's messages
HANDLE_DLGMSG(hwnd, WM_INITDIALOG, GMMFTest_OnInitDialog);
HANDLE_DLGMSG(hwnd, WM_COMMAND, GMMFTest_OnCommand);
}
return(FALSE); // We didn't process the message.
}
//////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hinstExe, HINSTANCE hinstPrev,
LPSTR lpszCmdLine, int nCmdShow) {
DialogBox(hinstExe, MAKEINTRESOURCE(IDD_GMMFTEST), NULL, GMMFTest_DlgProc);
return(0);
}
//////////////////////////////// End of File /////////////////////////////////