473,785 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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');
}
Jul 23 '05 #1
1 6610

"Matteo" <ny***********@ libero.it> wrote in message
news:60******** *************** ***@posting.goo gle.com...
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.program mer.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
2107
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 write a whole new app. Ideally, (if it worked like a BHO), I could have a dll installed that would load with icq and interecept messages, so instead of the icq window popup, something else would happen. Also, people could keep their icq...
1
6120
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 list of items (i.e. getItemList), and successfully receive the list. I then send a request for individual items from the list (i.e. getItem), and successfully receive the item.
3
5664
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
18362
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 of device it is. Is there a way to get this information ? Currenty my solution work in that way (Also from this group): protected override void WndProc(ref Message m) { switch(m.Msg)
1
2650
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 of device it is. Is there a way to get this information ? Currenty my solution work in that way (Also from this group): protected override void WndProc(ref Message m) { switch(m.Msg)
8
21612
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
8315
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 of example in C# code to no avail. Can someone point me to someplace with a little information how to do this?
4
1850
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
2243
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 auto-run features because this is actually running on Windows XP Embedded... so that's the first thing to be aware of. Using WM_DEVICECHANGE works about half the time...that is, it works when a DVD or CD is inserted, but it does not work for Zip...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.