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.WMDevice Change;
var Info1: PDev_Broadcast_ HDR; Info2: PDev_Broadcast_ Volume;
begin
Info1 := PDev_Broadcast_ HDR(Message.lPa ram);
Info2 := PDev_Broadcast_ Volume(Message. lParam);
case Message.wParam of
dbt_DeviceArriv al:
if Info1^.dbch_Dev iceType = dbt_DevTyp_Volu me then
if (Info2^.dbcv_Fl ags and dbtf_Media) = dbtf_Media then
begin
ShowMessage('CD INSERITO');
// controllo se il cd inserito è quello di RT ed eseguo
RT.exe
end;
dbt_DeviceRemov eComplete:
if Info1^.dbch_Dev iceType = dbt_DevTyp_Volu me then
if (Info2^.dbcv_Fl ags 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_OnDeviceCh ange (hwnd, wParam, lParam)
Description
Handles WM_DEVICECHANGE messages sent to the application's
top-level window.
--------------------------------------------------------------------*/
void Main_OnDeviceCh ange (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
PDEV_BROADCAST_ HDR lpdb = (PDEV_BROADCAST _HDR)lParam;
char szMsg[80];
switch(wParam)
{
case DBT_DEVICEARRIV AL:
// Check whether a CD or DVD was inserted into a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLU ME)
{
PDEV_BROADCAST_ VOLUME lpdbv = (PDEV_BROADCAST _VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media has arrived.\n",
FirstDriveFromM ask(lpdbv ->dbcv_unitmask) );
MessageBox (hwnd, szMsg, "WM_DEVICECHANG E", MB_OK);
}
}
break;
case DBT_DEVICEREMOV ECOMPLETE:
// Check whether a CD or DVD was removed from a drive.
if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLU ME)
{
PDEV_BROADCAST_ VOLUME lpdbv = (PDEV_BROADCAST _VOLUME)lpdb;
if (lpdbv -> dbcv_flags & DBTF_MEDIA)
{
wsprintf (szMsg, "Drive %c: Media was removed.\n",
FirstDriveFromM ask(lpdbv ->dbcv_unitmask) );
MessageBox (hwnd, szMsg, "WM_DEVICECHANG E", MB_OK);
}
}
break;
default:
/*
Process other WM_DEVICECHANGE notifications for other
devices or reasons.
*/
;
}
}
/*------------------------------------------------------------------
FirstDriveFromM ask (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 FirstDriveFromM ask (ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if (unitmask & 0x1)
break;
unitmask = unitmask >> 1;
}
return (i + 'A');
}