如何检测USB Key的插拔事件

angelrain 2008-06-24 09:39:11
需要做一个自定义的Gina,当USBKey拔出自动锁定,USBKey插入自动解锁,现在找到的资料太有限,请高手支招:)
...全文
722 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
清歌谁与和 2008-06-30
  • 打赏
  • 举报
回复
mark
sanshao27 2008-06-30
  • 打赏
  • 举报
回复
这里有说明,http://blog.csdn.net/yingfox/archive/2007/11/15/1887153.aspx
sanshao27 2008-06-30
  • 打赏
  • 举报
回复
看看,能不能解决你的问题
size_t szAllDriveStrings = GetLogicalDriveStrings(0,NULL);
char *pDriveStrings = new char[szAllDriveStrings + sizeof(_T( " "))];
GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings);
size_t szDriveString = strlen(pDriveStrings);
while(szDriveString > 0)
{
AfxMessageBox(pDriveStrings);
pDriveStrings += szDriveString + 1;
szDriveString = strlen(pDriveStrings);
}

// pDriveStrings 就索盘符撒
---------------------------------------------------------------

DiskType=GetDriveType(strTempDirver);
switch(DiskType)
{
case DRIVE_NO_ROOT_DIR:
return;
case DRIVE_REMOVABLE:
//::AfxMessageBox( "移动存储设备 ");
break;
case DRIVE_FIXED:
//::AfxMessageBox( "固定硬盘驱动器 ");
break;
case DRIVE_REMOTE:
//::AfxMessageBox( "这是网络驱动器 ");
return;
case DRIVE_CDROM:
//::AfxMessageBox( "这是光盘驱动器 ");
return;
Treeyan 2008-06-30
  • 打赏
  • 举报
回复
USB的Device的話應該要註冊USB Device跟USB HUB Controller,試試看下列的程式囉!!希望可以達到你要的.......

DEV_BROADCAST_DEVICEINTERFACE broadcastInterface;


// Register to receive notification when a USB device is plugged in.
broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;

memcpy( &(broadcastInterface.dbcc_classguid),
&(GUID_CLASS_USB_DEVICE),
sizeof(struct _GUID));

gNotifyDevHandle = RegisterDeviceNotification(hWnd,
&broadcastInterface,
DEVICE_NOTIFY_WINDOW_HANDLE);

// Now register for Hub notifications.
memcpy( &(broadcastInterface.dbcc_classguid),
&(GUID_CLASS_USBHUB),
sizeof(struct _GUID));

gNotifyHubHandle = RegisterDeviceNotification(hWnd,
&broadcastInterface,
DEVICE_NOTIFY_WINDOW_HANDLE);
JiffyChen 2008-06-25
  • 打赏
  • 举报
回复
如果是针对特定厂商的USB KEY的话,厂商会直接给API的

毕竟你如果要往里面写些东西的话还是用到厂商提供的API

但是由于开发的时候需要与厂商签些协议,具体就不说了,
当然了,说不定有些UKEY的api早给放到网络上了,找找说定也能找到!
scq2099yt 2008-06-24
  • 打赏
  • 举报
回复
捕获WM_DEVICECHANGE消息,找到自己需要的信息
king820802 2008-06-24
  • 打赏
  • 举报
回复
WM_DEVICECHANGE
The WM_DEVICECHANGE device message notifies an application or device driver of a change to the hardware configuration of a device or the computer.

Event = (UINT) wParam;
dwData = (DWORD) lParam;

Parameters
Event
Event type. This parameter can be one of the following values: Value Meaning
DBT_CONFIGCHANGECANCELED A request to change the current configuration (dock or undock) has been canceled.
DBT_CONFIGCHANGED The current configuration has changed, due to a dock or undock.
DBT_DEVICEARRIVAL A device has been inserted and is now available.
DBT_DEVICEQUERYREMOVE Permission is requested to remove a device. Any application can deny this request and cancel the removal.
DBT_DEVICEQUERYREMOVEFAILED A request to remove a device has been canceled.
DBT_DEVICEREMOVECOMPLETE A device has been removed.
DBT_DEVICEREMOVEPENDING A device is about to be removed. Cannot be denied.
DBT_DEVICETYPESPECIFIC A device-specific event has occurred.
DBT_QUERYCHANGECONFIG Permission is requested to change the current configuration (dock or undock).
DBT_USERDEFINED The meaning of this message is user-defined.


dwData
Address of a structure that contains event-specific data. Its meaning depends on the given event.
Return Values
Return TRUE to grant a requested action.

Return BROADCAST_QUERY_DENY to deny a requested action.

Remarks
For devices that offer software-controllable features, such as ejection and locking, the system typically sends a DBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully.

If the system forcibly removes a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so.

DBT_CONFIGCHANGECANCELED, DBT_CONFIGCHANGED, DBT_DEVICEARRIVAL, DBT_DEVICEQUERYREMOVE, DBT_DEVICEQUERYREMOVEFAILED, DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEREMOVEPENDING, DBT_DEVICETYPESPECIFIC, DBT_QUERYCHANGECONFIG, DBT_USERDEFINED

QuickInfo
Windows NT: Requires version 4.0 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winuser.h.

See Also
System Messages Overview, System Message Messages


angelrain 2008-06-24
  • 打赏
  • 举报
回复
有没有相关的代码或资料可以提供看看呢?
闪破风浪 2008-06-24
  • 打赏
  • 举报
回复
那岂不是鼠标、键盘也得。。。。。。。
我觉得遍历存储设备还是可以的。。。
菜牛 2008-06-24
  • 打赏
  • 举报
回复
当然是判断符合自己需要的消息。
angelrain 2008-06-24
  • 打赏
  • 举报
回复
WM_DEVICECHANGE消息检测出来的应该包含很多的设备的事件吧?还需要进行一一判别?
菜牛 2008-06-24
  • 打赏
  • 举报
回复
WM_DEVICECHANGE消息即可。
naixian1983 2008-06-24
  • 打赏
  • 举报
回复
查找usb的vid pid
cnzdgs 2008-06-24
  • 打赏
  • 举报
回复
MSDN中输入WM_DEVICECHANGE,查看相关说明。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧