USB WM_DEVICECHANGE - How create WndProc inside a component?

  • Thread starter Thread starter Ed Sutton
  • Start date Start date
E

Ed Sutton

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_mapComportNamesToPortInfo[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_mapComportNamesToPortInfo[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);
}
 
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]
- (e-mail address removed)

Ed Sutton said:
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_mapComportNamesToPortInfo[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_mapComportNamesToPortInfo[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);
}
 
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
 
Back
Top