⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dpmonitordlg.cpp

📁 windows 2000/xpWDM设备驱动程序开发光盘代码
💻 CPP
字号:
// DPMonitorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DPMonitor.h"
#include "DPMonitorDlg.h"

#include "..\ReadWriteDeviceinterface.h"	// Has class GUID definition
#include <winioctl.h>

// This function is found in module OpenByIntf.cpp
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);

GUID ClassGuid = ReadWriteDevice_CLASS_GUID;

typedef short CSHORT;

typedef struct _TIME_FIELDS {
    CSHORT Year;        // range [1601...]
    CSHORT Month;       // range [1..12]
    CSHORT Day;         // range [1..31]
    CSHORT Hour;        // range [0..23]
    CSHORT Minute;      // range [0..59]
    CSHORT Second;      // range [0..59]
    CSHORT Milliseconds;// range [0..999]
    CSHORT Weekday;     // range [0..6] == [Sunday..Saturday]
} TIME_FIELDS;
typedef TIME_FIELDS *PTIME_FIELDS;

HWND Print_Error::ViewHwnd = NULL;
HWND ViewHwnd1 = NULL;

volatile BOOL KeepRunning;

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

UINT ServiceThread(LPVOID pParam)
{
	HANDLE	m_hDevice,IOWaiter;
	char	*buf;
	DWORD	Error;
	ULONG	nBytesRead;
	CString	msg;

	m_hDevice = OpenByInterface( &ClassGuid, 0, &Error);
	//异步方式,见OpenByIntf.cpp文件中的CreateFile函数参数
	if (m_hDevice == INVALID_HANDLE_VALUE)
	{
		msg.Format("Cannot open device, error 0x%x\n", GetLastError());
		Print_Error::SendError(msg);
		return 1;
	}

	IOWaiter = CreateEvent( NULL, TRUE, FALSE, NULL);	//创建手动重置事件
	if( IOWaiter==NULL)
	{
		msg.Format("Cannot Create Event, error 0x%x\n", GetLastError());
		Print_Error::SendError(msg);
		CloseHandle(m_hDevice);
		return 1;
	}

	OVERLAPPED ol; 	//OVERLAPPED结构对象初始化
	ol.Offset = 0;
	ol.OffsetHigh = 0;
	ol.hEvent = IOWaiter;
	KeepRunning=TRUE; 	//线程运行标志
	for(;;)
	{
		ResetEvent(IOWaiter); 	//复位事件
		buf = new char[512];
		if (buf==NULL)
		{
			CloseHandle(m_hDevice);
			CloseHandle(IOWaiter);
			return 1;
		}
		if (!ReadFile(
			m_hDevice, 
			buf,
			512,
			&nBytesRead,
			&ol) ) //异步方式调用
		{
			if( GetLastError()!=ERROR_IO_PENDING)
			{	//返回错误
				msg.Format("GET_TIMESTAMP_DATA failed %x\n", GetLastError());
				Print_Error::SendError(msg);
				goto EXIT;
			}
			while( WaitForSingleObject( IOWaiter, 100)==WAIT_TIMEOUT)//等待IO完成
			{	//正常超时
				if(!KeepRunning)
				{
					// Cancel the pending read
					CancelIo(m_hDevice); 	//取消IO操作
					goto EXIT;
				}
			}
			//IO完成后,获取数据
			if( !GetOverlappedResult( m_hDevice, &ol, &nBytesRead, FALSE))
			{	//返回错误
				msg.Format("GetOverlappedResult failed %d (ol.Internal=%X)",
								GetLastError(),ol.Internal);
				Print_Error::SendError(msg);
				continue;
			}
		}
		buf[nBytesRead]=0;
		::PostMessage(ViewHwnd1, WM_PRINTEVENT, 0, (LPARAM)buf); //显示信息
	}
EXIT:	delete buf;
		CloseHandle(m_hDevice);	//关闭设备
		CloseHandle(IOWaiter); 	//关闭事件
		return 0;	
}

/////////////////////////////////////////////////////////////////////////////
// CDPMonitorDlg dialog

CDPMonitorDlg::CDPMonitorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDPMonitorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDPMonitorDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDPMonitorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDPMonitorDlg)
	DDX_Control(pDX, IDC_LIST1, m_Event);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDPMonitorDlg, CDialog)
	//{{AFX_MSG_MAP(CDPMonitorDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_PRINTEVENT,OnPrintEvent)
	ON_MESSAGE(WM_PRINTERROR,OnPrintError)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDPMonitorDlg message handlers

BOOL CDPMonitorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	KeepRunning=FALSE;
	CWnd* m_cWnd=AfxGetMainWnd();
	Print_Error::ViewHwnd = m_cWnd->m_hWnd;
	ViewHwnd1 = m_cWnd->m_hWnd;

	m_pThread=AfxBeginThread(ServiceThread, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
	//创建并挂起线程
	if(m_pThread==NULL)	{}
	else m_pThread->ResumeThread(); //恢复线程运行

	m_Event.InsertColumn(0,"Time",LVCFMT_LEFT,60,-1);	
	m_Event.InsertColumn(1,"Driver",LVCFMT_LEFT,100,-1);	
	m_Event.InsertColumn(2,"Info",LVCFMT_LEFT,240,-1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDPMonitorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDPMonitorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDPMonitorDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	m_Event.DeleteAllItems();
}

LONG CDPMonitorDlg::OnPrintEvent(UINT, LONG lParam)
{
	char *Buf,*event;
	CString msg;
	int ListnowItem;

	Buf=(char *)lParam;
	PTIME_FIELDS pTF = (PTIME_FIELDS)lParam;
	CTime EventTime(pTF->Year,pTF->Month,pTF->Day,pTF->Hour,pTF->Minute,pTF->Second);
	msg.Format("%s",EventTime.Format("%H:%M:%S"));
	ListnowItem=m_Event.GetItemCount( );
	m_Event.InsertItem(ListnowItem,msg);	//显示时间
	lParam+=sizeof(TIME_FIELDS);
	event=(char *)lParam;
	msg.Format("%s",event);
	int FirstTabPos = msg.Find(':');
	if( FirstTabPos==-1) return 0;
	CString Driver = msg.Left(FirstTabPos);
	m_Event.SetItemText(ListnowItem,1,Driver);//显示驱动程序名
	CString Info = msg.Mid(FirstTabPos+1);
	m_Event.SetItemText(ListnowItem,2,Info);//显示信息
	delete Buf;
	return 0;
}

void CDPMonitorDlg::OnPrintError(UINT, LONG lParam)
{
	CTime t = CTime::GetCurrentTime();
	CString msg;
	msg.Format("%s",t.Format("%H:%M:%S"));
	int ListnowItem;
	ListnowItem=m_Event.GetItemCount( );
	ListnowItem=m_Event.InsertItem(ListnowItem,msg);//显示时间
	m_Event.SetItemText(ListnowItem,1,"Monotor");

	Print_Error* pError = (Print_Error*)lParam;
	if (pError) 
	{
		m_Event.SetItemText(ListnowItem,2,pError->Message);
		//显示信息
		delete pError;
	}
}

void CDPMonitorDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	if (KeepRunning)
	{
		KeepRunning=FALSE;
		WaitForSingleObject(m_pThread->m_hThread,INFINITE);	//等待线程结束
		m_pThread = NULL;
	}
	CDialog::OnClose();
}

⌨️ 快捷键说明

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