www.pudn.com > disk-filter.gz > Diskf_AppView.cpp, change:2006-04-12,size:7182b


// Diskf_AppView.cpp : implementation of the CDiskf_AppView class 
// 
 
#include "stdafx.h" 
#include "Diskf_App.h" 
 
#include "Diskf_AppDoc.h" 
#include "Diskf_AppView.h" 
 
#include <winioctl.h> 
#include "..\sys\iocmd.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// 
//define the log format 
// 
typedef struct _LOG_ENTRY 
{ 
	ULONG  DiskNumber; 
	 
	// 
	//0 - Read disk 
	//1 - Write disk 
	// 
	ULONG  OperationType; 
     
	ULONG  SectorNumber; 
	// 
	//The size of read\write 
	// 
	ULONG  Length; 
 
	ULONG   Data[4]; 
 
}LOG_ENTRY,*PLOG_ENTRY; 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView 
 
IMPLEMENT_DYNCREATE(CDiskf_AppView, CListView) 
 
BEGIN_MESSAGE_MAP(CDiskf_AppView, CListView) 
	//{{AFX_MSG_MAP(CDiskf_AppView) 
	ON_COMMAND(ID_BTN_START, OnBtnStart) 
	ON_COMMAND(ID_BTN_STOP, OnBtnStop) 
	ON_WM_TIMER() 
	ON_UPDATE_COMMAND_UI(ID_BTN_START, OnUpdateBtnStart) 
	ON_UPDATE_COMMAND_UI(ID_BTN_STOP, OnUpdateBtnStop) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView construction/destruction 
 
CDiskf_AppView::CDiskf_AppView() 
{ 
	// TODO: add construction code here 
	m_hDevice = CreateFile("\\\\.\\Diskf", 
		                 GENERIC_READ | GENERIC_WRITE, 
						 0, 
						 NULL, 
						 OPEN_EXISTING, 
						 FILE_ATTRIBUTE_NORMAL, 
						 NULL 
						 ); 
 
	m_bLogStared = FALSE; 
} 
 
CDiskf_AppView::~CDiskf_AppView() 
{ 
   CloseHandle( m_hDevice ); 
   m_hDevice = NULL; 
} 
 
BOOL CDiskf_AppView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	if (!CListView::PreCreateWindow(cs)) 
		return FALSE; 
	cs.dwExStyle |= WS_EX_CLIENTEDGE; 
	cs.style &= ~WS_BORDER; 
	cs.style |= LVS_REPORT; 
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView drawing 
 
void CDiskf_AppView::OnDraw(CDC* pDC) 
{ 
	CDiskf_AppDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView printing 
 
BOOL CDiskf_AppView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CDiskf_AppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CDiskf_AppView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView diagnostics 
 
#ifdef _DEBUG 
void CDiskf_AppView::AssertValid() const 
{ 
	CListView::AssertValid(); 
} 
 
void CDiskf_AppView::Dump(CDumpContext& dc) const 
{ 
	CListView::Dump(dc); 
} 
 
CDiskf_AppDoc* CDiskf_AppView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDiskf_AppDoc))); 
	return (CDiskf_AppDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CDiskf_AppView message handlers 
 
void CDiskf_AppView::OnBtnStart()  
{ 
	// TODO: Add your command handler code here 
	m_bLogStared = TRUE; 
 
	if( INVALID_HANDLE_VALUE == m_hDevice ) 
	{ 
		AfxMessageBox("无法与驱动程序通信!"); 
		return; 
	} 
 
	BOOLEAN  b = TRUE; 
	DWORD  dwRet; 
	DeviceIoControl(m_hDevice, 
		            IOCTL_DISKF_SET_CONTROL_FLAG, 
		            &b,1, 
					NULL,0, 
					&dwRet, 
					NULL 
					); 
 
	DeviceIoControl(m_hDevice, 
		            IOCTL_DISKF_DUMP_PARAMETER, 
		            &b,1, 
					NULL,0, 
					&dwRet, 
					NULL 
					); 
 
	m_Timer = SetTimer( 1234, 1000 ,NULL); 
} 
 
void CDiskf_AppView::OnBtnStop()  
{ 
		m_bLogStared = FALSE; 
 
	if( INVALID_HANDLE_VALUE == m_hDevice ) 
	{ 
		AfxMessageBox("无法与驱动程序通信!"); 
		return; 
	} 
 
	BOOLEAN  b = FALSE; 
	DWORD  dwRet; 
	DeviceIoControl(m_hDevice, 
		            IOCTL_DISKF_SET_CONTROL_FLAG, 
		            &b,1, 
					NULL,0, 
					&dwRet, 
					NULL 
					); 
 
	DeviceIoControl(m_hDevice, 
		            IOCTL_DISKF_DUMP_PARAMETER, 
		            &b,1, 
					NULL,0, 
					&dwRet, 
					NULL 
					); 
 
	KillTimer( m_Timer ); 
} 
 
CHAR   g_LogBuffer[1024*64]; 
 
void CDiskf_AppView::OnTimer(UINT nIDEvent)  
{ 
	if( nIDEvent != 1234 ) 
	{ 
		CView::OnTimer( nIDEvent ); 
		return; 
	} 
	DWORD dwRet =0; 
 
	if( g_LogBuffer == NULL) 
		return; 
 
	CListCtrl& theCtrl = GetListCtrl(); 
	DWORD dwOutSize= 1024*64; 
 
	DeviceIoControl(m_hDevice, 
		            IOCTL_DISKF_GET_LOG_BUFFER, 
		            NULL, 
					0, 
					g_LogBuffer, 
					dwOutSize,					 
					&dwRet, 
					NULL 
					); 
 
	if( dwRet != 0 ) 
	{ 
		PLOG_ENTRY LogEntry; 
		DWORD      dwCount; 
		CHAR       Buffer[64]; 
 
		for( DWORD i=0 ;i < dwRet; i++ ) 
		{ 
			dwCount = theCtrl.GetItemCount( ); 
 
 
			DWORD offset= i* sizeof( LOG_ENTRY ); 
			 
			LogEntry = (PLOG_ENTRY)(&g_LogBuffer[offset]); 
			sprintf( Buffer, "%d", dwCount ); 
			theCtrl.InsertItem(dwCount, Buffer, 0 ); 
			theCtrl.SetItemText(dwCount, 0 , Buffer ); 
 
			sprintf( Buffer, "%d", LogEntry->DiskNumber ); 
			theCtrl.SetItemText(dwCount, 1 , Buffer ); 
 
			sprintf( Buffer, "%s", (LogEntry->OperationType== 0 )? "READ": "WRITE"); 
			theCtrl.SetItemText(dwCount, 2 , Buffer ); 
 
			sprintf( Buffer, "%d", LogEntry->SectorNumber ); 
			theCtrl.SetItemText(dwCount, 3 , Buffer ); 
 
			sprintf( Buffer, "%d", LogEntry->Length ); 
			theCtrl.SetItemText(dwCount, 4 , Buffer ); 
 
			sprintf( Buffer,  
				     "0x%08x--0x%08x--0x%08x--0x%08x\n", 
					 LogEntry->Data[0], 
					 LogEntry->Data[1], 
					 LogEntry->Data[2], 
					 LogEntry->Data[3] 
					 ); 
			theCtrl.SetItemText(dwCount, 5 , Buffer ); 
		} 
	} 
 
} 
 
void CDiskf_AppView::OnInitialUpdate()  
{ 
	CListView::OnInitialUpdate(); 
	CListCtrl& theCtrl = GetListCtrl(); 
 
	::SendMessage(theCtrl.m_hWnd,  
		          LVM_SETEXTENDEDLISTVIEWSTYLE, 
				  LVS_EX_FULLROWSELECT,  
				  LVS_EX_FULLROWSELECT 
				  ); 
 
	theCtrl.InsertColumn(0,"ID",LVCFMT_LEFT,100); 
	theCtrl.InsertColumn(1,"DiskNumber",LVCFMT_LEFT,100); 
	theCtrl.InsertColumn(2,"OperationType",LVCFMT_LEFT,100); 
	theCtrl.InsertColumn(3,"SectorNumber",LVCFMT_LEFT,100); 
	theCtrl.InsertColumn(4,"Length (in secotr)",LVCFMT_LEFT,100);	 
	theCtrl.InsertColumn(5,"Data (the first 16 byte)",LVCFMT_LEFT,100); 
} 
 
void CDiskf_AppView::OnUpdateBtnStart(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if( m_bLogStared == TRUE ) 
		pCmdUI->Enable(FALSE);	 
	else 
		pCmdUI->Enable(TRUE); 
} 
 
void CDiskf_AppView::OnUpdateBtnStop(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if( m_bLogStared == FALSE ) 
		pCmdUI->Enable(FALSE);	 
	else 
		pCmdUI->Enable(TRUE); 
}