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

📄 usdlg.cpp

📁 vc编写的usb程序
💻 CPP
字号:
// usDlg.cpp : implementation file
//

#include "stdafx.h"
#include "us.h"
#include "usDlg.h"
#include "Dbt.h"


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

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

/////////////////////////////////////////////////////////////////////////////
// CUsDlg dialog

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

void CUsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CUsDlg message handlers

BOOL CUsDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

LRESULT CUsDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CString   str;  
	DEV_BROADCAST_HDR*   dhr   =   (DEV_BROADCAST_HDR   *)lParam;  
	switch(wParam)  
	{  
 
	case   DBT_DEVICEARRIVAL:  
		if(dhr->dbch_devicetype   ==   DBT_DEVTYP_VOLUME)  
		{  
			CListBox * plist=(CListBox*) GetDlgItem(IDC_LIST1);
			int n;
			
			PDEV_BROADCAST_VOLUME   lpdbv   =   (PDEV_BROADCAST_VOLUME)dhr;  
		
            int k = 0;
			DWORD			MaxDriveSet, CurDriveSet;
			DWORD			drive, drivetype;
			TCHAR			szBuf[300];
			PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
			char* p;
	
			
			for(k=0; k<26; k++)
				szMoveDiskName[k] = '\0';	
			k = 1;		
			MaxDriveSet = CurDriveSet = 0;
			
			MaxDriveSet = GetLogicalDrives();
			CurDriveSet = MaxDriveSet;
			for ( drive = 0; drive < 32; ++drive )  
			{
				if ( MaxDriveSet & (1 << drive) )  
				{
					DWORD temp = 1<<drive;
					_stprintf( szDrvName, _T("%c:\\"), 'A'+drive );
					if(GetDriveType( szDrvName )== DRIVE_REMOVABLE)
					{
						drivetype = DRVREMOVE;
						
					sprintf(szBuf, "\\\\?\\%c:", 'A'+drive);
                    hDevice = CreateFile(szBuf, GENERIC_READ,
						FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
					


					if (hDevice != INVALID_HANDLE_VALUE)
					{
						
						pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
						
						pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
						
						if(GetDisksProperty(hDevice, pDevDesc))
						{
							szMoveDiskName[k] = chFirstDriveFromMask(temp);
							szMoveDiskName[0]=k;
							k++;
						
							p= (char*)pDevDesc;
							str = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
							n=plist->GetCount();
		                 	plist->InsertString(n,str);
	
							str=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
							n=plist->GetCount();
		         			plist->InsertString(n,str);
							
							str = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)");
							n=plist->GetCount();
		         			plist->InsertString(n,str);
							
							str = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");
							n=plist->GetCount();
		         			plist->InsertString(n,str);
						}
						
						delete pDevDesc;
						CloseHandle(hDevice);
					}
					}
				}
				
			}
			
			str.Format("\r\n U盘符:\r\n%s",&szMoveDiskName[1]);
			n=plist->GetCount();
		    plist->InsertString(n,str);


			
			if(szMoveDiskName[0]==0)
			{
				str="\r\n没有U盘\r\n";
			     n=plist->GetCount();
		        plist->InsertString(n,str);
			}
           
			
			
		}  
        
         
		//AfxMessageBox(str);
		break;  
	case   DBT_DEVICEREMOVECOMPLETE:  
		if(dhr->dbch_devicetype   ==   DBT_DEVTYP_VOLUME)  
		{  
			PDEV_BROADCAST_VOLUME   lpdbv   =   (PDEV_BROADCAST_VOLUME)dhr;  
			if(lpdbv->dbcv_flags   &   DBTF_MEDIA)  
			{  
				str.Format("Drive   %c   拔除");  
			}  
			else  
			{  
				str.Format("Drive   %c   拔除");  
			}  
			AfxMessageBox(str);  

			CListBox * plist=(CListBox*) GetDlgItem(IDC_LIST1);
			int n=plist->GetCount();
			CString s;
			s.Format("gdvrv");
	        plist->InsertString(n,s);


		}  
		break;  
	default:  
		break;  
	}  
    




	return CDialog::WindowProc(message, wParam, lParam);

}
char CUsDlg::chFirstDriveFromMask(ULONG unitmask)
{
	char i;
	for (i = 0; i < 26; ++i)  
	{
		if (unitmask & 0x1) 
			break;
		unitmask = unitmask >> 1;
	}
    return (i + 'A');
}


BOOL CUsDlg::GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
	STORAGE_PROPERTY_QUERY	Query;	
	DWORD dwOutBytes;				
	BOOL bResult;					
	
	Query.PropertyId = StorageDeviceProperty;
	Query.QueryType = PropertyStandardQuery;
	
	bResult = ::DeviceIoControl(hDevice,			
		IOCTL_STORAGE_QUERY_PROPERTY,			
		&Query, sizeof(STORAGE_PROPERTY_QUERY),	
		pDevDesc, pDevDesc->Size,				
		&dwOutBytes,							
		(LPOVERLAPPED)NULL);					
	
	return bResult;
}

⌨️ 快捷键说明

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