deviceiocontrolevent.cpp

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

CPP
160
字号
#include "stdafx.h"
#include "IOExplorer.h"
#include "DataArray.h"
#include "TraceEvent.h"
  #include "ioctl.h"
    #include "DeviceIoControlEvent.h"


/****************************************************************************
*                     DeviceIoControlEvent::DeviceIoControlEvent
* Inputs:
*       CString s: Name of handle
*	HANDLE  hv: Actual handle, or 0 if not known
*	IOCTL * ioctl:
*	BOOL InBufferEnabled
*	DWORD InSize:
*	BOOL OutBufferEnabled:
*	DWORD OutSize:
* Effect: 
*       Initializes the DeviceIoControlEvent
****************************************************************************/

DeviceIoControlEvent::DeviceIoControlEvent(CString s, 
					   HANDLE hv, 
					   IOCTL * ioctldata,
					   BOOL IBE,
					   DWORD insz,
					   BOOL OBE,
					   DWORD outsz)
    {
     h = hv;
     name = s;
     if(ioctldata != NULL)
         ioctl = *ioctldata; // makes a copy
     InBufferEnabled = IBE;
     InBufferSize = insz;
     OutBufferEnabled = OBE;
     OutBufferSize = outsz;
     NumberOfOutputBytes = 0;
    }

CString DeviceIoControlEvent::display_result()
    {
     CString s;

     if(result)
	s.LoadString(IDS_TRUE);
     else
	s.LoadString(IDS_FALSE);
     return s;
    }

/****************************************************************************
*                          DeviceIoControlEvent::execute
* Result: LRESULT
*       (LRESULT)TRUE if successful
*	(LRESULT)FALSE if failure
* Effect: 
*       Closes the handle whose handle value is supplied in the event block
****************************************************************************/

LRESULT DeviceIoControlEvent::execute()
    {
     if(h == NULL)
        { /* invalid handle */
	 lasterror = ERROR_INVALID_HANDLE;
	 return FALSE;  // bogus
	} /* invalid handle */

     CWaitCursor c;

     InBuffer.SetSize(InBufferSize);
     OutBuffer.SetSize(OutBufferSize);

     result = DeviceIoControl(h, ioctl.getCode(), 
     			      InBufferEnabled ? &InBuffer.ElementAt(0)
			      		      : NULL,
			      InBufferSize,
			      OutBufferEnabled ? &OutBuffer.ElementAt(0)
			      		      : NULL,
			      OutBufferSize,
			      &NumberOfOutputBytes,
			      NULL);
     				
     if(result)
	lasterror = 0;
     else
	lasterror = ::GetLastError();

     OutBuffer.SetSize(NumberOfOutputBytes); // truncate to exact size
     return result;
    }

/****************************************************************************
*                          DeviceIoControlEvent::display
* Result: CString
*       Display string for the event
* Effect: 
*       Creates a displayable string
****************************************************************************/

CString DeviceIoControlEvent::display()
    {
     CString handle;
     if(h == NULL)
        { /* no handle */
	 if(name.GetLength() == 0)
	    { /* no name */
	     handle = _T("");
	    } /* no name */
	 else
	    { /* has name */
	     handle = name;
	    } /* has name */
	} /* no handle */
     else
        { /* valid handle */
	 handle = name;
	} /* valid handle */

     // DeviceIoControl(handle, ioctl, InBuffer, InSize, OutBuffer, OutSize,
     //			&OutputBytes, NULL)
     CString rf;
     rf.Format(_T("DeviceIoControl(%s, %s, %s, %d, %s, %d, &BytesReturned, NULL)"),
     		handle, 
		(LPCTSTR)ioctl.getName(),
		InBufferEnabled ? _T("InBuffer") : _T("NULL"),
		InBufferEnabled ? InBufferSize : 0,
		OutBufferEnabled ? _T("OutBuffer") : _T("NULL"),
		OutBufferEnabled ? OutBufferSize : 0);

     return rf;
    }
 

/****************************************************************************
*                         DeviceIoControlEvent::copy
* Result: TraceEvent *
*       A new DeviceIoControlEvent
* Effect: 
*       Makes a complete copy of the DeviceIoControlEvent
****************************************************************************/

TraceEvent * DeviceIoControlEvent::copy()
    {
     DeviceIoControlEvent * e = new DeviceIoControlEvent(name, h, &ioctl,
     					InBufferEnabled,
					InBufferSize,
					OutBufferEnabled,
					OutBufferSize);
     e->result = result;
     e->NumberOfOutputBytes = NumberOfOutputBytes;
     if(InBufferSize > 0)
	e->InBuffer.Copy(InBuffer);
     if(OutBufferSize > 0)
	e->OutBuffer.Copy(OutBuffer);
     copyTraceData(e);
     return e;
    }

⌨️ 快捷键说明

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