Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / July 2006

Tip: Looking for answers? Try searching our database.

Detecting Device Change

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Gaines - 23 Jul 2006 12:15 GMT
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.

Signature

Jeff Gaines

"Peter Huang" [MSFT] - 24 Jul 2006 12:58 GMT
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.
mcstar@gmail.com - 24 Jul 2006 14:45 GMT
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.

> Hi Jeff,
>
[quoted text clipped - 16 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeff Gaines - 24 Jul 2006 14:55 GMT
>Hi Jeff,
>
[quoted text clipped - 9 lines]
>
>Peter Huang

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.

Signature

Jeff Gaines

"Peter Huang" [MSFT] - 25 Jul 2006 10:52 GMT
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.
Jeff Gaines - 25 Jul 2006 17:53 GMT
[snipped]

Many thanks, Peter :-)

I will experiment with your code!

Signature

Jeff Gaines

"Peter Huang" [MSFT] - 26 Jul 2006 04:49 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.