usb插入和拔出的判定

orcher 2002-08-24 05:19:55
当usb口插入设备时,系统能马上判定-系统托盘显示出来-然后调用相应的驱动程序,而usb设备拔出时,系统也能马上判定。它们各捕获的是什么消息?望大虾指教。我用GetDeviceChange能够捕捉到usb的变化,但是分辨不出,插入还是拔出设备,---不过奇怪的是,插入设备时,GetDeviceChange激活两次,而拔除只有一次
...全文
259 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x8000 -->DBT_DEVICEARRIVAL
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x8004 -->DBT_DEVICEREMOVECOMPLETE
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
0x0007 -->Unknwon DBT Event
  • 打赏
  • 举报
回复
#include <dbt.h>
//---------------------------------------------------------------------------
MESSAGE TForm1::GetDeviceChange(TMessage &Message)
{
AnsiString S;
S.printf("%#06x -->",Message.WParam);

switch (Message.WParam)
{

case DBT_DEVICEARRIVAL: // system detected a new device
ListBox->Items->Add(S+"DBT_DEVICEARRIVAL");
break;
case DBT_DEVICEQUERYREMOVE: // wants to remove, may fail
ListBox->Items->Add(S+"DBT_DEVICEQUERYREMOVE");
break;
case DBT_DEVICEQUERYREMOVEFAILED: // removal aborted
ListBox->Items->Add(S+"DBT_DEVICEQUERYREMOVEFAILED");
break;
case DBT_DEVICEREMOVEPENDING: // about to remove, still avail.
ListBox->Items->Add(S+"DBT_DEVICEREMOVEPENDING");
break;
case DBT_DEVICEREMOVECOMPLETE: // device is gone
ListBox->Items->Add(S+"DBT_DEVICEREMOVECOMPLETE");
break;
case DBT_DEVICETYPESPECIFIC: // type specific event
ListBox->Items->Add(S+"DBT_DEVICETYPESPECIFIC");
break;
#if(WINVER >= 0x040A)
case DBT_CUSTOMEVENT: // user-defined event
ListBox->Items->Add(S+"DBT_CUSTOMEVENT");
break;
#endif /* WINVER >= 0x040A */

default:
ListBox->Items->Add(S+"Unknwon DBT Event");
break;
}
}
你用这段代码测试一下,就很清楚了....:)
我插了一个USB硬盘,又移除结果如下:

orcher 2002-09-06
  • 打赏
  • 举报
回复
还有一个很器官的地方:

case DBT_DEVICEARRIVAL:

ShowMessage("New Device Arrivaled!");
break;
case DBT_DEVICEREMOVECOMPLETE:
ShowMessage("A Device Removed Complete");
break;

插入设备时,会谈出两次对话框"New Device Arrivaled"
拔除设备时,只弹出一次对话框“A Device Removed Complete”

为什么?.

orcher 2002-09-06
  • 打赏
  • 举报
回复
谢谢,能够回答我的第二个问题吗?
void __fastcall TForm1::GetDeviceChange(TMessage &Msg)
{
ShowMessage(Msg.WParam);
}
当插入usb设备时,系统就会不停的ShowMessage,这是为什么?
  • 打赏
  • 举报
回复
不是告诉你了么?

#include <dbt.h> //!!!!
orcher 2002-09-06
  • 打赏
  • 举报
回复
救救我呀!
  • 打赏
  • 举报
回复
#include <dbt.h>
//---------------------------------------------------------------------------
MESSAGE TForm1:: GetDeviceChange(TMessage &Message)
{
switch (Message.WParam)
{

case DBT_DEVICEARRIVAL:

ShowMessage("New Device Arrivaled!");
break;
case DBT_DEVICEREMOVECOMPLETE:
ShowMessage("A Device Removed Complete");
break;
}
}
orcher 2002-09-06
  • 打赏
  • 举报
回复
能不能帮帮忙?
orcher 2002-09-06
  • 打赏
  • 举报
回复
为什么没有人愿意回答?
orcher 2002-09-06
  • 打赏
  • 举报
回复
现在出现一个新问题
test.h 中
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DEVICECHANGE,TMessage,GetDeviceChange)
END_MESSAGE_MAP(TForm)

test.cpp中
void __fastcall TForm1::GetDeviceChange(TMessage &Msg)
{

switch (Msg.WParam)
{

case DBT_DEVICEARRIVAL:
// 判断CDROM碟片是否已经插入到光驱中
ShowMessage("start");
break;
case DBT_DEVICEREMOVECOMPLETE:
ShowMessage("test changed");
break;
}
}

总是提示DBT_DEVICEREMOVECOMPLETE,DBT_DEVICEARRIVAL Undefined
有什么办法解决吗?
如果我改成
void __fastcall TForm1::GetDeviceChange(TMessage &Msg)
{
ShowMessage(Msg.WParam);
}
当插入usb设备时,系统就会不停的ShowMessage,这是为什么?
kingcaiyao 2002-08-24
  • 打赏
  • 举报
回复
WM_DEVICECHANGE消息的参数已经解释得很清楚了:
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.
orcher 2002-08-24
  • 打赏
  • 举报
回复
WM_DEVICECHANGE是能够判断插入和拔出呀,但是怎么分辨到底是插入还是拔出呢?我不关心的他是何种设备
kingcaiyao 2002-08-24
  • 打赏
  • 举报
回复
用WM_DEVICECHANGE能判断出设备的插入或拔出,但具体判断是何种设备以及是找到它的驱动程序这个过程由windows完成。
耙子 2002-08-24
  • 打赏
  • 举报
回复
第8期 程序员 杂志 有很多关于USB编程的文章。希望对你有帮助。
cdsn出的。网站有的卖。

13,822

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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