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

📄 multioutdlg.cpp

📁 设计一个读不死与写不死的USB程序.一般USB连写四次,别人不去读那个数据.第五次写这个数据就会死掉.本程序就是解决这个问题。
💻 CPP
字号:
// MultiOutDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiOut.h"
#include "MultiOutDlg.h"

#include "STUSB.h"

#include <process.h>

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

//加载相应的头文件与lib文件
#include "CyAPI.h"
#pragma comment(lib,"CyAPI.lib")

unsigned char Cmd[10];
unsigned char TempBuffer[10];

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

/////////////////////////////////////////////////////////////////////////////
// CMultiOutDlg dialog

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

void CMultiOutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMultiOutDlg)
	DDX_Text(pDX, IDC_Receive, m_strReceive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultiOutDlg, CDialog)
	//{{AFX_MSG_MAP(CMultiOutDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Write, OnWrite)
	ON_BN_CLICKED(IDC_Read, OnRead)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiOutDlg message handlers

BOOL CMultiOutDlg::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

	//打开写管理
	hSTWritePipe = open_file(outPipe);
	//打开读管道
	hSTReadPipe = open_file(inPipe);

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

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

void CMultiOutDlg::OnWrite() 
{
	// TODO: Add your control notification handler code here

	//
	int i;
	//
	unsigned int id;
	//
	HANDLE WriteHANDLE;
	//
	unsigned long TimeOut;
	//
	UpdateData(TRUE);
	//
	::EnableWindow(::GetDlgItem(m_hWnd,IDC_Write),FALSE);
	//
	WriteHANDLE = (HANDLE)::_beginthreadex(NULL,0,&WritePipe,NULL,0,&id);
	//
	TimeOut = ::WaitForSingleObject(WriteHANDLE,3000);

	if ( WAIT_TIMEOUT == TimeOut )
	{
		//
		::TerminateThread(WriteHANDLE,0);
		//
		::CloseHandle(WriteHANDLE);
		//
		m_strReceive +="\r\n";
		//
		m_strReceive +="Write data fail";
		//
		usb_reset(hSTReadPipe);
	}

	if ( WAIT_OBJECT_0 == TimeOut )
	{
		//
		::CloseHandle(WriteHANDLE);
		//
		m_strReceive +="\r\n";

		m_strReceive += "Write data :\r\n";
		
		for (i=0;i<sizeof(Cmd);i++)
		{
			m_strReceive += ( 0x30 + Cmd[i]);
			m_strReceive += " ";
		}
	}

	::EnableWindow(::GetDlgItem(m_hWnd,IDC_Write),TRUE);
	
	UpdateData(FALSE);
}

unsigned int __stdcall WritePipe(void *)
{
	//
	int i;
	//
	int status;
	//
	DWORD ReadNumber; 
	
	//
	for (i=0;i<sizeof(Cmd);i++)
		Cmd[i] = i; 

	//
	if ( (hSTWritePipe == NULL) || ( hSTWritePipe == INVALID_HANDLE_VALUE ) )
	{
		::MessageBox(NULL,"建立写管道错误","建立写管道错误码",MB_OK);
		return 1;
	}

	//
	status = ::WriteFile(hSTWritePipe,&Cmd[0],sizeof(Cmd),&ReadNumber,NULL);
		
	if ( status == FALSE )
	{
		::MessageBox(NULL,"调用WriteFile出错","调用WriteFile出错",MB_OK);
		return 1;
	}

	if ( sizeof(Cmd) != ReadNumber )
	{
		::MessageBox(NULL,"写入数不等于要写入数","写入数不等于要写入数",MB_OK);
		return 1;
	}

	return 0;
}

void CMultiOutDlg::OnRead() 
{
	// TODO: Add your control notification handler code here

	//
	int i;
	//
	unsigned int id;
	//
	HANDLE ReadHANDLE;
	//
	unsigned long TimeOut;
	//
	UpdateData(TRUE);
	//
	::EnableWindow(::GetDlgItem(m_hWnd,IDC_Read),FALSE);
	//
	ReadHANDLE = (HANDLE)::_beginthreadex(NULL,0,&ReadPipe,NULL,0,&id);
	//
	TimeOut = ::WaitForSingleObject(ReadHANDLE,3000);

	if ( WAIT_TIMEOUT == TimeOut )
	{
		//
		::TerminateThread(ReadHANDLE,0);
		//
		::CloseHandle(ReadHANDLE);
		//
		m_strReceive +="\r\n";
		//
		m_strReceive += "Read data fail";
		//
		usb_reset(hSTWritePipe);
	}

	if ( WAIT_OBJECT_0 == TimeOut )
	{
		//
		::CloseHandle(ReadHANDLE);
		//
		m_strReceive += "\r\n";

		m_strReceive += "Receive data :\r\n";

		for (i=0;i<sizeof(TempBuffer);i++)
		{
			m_strReceive += ( 0x30 + TempBuffer[i]);
			m_strReceive += " ";
		}
	}

	::EnableWindow(::GetDlgItem(m_hWnd,IDC_Read),TRUE);
	//
	UpdateData(FALSE);
}

unsigned int __stdcall ReadPipe(void *)
{
	//
	int status;
	//
	DWORD ReadNumber;
	//
	int i;
	//

	for (i=0;i<sizeof(TempBuffer);i++)
		TempBuffer[i] = 0xa5;

	if ( (hSTReadPipe == NULL) || ( hSTReadPipe == INVALID_HANDLE_VALUE ) )
	{
		::MessageBox(NULL,"建立读管道错误","建立读管道错误",MB_OK);
		return 1;
	}

	//
	status = ::ReadFile(hSTReadPipe,&TempBuffer[0],sizeof(TempBuffer),&ReadNumber,NULL);
	
	if ( status == FALSE )
	{
		::MessageBox(NULL,"调用ReadFile出错","调用ReadFile出错",MB_OK);
		return 1;
	}

	if ( sizeof(TempBuffer) != ReadNumber )
	{
		::MessageBox(NULL,"读入数不等于要读入数","读入数不等于要读入数",MB_OK);
		return 1;
	}

	return 0;
}

void CMultiOutDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
	//
	CloseHandle(hSTWritePipe);
	//
	CloseHandle(hSTReadPipe);
}

⌨️ 快捷键说明

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