🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #274

I don't get DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE

#274
From: Peter Stefanos
Date: Mon, 26 May 2008 01:17
94 lines
3819 bytes
Hello all,

I have been unsuccessfully trying to get DBT_DEVICEARRIVAL and 
DBT_DEVICEREMOVECOMPLETE when listening to WM_DEVICECHANGE messages when 
I plug and unplug my USB device. The USB device has been made to look 
like a COM port and comes up as COM9 on my computer.

I have done the following:

1. Built a USB device (LPC2148 micro), used the LPCUSB USB stack
2. I have used the usbser.sys driver under Windows XP.
3. Registered for notification using RegisterDeviceNotification().
4. If I plug in a commercially available USB to serial device, I get the 
DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE message types successfully.

Can anyone tell me what I have done wrong? I hope its obvious.

Could it be the way the driver was installed?

I am very inexperienced with this stuff... I have been stuck on this one 
for many days now...

Here is my code:

Header File:

...
class TForm1 : public TForm
{
__published:	// IDE-managed Components
   TListView *ListView1;
   void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
private:	// User declarations
public:		// User declarations
   __fastcall TForm1(TComponent* Owner);
   void LogData(char *Format, ...);
   void __fastcall USBConnectHandler(TMessage &Message);

BEGIN_MESSAGE_MAP
   MESSAGE_HANDLER(WM_DEVICECHANGE, TMessage, USBConnectHandler);
END_MESSAGE_MAP(TForm)
};
...


Main CPP file:

...
HDEVNOTIFY hDevNotify;
...
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
   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_DEVINTERFACE_SERENUM_BUS_ENUMERATOR;

   hDevNotify = RegisterDeviceNotification(Application->Handle, 
&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

   if (!hDevNotify) {
     Application->MessageBox(Message.c_str(), "Error", MB_OK);
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::USBConnectHandler(TMessage &Message)
{

   PDEV_BROADCAST_DEVICEINTERFACE pInterface;

   DebugOut("WM_DEVICECHANGE: %d, %d, %d",
     Message.Msg, Message.WParam, Message.LParam);
   switch (Message.WParam) {
   case DBT_DEVICEARRIVAL:
     DebugOut("Got DBT_DEVICEARRIVAL");
     break;
   case DBT_DEVICEREMOVECOMPLETE:
     DebugOut("Got DBT_DEVICEREMOVECOMPLETE");
     break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
   UnregisterDeviceNotification(hDevNotify);
}
//---------------------------------------------------------------------------
Technical Details
Message-ID: <483a0f9d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1115be856d123060,start
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 26 May 2008 11:17:21 +1000
From: Peter Stefanos <peterATpennywiseDOTcomDOTau>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: I don't get DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 202.45.113.90
Message-ID: <483a0f9d$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1211764637 202.45.113.90 (25 May 2008 18:17:17 -0700)
Lines: 95
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
X-Original-Bytes: 3659