WM_DEVICECHANGE看别人的东东

昨天有人问我,如何”hook” Windows的WM_DEVICECHANGE消息,相关的问题以前在blog上写过一个方法,但那是适用于有窗口的。我想他要实现的是在无窗口的情况下只用一个dll实现,觉得新鲜,于是试了一下。
我用的是全局的hook,首先用的是
SetWindowsHookEx( WH_GETMESSAGE, UsbProc, g_hInst, 0 );
钩子函数是这样定义的
LRESULT CALLBACK UsbProc(int nCode,WPARAM wParam,LPARAM lParam)
{
switch(nCode)
{
case HC_ACTION:
if ( wParam == PM_NOREMOVE)
{
PMSG pMsg = PMSG(lParam);
if ( pMsg->message == WM_DEVICECHANGE )
{
switch( pMsg->wParam )
{
case DBT_DEVICEARRIVAL:
MessageBox( NULL, "Device arrival!", "警告", MB_OK );
break;
case DBT_DEVICEQUERYREMOVE:
// Handle device removal request
MessageBox( NULL, "Device request remove!", "警告", MB_OK );
break;
case DBT_DEVICEREMOVECOMPLETE:
// Handle device removal
MessageBox( NULL, "Device removal!", "警告", MB_OK );
break;
}
}
}
}
return CallNextHookEx( NULL,nCode,wParam,lParam);
}
这样的话,当U盘插入的时候,并没有出现设备插入的提示,但是把DBT_DEVICEARRIVAL换成DBT_DEVNODES_CHANGED后就可以,只是有20几个提示。
于是我改用下面的方法:
SetWindowsHookEx( WH_CALLWNDPROC, UsbProc, g_hInst, 0 );
LRESULT CALLBACK UsbProc(int nCode,WPARAM wParam,LPARAM lParam)
{
switch(nCode)
{
case HC_ACTION:
PCWPSTRUCT pMsg = PCWPSTRUCT(lParam);
if ( pMsg->message == WM_DEVICECHANGE )
{
switch( pMsg->wParam )
{
case DBT_DEVICEARRIVAL:
MessageBox( NULL, "Device arrival!", "警告", MB_OK );
break;
case DBT_DEVICEQUERYREMOVE:
// Handle device removal request
MessageBox( NULL, "Device request remove!", "警告", MB_OK );
break;
case DBT_DEVICEREMOVECOMPLETE:
// Handle device removal
MessageBox( NULL, "Device removal!", "警告", MB_OK );
break;
}
}
}
return CallNextHookEx( NULL,nCode,wParam,lParam);
}

这样出现了预定的提示,但是是一直提示。
在网上得到了下面的资料:
There are 14 types of hooks:
WH_CALLWNDPROC called when SendMessage is called
WH_CALLWNDPROCRET called when SendMessage returns
WH_GETMESSAGE called when GetMessage or PeekMessage is called
WH_KEYBOARD called when GetMessage or PeekMessage retrieves WM_KEYUP or WM_KEYDOWN from the message queue
WH_MOUSE called when GetMessage or PeekMessage retrieves a mouse message from the message queue
WH_HARDWARE called when GetMessage or PeekMessage retrieves some hardware message that is not related to keyboard or mouse.
WH_MSGFILTER called when a dialog box, menu or scrollbar is about to process a message. This hook is local. It’s specifically for those objects which have their own internal message loops.
WH_SYSMSGFILTER same as WH_MSGFILTER but system-wide
WH_JOURNALRECORD called when Windows retrieves message from the hardware input queue
WH_JOURNALPLAYBACK called when an event is requested from the system’s hardware input queue.
WH_SHELL called when something interesting about the shell occurs such as when the task bar needs to redraw its button.
WH_CBT used specifically for computer-based training (CBT).
WH_FOREGROUNDIDLE used internally by Windows. Little use for general applications
WH_DEBUG used to debug the hooking procedure

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_DEVICECHANGE
WPARAM wParam, // device-change event
LPARAM lParam // event-specific data
);
Parameters
hwnd
Handle to window.
uMsg
WM_DEVICECHANGE message identifier.
wParam
Set to DBT_DEVNODES_CHANGED.
lParam
Set to zero.
Return Values
Return TRUE.

Remarks
There is no additional information about which device has been added to or removed from the
system. Applications that require more information should register for device notification.
难道必须要用RegisterDeviceNotification吗?看来这个问题还是要好好研究一下

本文转自
http://www.blog.edu.cn/user4/pafone/archives/2007/1660551.shtml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值