473,770 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
+ 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.CP ortInfo portInfo;

Debug.Write( "WM_DEVICECHANG E: ");
switch((int)mes sage.WParam)
{
case DBT_DEVNODES_CH ANGED:
Debug.WriteLine ( "Device tree has changed");
break;
case DBT_DEVICEARRIV AL:
Debug.WriteLine ( "System detected a new device");
enumerateSerial Ports();

// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPort InfoKey != null)
{
portInfo =
(DeviceHelper.C PortInfo)m_mapC omportNamesToPo rtInfo[m_connectedPort InfoKey];

if(readerInterf ace.Open == false && portInfo != null )
{
readerInterface .CommPort = portInfo.PortNu mber;
readerInterface .Open = true;
readerInterface .Ping();
}
}

break;
case DBT_DEVICEREMOV ECOMPLETE:
Debug.WriteLine ( "Device has been removed");
enumerateSerial Ports();

// If we were connected, and the device we were connected
to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPort InfoKey != null)
{
portInfo =
(DeviceHelper.C PortInfo)m_mapC omportNamesToPo rtInfo[m_connectedPort InfoKey];
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(re f message);
}
Nov 15 '05 #1
2 21468
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 AddMessageFilte r 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
CreateParameter s 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************* *@exisconsultin g.com

"Ed Sutton" <no************ ****@nomadics.c om> wrote in message
news:%2******** *******@TK2MSFT NGP10.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.CP ortInfo portInfo;

Debug.Write( "WM_DEVICECHANG E: ");
switch((int)mes sage.WParam)
{
case DBT_DEVNODES_CH ANGED:
Debug.WriteLine ( "Device tree has changed");
break;
case DBT_DEVICEARRIV AL:
Debug.WriteLine ( "System detected a new device");
enumerateSerial Ports();

// If we were previously connected to a device,
// automatically reconnect if we are reconnected
if( m_connectedPort InfoKey != null)
{
portInfo =
(DeviceHelper.C PortInfo)m_mapC omportNamesToPo rtInfo[m_connectedPort InfoKey];
if(readerInterf ace.Open == false && portInfo != null ) {
readerInterface .CommPort = portInfo.PortNu mber;
readerInterface .Open = true;
readerInterface .Ping();
}
}

break;
case DBT_DEVICEREMOV ECOMPLETE:
Debug.WriteLine ( "Device has been removed");
enumerateSerial Ports();

// If we were connected, and the device we were connected to was removed, then disconnect it
// Automatically reconnect if it comes back
if( m_connectedPort InfoKey != null)
{
portInfo =
(DeviceHelper.C PortInfo)m_mapC omportNamesToPo rtInfo[m_connectedPort InfoKey]; 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(re f 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 AddMessageFilte r
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 CreateParameter s 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
3622
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 for its repective form. I am trying to do the following: class myWinClass {
1
6607
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) 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...
0
2062
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 got in C# -------------------------------------------------------------------------- listeningSock = new Socket(AddressFamily.InterNetwork, SocketType.Seqpacket, ProtocolType.Unknown);
0
1803
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 are sent trough PostMessage and WM_DEVICECHANGE with wParam set to DBT_DEVICEARRIVAL and this one is sent to me through SendMessage(by windows) I checked this through Spy++(I don't know why last one is sent through SendMessage). The
2
2623
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 message msg is now in the wparam position and msg is set to 0 (NULL message). Does anyone know why this behaviour is occurring ? I've included a simple test form below to illustrate this...
3
3820
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 to post messages from the middleware since the windows handle is in a different context and therefore the PostMessage() call returns an INVALID_WINDOW_HANDLE error. Has anyone else experienced this? Is there a way to force WndProc, the form,...
4
1724
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: Imports System.IO Imports System.Text Imports System.Security.Cryptography Imports System.Net Imports Microsoft.VisualBasic.Conversion Imports System.Management
3
9013
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
1786
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 figured out something pretty weird: I downloaded a source code that allowed me to do what I wanted.
0
9617
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...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
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
9904
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
8931
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...
0
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.