473,485 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

USB WM_DEVICECHANGE - How create WndProc inside a component?

How can a WndProc be created inside a component?

Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE
messages for connection and removal events for a USB/Serial device. My
frmMain uses a component to communicate with a USB/Serial device. I would
prefer that this component handle the WM_DEVICECHANGE messages internally
and only fires events to my frmMain when my USB device connects or
disconnects.

In case anyone is Googling for in WM_DEVICECHANGE I pasted the WndProc
method from my frmMain below:

Thanks in advance for any tips or suggestions,

-Ed Sutton

protected override void WndProc(ref Message message)
{
if(message.Msg == WM_DEVICECHANGE)
{
DeviceHelper.CPortInfo portInfo;

Debug.Write( "WM_DEVICECHANGE: ");
switch((int)message.WParam)
{
case DBT_DEVNODES_CHANGED:
Debug.WriteLine( "Device tree has changed");
break;
case DBT_DEVICEARRIVAL:
Debug.WriteLine( "System detected a new device");
enumerateSerialPorts();

// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInf o[m_connectedPortInfoKey];

if(readerInterface.Open == false && portInfo != null )
{
readerInterface.CommPort = portInfo.PortNumber;
readerInterface.Open = true;
readerInterface.Ping();
}
}

break;
case DBT_DEVICEREMOVECOMPLETE:
Debug.WriteLine( "Device has been removed");
enumerateSerialPorts();

// If we were connected, and the device we were connected
to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInf o[m_connectedPortInfoKey];
if( readerInterface.Open == true
&& portInfo == null)
{
readerInterface.Open = false;
MessageBox.Show("Please reconnect the reader or turn
power back on", "Device Removal" );
}
}
break;
default:
Debug.Write( String.Format( "What's this?? message.WParam
= {0:X}",message.WParam));
break;
}
}
base.WndProc(ref message);
}
Nov 15 '05 #1
2 21436
Ed,

You have a few options here. If your component is hosted in a windows
forms application, then you can implement the IMessageFilter interface and
pass the implementation to the static AddMessageFilter method on the
Application class. Once you do this, your IMessageFilter interface will be
called when new messages come in.

If you are not in a windows app, then you will have to derive a class
from the NativeWindow class in the System.Windows.Forms namespace. In this,
you would have to override the WndProc method to handle the message. Once
you have that, you can call the CreateHandle method, passing in the
CreateParameters instance which has the details about the window you want to
create which will be notified of this message (this message should be sent
to all top-level windows).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Ed Sutton" <no****************@nomadics.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
How can a WndProc be created inside a component?

Currently I have a WndProc in my frmMain. It looks for WM_DEVICECHANGE
messages for connection and removal events for a USB/Serial device. My
frmMain uses a component to communicate with a USB/Serial device. I would
prefer that this component handle the WM_DEVICECHANGE messages internally
and only fires events to my frmMain when my USB device connects or
disconnects.

In case anyone is Googling for in WM_DEVICECHANGE I pasted the WndProc
method from my frmMain below:

Thanks in advance for any tips or suggestions,

-Ed Sutton

protected override void WndProc(ref Message message)
{
if(message.Msg == WM_DEVICECHANGE)
{
DeviceHelper.CPortInfo portInfo;

Debug.Write( "WM_DEVICECHANGE: ");
switch((int)message.WParam)
{
case DBT_DEVNODES_CHANGED:
Debug.WriteLine( "Device tree has changed");
break;
case DBT_DEVICEARRIVAL:
Debug.WriteLine( "System detected a new device");
enumerateSerialPorts();

// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInf o[m_connectedPortInfoKey];
if(readerInterface.Open == false && portInfo != null ) {
readerInterface.CommPort = portInfo.PortNumber;
readerInterface.Open = true;
readerInterface.Ping();
}
}

break;
case DBT_DEVICEREMOVECOMPLETE:
Debug.WriteLine( "Device has been removed");
enumerateSerialPorts();

// If we were connected, and the device we were connected to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPortInfoKey != null)
{
portInfo =
(DeviceHelper.CPortInfo)m_mapComportNamesToPortInf o[m_connectedPortInfoKey]; if( readerInterface.Open == true
&& portInfo == null)
{
readerInterface.Open = false;
MessageBox.Show("Please reconnect the reader or turn power back on", "Device Removal" );
}
}
break;
default:
Debug.Write( String.Format( "What's this?? message.WParam = {0:X}",message.WParam));
break;
}
}
base.WndProc(ref message);
}

Nov 15 '05 #2
Nicholas,

Thank you very much for your reply. You explained the options very well.

I want to easily be able to re-use this component. Deriving from the
NativeWindow class in System.Windows.Forms is the most logical approach.

Thanks again!

-Ed
You have a few options here. If your component is hosted in a
windows forms application, then you can implement the IMessageFilter
interface and pass the implementation to the static AddMessageFilter
method on the Application class. Once you do this, your
IMessageFilter interface will be called when new messages come in.

If you are not in a windows app, then you will have to derive a
class from the NativeWindow class in the System.Windows.Forms
namespace. In this, you would have to override the WndProc method to
handle the message. Once you have that, you can call the
CreateHandle method, passing in the CreateParameters instance which
has the details about the window you want to create which will be
notified of this message (this message should be sent to all
top-level windows).

Hope this helps.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3589
by: Turbo_King | last post by:
Hi, I am trying to write a class which creates a window and then assigns a method of the class as the wndproc function for the window so that each instance of the class can handle the messages...
1
6585
by: Matteo | last post by:
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)...
0
2019
by: Vladimir Lushnikov | last post by:
Hi, I'm experimenting with the Seqpacket protocol, because the connectionless protocols aren't enough for my needs, and TCP/IP doesn't preserve message boundaries. This is what I've currently...
0
1774
by: Erwin Hill | last post by:
Hi guys, I have a weird problem. I wrote an application that has 2 UI threads both windows are top-level. When I insert my USB disk on key into computer I receive several WM_DEVICECHANGE which...
2
2608
by: Lenster | last post by:
When using PostMessage to post myself a message, the msg and wparam parameters somehow get swapped over. They are in the correct order when calling PostMessage but by the time wndproc handles the...
3
3801
by: mike2036 | last post by:
Hi all, I'm seeing cases running my application outside the debugging environment where WndProc is running in a separate thread context from the form and mainline. This is causing issues trying...
4
1703
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Here is my problem. I have a program that will create a registry key, however it is not and here is the code and also the error that follows: ...
3
8996
by: =?Utf-8?B?Sks=?= | last post by:
I need to load the contents of a file from a USB flash drive when it inserted. Can someone please provide a sample of how this can be achieved?
0
1761
by: Brian Roisentul | last post by:
Hi All, I want to programmatically(c# fmw 2.0) create a folder under my IIS Web Site. I figured that I needed to create an entry in the IIS Metabase. So, after investigating a while and i...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7161
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
5418
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,...
1
4857
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...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
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 ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
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...

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.