473,387 Members | 1,611 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.

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 21414
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
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
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...

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.