Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I wrote this program for detect usb hid class but I need to Extract all field of PDEV_BROADCAST_DEVICEINTERFACE after
PDEV_BROADCAST_DEVICEINTERFACE pdbch = (PDEV_BROADCAST_DEVICEINTERFACE)dwData;
line ..
I saw msdn help but i couldn't do it help me please..
my point is fill devicetype and class guides

IMAGE:
http://tinypic.com?ref=2i1e788





C#
#include "stdafx.h"
#include "USBDetect.h"
#include "USBDetectDlg.h"
#include "afxdialogex.h"
#include "Guid.h"    // includes my usb HID class number
#include "Winuser.h"
#include "Dbt.h"


C#
// TODO: Add extra initialization here
    DEV_BROADCAST_DEVICEINTERFACE dbch;
    dbch.dbcc_size = sizeof(dbch);
    dbch.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    //for (int i = 0; i < _countof(GuidInterfaceList); i++)
    for (int i = 0; i < sizeof(GuidInterfaceList); i++)
    {
        //dbch.dbcc_classguid = 
        static int count=0;
        count++;
        dbch.dbcc_classguid = GuidInterfaceList[i];
        dbch.dbcc_name[0] = '\0';
        NotificationHandle = RegisterDeviceNotification( GetSafeHwnd(), &dbch, DEVICE_NOTIFY_WINDOW_HANDLE);


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


C#
BOOL CUSBDetectDlg::OnDeviceChange(UINT EventType, DWORD dwData)
{
	char Word [50];
	char Word1 [57]; 
	char Word2 [50]; 
	char Word3 [50];
	char ch = ' ';
	CString Msg = "duh";
	switch( EventType)
	{
	case DBT_CONFIGCHANGECANCELED:
		Msg.Format("DBT_CONFIGCHANGECANCELED");
		break;
	case DBT_CONFIGCHANGED:
		Msg.Format("DBT_CONFIGCHANGED");
		break;
	case DBT_CUSTOMEVENT:
		Msg.Format("DBT_CUSTOMEVENT");
		break;
	case DBT_DEVICEARRIVAL:
		Msg.Format("DBT_DEVICEARRIVAL");
		break;
	case DBT_DEVICEQUERYREMOVE:
		Msg.Format("DBT_DEVICEQUERYREMOVE");
		break;
	case DBT_DEVICEQUERYREMOVEFAILED:
		Msg.Format("DBT_DEVICEQUERYREMOVEFAILED");
		break;
	case DBT_DEVICEREMOVEPENDING:
		Msg.Format("DBT_DEVICEREMOVEPENDING");
		break;
	case DBT_DEVICEREMOVECOMPLETE:
		Msg.Format("DBT_DEVICEREMOVECOMPLETE");
		break;
	case DBT_DEVICETYPESPECIFIC:
		Msg.Format("DBT_DEVICETYPESPECIFIC");
		break;
	case DBT_QUERYCHANGECONFIG:
		Msg.Format("DBT_QUERYCHANGECONFIG");
		break;
	case DBT_DEVNODES_CHANGED:
		Msg.Format("DBT_DEVNODES_CHANGED");
		break;
	case DBT_USERDEFINED:
		Msg.Format("DBT_USERDEFINED");
		break;
	default:
		Msg.Format("Event type %d",EventType);
	}

	PDEV_BROADCAST_DEVICEINTERFACE pdbch = (PDEV_BROADCAST_DEVICEINTERFACE)dwData;

	// @ ToDo Extract all field of PDEV_BROADCAST_DEVICEINTERFACE 




C#
if( pdbch!=NULL && pdbch->dbcc_devicetype==DBT_DEVTYP_DEVICEINTERFACE)
    {
        CString Msg2;
        Msg2.Format("%s: %s",Msg,pdbch->dbcc_name);
        Msg = Msg2;

    }
    if (Msg == "DBT_DEVNODES_CHANGED")
    {
        CListBox* EventList = (CListBox*)GetDlgItem(IDC_EVENT_LIST);
        EventList->AddString(Msg);
    }
    else
    {
        strncpy_s(Word,Msg,17);
        Word[17] = '\0';
        if ( strcmp ( Word, "DBT_DEVICEARRIVAL" )== 0 )
        {
            strncpy_s(Word1,Msg,44);
            Word1[44] = '\0';
            strncpy_s(Word2, Word1, 17);
            Word2[17] = '\0';
            strncpy_s(Word3, &Word1[27], 17);
            Word3[17] = '\0';
        }
        else
        {
            strncpy_s(Word,Msg,24);
            Word[24] = '\0';
            if ( strcmp ( Word, "DBT_DEVICEREMOVECOMPLETE" )== 0 )
            {                   strncpy_s(Word1,Msg,51);
                Word1[51] = '\0';
                strncpy_s(Word2, Word1, 24);
                Word2[24] = '\0';
                strncpy_s(Word3, &Word1[34], 17);
                Word3[17] = '\0';               }
            }
            if (Word3[0]== 'V')
            {
                CListBox* EventList = (CListBox*)GetDlgItem(IDC_EVENT_LIST);
                EventList->AddString(Word2);
                EventList->AddString(Word3);
            }
        }
    return TRUE; // or BROADCAST_QUERY_DENY to deny a query remove


C#
BOOL CUSBDetectDlg::DestroyWindow()
{
    if( NotificationHandle)
    {
        UnregisterDeviceNotification(NotificationHandle);
        NotificationHandle = NULL;
    }
    return CDialog::DestroyWindow();
Posted
Updated 20-Nov-13 2:36am
v10

1 solution

The dwData parameter of the device change handler is a pointer to a event specific structure. So you must cast to the type of structure used by the specific event. Note that some events did not use this parameter (dwData is set to zero).

Device structures (used by DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE and others) have identical members at the beginning. These identical members are covered by the DEV_BROADCAST_HDR structure. So you should first cast to this structure, check the device type, and cast then to the device speficic structure:
C++
if (dwData && 
    (DBT_DEVICEARRIVAL == EventType || DBT_DEVICEREMOVECOMPLETE == EventType))
{
    PDEV_BROADCAST_HDR pHdr = reinterpret_cast<PDEV_BROADCAST_HDR>(dwData);
    switch (pHdr->dbch_devicetype)
    {
    case DBT_DEVTYP_DEVICEINTERFACE :
        {
            PDEV_BROADCAST_DEVICEINTERFACE pIf = 
                reinterpret_cast<PDEV_BROADCAST_DEVICEINTERFACE>(dwData);
            // Handle device class here
        }
        break;
    case DBT_DEVTYP_PORT :
        {
            PDEV_BROADCAST_PORT pPort = 
                reinterpret_cast<PDEV_BROADCAST_PORT>(dwData);
            // Handle ports (COM, LPT) here
        }
        break;
    // Handle other types here
    }
}
 
Share this answer
 
Comments
hooman khebrat 20-Nov-13 8:10am    
Thank you so much man , where is the device type ?! these are my device information and class I don't know how can I fix it :( I want to detect magnetic card reader only and report me usb status (change, remove,arrival..)
I'm getting counfus :(
Jochen Arndt 20-Nov-13 8:41am    
The device type is DBT_DEVTYP_DEVICEINTERFACE. There is no field that identifies an 'USB card reader'. You can compare pIf->dbcc_classguid with the list of registered GUIDs to know for which one the event has been signaled. The only other available information is pIf->dbcc_name which may be used to guess the type of the device.

It all depends on the GUIDs that are registered. You can register a specific device by using it's unique GUID (e.g. by getting it from the installer INF file) or groups of devices by using pre-defined GUIDs (e.g. all USB devices).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900