ne0_
September 12th, 2007, 12:38 PM
hey hi all... i made a c++ program in which i need to detect USB device inserted event... but my windowProc function is not getting called.. i don't know whats wrong with the program.... i also tried to check the error by using GetLastError() but i got nothing.. code goes like this
#ifdef WINVER
#undef WINVER
#endif
#define WINVER 0x0501
#include <windows.h>
#include <dbt.h>
#include <iostream>
#include <conio.h>
#include <initguid.h>
DEFINE_GUID(GUID_DEVINTERFACE_VOLUME, 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
// don't know if this is the correct GUID for USB device interface... i got it from MSDN website....
#define guid GUID_DEVINTERFACE_VOLUME
using namespace std;
HDEVNOTIFY RegisterDeviceNotificationA(
HANDLE hRecipient,
LPVOID NotificationFilter,
DWORD Flags
);
#define RegisterDeviceNotification RegisterDeviceNotificationA
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // device-change event
LPARAM lParam // event-specific data
);
int main( )
{
HWND hwnd = FindWindow( "ConsoleWindowClass", NULL);
cout<<hwnd<<endl;// TO check if im able to get hwnd or not......
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = guid;
HDEVNOTIFY hDevNotify;
hDevNotify = RegisterDeviceNotification( hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
cout<<hDevNotify<<endl;// to check the value
if( !hDevNotify )
{
cout<<"Registration of device notification failed"<<endl;
}
else
{
cout<<"Registration of device complete"<<endl;
}
DWORD dword;
dword = GetLastError( );
cout<<dword<<endl;// to check for any error if any....
while(1)
{
Sleep(1000);
}
return 0;
}
LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
cout<<"WindowProc called"<<endl;// to check if its getting called or not.......
if( uMsg == WM_DEVICECHANGE )
{
cout<<"device change"<<endl;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);//call to WINDOW API function defWindowProc() which
// provides default message processing for the window messages that were not processed by this window.
}
output im getting is
0x290392 // for hwnd
0x245d08 // for hDevNotify
Registration of device complete
0 // for GetLastError()
and nothing is happening when im inserting USB.....nyone knw about this
#ifdef WINVER
#undef WINVER
#endif
#define WINVER 0x0501
#include <windows.h>
#include <dbt.h>
#include <iostream>
#include <conio.h>
#include <initguid.h>
DEFINE_GUID(GUID_DEVINTERFACE_VOLUME, 0x53f5630dL, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b);
// don't know if this is the correct GUID for USB device interface... i got it from MSDN website....
#define guid GUID_DEVINTERFACE_VOLUME
using namespace std;
HDEVNOTIFY RegisterDeviceNotificationA(
HANDLE hRecipient,
LPVOID NotificationFilter,
DWORD Flags
);
#define RegisterDeviceNotification RegisterDeviceNotificationA
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // device-change event
LPARAM lParam // event-specific data
);
int main( )
{
HWND hwnd = FindWindow( "ConsoleWindowClass", NULL);
cout<<hwnd<<endl;// TO check if im able to get hwnd or not......
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = guid;
HDEVNOTIFY hDevNotify;
hDevNotify = RegisterDeviceNotification( hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
cout<<hDevNotify<<endl;// to check the value
if( !hDevNotify )
{
cout<<"Registration of device notification failed"<<endl;
}
else
{
cout<<"Registration of device complete"<<endl;
}
DWORD dword;
dword = GetLastError( );
cout<<dword<<endl;// to check for any error if any....
while(1)
{
Sleep(1000);
}
return 0;
}
LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
cout<<"WindowProc called"<<endl;// to check if its getting called or not.......
if( uMsg == WM_DEVICECHANGE )
{
cout<<"device change"<<endl;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);//call to WINDOW API function defWindowProc() which
// provides default message processing for the window messages that were not processed by this window.
}
output im getting is
0x290392 // for hwnd
0x245d08 // for hDevNotify
Registration of device complete
0 // for GetLastError()
and nothing is happening when im inserting USB.....nyone knw about this