www.gusucode.com > USB驱动编程VC++实战源码-源码程序 > USB驱动编程VC++实战源码-源码程序\code\TestUsb5Dlg.cpp

    //Download by http://www.NewXing.com
// TestUsb5Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestUsb5.h"
#include "TestUsb5Dlg.h"


///////以下为OpenByInterface()用到的头文件(begin)
#define NOCRYPT			// prevent attempt to include missing files
#define _INC_EXCPT		// prevent excpt.h from being included

#include <stdlib.h>
#include <windows.h>
#include <winioctl.h>
#include <devintf.h>	// DriverWorks中的一个头文件

#include "UsbTestDeviceinterface.h"
///////(end)

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

HANDLE hDevice=INVALID_HANDLE_VALUE;
GUID ClassGuid= UsbTestDevice_CLASS_GUID;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestUsb5Dlg dialog

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

void CTestUsb5Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestUsb5Dlg)
	DDX_Text(pDX, IDC_EDIT_REPORT, m_strReport);
	DDX_Text(pDX, IDC_EDIT_WRITE, m_strWrite);
	DDV_MaxChars(pDX, m_strWrite, 64);
	DDX_Text(pDX, IDC_EDIT_READ, m_strRead);
	DDV_MaxChars(pDX, m_strRead, 64);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestUsb5Dlg, CDialog)
	//{{AFX_MSG_MAP(CTestUsb5Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_READ, OnRead)
	ON_BN_CLICKED(IDC_BUTTON_WRITE, OnWrite)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnRefresh)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestUsb5Dlg message handlers

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


	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	OnOpenDevice();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestUsb5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CTestUsb5Dlg::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();
	}
/*	CWnd * pWnd;
	CRect    rect;
	CDC * pDC ;
	//pDC = this->GetDC();

	CBrush brush;

	brush.CreateSolidBrush(RGB(255,255,0));

	pWnd = this->GetDlgItem(IDC_EDIT_READ);
	pWnd->GetClientRect(&rect);
//	CClientDC dc(this);
	pDC = pWnd->GetDC();
	

//	this->ClientToScreen(rect);
	pDC->SelectObject(&brush);
	pDC->FillRect(rect,&brush);

//	this->ClientToScreen(rect);
//	InvalidateRect(rect);
*/
}

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

//DEL void CTestUsb5Dlg::OnOpenDevice() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL 	DWORD	Error;
//DEL 	m_strLog.Empty ();
//DEL 	m_strLog = _T("正在查找设备,请稍候...\r\n");
//DEL 	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
//DEL 
//DEL     hDevice = OpenByInterface( &ClassGuid, 0, &Error);
//DEL 	if (hDevice == INVALID_HANDLE_VALUE)
//DEL 	{
//DEL 		m_strLog+= _T("Error in found device.\r\n");
//DEL 		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
//DEL 	}
//DEL 	else
//DEL 	{
//DEL 		m_strLog += _T("Device found,handle open.\r\n");
//DEL 		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
//DEL 	}
//DEL }

void CTestUsb5Dlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	char	*buf = NULL;
	ULONG	nRead;
    int	    n = 65;

	CString p;
	buf = new char[n+1];
	memset(buf,0,sizeof(char)*n);

	if (buf == NULL)
	{
		m_strLog+= _T("准备读取数据时分配内存失败.\r\n");
		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	}

	if (hDevice == INVALID_HANDLE_VALUE)
	    return ;
	else
	{
    m_strLog+= _T("正在从设备读取数据 ...\r\n");
	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	
    ReadFile(hDevice, buf, n, &nRead, NULL);
    
    m_strRead=buf;
	UpdateData(FALSE);
	m_strLog+= _T("从设备读取的数据是: "+m_strRead+"\r\n");
	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	}
	
	if(buf)
		delete[] buf;
	buf = NULL;

}

void CTestUsb5Dlg::OnWrite() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	char *buf = NULL;
	ULONG	nWritten;
	
	int n=65;
	buf=new char[n+1];
	memset(buf,0,sizeof(char)*n);//设置buf为0
	UpdateData(FALSE);               //更新m_strWrite变量的值
	
	strcpy(buf,m_strWrite);          //把m_strWrite的值写入buf
    CString p;
	if (buf == NULL)
	{
		
		m_strLog+= _T("准备向设备写数据时分配内存失败.\r\n");
		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	}

	if (hDevice == INVALID_HANDLE_VALUE)
		return;
	else
	{

    m_strLog+= _T("正在向设备写数据... \r\n");
	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	WriteFile(hDevice, buf, n, &nWritten, NULL);
	
    p = buf;
    m_strLog+= _T("写入设备的数据是:  "+p+"\r\n");
	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
	}

	if(buf)
		delete[] buf;
	buf = NULL;
}

void CTestUsb5Dlg::OnRefresh() 
{
	// TODO: Add your control notification handler code here
	m_strRead = _T("");
	m_strWrite = _T("");
	m_strReport=_T("");
    
 	UpdateData(FALSE);
    OnOpenDevice();
	UpdateData();
}

HANDLE CTestUsb5Dlg::OpenByInterface(GUID *pClassGuid, DWORD instance, PDWORD pError)
{
    HANDLE hDev;
	CDeviceInterfaceClass DevClass(pClassGuid, pError);

	if (*pError != ERROR_SUCCESS)
		return INVALID_HANDLE_VALUE;

	CDeviceInterface DevInterface(&DevClass, instance, pError);

	if (*pError != ERROR_SUCCESS)
		return INVALID_HANDLE_VALUE;

	hDev = CreateFile(
		DevInterface.DevicePath(),
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
		);

	if (hDev == INVALID_HANDLE_VALUE)
	{
		*pError = GetLastError();
	}
	return hDev; 
}

void CTestUsb5Dlg::OnOpenDevice()
{
    DWORD	Error;
	m_strLog.Empty ();
 	//m_strLog = _T("正在查找设备,请稍候...\r\n");
 	SetDlgItemText(IDC_EDIT_REPORT,m_strLog);

    hDevice = OpenByInterface( &ClassGuid, 0, &Error);
 	if (hDevice == INVALID_HANDLE_VALUE)
 	{
		m_strLog+= _T("查找设备失败.\r\n");
		
 		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
		m_strLog+= _T("请确认设备是否正确连接到计算机.\r\n");
		
 		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
 	}
 	else
 	{
 		m_strLog += _T("查找设备成功.\r\n");
 		SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
 	}
}

HBRUSH CTestUsb5Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO: Change any attributes of the DC here


	// TODO: Return a different brush if the default is not desired
	return hbr;
}