all groups > dotnet interop > july 2006 >
You're in the

dotnet interop

group:

Detecting Device Change


Detecting Device Change Jeff Gaines
7/23/2006 4:15:37 AM
dotnet interop:

I want to be able to detect a CD change (along with other removable
devices if possible). I have trapped WM_DEVICECHANGE in WndProc but after
testing it seems only to work if CD AutoPlay is enabled.

Is there a way to trap a device change - native .NET if possible -
without AutoPlay enabled? I may be able to use a timer but that could be
resource hungry in a system with several removable devices.

Many thanks.

--
Re: Detecting Device Change mcstar NO[at]SPAM gmail.com
7/24/2006 6:45:42 AM
Here's an idea,
If you answer both questions, then you have a 100% chance of helping
the poster. If you answer only one (because you only know one answer)
then you have a 50% chance of helping the poster and a 100% chance of
helping someone else who may have found this post via google.

If you don't know the answer to either question, then you probably
shouldn't respond at all.


[quoted text, click to view]
Re: Detecting Device Change Jeff Gaines
7/24/2006 6:55:03 AM
[quoted text, click to view]

Hi Peter,

I would like to detect if a CD (or DVD) has been changed - either removed
by ejecting or a new one inserted. I am not worried (at this stage) by
adding/removing a device.

--
RE: Detecting Device Change v-phuang NO[at]SPAM online.microsoft.com (
7/24/2006 11:58:42 AM
Hi Jeff,

Thanks for your posting!
Can you describe the scenario more clear about what do you mean by "CD
changed".
Do you mean you eject the CD Rom and put another CD into the CDRom?
or
You mean you attach another CD Rom onto your machine.
e.g. so far we can use USB CD rom, so we can hot attach-deattach a CD rom.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Re: Detecting Device Change v-phuang NO[at]SPAM online.microsoft.com (
7/25/2006 9:52:30 AM
Hi Jeff,

Thanks for your quickly reply!
Firstly please take a look at the link below.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/pla
tform/shell/programmersguide/shell_basics/shell_basics_extending/autorun/aut
oplay_reg.asp

I understand that you are using the registry approach to disable the
autoplay.
As we know the OS will interactive with Hardware via driver. So but the
Cdrom driver will check the registry key to see if the key is set, if it is
disable, then it will now send notify message. So this behavior is by
design.

But for your scenario, I think you may try another approach to cancel the
autoplay "Suppressing AutoRun Programmatically" in the link above.
Here is some "C#" code which works at my side.
private const int WM_DEVICECHANGE = 0x219;
private const int DBT_DEVICEARRIVAL = 32806;
private const int DBT_DEVICEREMOVECOMPLETE = 32809;
[DllImport("user32.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
/* only needed if your application is using a dialog box and needs to
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE
or FALSE.
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
*/
//provide a private internal message id
private UInt32 queryCancelAutoPlay = 0;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DEVICECHANGE)
{
Debug.WriteLine("Devide Changed");
}

//calling the base first is important, otherwise the values you
set later will be lost
base.WndProc(ref m);

//if the QueryCancelAutoPlay message id has not been
registered...
if (queryCancelAutoPlay == 0)
queryCancelAutoPlay =
RegisterWindowMessage("QueryCancelAutoPlay");
//if the window message id equals the QueryCancelAutoPlay
message id
if ((UInt32)m.Msg == queryCancelAutoPlay)
{
/* only needed if your application is using a dialog box
and needs to
* respond to a "QueryCancelAutoPlay" message, it cannot
simply return TRUE or FALSE.
SetWindowLong(this.Handle, 0, 1);
*/
m.Result = (IntPtr)1;
Debug.WriteLine("Cancel");
}


Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Re: Detecting Device Change Jeff Gaines
7/25/2006 9:53:16 AM
[quoted text, click to view]

[snipped]

Many thanks, Peter :-)

I will experiment with your code!

--
Re: Detecting Device Change v-phuang NO[at]SPAM online.microsoft.com (
7/26/2006 12:00:00 AM
Hi Jeff,

I appreciate your prompt response. : )
I understand your situation and please take your time to perform the steps
that I have provided you. If there is anything unclear, please feel free to
post back. I am always very happy to be of assistance.

Have a nice day!


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button