473,387 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Intercept Messages (and WM_DEVICECHANGE)

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
1 6580

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Todd Smith | last post by:
I've read about Browser Helper Objects and how they load in a dll with IE and can intercept messages and change things. I've got a neat idea for a change to the icq interface, but I don't want to...
1
by: Imran | last post by:
Hi, Please bear with me as I have only 1 weeks .NET experience. I am using VB.NET to write a stand-alone client application that connects to a Web service. I successfully send a request for a...
3
by: Eka Gautama | last post by:
Hi all, Is it possible to intercept net send message? I don't want windows show the message to screen directly, but i want process first, then display to the screen... Thanks
1
by: Daniel Diehl | last post by:
Hi, I'm looking for a solution to detect if a USB Sticker (USB Drive) ist plugged in or plugged out. Currently Im able to detect Hardware Plugin and Plugout but don't get any information what type...
1
by: Daniel Diehl | last post by:
Hi, I'm looking for a solution to detect if a USB Sticker (USB Drive) ist plugged in or plugged out. Currently Im able to detect Hardware Plugin and Plugout but don't get any information what type...
8
by: MrNobody | last post by:
How do I change a window's behavior so when someone clicks the little 'X' in the upper right corner it actually hides the dialog, instead of disposing it?
4
by: KlassifiedBBS | last post by:
I want to create a game bot for a video chat program with a closed API. I can see the windows messages when I hook up Spy++ to the window in question. I have done numerous Googles for some kind...
4
by: Zoplax | last post by:
Let's say I want to watch a RichEdit control (e.g. a chat window) outside of my own C# application and intercept its Windows messages. Is there a way to do this?
0
by: Ryan | last post by:
I'm writing a utility that needs to be aware of media (CD, DVD, Zip disk, usb stick, etc) insertions and removals. Which seems to be more of a problem than I original imagined. I can't use any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.