deviceiocontrol.cpp

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

CPP
416
字号
// DeviceIoControl.cpp : implementation file
//

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

#include "help.h"
#include "Status.h"
#include "IoExplorer.h"
#include "NumericEdit.h"
#include "DataArray.h"
  #include "HexDisp.h"
#include "TraceEvent.h"
#include "EventList.h"
  #include "EventLog.h"
#include "IDCombo.h"
#include "Handle.h"
  #include "HandleCombo.h"
  #include "HandleManager.h"
  #include "TraceManager.h"
    #include "CreateFileSheet.h"
    #include "ioctl.h"
      #include "DeviceIoControlEvent.h"
#include "HandlePage.h"
#include "DeviceIoControl.h"

#include "ioctls.h"
  #include "HSListBox.h"
    #include "IOCTLListBox.h"
      #include "IOCTLManager.h"

#include "registry.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDeviceIoControl property page

IMPLEMENT_DYNCREATE(CDeviceIoControl, CHandlePage)

CDeviceIoControl::CDeviceIoControl() : CHandlePage(CDeviceIoControl::IDD)
{
	//{{AFX_DATA_INIT(CDeviceIoControl)
	//}}AFX_DATA_INIT
	initialized = FALSE;
}

CDeviceIoControl::~CDeviceIoControl()
{
}

void CDeviceIoControl::DoDataExchange(CDataExchange* pDX)
{
	CHandlePage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDeviceIoControl)
	DDX_Control(pDX, IDC_BYTESRETURNED_CAPTION, c_c_BytesReturned);
	DDX_Control(pDX, IDC_OUTBUFFER_CAPTION, c_c_OutBufferSize);
	DDX_Control(pDX, IDC_INBUFFER_CAPTION, c_c_InBufferSize);
	DDX_Control(pDX, IDC_HEXOUT, c_HexOut);
	DDX_Control(pDX, IDC_HEXIN, c_HexIn);
	DDX_Control(pDX, IDC_SPIN_OUTBUFFERSIZE, c_SpinOutBufferSize);
	DDX_Control(pDX, IDC_BYTESRETURNED, c_BytesReturned);
	DDX_Control(pDX, IDC_OUTBYTES, c_OutBytes);
	DDX_Control(pDX, IDC_OUTBUFFERSIZE, c_OutBufferSize);
	DDX_Control(pDX, IDC_OUTBUFFER_ENABLE, c_OutBufferEnable);
	DDX_Control(pDX, IDC_IOCTLS, c_IOCTLs);
	DDX_Control(pDX, IDC_INBYTES, c_InBytes);
	DDX_Control(pDX, IDC_INBUFFERSIZE, c_InBufferSize);
	DDX_Control(pDX, IDC_INBUFFER_ENABLE, c_InBufferEnable);
	DDX_Control(pDX, IDC_DEVICEIOCONTROL, c_DeviceIoControl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDeviceIoControl, CHandlePage)
	//{{AFX_MSG_MAP(CDeviceIoControl)
	ON_BN_CLICKED(IDC_DEVICEIOCONTROL, OnDeviceiocontrol)
	ON_BN_CLICKED(IDC_INBUFFER_ENABLE, OnInbufferEnable)
	ON_EN_CHANGE(IDC_INBUFFERSIZE, OnChangeInbuffersize)
	ON_EN_CHANGE(IDC_INBYTES, OnChangeInbytes)
	ON_CBN_SELENDOK(IDC_IOCTLS, OnSelendokIoctls)
	ON_BN_CLICKED(IDC_OUTBUFFER_ENABLE, OnOutbufferEnable)
	ON_EN_CHANGE(IDC_OUTBUFFERSIZE, OnChangeOutbuffersize)
	ON_BN_CLICKED(IDC_HEXIN, OnHexin)
	ON_BN_CLICKED(IDC_HEXOUT, OnHexout)
	ON_BN_CLICKED(IDC_IOCTL, OnIoctl)
	ON_BN_CLICKED(IDC_HELPBUTTON, OnHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeviceIoControl message handlers

BOOL CDeviceIoControl::OnInitDialog() 
{
 	initialized = FALSE;
	CHandlePage::OnInitDialog();
	
	c_SpinOutBufferSize.SetRange(0, 2048);
	
	loadIOCTLs();

	initialized = TRUE;

	updateControls();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/****************************************************************************
*                        CDeviceIoControl::loadIOCTLs
* Result: void
*       
* Effect: 
*       Loads the Combo Box from the \Software\HWSimulator\Active set
****************************************************************************/

void CDeviceIoControl::loadIOCTLs()
    {
     CString id;
     id.LoadString(IDS_RHS);

     c_IOCTLs.ResetContent();

     CStringArray * ioctls = EnumRegistryValues(HKEY_CURRENT_USER, id);
     if(ioctls == NULL)
	return; // nothing to load

     for(int i = 0; i < ioctls->GetSize(); i++)
        { /* load each */
	 DWORD code;
	 CString key;
	 key = id;
	 key += _T("\\");
	 key += ioctls->ElementAt(i);
	 if(GetRegistryInt(HKEY_CURRENT_USER, key, code))
	    { /* success */
	     int n = c_IOCTLs.AddString(ioctls->ElementAt(i));
	     IOCTL * ioctl = new IOCTL(ioctls->ElementAt(i), code);
	     c_IOCTLs.SetItemDataPtr(n, ioctl);
	    } /* success */
	} /* load each */

     delete ioctls; // free up array
     
    }

/****************************************************************************
*                     CDeviceIoControl::OnDeviceiocontrol
* Result: void
*       
* Effect: 
*       Actually executes the DeviceIoControl operation
****************************************************************************/

void CDeviceIoControl::OnDeviceiocontrol() 
{
 CCreateFileSheet * sheet = getSheet();
 CHandle * ho = sheet->HandleList.getCurrentHandle();
	
 IOCTL * ioctl = NULL;
 if(c_IOCTLs.GetCurSel() != CB_ERR)
    ioctl = (IOCTL *)c_IOCTLs.GetItemData(c_IOCTLs.GetCurSel());

 DeviceIoControlEvent * e = new DeviceIoControlEvent(
 			    ho == NULL ? _T("") : ho->Name,
     			    ho == NULL ? 0 : ho->h,
			    ioctl,			    
			    c_InBufferEnable.GetCheck() == BST_CHECKED,
			    c_InBufferSize.GetWindowInt(),
			    c_OutBufferEnable.GetCheck() == BST_CHECKED,
			    c_SpinOutBufferSize.GetPos()
			    ); // NYI: more stuff
 if(c_InBufferSize.GetWindowInt() > 0)
    e->InBuffer.Copy(c_InBytes.getRawData());
 e->execute();
 if(!e->result)
    { /* error */
     c_Status.SetWindowText(e->lasterror);
    } /* error */
 else
    { /* success */
     c_Status.SetWindowText(e->display_result());
    } /* success */

 sheet->log->add(e);

 setEvent(*e);
}

/****************************************************************************
*                         CDeviceIoControl::setEvent
* Inputs:
*       DeviceIoControlEvent & e:
* Result: void
*       
* Effect: 
*       Transfers contents of the event to the controls
****************************************************************************/

void CDeviceIoControl::setEvent(DeviceIoControlEvent & e)
    {
     if(e.OutBufferEnabled)
        { /* has output */
	 c_BytesReturned.SetWindowText(e.NumberOfOutputBytes);
	 if(e.NumberOfOutputBytes > 0)
	    { /* has data */
	     BOOL hex = c_OutBytes.SetWindowTextHexMaybe(e.OutBuffer);
	     if(hex)
		c_HexOut.SetCheck(BST_CHECKED);
	     
	    } /* has data */
	} /* has output */
     else
        { /* no output */
	 c_BytesReturned.Blank();
	 c_OutBytes.SetWindowText(_T(""));
	} /* no output */
     updateControls();
    }

void CDeviceIoControl::OnInbufferEnable() 
{
 updateControls();	
}

void CDeviceIoControl::OnChangeInbuffersize() 
{
 updateControls();	
}

void CDeviceIoControl::OnChangeInbytes() 
{
 updateControls();	
}

void CDeviceIoControl::OnSelendokIoctls() 
{
 updateControls();	
}

/****************************************************************************
*                     CDeviceIoControl::OnOutbufferEnable
* Result: void
*       
* Effect: 
*       Updates the display
****************************************************************************/

void CDeviceIoControl::OnOutbufferEnable() 
{
 updateControls();	
}

/****************************************************************************
*                   CDeviceIoControl::OnChangeOutbuffersize
* Result: void
*       
* Effect: 
*       Updates the display
****************************************************************************/

void CDeviceIoControl::OnChangeOutbuffersize() 
{
 updateControls();	
}

/****************************************************************************
*                         CDeviceIoControl::showInput
* Inputs:
*       BOOL mode: TRUE to show it
* Result: void
*       
* Effect: 
*       Shows or hides (or enables or disables) the input buffer information
****************************************************************************/

void CDeviceIoControl::showInput(BOOL mode)
    {
     UINT show = mode ? SW_SHOW : SW_HIDE;
     c_InBufferSize.ShowWindow(show);
     c_c_InBufferSize.ShowWindow(show);
     c_InBytes.ShowWindow(show);
     c_HexIn.ShowWindow(show);
    }

/****************************************************************************
*                         CDeviceIoControl::showOutput
* Inputs:
*       BOOL mode: TRUE to show it
* Result: void
*       
* Effect: 
*       Shows or hides (or enables or disables) the output buffer information
****************************************************************************/

void CDeviceIoControl::showOutput(BOOL mode)
    {
     UINT show = mode ? SW_SHOW : SW_HIDE;
     c_OutBufferSize.ShowWindow(show);
     c_SpinOutBufferSize.ShowWindow(show);
     c_c_OutBufferSize.ShowWindow(show);
     c_OutBytes.ShowWindow(show);
     c_HexOut.ShowWindow(show);
     c_BytesReturned.ShowWindow(show);
     c_c_BytesReturned.ShowWindow(show);
    }

/****************************************************************************
*                      CDeviceIoControl::updateControls
* Result: void
*       
* Effect: 
*       Updates the controls
****************************************************************************/

void CDeviceIoControl::updateControls()
    {
     if(!initialized)
	return;

     CCreateFileSheet * sheet = getSheet();

     CHandle * ho = sheet->HandleList.getCurrentHandle();

     IOCTL * ioctl = NULL;
     if(c_IOCTLs.GetCurSel() != CB_ERR)
	ioctl = (IOCTL *)c_IOCTLs.GetItemData(c_IOCTLs.GetCurSel());

     DeviceIoControlEvent e(ho == NULL ? _T("") : ho->Name,
     			    ho == NULL ? 0 : ho->h,
			    ioctl,			    
			    c_InBufferEnable.GetCheck() == BST_CHECKED,
			    c_InBufferSize.GetWindowInt(),
			    c_OutBufferEnable.GetCheck() == BST_CHECKED,
			    c_SpinOutBufferSize.GetPos()
			    ); // NYI: more stuff
			    
     CString s;
     s = e.display();
     c_Text.SetWindowText(s);

     // The show/enable logic
     showInput(c_InBufferEnable.GetCheck() == BST_CHECKED);
     showOutput(c_OutBufferEnable.GetCheck() == BST_CHECKED); 

     BOOL enable = TRUE;

     enable &= c_IOCTLs.GetCurSel() != CB_ERR;
     c_DeviceIoControl.EnableWindow(enable);

     c_InBufferSize.SetWindowText(c_InBytes.GetWindowTextLength());

     c_HexIn.EnableWindow(c_InBytes.canAscii());
     c_HexOut.EnableWindow(c_OutBytes.canAscii());
    }

/****************************************************************************
*                          CDeviceIoControl::OnHexin
* Result: void
*       
* Effect: 
*       Switches the input display between hex and ASCII
****************************************************************************/

void CDeviceIoControl::OnHexin() 
{
 if(c_HexIn.GetCheck() == BST_CHECKED)
    c_InBytes.toHex();
 else
    c_InBytes.toAscii();
}

/****************************************************************************
*                         CDeviceIoControl::OnHexout
* Result: void
*       
* Effect: 
*       Switches the output display between hex and ASCII
****************************************************************************/

void CDeviceIoControl::OnHexout() 
{
 if(c_HexOut.GetCheck() == BST_CHECKED)
    c_OutBytes.toHex();
 else
    c_OutBytes.toAscii();
}

/****************************************************************************
*                          CDeviceIoControl::IoIoctl
* Result: void
*       
* Effect: 
*       Invokes the IOCTL Manager dialog, which updates the registry, and
*	then forces a reload of the IOCTL display
****************************************************************************/

void CDeviceIoControl::OnIoctl() 
{
 CIOCTLManager dlg;
 if(dlg.DoModal() == IDOK)
    loadIOCTLs();
}


void CDeviceIoControl::OnHelp()
{
 AfxGetApp()->WinHelp(DeviceIoControlHelp, HELP_CONTEXT);
}

⌨️ 快捷键说明

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