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

📄 sniffusbdlg.cpp

📁 禁止usb,非常好的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SniffUSBDlg.cpp : implementation file
//

#include "StdAfx.h"
#include "SniffUSB.h"
#include "SniffUSBDlg.h"
#include "devicemgr.h"
#include "device.h"
#include "multisz.h"
#include <setupapi.h>
#include <winsvc.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

#include  <tchar.h>  
#include  <stdio.h>  
#include  <windows.h>  
#include  <devguid.h>  

#define  DWORD_PTR  DWORD  
#define  ULONG_PTR  DWORD  
 
//#include <hidsdi.h>
extern  "C"  {    
#include  "hidsdi.h"
} 

//  需加入hid.lib  
 
#include  <setupapi.h>  
//  需加入setupapi.lib  
 
#include  <regstr.h>  
#include  <winbase.h>  

#include  <cfgmgr32.h> 
//  需要加入cfgmgr32.lib  
 
#include  <initguid.h>  
//#include  <usbiodef.h>  
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,  
   0xA5DCBF10L,  0x6530,  0x11D2,  0x90,  0x1F,  0x00,  0xC0,  0x4F,  0xB9,  0x51,  0xED);  
#define  GUID_CLASS_USB_DEVICE                      GUID_DEVINTERFACE_USB_DEVICE  


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

/////////////////////////////////////////////////////////////////////////////
// CSniffUSBDlg dialog

CSniffUSBDlg::CSniffUSBDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSniffUSBDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSniffUSBDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_sFilterName.LoadString(IDS_FILTERNAME);
    m_sFilterName.MakeLower();
    m_sLowerFilters.LoadString(IDS_LOWERFILTERS);
}

void CSniffUSBDlg::DoDataExchange(CDataExchange* pDX)
{
	
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSniffUSBDlg)
	DDX_Control(pDX, IDC_USBDEVS, m_cDevs);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSniffUSBDlg, CDialog)
	//{{AFX_MSG_MAP(CSniffUSBDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
	ON_BN_CLICKED(IDC_INSTALL, OnInstall)
	ON_BN_CLICKED(IDC_UNINSTALL, OnUninstall)
	ON_NOTIFY(NM_RCLICK, IDC_USBDEVS, OnRclickUsbdevs)
	ON_COMMAND(ID_SNOOPUSB_INSTALL, OnSnoopusbInstall)
	ON_COMMAND(ID_SNOOPUSB_UNINSTALL, OnSnoopusbUninstall)
	ON_BN_CLICKED(IDC_REPLUG, OnReplug)
	ON_COMMAND(ID_SNOOPUSB_REPLUG, OnSnoopusbReplug)
	ON_BN_CLICKED(IDC_FILTERINSTALL, OnFilterInstall)
	//}}AFX_MSG_MAP
	ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSniffUSBDlg message handlers

BOOL CSniffUSBDlg::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
	
    // we want report style and more...
    m_cDevs.ModifyStyle(LVS_TYPEMASK, 
        LVS_AUTOARRANGE | LVS_SORTASCENDING | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS);
    
    // setup the columns
    CString sHeading;
    sHeading.LoadString(IDS_COL_VIDPID);
    m_cDevs.InsertColumn(0, sHeading);
    m_nCX0 = 5*m_cDevs.GetStringWidth(sHeading)/4;

    sHeading.LoadString(IDS_COL_FILTERINSTALLED);
    m_cDevs.InsertColumn(1, sHeading, LVCFMT_CENTER);
    m_nCX1 = 5*m_cDevs.GetStringWidth(sHeading)/4;

    sHeading.LoadString(IDS_COL_DESCRIPTION);
    m_cDevs.InsertColumn(2, sHeading);
    m_nCX2 = 5*m_cDevs.GetStringWidth(sHeading)/4;
	

/*	sHeading.LoadString(IDS_COL_PRESENT);
	m_cDevs.InsertColumn(3, sHeading);

	sHeading.LoadString(IDS_COL_DRIVER);
	m_cDevs.InsertColumn(4, sHeading);
*/

    m_cDevs.SetColumnWidth(0, m_nCX0);
    m_cDevs.SetColumnWidth(1, m_nCX1);
    m_cDevs.SetColumnWidth(2, m_nCX2);

    // fill it up, please
	
    OnRefresh();
	CheckService();
    
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSniffUSBDlg::OnRefresh() 
{
	CString selectedDriver;
	DWORD idx=0;
	// get the currently selected item

	//GetSelectedDriver(selectedDriver);

	// reset the list of devices

	m_cDevs.DeleteAllItems();

	// loop through the list of device

	CDeviceMgr mgr;
	POSITION pos,pos_prev;
	pos = mgr.GetHeadPosition();
	while(pos != NULL)
	{
		CString sName, sFilter, sDescription, sPresent, sDriver;
		POSITION current;
		CDevice& device = mgr.GetNext(pos);
    
		sName = device.GetHardwareID();
		sDescription = device.GetDeviceDesc();
		sDriver = device.GetDriver();
		pos_prev = mgr.GetHeadPosition();
		current = mgr_prev.FindDevice(sName);
		
		
		if(current == NULL)
		{
			SP_DEVINFO_DATA  devinfo;
			devinfo.cbSize = sizeof(devinfo);

			CString sName, sFilter, sDescription;

		//	BYTE Buffer[200];
			DWORD BufferSize = 0;
		//	DWORD DataType;
	
			if (!SetupDiEnumDeviceInfo(mgr.m_hDev,idx,&devinfo))
			{
				if (GetLastError() != ERROR_NO_MORE_ITEMS)
				{
					//MessageBox("Error while enumerating USB devices");
					TRACE("SetupDiEnumDeviceInfo = %d\n",GetLastError());
				}
				break;
			}
			{
						CWaitCursor waitcursor;
					
						if (SetupDiRemoveDevice(mgr.m_hDev,&devinfo))
						{
							if (SetupDiUnremoveDevice(mgr.m_hDev,&devinfo))
								;
//								MessageBox("Gotcha!");
							else
								TRACE("SetupDiUnremoveDevice = %d\n",GetLastError());
						}
						else
							TRACE("SetupDiRemoveDevice = %d\n",GetLastError());
			}
			
		//	mgr.m_hDev
		//	device.Forbid();
			
			//ForbidUSB();
		}
		idx++;
		//SetupDiDestroyDeviceInfoList(mgr.m_hDev);
			
	}

	
	
	pos = mgr_prev.GetHeadPosition();
	while (pos != NULL)
	{
		CString sName, sFilter, sDescription, sPresent, sDriver;
		CDevice& device = mgr_prev.GetNext(pos);
    
		sName = device.GetHardwareID();
		sDescription = device.GetDeviceDesc();
		sDriver = device.GetDriver();

		if (device.GetPresent())
			sPresent = "Yes";
		else
			sPresent = "No";

		sFilter.LoadString(IDS_NOTINSTALLED);
//		if (device.GetLowerFilters().FindString(m_sFilterName))
//			sFilter.LoadString(IDS_INSTALLED);

		// add to the list control

        int nIndex = m_cDevs.InsertItem(-1, sName);
        m_cDevs.SetItemText(nIndex, 1, sFilter);
        m_cDevs.SetItemText(nIndex, 2, sDescription);
        m_cDevs.SetItemText(nIndex, 3, sPresent);
		m_cDevs.SetItemText(nIndex, 4, sDriver);

		// set again the currently selected item

		if (sDriver == selectedDriver)
			m_cDevs.SetItemState(nIndex, LVIS_SELECTED, LVIS_SELECTED);

    }

	// automatically adjust the width of each column

    m_cDevs.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER );
    m_cDevs.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER );
    m_cDevs.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER );
    m_cDevs.SetColumnWidth(3, LVSCW_AUTOSIZE_USEHEADER );
    m_cDevs.SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER );

	// update the log filename and size
	//CheckLogFile();
	
}
/*
void CSniffUSBDlg::OnRefresh() 
{
    UINT nSelCount = m_cDevs.GetSelectedCount();
    int nSelection = -1;
    CString sSelection;
    if(0 < nSelCount)
    {
        nSelection = m_cDevs.GetNextItem(-1, LVNI_SELECTED);
        sSelection = m_cDevs.GetItemText(nSelection, 0);
    }
    m_cDevs.DeleteAllItems();
    int cx0 = m_nCX0;
    int cx1 = m_nCX1;
    int cx2 = m_nCX2;
    
 	HDEVINFO hdev;
	
	hdev = SetupDiGetClassDevs(0,NULL,0,DIGCF_ALLCLASSES);
	if (hdev == INVALID_HANDLE_VALUE )
	{
		MessageBox("Unable to enumerate USB device");
		TRACE("SetupDiGetClassDevs = %d\n",GetLastError());
		return ;
	}

	for (DWORD idx=0;;idx++)
	{
		SP_DEVINFO_DATA  devinfo;
		devinfo.cbSize = sizeof(devinfo);

		CString sName, sFilter, sDescription;

		BYTE Buffer[200];
		DWORD BufferSize = 0;
		DWORD DataType;

		if (!SetupDiEnumDeviceInfo(hdev,idx,&devinfo))
		{
			if (GetLastError() != ERROR_NO_MORE_ITEMS)
			{
				MessageBox("Error while enumerating USB devices");
				TRACE("SetupDiEnumDeviceInfo = %d\n",GetLastError());
			}
			break;
		}

		if (SetupDiGetDeviceRegistryProperty(hdev,&devinfo,SPDRP_FRIENDLYNAME,//SPDRP_ENUMERATOR_NAME,
			&DataType,Buffer,sizeof(Buffer),&BufferSize))
		{
			if (strcmp((const char *)Buffer,"USB")!=0)
				continue;
		}
 
		if (SetupDiGetDeviceRegistryProperty(hdev,&devinfo,SPDRP_HARDWAREID  ,
			&DataType,Buffer,sizeof(Buffer),&BufferSize))
		{
			sName = (const char *)Buffer;
		}

		if (SetupDiGetDeviceRegistryProperty(hdev,&devinfo,SPDRP_DEVICEDESC,
			&DataType,Buffer,sizeof(Buffer),&BufferSize))
		{
			sDescription = (const char *) Buffer;
		}

		sFilter.LoadString(IDS_NOTINSTALLED);
		if (SetupDiGetDeviceRegistryProperty(hdev,&devinfo,SPDRP_LOWERFILTERS ,
			&DataType,Buffer,sizeof(Buffer),&BufferSize))
		{
			if (strcmp((const char *)Buffer,"usbsnoop")==0)
				sFilter.LoadString(IDS_INSTALLED);
		}

        int nIndex = m_cDevs.InsertItem(-1, sName);
        m_cDevs.SetItemText(nIndex, 1, sFilter);
        m_cDevs.SetItemText(nIndex, 2, sDescription);
        cx0 = max(cx0, 5*m_cDevs.GetStringWidth(sName)/4);
        cx1 = max(cx1, 5*m_cDevs.GetStringWidth(sFilter)/4);
        cx2 = max(cx2, 5*m_cDevs.GetStringWidth(sDescription)/4);


    }

	SetupDiDestroyDeviceInfoList(hdev);

    m_cDevs.SetColumnWidth(0, cx0);
    m_cDevs.SetColumnWidth(1, cx1);
    m_cDevs.SetColumnWidth(2, cx2);

    if(-1 != nSelection)
    {
        LVFINDINFO fi;
        ZeroMemory(&fi, sizeof(fi));
        fi.flags = LVFI_STRING;
        fi.psz = sSelection;
        nSelection = m_cDevs.FindItem(&fi);
        m_cDevs.SetItemState(nSelection, LVIS_SELECTED, LVIS_SELECTED);
    }
}
*/

void CSniffUSBDlg::GetDescriptionForVidPid(LPCTSTR szVidPid, CString &sDescription)
{
    sDescription = "?";
    TCHAR szEnumKey[MAX_PATH];
    _tcscpy(szEnumKey, "Enum\\USB\\");
    _tcscat(szEnumKey, szVidPid);
    HKEY hKey = NULL;
    if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szEnumKey, 0, KEY_ALL_ACCESS, &hKey))
    {
        DWORD dwIndex = 0;
        TCHAR sName[MAX_PATH];
        while((sDescription == "?") && (ERROR_SUCCESS == RegEnumKey(hKey, dwIndex, sName, MAX_PATH)))
        {
            TRACE("Enumerated >%s<\n", sName);
            HKEY hInstKey = NULL;
            TCHAR szInstanceKey[MAX_PATH];
            _tcscpy(szInstanceKey, szEnumKey);
            _tcscat(szInstanceKey, _T("\\"));
            _tcscat(szInstanceKey, sName);
            if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szInstanceKey, 0, KEY_ALL_ACCESS, &hInstKey))
            {
                TCHAR szData[MAX_PATH];
                DWORD dwType = REG_SZ;
                DWORD dwSize = MAX_PATH;
                if(ERROR_SUCCESS == RegQueryValueEx(hInstKey, "DeviceDesc", NULL, &dwType, (LPBYTE) szData, &dwSize))
                {
                    sDescription = szData;

⌨️ 快捷键说明

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