Click here to Skip to main content
15,887,294 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:01
Joseph Dempsey5-Nov-10 8:01 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:02
professional«_Superman_»5-Nov-10 8:02 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:17
Joseph Dempsey5-Nov-10 8:17 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:56
professional«_Superman_»5-Nov-10 8:56 
GeneralRe: Compiler Error in Template Functions...? Pin
Aescleal5-Nov-10 9:12
Aescleal5-Nov-10 9:12 
AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Aescleal5-Nov-10 9:08
Aescleal5-Nov-10 9:08 
AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Paul Michalik6-Nov-10 1:42
Paul Michalik6-Nov-10 1:42 
QuestionUsing a Message-Only Window to Receive Device Change Notification Messages [modified] Pin
Jim Fell5-Nov-10 5:11
Jim Fell5-Nov-10 5:11 
I'm trying to get write a message-only window to receive device change notification messages for a USB device. I am using MFC, C++, and Visual Studio 2008. Everything compiles, and it runs without crashing or locking up, but the event handler is never triggered. The device of interest is installed on Windows as a virtual COM port.

My main application instantiates the class described below then waits for a character input from the keyboard polling using a while loop. It is during this wait time that I remove and insert my USB device expecting the event to get fired.

class CMessageOnlyWindow : public CWnd
{
    DECLARE_DYNAMIC(CMessageOnlyWindow)
private:
    DEV_BROADCAST_DEVICEINTERFACE * _pDevIF; // The notification filter.
    HDEVNOTIFY _hNotifyDev;             // The device notification handle.
public:
    CMessageOnlyWindow();
    virtual ~CMessageOnlyWindow();
protected:
    afx_msg BOOL OnDeviceChange( UINT nEventType, DWORD dwData );
private:
    void RegisterNotification( void );
    void UnregisterNotification( void );
protected:
    DECLARE_MESSAGE_MAP()               // Must be last.
};


For simplicity, I've removed all the cleanup and error-handling:

DEFINE_GUID(GUID_INTERFACE_CP210x, 0x993f7832, 0x6e2d, 0x4a0f, \
    0xb2, 0x72, 0xe2, 0xc7, 0x8e, 0x74, 0xf9, 0x3e);

IMPLEMENT_DYNAMIC(CMessageOnlyWindow, CWnd)

CMessageOnlyWindow::CMessageOnlyWindow()
{
    CString cstrWndClassName = ::AfxRegisterWndClass( NULL );
    BOOL bCreated = this->CreateEx( 0, cstrWndClassName,
        L"CMessageOnlyWindow", 0, 0, 0, 0, 0, HWND_MESSAGE, 0 );
    this->RegisterNotification();
}

CMessageOnlyWindow::~CMessageOnlyWindow() {}

BEGIN_MESSAGE_MAP(CMessageOnlyWindow, CWnd)
    ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()

afx_msg BOOL CMessageOnlyWindow::OnDeviceChange( UINT nEventType, DWORD dwData )
{
    switch ( nEventType ) // <-- Never gets here.
    {
    case DBT_DEVICEARRIVAL:
        break;

    case DBT_DEVICEREMOVECOMPLETE:
        break;

    default:
        break;
    }

    return TRUE;
}

void CMessageOnlyWindow::RegisterNotification(void)
{
    _pDevIF = (DEV_BROADCAST_DEVICEINTERFACE *)malloc( sizeof(DEV_BROADCAST_DEVICEINTERFACE) );
    memset( _pDevIF, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE) );
    _pDevIF->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    _pDevIF->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    _pDevIF->dbcc_classguid = GUID_INTERFACE_CP210x;
    _hNotifyDev = RegisterDeviceNotification( this->m_hWnd, _pDevIF, DEVICE_NOTIFY_WINDOW_HANDLE );
}

void CMessageOnlyWindow::UnregisterNotification(void)
{
    UnregisterDeviceNotification( _hNotifyDev );
}


Any thoughts or suggestions would be much appreciated. If any details are missing, let me know, and I will be glad to add them. Thanks.

Does the message-only window need to be started in a new thread, or does creating a new window automatically spin off a new thread?

ADDITIONAL INFO

Changing the type of recipient handle passed to `RegisterDeviceNotification` to `DEVICE_NOTIFY_ALL_INTERFACE_CLASSES` caused it to start working. Do you know why that would make a difference, or are there any problems, if I leave it like this?

modified on Friday, November 5, 2010 12:18 PM

AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:32
sitebuilderLuc Pattyn5-Nov-10 5:32 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Jim Fell5-Nov-10 5:41
Jim Fell5-Nov-10 5:41 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:49
sitebuilderLuc Pattyn5-Nov-10 5:49 
AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn8-Nov-10 12:17
sitebuilderLuc Pattyn8-Nov-10 12:17 
QuestionWhy IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 2:17
professionalChesnokov Yuriy5-Nov-10 2:17 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 3:32
mveCPallini5-Nov-10 3:32 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 3:55
professionalChesnokov Yuriy5-Nov-10 3:55 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 4:13
mveCPallini5-Nov-10 4:13 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 5:50
professionalChesnokov Yuriy5-Nov-10 5:50 
QuestionProverbial good example of CTreeCtrl Pin
federico.strati4-Nov-10 23:56
federico.strati4-Nov-10 23:56 
AnswerRe: Proverbial good example of CTreeCtrl Pin
Sauro Viti5-Nov-10 1:41
professionalSauro Viti5-Nov-10 1:41 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 2:51
federico.strati5-Nov-10 2:51 
GeneralRe: Proverbial good example of CTreeCtrl Pin
CPallini5-Nov-10 3:04
mveCPallini5-Nov-10 3:04 
GeneralRe: Proverbial good example of CTreeCtrl PinPopular
David Crow5-Nov-10 3:09
David Crow5-Nov-10 3:09 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 3:38
federico.strati5-Nov-10 3:38 
JokeRe: Proverbial good example of CTreeCtrl Pin
CPallini5-Nov-10 3:46
mveCPallini5-Nov-10 3:46 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 3:59
federico.strati5-Nov-10 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.