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

📄 test_regsampledlg.cpp

📁 windows 2000/XP WDM设备驱动程序开发 附书光盘 武安河著
💻 CPP
字号:
// Test_RegSampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test_RegSample.h"
#include "Test_RegSampleDlg.h"

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

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

// Class GUID used to open device
//
GUID ClassGuid = RegSampleDevice_CLASS_GUID;

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

/////////////////////////////////////////////////////////////////////////////
// CTest_RegSampleDlg dialog

CTest_RegSampleDlg::CTest_RegSampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest_RegSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest_RegSampleDlg)
		// 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);
	m_hDevice = INVALID_HANDLE_VALUE;
}

CTest_RegSampleDlg::~CTest_RegSampleDlg()
{
	if (m_hDevice != INVALID_HANDLE_VALUE)
		CloseHandle(m_hDevice);	//关闭设备
}

void CTest_RegSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest_RegSampleDlg)
	DDX_Control(pDX, IDOK, m_OK);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest_RegSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CTest_RegSampleDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest_RegSampleDlg message handlers

BOOL CTest_RegSampleDlg::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
	DWORD Error;
	m_hDevice = OpenByInterface( &ClassGuid, 0, &Error); //打开设备
	if (m_hDevice == INVALID_HANDLE_VALUE)
		{
			MessageBox("设备打不开", "错误", MB_OK | MB_ICONHAND);
		}
	else m_OK.EnableWindow(TRUE);	//若打开设备,则使能"确定"按钮

	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_RegSampleDlg::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_RegSampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTest_RegSampleDlg::OnOK() 
{
	//三个读操作
	Test_READ_DWORD();
	Test_READ_STRING();
	Test_READ_BOOLEAN();
}

void CTest_RegSampleDlg::Test_READ_DWORD(void)
{
	ULONG	bufOutput;	// Output from device
	ULONG	nOutput;	// Count written to bufOutput

	// Call device IO Control interface (READ_DWORD) in driver
	if (!DeviceIoControl(m_hDevice,
						 READ_DWORD,
						 NULL,
						 0,
						 &bufOutput,
						 sizeof(ULONG),
						 &nOutput,
						 NULL)
	   )
	{
		MessageBox("READ_DWORD", "错误", MB_OK | MB_ICONHAND);
	}
	else
	{
		CString str;
		str.Format("0x%x",bufOutput);
		SetDlgItemText(IDC_DWORD,str);
	}
}

void CTest_RegSampleDlg::Test_READ_STRING(void)
{
	CHAR	bufOutput[512];	// Output from device
	ULONG	nOutput;		// Count written to bufOutput

	// Call device IO Control interface (READ_STRING) in driver
	if (!DeviceIoControl(m_hDevice,
						 READ_STRING,
						 NULL,
						 0,
						 bufOutput,
						 512,
						 &nOutput,
						 NULL)
	   )
	{
		MessageBox("READ_STRING", "错误", MB_OK | MB_ICONHAND);
	}
	else
	{
		CString str((LPCWSTR)bufOutput);
		SetDlgItemText(IDC_STRING,str);
	}
}

void CTest_RegSampleDlg::Test_READ_BOOLEAN(void)
{
	ULONG	bufOutput;	// Output from device
	ULONG	nOutput;	// Count written to bufOutput

	// Call device IO Control interface (READ_BOOLEAN) in driver
	if (!DeviceIoControl(m_hDevice,
						 READ_BOOLEAN,
						 NULL,
						 0,
						 &bufOutput,
						 sizeof(ULONG),
						 &nOutput,
						 NULL)
	   )
	{
		MessageBox("READ_BOOLEAN", "错误", MB_OK | MB_ICONHAND);
	}
	else SetDlgItemInt(IDC_BOOLEAN,bufOutput);
}

⌨️ 快捷键说明

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