using WM_DEVICECHANGE in console application

S

sachinahuja82

Hi All
I want to detect a USB device through my console application.
I cam to know that windows sends WM_DEVICECHANGE message whenever a
USB device is inserted or removed.
Now I want to handle this message in my console application.Following
code I have written for it.
But I am not able to handle this event.

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
HANDLE hEvent = CreateEvent(NULL , FALSE , FALSE , NULL);
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM
wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{

case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
Ellipse (hdc, 0, 0, 200, 100);
EndPaint (hwnd, &ps);
return 0;

case WM_DESTROY:
PostQuitMessage (0);
return 0;
case WM_DEVICECHANGE:
SetEvent(hEvent);
return 0;

}
return DefWindowProc (hwnd, message, wParam, lParam);
}

DWORD WINAPI fnThreadFunc(LPVOID lpParam)
{

WNDCLASS wc;
HWND hwnd;
MSG msg;
HANDLE hInstance = GetModuleHandle(NULL);
wc.style = 0; // Class style
wc.lpfnWndProc = (WNDPROC) WndProc; // Window
procedure address
wc.cbClsExtra = 0; // Class extra
bytes
wc.cbWndExtra = 0; // Window extra
bytes
wc.hInstance = (HINSTANCE)hInstance; //
Instance handle
wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); // Icon handle
wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Cursor handle
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Background
color
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = "MyWndClass"; // WNDCLASS name

RegisterClass (&wc);


hwnd = CreateWindow (
"MyWndClass", // WNDCLASS name
"RegisterApplication", // Window title
WS_DISABLED, // Window style
CW_USEDEFAULT, // Horizontal position
CW_USEDEFAULT, // Vertical position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
HWND_DESKTOP, // Handle of parent window
NULL, // Menu handle
NULL, // Application's instance handle
NULL // Window-creation data
);

// ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);

while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}

return msg.wParam;



}
int main(int argc, char* argv[])
{
DWORD dwThreadId;

HANDLE hHandle = CreateThread(NULL , 0 , fnThreadFunc , NULL , 0 ,
&dwThreadId);
if(hHandle == NULL)
{
return 0;
}
WaitForSingleObject(hEvent , INFINITE);
return 1;
}


I have created a thread and in thread function i have created a
window.
Now in WINPROC i tried to handle WM_DEVICECHANGE.
But i am not able to get this message when I inserted USB device.
Can anybody help me in this.
Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top