BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OnDeviceChange Problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Uri
Guest





PostPosted: Tue Oct 17, 2006 5:57 pm    Post subject: OnDeviceChange Problem Reply with quote



Hi, when I connect OR disconnect the USB connector I reach the
breakpoint in function "OnDeviceChange", but the parameters
of 'm' are stay the same (it is not right), why?

Thanks.


public: // User declarations
__fastcall TUSBApp_form(TComponent* Owner);
// Overriding a message handle

//Messages::TMessage message;
void OnDeviceChange(Messages::TMessage );

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DEVICECHANGE , Messages::TMessage , OnDeviceChange)
END_MESSAGE_MAP(TComponent)

//---------------------------------------------------------------

void TUSBApp_form::OnDeviceChange(Messages::TMessage m)
{
//USBC->OnDeviceChange(m);

return; // Breakpoint Here
}
Back to top
Michel Leunen
Guest





PostPosted: Tue Oct 17, 2006 7:38 pm    Post subject: Re: OnDeviceChange Problem Reply with quote



Uri wrote:
Quote:
Hi, when I connect OR disconnect the USB connector I reach the
breakpoint in function "OnDeviceChange", but the parameters
of 'm' are stay the same (it is not right), why?

void TUSBApp_form::OnDeviceChange(Messages::TMessage m)

You have to test against m.WParam. Something like:

switch(m.WParam)
{
case DBT_DEVICEARRIVAL:
//a new device has been inserted
break;
case DBT_DEVICEREMOVECOMPLETE:
//a device has been removed
break;
}

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Uri
Guest





PostPosted: Tue Oct 17, 2006 7:45 pm    Post subject: Re: OnDeviceChange Problem Reply with quote



I tried that, it's not working, every time when the function is
call (when I connect or remove the USB device from the PC) the
parameter 'm.WParam' stay 7 (m.WParam = 7) Why does it not chaning?

Uri.


Quote:
You have to test against m.WParam. Something like:

switch(m.WParam)
{
case DBT_DEVICEARRIVAL:
//a new device has been inserted
break;
case DBT_DEVICEREMOVECOMPLETE:
//a device has been removed
break;
}

Michel
Back to top
Bob Gonder
Guest





PostPosted: Tue Oct 17, 2006 9:08 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:

Quote:
I tried that, it's not working, every time when the function is
call (when I connect or remove the USB device from the PC) the
parameter 'm.WParam' stay 7 (m.WParam = 7) Why does it not chaning?

Look in DBT.H
7 is DBT_DEVNODES_CHANGED which you should be ignoring as Michel
pointed out.

Quote:
switch(m.WParam)
{
case DBT_DEVICEARRIVAL:
//a new device has been inserted
break;
case DBT_DEVICEREMOVECOMPLETE:
//a device has been removed
break;
}

Michel

Just a thought:
It could be that the mystery device you are unplugging isn't supposed
to report device arrival/removed. You might ask the manufacturer.
Back to top
Michel Leunen
Guest





PostPosted: Tue Oct 17, 2006 9:12 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:
Quote:
I tried that, it's not working, every time when the function is
call (when I connect or remove the USB device from the PC) the
parameter 'm.WParam' stay 7 (m.WParam = 7) Why does it not chaning?

That's a DBT_DEVNODES_CHANGED. According to MSDN :
"The system broadcasts the DBT_DEVNODES_CHANGED device event when a
device has been added to or removed from the system."
It seems there is no way to know if the device has been added or removed
from the system.

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Oct 17, 2006 10:04 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

"Uri" <Uri (AT) MyMail (DOT) com> wrote in message
news:4534d31d$1 (AT) newsgroups (DOT) borland.com...

Quote:
void OnDeviceChange(Messages::TMessage );

Just an FYI, in a message map, you need to pass TMessage by reference, not
by value, just like when overriding WndProc():

void __fastcall OnDeviceChange(Messages::TMessage &);


Gambit
Back to top
Uri
Guest





PostPosted: Wed Oct 18, 2006 5:41 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

No, it's not working either.... when I insert or remove my USB
device the 'WMDiskChange' function is call several times but at
each call I see in the debuger that Message.WParam = 7, when I
insert, or when I remove the USB connector of the device.

Any idea why? can it be a problem in the device itself?

Here is my last code -


void __fastcall WMDiskChange( TMessage &Message );

BEGIN_MESSAGE_MAP
MESSAGE_HANDLER( WM_DEVICECHANGE, TMessage, WMDiskChange )
END_MESSAGE_MAP( TControl )


void __fastcall TForm1::WMDiskChange( TMessage &Message )
{
int Value;

Value = Message.WParam;
Value = Value; // I set my breakpoint here
}



Quote:
Just an FYI, in a message map, you need to pass TMessage by reference, not
by value, just like when overriding WndProc():

void __fastcall OnDeviceChange(Messages::TMessage &);


Gambit
Back to top
Uri
Guest





PostPosted: Wed Oct 18, 2006 5:48 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Quote:
Just a thought:
It could be that the mystery device you are unplugging isn't supposed
to report device arrival/removed. You might ask the manufacturer.


How can the device send 'removed' report when I remove it?
I mean, when I remove it, I disconnect the USB cable, so the
device can't send anything becouse the cable is not connected
any more to the PC..... am I wrong?

Uri.
Back to top
Michel Leunen
Guest





PostPosted: Wed Oct 18, 2006 6:14 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:
Quote:
No, it's not working either....

Have a look at RegisterDeviceNotification() in MSDN. It seems to be the
correct way to do what you want.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/registering_for_device_notification.asp

or

http://tinyurl.com/w9h6w

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Uri
Guest





PostPosted: Wed Oct 18, 2006 6:44 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Quote:
Have a look at RegisterDeviceNotification() in MSDN. It seems to be the
correct way to do what you want.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/registering_for_device_notification.asp

or

http://tinyurl.com/w9h6w

Michel


Hi, I did not understand how to use it... from where do I call
to this function?

what is the 'GUID InterfaceClassGuid' parameter from where do I
take it?

Where exactly 'hWnd' get it's value?

Where do I find the defenition of DEV_BROADCAST_DEVICEINTERFACE ?

Which paarmeter shows me if the device was Inserted or Removed ?

Thanks.
Back to top
Uri
Guest





PostPosted: Wed Oct 18, 2006 7:20 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

I tried the following, 'NotificationFilter' data looks exactly
the same for 'Insert' or 'Remove'.

also, I did not know from where to take
'NotificationFilter.dbcc_classguid' data.

Thanks.



void __fastcall TForm1::WMDiskChange( TMessage &Message )
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
char szMsg[80];

ZeroMemory( &NotificationFilter, sizeof( NotificationFilter ) );

NotificationFilter.dbcc_size = sizeof( DEV_BROADCAST_DEVICEINTERFACE );
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
//NotificationFilter.dbcc_classguid = InterfaceClassGuid; // ? ? ? ? ?

RegisterDeviceNotification( Application->Handle, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE );
}
Back to top
Michel Leunen
Guest





PostPosted: Wed Oct 18, 2006 8:08 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:

Quote:
Hi, I did not understand how to use it.

Sorry, I don't know either. I just did a search on MSDN and they mention
that function in their article about Device Events. That's all I know
about it.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/device_events.asp

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Michel Leunen
Guest





PostPosted: Wed Oct 18, 2006 8:15 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:

Quote:
what is the 'GUID InterfaceClassGuid' parameter from where do I
take it?

Seems you can found it here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devinst_d/hh/DevInst_d/setup-cls_f7544122-69a3-4b34-85f5-db3714408026.xml.asp

Quote:

Where exactly 'hWnd' get it's value?

That's the handle of *your* window receiving notification.

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Oct 18, 2006 9:45 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

"Uri" <Uri (AT) nomail (DOT) com> wrote in message
news:453622af$1 (AT) newsgroups (DOT) borland.com...

Quote:
How can the device send 'removed' report when I remove it?
I mean, when I remove it, I disconnect the USB cable, so the
device can't send anything becouse the cable is not connected
any more to the PC..... am I wrong?

The device itself is not the one sending the notifications. The OS is, in
response to the device driver reporting that the device is
attached/detached.


Gambit
Back to top
Michel Leunen
Guest





PostPosted: Wed Oct 18, 2006 10:46 pm    Post subject: Re: OnDeviceChange Problem Reply with quote

Uri wrote:

Quote:
Hi, I did not understand how to use it.

Ok, I tried with my usb key and it works.

RegisterDeviceNotification() tells the OS to send to your app the
notification messages you want. Without this as you've noted, you don't
receive the arrival or removal of your device.

First add this to your form constructor:

DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = {0};
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_DEVCLASS_USB;
RegisterDeviceNotification(Handle,
&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);

And then in your message handler (WndProc or whatever):

if(Message.Msg == WM_DEVICECHANGE)
{
switch(Message.WParam)
{
case DBT_DEVICEARRIVAL:
Application->MessageBox("Arrival","USB",MB_OK);
break;
case DBT_DEVICEREMOVECOMPLETE:
Application->MessageBox("Removal","USB",MB_OK);
break;
}
}

Don't know if it works for any USB devices but at least it works for my
usb key.

HTH
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.