createfile.cpp

来自「IO函数调用测试」· C++ 代码 · 共 360 行

CPP
360
字号
// CreateFile.cpp : implementation file
//

#include "stdafx.h"
#include <afxtempl.h>

#include "help.h"
#include "Status.h"
#include "IOExplorer.h"
#include "idcombo.h"
#include "is95.h"
#include "TraceEvent.h"
  #include "EventList.h"
    #include "EventLog.h"
#include "handle.h"
#include "HandleCombo.h"
    #include "handleManager.h"
    #include "traceManager.h"
      #include "CreateFileSheet.h"
  #include "HandlePage.h"
  #include "CreateFileEvent.h"
    #include "CreateFile.h"
#include "orstring.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCreateFile property page

IMPLEMENT_DYNCREATE(CCreateFile, CHandlePage)

CCreateFile::CCreateFile() : CHandlePage(CCreateFile::IDD)
{
 LastError = 0;
 lasthandle = NULL;
 create_pending = FALSE;

	//{{AFX_DATA_INIT(CCreateFile)
	//}}AFX_DATA_INIT
}

CCreateFile::~CCreateFile()
{
}

void CCreateFile::DoDataExchange(CDataExchange* pDX)
{
	CHandlePage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCreateFile)
	DDX_Control(pDX, IDC_SHARE_WRITE, c_ShareWrite);
	DDX_Control(pDX, IDC_SHARE_READ, c_ShareRead);
	DDX_Control(pDX, IDC_SHARE_DELETE, c_ShareDelete);
	DDX_Control(pDX, IDC_SECURITY_CAPTION, c_c_Security);
	DDX_Control(pDX, IDC_INHERIT_HANDLES, c_InheritHandles);
	DDX_Control(pDX, IDC_GENERIC_WRITE, c_GenericWrite);
	DDX_Control(pDX, IDC_GENERIC_READ, c_GenericRead);
	DDX_Control(pDX, IDC_FILENAME, c_FileName);
	DDX_Control(pDX, IDC_CREATION_DISTRIBUTION, c_CreationDistribution);
	DDX_Control(pDX, IDC_CREATE, c_Create);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCreateFile, CHandlePage)
	//{{AFX_MSG_MAP(CCreateFile)
	ON_BN_CLICKED(IDC_CREATE, OnCreateFile)
	ON_EN_CHANGE(IDC_FILENAME, OnChangeFilename)
	ON_BN_CLICKED(IDC_GENERIC_READ, OnGenericRead)
	ON_BN_CLICKED(IDC_GENERIC_WRITE, OnGenericWrite)
	ON_BN_CLICKED(IDC_INHERIT_HANDLES, OnInheritHandles)
	ON_BN_CLICKED(IDC_SET_FLAGS, OnSetFlags)
	ON_BN_CLICKED(IDC_SHARE_DELETE, OnShareDelete)
	ON_BN_CLICKED(IDC_SHARE_READ, OnShareRead)
	ON_BN_CLICKED(IDC_SHARE_WRITE, OnShareWrite)
	ON_CBN_SELENDOK(IDC_CREATION_DISTRIBUTION, OnSelendokCreationDistribution)
	ON_BN_CLICKED(IDC_HELPBUTTON, OnHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCreateFile message handlers

void CCreateFile::OnCreateFile() 
{
 CreateFileEvent * e = new CreateFileEvent();

 getCreateFileEvent(e);
 
 LRESULT result = e->execute();
 create_pending = TRUE;

 CCreateFileSheet * sheet = getSheet();

 sheet->log->add(e);

 if(result == (LRESULT)INVALID_HANDLE_VALUE)
    { /* failed */
     LastError = ::GetLastError();
     getSheet()->HandleList.setCurrentHandle(NULL); // no current handle
    } /* failed */
 else
    { /* success */
     LastError = 0;
     POSITION pos = getSheet()->HandleList.Append(e, (HANDLE)result);
     getSheet()->HandleList.setCurrentHandle(getSheet()->HandleList.GetAt(pos));
    } /* success */

 // LOG EVENT HERE

 updateStatus();
 updateControls();
}

void CCreateFile::OnChangeFilename() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnGenericRead() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnGenericWrite() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnInheritHandles() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnSetFlags() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnShareDelete() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnShareRead() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnShareWrite() 
{
 updateCommand();
 updateStatus();
}

void CCreateFile::OnSelendokCreationDistribution() 
{
 updateCommand();
 updateStatus();
}

/****************************************************************************
*                        CCreateFile::getDesiredAccess
* Result: DWORD
*       0, GENERIC_READ | GENERIC_WRITE
****************************************************************************/

DWORD CCreateFile::getDesiredAccess()
    {
     DWORD result = 0;

     if(c_GenericRead.GetCheck())
	result |= GENERIC_READ;
     if(c_GenericWrite.GetCheck())
	result |= GENERIC_WRITE;
     return result;

    }

/****************************************************************************
*                          CCreateFile::getShareMode
* Result: DWORD
*       Share mode
*	SHARE_READ | SHARE_WRITE | SHARE_DELETE
****************************************************************************/

DWORD CCreateFile::getShareMode()
    {
     DWORD result = 0;

     if(c_ShareRead.GetCheck())
	result |= FILE_SHARE_READ;
     if(c_ShareWrite.GetCheck())
	result |= FILE_SHARE_WRITE;
     if(c_ShareDelete.GetCheck())
	result |= FILE_SHARE_DELETE;
     return result;

    }

/****************************************************************************
*                       CCreateFile::getCreateFileEvent
* Inputs:
*       CreateFileEvent * p:
* Result: void
*       
* Effect: 
*       Initializes the fields of the CreateFileEvent from the controls
****************************************************************************/

void CCreateFile::getCreateFileEvent(CreateFileEvent * p)
    {
     CCreateFileSheet * sheet = getSheet();
     c_FileName.GetWindowText(p->FileName);

     p->DesiredAccess = getDesiredAccess();
     p->ShareMode = getShareMode();
     p->inherit = c_InheritHandles.GetCheck();
     p->FlagsAndAttributes = sheet->FlagsAndAttributes;

     p->CreationDistribution = c_CreationDistribution.GetCurItem();
    }

/****************************************************************************
*                         CCreateFile::updateCommand
* Result: void
*       
* Effect: 
*       Updates the command line displayed
*
*	CreateFile(filename, desiredaccess,
****************************************************************************/

void CCreateFile::updateCommand()
    {
     CCreateFileSheet * sheet = getSheet();
     CreateFileEvent p;

     getCreateFileEvent(&p);
     create_pending = FALSE;

     c_Text.SetWindowText(p.display());
    }

BOOL CCreateFile::OnInitDialog() 
{
	CHandlePage::OnInitDialog();
	
 	c_Text.SetWindowText(_T(""));
	c_Status.SetWindowText(_T(""));
	c_CreationDistribution.AddStrings(
			   CreateFileEvent::getCreationDistributionTable());
	c_CreationDistribution.Select(OPEN_EXISTING);

	if(Is95())
	   { /* W95 */
	    c_c_Security.EnableWindow(FALSE);
	    c_InheritHandles.EnableWindow(FALSE);
	   } /* W95 */

	updateStatus();
	updateCommand();
	updateControls();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/****************************************************************************
*                         CCreateFile::updateControls
* Result: void
*       
* Effect: 
*       Updates the controls
****************************************************************************/

void CCreateFile::updateControls()
    {
     CCreateFileSheet * sheet = getSheet();

     // c_Create.EnableWindow(sheet->HandleList.getCurrentHandle() == NULL);
    }

/****************************************************************************
*                          CCreateFile::updateStatus
* Result: void
*       
* Effect: 
*       Updates the status line
****************************************************************************/

void CCreateFile::updateStatus()
    {
     CCreateFileSheet * sheet = getSheet();
     
     if(sheet->HandleList.getCurrentHandle() != NULL)
        { /* show handle */
	 CString s;
	 s.Format(_T("%08x"), sheet->HandleList.getCurrentHandle()->h);
	 c_Handle.SetWindowText(s);
	} /* show handle */
     else
        { /* no handle */
	 c_Handle.SetWindowText(_T(""));
	} /* no handle */

     if(LastError != 0 && create_pending)
	{ /* format error */
	 c_Status.SetWindowText(LastError);
	} /* format error */
     else
	{ /* no error found */
	 CString s;
	 if(sheet->HandleList.getCurrentHandle() == NULL && create_pending)
	    s.LoadString(IDS_GETLASTERROR_0);
	 c_Status.SetWindowText(s);
	} /* no error found */
    }

BOOL CCreateFile::OnSetActive() 
{
     // These two lines see if the place we went killed the handle
     // and if so, sets the create_pending flag to FALSE so we know
     // the juxtaposition of error codes and NULL handle indicate that
     // no error exists.

     CCreateFileSheet * sheet = getSheet();
     if(sheet->HandleList.getCurrentHandle() == NULL & lasthandle != NULL)
	create_pending = FALSE;

     updateStatus();
     updateCommand();
     updateControls();
	
     return CHandlePage::OnSetActive();
}

BOOL CCreateFile::OnKillActive() 
    {
     CCreateFileSheet * sheet = getSheet();
     lasthandle = sheet->HandleList.getCurrentHandle();
	
     return CHandlePage::OnKillActive();
}

void CCreateFile::OnHelp() 
{
 AfxGetApp()->WinHelp(CreateFileHelp, HELP_CONTEXT);	
}

⌨️ 快捷键说明

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