Intercept Messages (and WM_DEVICECHANGE)

M

Matteo

Hello everyone,
I'm trying to write a little app that wait for the WM_DEVICECHANGE
message from Windows that tht OS raises when a device (such as CD,
DVD,...) is inserted or ejected (in BC++ 6)
I've tried whith the Application->OnMessage() but it never receives
the message, so I've tried with the esample in the MSDN, write below,
but I dont know how to register the message handler in the
application.
I know that it could works because the Delphi version of the
application works fine...

Some suggestion?

Tnaks all,

Matteo

----------------------------------------------------------------------
Delphi version
----------------------------------------------------------------------

procedure WMDeviceChange(var Message: TMessage); message
wm_DeviceChange;
..
..
..
procedure TForm1.WMDeviceChange;
var Info1: PDev_Broadcast_HDR; Info2: PDev_Broadcast_Volume;
begin
Info1 := PDev_Broadcast_HDR(Message.lParam);
Info2 := PDev_Broadcast_Volume(Message.lParam);
case Message.wParam of
dbt_DeviceArrival:
if Info1^.dbch_DeviceType = dbt_DevTyp_Volume then
if (Info2^.dbcv_Flags and dbtf_Media) = dbtf_Media then
begin
ShowMessage('CD INSERITO');
// controllo se il cd inserito è quello di RT ed eseguo
RT.exe

end;
dbt_DeviceRemoveComplete:
if Info1^.dbch_DeviceType = dbt_DevTyp_Volume then
if (Info2^.dbcv_Flags and dbtf_Media) = dbtf_Media then
begin
ShowMessage('CD RIMOSSO');
end;
end;
end;



----------------------------------------------------------------------
MSDN version
----------------------------------------------------------------------

(http://msdn.microsoft.com/library/d...base/detecting_media_insertion_or_removal.asp)

/*------------------------------------------------------------------
Main_OnDeviceChange (hwnd, wParam, lParam)

Description
Handles WM_DEVICECHANGE messages sent to the application's
top-level window.
--------------------------------------------------------------------*/
void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
char szMsg[80];

switch(wParam)
{
case DBT_DEVICEARRIVAL:
// Check whether a CD or DVD was inserted into a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media has arrived.\n",
FirstDriveFromMask(lpdbv ->dbcv_unitmask));
MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
}
}
break;

case DBT_DEVICEREMOVECOMPLETE:
// Check whether a CD or DVD was removed from a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media was removed.\n",
FirstDriveFromMask(lpdbv ->dbcv_unitmask));
MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
}
}
break;

default:
/*
Process other WM_DEVICECHANGE notifications for other
devices or reasons.
*/
;
}
}



/*------------------------------------------------------------------
FirstDriveFromMask (unitmask)

Description
Finds the first valid drive letter from a mask of drive letters.
The mask must be in the format bit 0 = A, bit 1 = B, bit 3 = C,
etc. A valid drive letter is defined when the corresponding bit
is set to 1.

Returns the first drive letter that was found.
--------------------------------------------------------------------*/
char FirstDriveFromMask (ULONG unitmask)
{
char i;

for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}

return (i + 'A');
}
 
A

Allan Bruce

Matteo said:
Hello everyone,
I'm trying to write a little app that wait for the WM_DEVICECHANGE
message from Windows that tht OS raises when a device (such as CD,
DVD,...) is inserted or ejected (in BC++ 6)
I've tried whith the Application->OnMessage() but it never receives
the message, so I've tried with the esample in the MSDN, write below,
but I dont know how to register the message handler in the
application.
I know that it could works because the Delphi version of the
application works fine...

Some suggestion?

Tnaks all,

Matteo

For future reference, this is not the place to ask about implementation
specific questions - for windows questions ask to
comp.os.ms-windows.programmer.win32

<OT>
This is a trivial problem with a windows message loop, I suggest you look at
MSDN again, there are multiple entries for WM_DEVICECHANGE.
</OT>

Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top