Sign in to follow this  
Followers 0
Buffo

Detect CD/DVD media replacement

11 posts in this topic

Hi!

I want to run a script in background that notifies a CD/DVD media replacement. How ist this to realize?

If I use DriveStatus() the drive is always running on each query. Perhaps is there the possibiltiy to use WMI? I don't know :)

I hope you can help me <_<

Thx in advance!

Regards,

Buffo

Share this post


Link to post
Share on other sites



Windows sends WM_DEVICECHANGE message to all top level windows, whenever a removable media is inserted or removed (applies to CD/DVD as well as to USB-Drives etc.).

If your script has a GUI (even hidden), you could try this:

Const $DBT_DEVICEARRIVAL              = 0x8000 ; system detected a new device
Const $DBT_DEVICEQUERYREMOVE          = 0x8001 ; wants to remove, may fail
Const $DBT_DEVICEQUERYREMOVEFAILED  = 0x8002 ; removal aborted
Const $DBT_DEVICEREMOVEPENDING      = 0x8003 ; about to remove, still avail.
Const $DBT_DEVICEREMOVECOMPLETE    = 0x8004 ; device is gone
Const $DBT_DEVICETYPESPECIFIC        = 0x8005 ; type specific event

Const $WM_DEVICECHANGE = 0x0219

GUIRegisterMsg($WM_DEVICECHANGE, "MY_WM_DEVICECHANGE")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
Wend

Func MY_WM_DEVICECHANGE($hWnd, $msg, $wParam, $lParam)
    Switch $wParam
        Case $DBT_DEVICEARRIVAL
        ; do something
        Case $DBT_DEVICEREMOVECOMPLETE
        ; do something

    EndSwitch
        Return 1
EndFunc

UDFS & Apps:


DDEML.au3 - DDE Client + Server[*]
Localization.au3- localize your scripts[*]
TLI.au3 - type information on COM objects (TLBINF emulation)[*]
TLBAutoEnum.au3 - auto-import of COM constants (enums)[*]
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector

- OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Share this post


Link to post
Share on other sites

Thx for your reply <_<

Unfortunately this doesn't work...

Has anyone other suggestions?

Regards,

Buffo

Share this post


Link to post
Share on other sites

Unfortunately this doesn't work...

Can you explain 'doesn't work'? What are you trying to do? Your code?


UDFS & Apps:


DDEML.au3 - DDE Client + Server[*]
Localization.au3- localize your scripts[*]
TLI.au3 - type information on COM objects (TLBINF emulation)[*]
TLBAutoEnum.au3 - auto-import of COM constants (enums)[*]
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector

- OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Share this post


Link to post
Share on other sites

something to start...

$oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$oEvents = $oWMI.ExecNotificationQuery("SELECT * FROM Win32_DeviceChangeEvent")

$oReceivedEvent = $oEvents.NextEvent
ConsoleWrite('The event has occured.' & @LF)

Share this post


Link to post
Share on other sites

@doudou: It happens nothing if I change media or open and close my cd door <_<

My Code:

#include <GUIConstants.au3>


GuiCreate("", 600, 400)


Const $DBT_DEVICEARRIVAL              = 0x8000; system detected a new device
Const $DBT_DEVICEQUERYREMOVE          = 0x8001; wants to remove, may fail
Const $DBT_DEVICEQUERYREMOVEFAILED  = 0x8002; removal aborted
Const $DBT_DEVICEREMOVEPENDING      = 0x8003; about to remove, still avail.
Const $DBT_DEVICEREMOVECOMPLETE    = 0x8004; device is gone
Const $DBT_DEVICETYPESPECIFIC        = 0x8005; type specific event

Const $WM_DEVICECHANGE = 0x0219

GUIRegisterMsg($WM_DEVICECHANGE, "MY_WM_DEVICECHANGE")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
Wend

Func MY_WM_DEVICECHANGE($hWnd, $msg, $wParam, $lParam)
    Switch $wParam
        Case $DBT_DEVICEARRIVAL
            MsgBox(0, "Arrival", "")
        Case $DBT_DEVICEREMOVECOMPLETE
            MsgBox(0, "Removecomplete", "")

    EndSwitch
        Return 1
EndFunc

@Joon: I tried this:

$oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")


While 1
    $oEvents = $oWMI.ExecNotificationQuery("SELECT * FROM Win32_DeviceChangeEvent")

    $oReceivedEvent = $oEvents.NextEvent
    ConsoleWrite('The event has occured.' & @LF)
    Sleep(500)
WEnd

Nothing happens here, too.

Regards,

Buffo

Share this post


Link to post
Share on other sites

Just making sure...

Do you have actual CD in the drive? Empty CD Tray triggers nothing.

Share this post


Link to post
Share on other sites

Yes, media is inserted.

Share this post


Link to post
Share on other sites

@doudou: It happens nothing if I change media or open and close my cd door <_<

Hmm, strange...

Have you tried to show the main window just before entering GUIGetMsg() loop?

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()...

UDFS & Apps:


DDEML.au3 - DDE Client + Server[*]
Localization.au3- localize your scripts[*]
TLI.au3 - type information on COM objects (TLBINF emulation)[*]
TLBAutoEnum.au3 - auto-import of COM constants (enums)[*]
AU3Automation - export AU3 scripts via COM interfaces
TypeLibInspector

- OleView was yesterday

Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCE 

Share this post


Link to post
Share on other sites

Yes, I tried but it doesn't work <_<

Share this post


Link to post
Share on other sites

Now it works with GetDriveStatus(). I don't know why it didn't work before. The drive isn't spinning up any more.

Thx to the guys at German AutoIt Forum <_<

Regards,

Buffo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0