// JP opened flex table

Click to See Complete Forum and Search --> : windowProc not getting called...


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

JamesSchumacher
September 13th, 2007, 04:01 PM
When you use FindWindow, you are getting the window handle of the console window. Your WindowProcess is NOT the window process of the console. You have to create a window that uses your WindowProcess.

See RegisterClassEx and CreateWindowEx.


#define _WINVER 0x0502
#define _WIN32_WINNT 0x0502
#define _WIN32_WINDOWS 0x0410
#define _WIN32_IE 0x0600
#include <windows.h>

INT_PTR WINAPI HandleWindowProcess(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
bool InitWindowClass();
HWND CreateAppWindow();
void MessageLoop();

extern "C" int __stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
char * lpstrCmdLine,int nShowCmd)
{
if (!InitWindowClass())
{
::MessageBoxA(HWND_DESKTOP,"Failed to register window class.","AppTitle",MB_OK);

return -1;
}

HWND hWnd = CreateAppWindow();

if (hWnd == 0)
{
::MessageBoxA(HWND_DESKTOP,"Failed to create main window.","AppTitle",MB_OK);

return -1;
}

::ShowWindow(hWnd,SW_SHOWNORMAL);

MessageLoop();

return 0;
}

INT_PTR WINAPI HandleWindowProcess(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if (message == WM_PAINT)
{
// Do painting here
}
else if (message == WM_CLOSE)
{
::DestroyWindow(hWnd);

return 0;
}
else if (message == WM_CREATE)
{
return 0;
}
else if (message == WM_NCCREATE)
{
::DefWindowProcW(hWnd,message,wParam,lParam);

return 1;
}
else if (message == WM_DESTROY)
{
::PostQuitMessage(0);

return 0;
}

return ::DefWindowProcW(hWnd,message,wParam,lParam);
}

bool InitWindowClass()
{
WNDCLASSEXW wndClass;

wndClass.cbSize = sizeof(WNDCLASSEXW);
wndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndClass.hInstance = reinterpret_cast<HINSTANCE>(::GetModuleHandle(0));
wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>(HandleWindowProcess);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hIcon = ::LoadIcon(0,IDI_APPLICATION);
wndClass.hbrBackground = ::CreateSolidBrush(RGB(192,192,192));
wndClass.hCursor = ::LoadCursor(0,IDC_ARROW);
wndClass.lpszClassName = L"NameOfYourClass";
wndClass.lpszMenuName = NULL;
wndClass.hIconSm = wndClass.hIcon;

return ::RegisterClassExW(&wndClass) != 0;
}

HWND CreateAppWindow()
{
return ::CreateWindowExW(WS_EX_CLIENTEDGE | WS_EX_APPWINDOW,
L"NameOfYourClass",L"Window Title",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,800,600,NULL,NULL,NULL,NULL);
}

void MessageLoop()
{
MSG msg;
int nCheck;

do
{
nCheck = ::GetMessage(&msg,0,0,0);

if (nCheck == 0 || nCheck == -1)
{
break;
}

::TranslateMessage(&msg);
::DispatchMessage(&msg);
} while (true);
}

JamesSchumacher
September 13th, 2007, 06:49 PM
Meant for another thread...

ne0_
September 13th, 2007, 10:46 PM
thnx... not its working fine.....

//JP added flex table