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

📄 test_intrdemodlg.cpp

📁 Windows 2000/XP WDM設備驅動程式開發 使用 Numega 公司出版的軟體快速建置驅動程式
💻 CPP
字号:
// Test_intrdemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test_intrdemo.h"
#include "Test_intrdemoDlg.h"

#include "..\common.h"
#include <winioctl.h>

HWND Print_Event::ViewHwnd = NULL;

BOOL KeepRunning;

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

DWORD WINAPI ServiceThread(LPVOID pParam)
{
	HANDLE		m_hDevice,IOWaiter;
	ULONG		nItems, nBytesRead;
	TIMESTAMP	tsbuf[FIFOSIZE];
	PTIMESTAMP	pts;
	CString		msg;

	m_hDevice = CreateFile("\\\\.\\IntrDemo0",
			GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
            NULL);
	if (m_hDevice == INVALID_HANDLE_VALUE)
	{
		msg.Format("Cannot open device, error 0x%x\n", GetLastError());
		Print_Event::SendEvent(msg);
		return 1;
	}

	IOWaiter = CreateEvent( NULL, TRUE, FALSE, NULL);
	if( IOWaiter==NULL)
	{
		msg.Format("Cannot Create Event, error 0x%x\n", GetLastError());
		Print_Event::SendEvent(msg);
		CloseHandle(m_hDevice);
		return 1;
	}

	OVERLAPPED ol;
	ol.Offset = 0;
	ol.OffsetHigh = 0;
	ol.hEvent = IOWaiter;
	KeepRunning=TRUE;
	for(;;)
	{
		ResetEvent(IOWaiter);
		if (!DeviceIoControl(
			m_hDevice, 
			IOCTL_GET_TIMESTAMP_DATA, 
			NULL, 
			0, 
			tsbuf, 
			sizeof(tsbuf),
			&nBytesRead,
			&ol) )
		{
			if( GetLastError()!=ERROR_IO_PENDING)
			{
				msg.Format("GET_TIMESTAMP_DATA failed %x\n", GetLastError());
				if(KeepRunning) Print_Event::SendEvent(msg);
				goto EXIT;
			}
			while( WaitForSingleObject( IOWaiter, 100)==WAIT_TIMEOUT)
			{
				if(!KeepRunning)
				{
					// Cancel the pending read
					CancelIo(m_hDevice);
					goto EXIT;
				}
			}
			if( !GetOverlappedResult( m_hDevice, &ol, &nBytesRead, FALSE))
			{
				msg.Format("GetOverlappedResult failed %d (ol.Internal=%X)",
								GetLastError(),ol.Internal);
				if(KeepRunning) Print_Event::SendEvent(msg);
				continue;
			}
		}
		nItems = nBytesRead/sizeof(TIMESTAMP);
		for (pts = tsbuf; nItems; nItems--, pts++)
		{
			msg.Format("Interrupt time %08x (High=%08x) count %x\n", pts->ts_time, pts->ts_interrupt_count);
			if(KeepRunning) Print_Event::SendEvent(msg);

		}
	}
EXIT:	CloseHandle(m_hDevice);
		CloseHandle(IOWaiter);
		return 0;	
}

/////////////////////////////////////////////////////////////////////////////
// CTest_intrdemoDlg dialog
CTest_intrdemoDlg::CTest_intrdemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest_intrdemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest_intrdemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CTest_intrdemoDlg::~CTest_intrdemoDlg()
{
}

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

BEGIN_MESSAGE_MAP(CTest_intrdemoDlg, CDialog)
	//{{AFX_MSG_MAP(CTest_intrdemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_PRINTEVENT,OnPrintEvent)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest_intrdemoDlg message handlers

BOOL CTest_intrdemoDlg::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_Event::ViewHwnd = m_cWnd->m_hWnd;
	DWORD Tid;		  
	Thread1=CreateThread(0, 0x1000, ServiceThread, 0, 0, &Tid);

	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 CTest_intrdemoDlg::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 CTest_intrdemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CTest_intrdemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (KeepRunning) {
		KeepRunning=FALSE;
		WaitForSingleObject(Thread1,INFINITE);
	}
}

LONG CTest_intrdemoDlg::OnPrintEvent(UINT, LONG lParam)
{
	Print_Event* pEvent = (Print_Event*)lParam;
	if (pEvent) 
	{
		m_Event.AddString(pEvent->Message);
		delete pEvent;
	}
	return 0;
}

⌨️ 快捷键说明

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