By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

Intercept Messages (and WM_DEVICECHANGE)

P: n/a
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/de...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');
}
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Matteo" <ny***********@libero.it> wrote in message
news:60**************************@posting.google.c om...
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.