470,815 Members | 1,277 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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 20946
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Turbo_King | last post: by
1 post views Thread by Matteo | last post: by
reply views Thread by Vladimir Lushnikov | last post: by
3 posts views Thread by mike2036 | last post: by
4 posts views Thread by eSolTec, Inc. 501(c)(3) | last post: by
3 posts views Thread by =?Utf-8?B?Sks=?= | last post: by
reply views Thread by Brian Roisentul | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.